Source file declareUniv.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
open Names
open Univ
exception AlreadyDeclared of (string option * Id.t)
let _ = CErrors.register_handler (function
| AlreadyDeclared (kind, id) ->
Some
Pp.(seq [ Pp.pr_opt_no_spc (fun s -> str s ++ spc ()) kind
; Id.print id; str " already exists."])
| _ ->
None)
type universe_source =
| BoundUniv
| QualifiedUniv of Id.t
| UnqualifiedUniv
type universe_name_decl = {
udecl_src : universe_source;
udecl_named : (Id.t * UGlobal.t) list;
udecl_anon : UGlobal.t list;
}
let check_exists_universe sp =
if Nametab.exists_universe sp then
raise (AlreadyDeclared (Some "Universe", Libnames.basename sp))
else ()
let qualify_univ i dp src id =
match src with
| BoundUniv | UnqualifiedUniv ->
i, Libnames.make_path dp id
| QualifiedUniv l ->
let dp = DirPath.repr dp in
Nametab.map_visibility succ i, Libnames.make_path (DirPath.make (l::dp)) id
let do_univ_name ~check i dp src (id,univ) =
let i, sp = qualify_univ i dp src id in
if check then check_exists_universe sp;
Nametab.push_universe i sp univ
let get_names decl =
let fold accu (id, _) = Id.Set.add id accu in
let names = List.fold_left fold Id.Set.empty decl.udecl_named in
let fold u ((names, cnt), accu) =
let rec aux i =
let na = Id.of_string ("u"^(string_of_int i)) in
if Id.Set.mem na names then aux (i+1) else (na, i)
in
let (id, cnt) = aux cnt in
((Id.Set.add id names, cnt + 1), ((id, u) :: accu))
in
let _, univs = List.fold_right fold decl.udecl_anon ((names, 0), decl.udecl_named) in
univs
let cache_univ_names (prefix, decl) =
let depth = Lib.sections_depth () in
let dp = Libnames.pop_dirpath_n depth prefix.Nametab.obj_dir in
let names = get_names decl in
List.iter (do_univ_name ~check:true (Nametab.Until 1) dp decl.udecl_src) names
let load_univ_names i (prefix, decl) =
let names = get_names decl in
List.iter (do_univ_name ~check:false (Nametab.Until i) prefix.Nametab.obj_dir decl.udecl_src) names
let open_univ_names i (prefix, decl) =
let names = get_names decl in
List.iter (do_univ_name ~check:false (Nametab.Exactly i) prefix.Nametab.obj_dir decl.udecl_src) names
let discharge_univ_names decl = match decl.udecl_src with
| BoundUniv -> None
| (QualifiedUniv _ | UnqualifiedUniv) -> Some decl
let input_univ_names : universe_name_decl -> Libobject.obj =
let open Libobject in
declare_named_object_gen
{ (default_object "Global universe name state") with
cache_function = cache_univ_names;
load_function = load_univ_names;
open_function = simple_open open_univ_names;
discharge_function = discharge_univ_names;
subst_function = (fun (subst, a) -> a);
classify_function = (fun a -> Substitute) }
let input_univ_names (src, l, a) =
if CList.is_empty l && CList.is_empty a then ()
else Lib.add_leaf (input_univ_names { udecl_src = src; udecl_named = l; udecl_anon = a })
let label_of = let open GlobRef in function
| ConstRef c -> Label.to_id @@ Constant.label c
| IndRef (c,_) -> Label.to_id @@ MutInd.label c
| VarRef id -> id
| ConstructRef _ ->
CErrors.anomaly ~label:"declare_univ_binders"
Pp.(str "declare_univ_binders on a constructor reference")
let declare_univ_binders gr (univs, pl) =
let l = label_of gr in
match univs with
| UState.Polymorphic_entry _ -> ()
| UState.Monomorphic_entry (levels, _) ->
let qs, pl = pl in
assert (Id.Map.is_empty qs);
let named, univs = Id.Map.fold (fun id univ (named,univs) ->
let univs = match Level.name univ with
| None -> assert false
| Some univ -> (id,univ)::univs
in
let named = Level.Set.add univ named in
named, univs)
pl (Level.Set.empty,[])
in
let fold u accu = Option.get (Level.name u) :: accu in
let anonymous = Level.Set.fold fold (Level.Set.diff levels named) [] in
input_univ_names (QualifiedUniv l, univs, anonymous)
let do_universe ~poly l =
let in_section = Lib.sections_are_opened () in
let () =
if poly && not in_section then
CErrors.user_err
(Pp.str"Cannot declare polymorphic universes outside sections.")
in
let l = List.map (fun {CAst.v=id} -> (id, UnivGen.new_univ_global ())) l in
let src = if poly then BoundUniv else UnqualifiedUniv in
let () = input_univ_names (src, l, []) in
match poly with
| false ->
let ctx = List.fold_left (fun ctx (_,qid) -> Level.Set.add (Level.make qid) ctx)
Level.Set.empty l, Constraints.empty
in
Global.push_context_set ~strict:true ctx
| true ->
let names = CArray.map_of_list (fun (na,_) -> Name na) l in
let us = CArray.map_of_list (fun (_,l) -> Level.make l) l in
let ctx =
UVars.UContext.make ([||],names) (UVars.Instance.of_array ([||],us), Constraints.empty)
in
Global.push_section_context ctx
let do_constraint ~poly l =
let open Univ in
let evd = Evd.from_env (Global.env ()) in
let constraints = List.fold_left (fun acc cst ->
let cst = Constrintern.interp_univ_constraint evd cst in
Constraints.add cst acc)
Constraints.empty l
in
match poly with
| false ->
let uctx = ContextSet.add_constraints constraints ContextSet.empty in
Global.push_context_set ~strict:true uctx
| true ->
let uctx = UVars.UContext.make
([||],[||])
(UVars.Instance.empty,constraints)
in
Global.push_section_context uctx