| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Names |
| 12 |
open Univ |
| 13 |
|
| 14 |
|
| 15 |
exception AlreadyDeclared of (string option * Id.t) |
| 16 |
|
| 17 |
let () = CErrors.register_handler (function |
| 18 |
| AlreadyDeclared (kind, id) -> |
| 19 |
Some |
| 20 |
Pp.(seq [ Pp.pr_opt_no_spc (fun s -> str s ++ spc ()) kind |
| 21 |
; Id.print id; str " already exists."]) |
| 22 |
| _ -> |
| 23 |
None) |
| 24 |
|
| 25 |
type universe_source = |
| 26 |
| BoundUniv |
| 27 |
| QualifiedUniv of DirPath.t |
| 28 |
| UnqualifiedUniv |
| 29 |
|
| 30 |
type universe_name_decl = universe_source * (Id.t * UGlobal.t) list |
| 31 |
|
| 32 |
type sort_source = |
| 33 |
| BoundQuality |
| 34 |
| UnqualifiedQuality |
| 35 |
|
| 36 |
type sort_name_decl = { |
| 37 |
sdecl_src : sort_source; |
| 38 |
sdecl_named : (Id.t * Sorts.QGlobal.t) list; |
| 39 |
} |
| 40 |
|
| 41 |
let check_exists_universe sp = |
| 42 |
if Nametab.exists_universe sp then |
| 43 |
raise (AlreadyDeclared (Some "Universe", Libnames.basename sp)) |
| 44 |
else () |
| 45 |
|
| 46 |
let qualify_univ i dp src id = |
| 47 |
match src with |
| 48 |
| BoundUniv | UnqualifiedUniv -> |
| 49 |
i, Libnames.add_path_suffix dp id |
| 50 |
| QualifiedUniv l -> |
| 51 |
let path = Libnames.append_path dp l in |
| 52 |
Nametab.map_visibility (fun n -> n + List.length (DirPath.repr l)) i, Libnames.add_path_suffix path id |
| 53 |
|
| 54 |
let do_univ_name ~check i dp src (id,univ) = |
| 55 |
let i, sp = qualify_univ i dp src id in |
| 56 |
if check then check_exists_universe sp; |
| 57 |
Nametab.push_universe i sp univ |
| 58 |
|
| 59 |
let cache_univ_names (prefix, (src, univs)) = |
| 60 |
let depth = Lib.sections_depth () in |
| 61 |
let dp = Libnames.path_pop_n_suffixes depth prefix.Libobject.obj_path in |
| 62 |
List.iter (do_univ_name ~check:true (Nametab.Until 1) dp src) univs |
| 63 |
|
| 64 |
let load_univ_names i (prefix, (src, univs)) = |
| 65 |
List.iter (do_univ_name ~check:false (Nametab.Until i) prefix.Libobject.obj_path src) univs |
| 66 |
|
| 67 |
let open_univ_names i (prefix, (src, univs)) = |
| 68 |
List.iter (do_univ_name ~check:false (Nametab.Exactly i) prefix.Libobject.obj_path src) univs |
| 69 |
|
| 70 |
let discharge_univ_names = function |
| 71 |
| BoundUniv, _ -> None |
| 72 |
| (QualifiedUniv _ | UnqualifiedUniv), _ as x -> Some x |
| 73 |
|
| 74 |
let input_univ_names : universe_name_decl -> Libobject.obj = |
| 75 |
let open Libobject in |
| 76 |
declare_named_object_gen |
| 77 |
{ (default_object "Global universe name state") with |
| 78 |
cache_function = cache_univ_names; |
| 79 |
load_function = load_univ_names; |
| 80 |
open_function = filtered_open open_univ_names; |
| 81 |
discharge_function = discharge_univ_names; |
| 82 |
classify_function = (fun _ -> Escape) } |
| 83 |
|
| 84 |
let input_univ_names (src, l) = |
| 85 |
if CList.is_empty l then () |
| 86 |
else Lib.add_leaf (input_univ_names (src, l)) |
| 87 |
|
| 88 |
let invent_name prefix (named,cnt) u = |
| 89 |
let rec aux i = |
| 90 |
let na = Id.of_string ("u"^(string_of_int i)) in |
| 91 |
let sp = Libnames.add_path_suffix prefix na in |
| 92 |
if Id.Map.mem na named || Nametab.exists_universe sp then aux (i+1) |
| 93 |
else na, (Id.Map.add na u named, i+1) |
| 94 |
in |
| 95 |
aux cnt |
| 96 |
|
| 97 |
let check_exists_sort sp = |
| 98 |
if Nametab.Quality.exists sp then |
| 99 |
raise (AlreadyDeclared (Some "Sort", Libnames.basename sp)) |
| 100 |
else () |
| 101 |
|
| 102 |
let qualify_sort i dp id = |
| 103 |
i, Libnames.add_path_suffix dp id |
| 104 |
|
| 105 |
let do_sort_name ~check i dp (id,quality) = |
| 106 |
let i, sp = qualify_sort i dp id in |
| 107 |
if check then check_exists_sort sp; |
| 108 |
Nametab.Quality.push i sp quality |
| 109 |
|
| 110 |
let cache_sort_names (prefix, decl) = |
| 111 |
let depth = Lib.sections_depth () in |
| 112 |
let dp = Libnames.path_pop_n_suffixes depth prefix.Libobject.obj_path in |
| 113 |
List.iter (do_sort_name ~check:true (Nametab.Until 1) dp) decl.sdecl_named |
| 114 |
|
| 115 |
let load_sort_names i (prefix, decl) = |
| 116 |
List.iter (do_sort_name ~check:false (Nametab.Until i) prefix.Libobject.obj_path) decl.sdecl_named |
| 117 |
|
| 118 |
let open_sort_names i (prefix, decl) = |
| 119 |
List.iter (do_sort_name ~check:false (Nametab.Exactly i) prefix.Libobject.obj_path) decl.sdecl_named |
| 120 |
|
| 121 |
let discharge_sort_names decl = |
| 122 |
match decl.sdecl_src with |
| 123 |
| BoundQuality -> None |
| 124 |
| UnqualifiedQuality -> Some decl |
| 125 |
|
| 126 |
let input_sort_names : sort_name_decl -> Libobject.obj = |
| 127 |
let open Libobject in |
| 128 |
declare_named_object_gen |
| 129 |
{ (default_object "Global sort name state") with |
| 130 |
cache_function = cache_sort_names; |
| 131 |
load_function = load_sort_names; |
| 132 |
open_function = filtered_open open_sort_names; |
| 133 |
discharge_function = discharge_sort_names; |
| 134 |
classify_function = (fun a -> Escape) } |
| 135 |
|
| 136 |
let input_sort_names (src, l) = |
| 137 |
if CList.is_empty l then () |
| 138 |
else Lib.add_leaf (input_sort_names { sdecl_src = src; sdecl_named = l }) |
| 139 |
|
| 140 |
|
| 141 |
let label_of = let open GlobRef in function |
| 142 |
| ConstRef c -> Constant.label c |
| 143 |
| IndRef (c,_) -> MutInd.label c |
| 144 |
| VarRef id -> id |
| 145 |
| ConstructRef _ -> |
| 146 |
CErrors.anomaly ~label:"declare_univ_binders" |
| 147 |
Pp.(str "declare_univ_binders on a constructor reference") |
| 148 |
|
| 149 |
let declare_univ_binders gr (univs, pl) = |
| 150 |
let l = label_of gr in |
| 151 |
match univs with |
| 152 |
| UState.Polymorphic_entry _ -> () |
| 153 |
| UState.Monomorphic_entry (levels, _) -> |
| 154 |
let qs, pl = pl in |
| 155 |
assert (Id.Map.is_empty qs); |
| 156 |
|
| 157 |
let named, univs = Id.Map.fold (fun id univ (named,univs) -> |
| 158 |
let univs = match Level.name univ with |
| 159 |
| None -> assert false |
| 160 |
| Some univ -> (id,univ)::univs |
| 161 |
in |
| 162 |
let named = Level.Set.add univ named in |
| 163 |
named, univs) |
| 164 |
pl (Level.Set.empty,[]) |
| 165 |
in |
| 166 |
|
| 167 |
let prefix = Libnames.add_path_suffix (Lib.cwd_except_section()) l in |
| 168 |
let _, univs = Level.Set.fold (fun univ (aux,univs) -> |
| 169 |
let id, aux = invent_name prefix aux univ in |
| 170 |
let univ = Option.get (Level.name univ) in |
| 171 |
aux, (id,univ) :: univs) |
| 172 |
(Level.Set.diff levels named) ((pl,0),univs) |
| 173 |
in |
| 174 |
input_univ_names (QualifiedUniv (DirPath.make [l]), univs) |
| 175 |
|
| 176 |
let name_mono_section_univs univs = |
| 177 |
if Level.Set.is_empty univs then () |
| 178 |
else |
| 179 |
let prefix = Lib.cwd () in |
| 180 |
let sections = let open Libnames in |
| 181 |
drop_dirpath_prefix (dirpath_of_path @@ Lib.cwd_except_section()) |
| 182 |
(dirpath_of_path prefix) |
| 183 |
in |
| 184 |
let _, univs = Level.Set.fold (fun univ (aux,univs) -> |
| 185 |
let id, aux = invent_name prefix aux univ in |
| 186 |
let univ = Option.get (Level.name univ) in |
| 187 |
aux, (id,univ) :: univs) |
| 188 |
univs ((Id.Map.empty, 0), []) |
| 189 |
in |
| 190 |
input_univ_names (QualifiedUniv sections, univs) |
| 191 |
|
| 192 |
let do_universe ~poly l = |
| 193 |
let in_section = Lib.sections_are_opened () in |
| 194 |
let () = |
| 195 |
if poly && not in_section then |
| 196 |
CErrors.user_err |
| 197 |
(Pp.str"Cannot declare polymorphic universes outside sections.") |
| 198 |
in |
| 199 |
let l = List.map (fun {CAst.v=id} -> (id, UnivGen.new_univ_global ())) l in |
| 200 |
let src = if poly then BoundUniv else UnqualifiedUniv in |
| 201 |
let () = input_univ_names (src, l) in |
| 202 |
match poly with |
| 203 |
| false -> |
| 204 |
let ctx = List.fold_left (fun ctx (_,qid) -> Level.Set.add (Level.make qid) ctx) |
| 205 |
Level.Set.empty l, Univ.UnivConstraints.empty |
| 206 |
in |
| 207 |
Global.push_context_set ctx |
| 208 |
| true -> |
| 209 |
let names = CArray.map_of_list (fun (na,_) -> Name na) l in |
| 210 |
let us = CArray.map_of_list (fun (_,l) -> Level.make l) l in |
| 211 |
let ctx = |
| 212 |
UVars.UContext.make {quals = [||]; univs = names} |
| 213 |
(UVars.Instance.of_array ([||],us), PConstraints.empty) |
| 214 |
in |
| 215 |
Global.push_section_context ctx |
| 216 |
|
| 217 |
let do_sort ~poly l = |
| 218 |
let in_section = Lib.sections_are_opened () in |
| 219 |
let () = |
| 220 |
if poly && not in_section then |
| 221 |
CErrors.user_err |
| 222 |
(Pp.str"Cannot declare polymorphic sorts outside sections.") |
| 223 |
in |
| 224 |
let l = List.map (fun {CAst.v=id} -> (id, UnivGen.new_sort_global id)) l in |
| 225 |
let src = if poly then BoundQuality else UnqualifiedQuality in |
| 226 |
let () = input_sort_names (src, l) in |
| 227 |
match poly with |
| 228 |
| false -> |
| 229 |
let qs = List.fold_left (fun qs (_, qv) -> Sorts.QVar.(Set.add (make_global qv) qs)) |
| 230 |
Sorts.QVar.Set.empty l |
| 231 |
in |
| 232 |
let rigid = false in |
| 233 |
Global.push_qualities ~rigid (qs, Sorts.ElimConstraints.empty) |
| 234 |
| true -> |
| 235 |
let names = CArray.map_of_list (fun (na,_) -> Name na) l in |
| 236 |
let qs = CArray.map_of_list (fun (_,sg) -> Sorts.Quality.global sg) l in |
| 237 |
let ctx = |
| 238 |
UVars.UContext.make {quals=names; univs=[||]} |
| 239 |
(UVars.Instance.of_array (qs,[||]), PConstraints.empty) |
| 240 |
in |
| 241 |
Global.push_section_context ctx |
| 242 |
|
| 243 |
let do_constraint ~poly l = |
| 244 |
let evd = Evd.from_env (Global.env ()) in |
| 245 |
let constraints = List.fold_left (fun acc cst -> |
| 246 |
match cst with |
| 247 |
| Constrexpr.UnivCst (l,d,r as cst) -> |
| 248 |
let cst = Constrintern.interp_univ_constraint evd cst in |
| 249 |
PConstraints.add_univ cst acc |
| 250 |
| Constrexpr.ElimCst (l,d,r as cst) -> |
| 251 |
let cst = Constrintern.interp_elim_constraint evd cst in |
| 252 |
PConstraints.add_quality cst acc) |
| 253 |
PConstraints.empty l |
| 254 |
in |
| 255 |
match poly with |
| 256 |
| false -> |
| 257 |
let qcst, ucst = constraints in |
| 258 |
let () = Global.push_qualities ~rigid:true (Sorts.QVar.Set.empty, qcst) in |
| 259 |
Global.push_context_set (Univ.Level.Set.empty, ucst) |
| 260 |
| true -> |
| 261 |
let uctx = UVars.UContext.make |
| 262 |
UVars.empty_bound_names |
| 263 |
(UVars.Instance.empty,constraints) |
| 264 |
in |
| 265 |
Global.push_section_context uctx |
| 266 |
|
| 267 |
let constraint_sources = Summary.ref ~name:"univ constraint sources" [] |
| 268 |
|
| 269 |
let cache_constraint_source x = constraint_sources := x :: !constraint_sources |
| 270 |
|
| 271 |
let constraint_sources () = !constraint_sources |
| 272 |
|
| 273 |
let constraint_obj = |
| 274 |
Libobject.declare_object { |
| 275 |
(Libobject.default_object "univ constraint sources") with |
| 276 |
cache_function = cache_constraint_source; |
| 277 |
load_function = (fun _ c -> cache_constraint_source c); |
| 278 |
discharge_function = (fun x -> Some x); |
| 279 |
classify_function = (fun _ -> Escape); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
let add_constraint_source x ctx = |
| 285 |
let _, csts = ctx in |
| 286 |
if Univ.UnivConstraints.is_empty csts then () |
| 287 |
else |
| 288 |
let v = x, csts in |
| 289 |
Lib.add_leaf (constraint_obj v) |