| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open CErrors |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
open Univ |
| 15 |
open Sorts |
| 16 |
open UVars |
| 17 |
|
| 18 |
module PContextSet = struct |
| 19 |
open PConstraints |
| 20 |
type t = Level.Set.t constrained |
| 21 |
|
| 22 |
let empty = (Level.Set.empty, empty) |
| 23 |
let is_empty (univs, cst) = Level.Set.is_empty univs && is_empty cst |
| 24 |
|
| 25 |
let union (univs, cst as x) (univs', cst' as y) = |
| 26 |
if x == y then x |
| 27 |
else Level.Set.union univs univs', union cst cst' |
| 28 |
|
| 29 |
let add_level u (univs, cst) = |
| 30 |
Level.Set.add u univs, cst |
| 31 |
|
| 32 |
let pr prv prl (univs, cst) = |
| 33 |
UnivGen.pr_sort_context prv prl ((Sorts.QVar.Set.empty, univs), cst) |
| 34 |
|
| 35 |
let univ_context_set (uvars, (_, uctx)) = (uvars, uctx) |
| 36 |
let univ_constraints (_, (_,csts)) = csts |
| 37 |
let levels (univs, _cst) = univs |
| 38 |
|
| 39 |
end |
| 40 |
|
| 41 |
let template_default_univs = Summary.ref ~name:"template default univs" Univ.Level.Set.empty |
| 42 |
|
| 43 |
let cache_template_default_univs us = |
| 44 |
template_default_univs := Univ.Level.Set.union !template_default_univs us |
| 45 |
|
| 46 |
let template_default_univs_obj = |
| 47 |
Libobject.declare_object { |
| 48 |
(Libobject.default_object "template default univs") with |
| 49 |
cache_function = cache_template_default_univs; |
| 50 |
load_function = (fun _ us -> cache_template_default_univs us); |
| 51 |
discharge_function = (fun x -> Some x); |
| 52 |
classify_function = (fun _ -> Escape); |
| 53 |
} |
| 54 |
|
| 55 |
let add_template_default_univs env kn = |
| 56 |
match (Environ.lookup_mind kn env).mind_template with |
| 57 |
| None -> () |
| 58 |
| Some template -> |
| 59 |
let _, us = UVars.Instance.levels template.template_defaults in |
| 60 |
Lib.add_leaf (template_default_univs_obj us) |
| 61 |
|
| 62 |
let template_default_univs () = !template_default_univs |
| 63 |
|
| 64 |
module UnivFlex = UnivFlex |
| 65 |
|
| 66 |
type universes_entry = |
| 67 |
| Monomorphic_entry of Univ.ContextSet.t |
| 68 |
| Polymorphic_entry of UVars.UContext.t |
| 69 |
|
| 70 |
module UNameMap = Id.Map |
| 71 |
|
| 72 |
type uinfo = { |
| 73 |
uname : Id.t option; |
| 74 |
uloc : Loc.t option; |
| 75 |
} |
| 76 |
|
| 77 |
open Quality |
| 78 |
|
| 79 |
let sort_inconsistency ?explain cst l r = |
| 80 |
let explain = Option.map (fun p -> UGraph.Other p) explain in |
| 81 |
raise (UGraph.UniverseInconsistency (None, (cst, l, r, explain))) |
| 82 |
|
| 83 |
module QSet = QVar.Set |
| 84 |
module QMap = QVar.Map |
| 85 |
|
| 86 |
module QState : sig |
| 87 |
type t |
| 88 |
type elt = QVar.t |
| 89 |
val empty : t |
| 90 |
val union : fail:(t -> Quality.t -> Quality.t -> t) -> t -> t -> t |
| 91 |
val add : check_fresh:bool -> rigid:bool -> elt -> t -> t |
| 92 |
val repr : elt -> t -> Quality.t |
| 93 |
val is_rigid : t -> QVar.t -> bool |
| 94 |
val is_above_prop : t -> QVar.t -> bool |
| 95 |
val unify_quality : fail:(unit -> t) -> Conversion.conv_pb -> Quality.t -> Quality.t -> t -> t |
| 96 |
val undefined : t -> QVar.Set.t |
| 97 |
val collapse_above_prop : to_prop:bool -> t -> t |
| 98 |
val collapse : ?except:QVar.Set.t -> t -> t |
| 99 |
val pr : (QVar.t -> Libnames.qualid option) -> t -> Pp.t |
| 100 |
val of_elims : QGraph.t -> t |
| 101 |
val elims : t -> QGraph.t |
| 102 |
val set_elims : QGraph.t -> t -> t |
| 103 |
val initial_elims : t -> QGraph.t |
| 104 |
val merge_constraints : (QGraph.t -> QGraph.t) -> t -> t |
| 105 |
val normalize_elim_constraints : t -> ElimConstraints.t -> ElimConstraints.t |
| 106 |
end = |
| 107 |
struct |
| 108 |
|
| 109 |
type node = |
| 110 |
| Equiv of Quality.t |
| 111 |
| Canonical of { rigid : bool } |
| 112 |
|
| 113 |
|
| 114 |
type t = { |
| 115 |
qmap : node QMap.t; |
| 116 |
|
| 117 |
above_prop : QSet.t; |
| 118 |
|
| 119 |
*) |
| 120 |
elims : QGraph.t; |
| 121 |
|
| 122 |
initial_elims : QGraph.t; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
type elt = QVar.t |
| 127 |
|
| 128 |
let empty = { qmap = QMap.empty; above_prop = QSet.empty; |
| 129 |
elims = QGraph.initial_graph; initial_elims = QGraph.initial_graph } |
| 130 |
|
| 131 |
let rec repr q m = match QMap.find q m.qmap with |
| 132 |
| Canonical _ -> QVar q |
| 133 |
| Equiv (QVar q) -> repr q m |
| 134 |
| Equiv (QConstant _ as q) -> q |
| 135 |
| exception Not_found -> QVar q |
| 136 |
|
| 137 |
type repr = |
| 138 |
| ReprConstant of Quality.constant |
| 139 |
| ReprVar of QVar.t * bool |
| 140 |
|
| 141 |
let rec repr_node q m = match QMap.find q m.qmap with |
| 142 |
| Canonical { rigid } -> ReprVar (q, rigid) |
| 143 |
| Equiv (QVar q) -> repr_node q m |
| 144 |
| Equiv (QConstant qc) -> ReprConstant qc |
| 145 |
| exception Not_found -> ReprVar (q, true) |
| 146 |
|
| 147 |
let is_above_prop m q = QSet.mem q m.above_prop |
| 148 |
|
| 149 |
let eliminates_to_prop m q = |
| 150 |
QGraph.eliminates_to_prop m.elims (QVar q) |
| 151 |
|
| 152 |
let is_rigid m q = match repr_node q m with |
| 153 |
| ReprVar (_, rigid) -> rigid |
| 154 |
| ReprConstant _ -> true |
| 155 |
|
| 156 |
let set q qv m = |
| 157 |
let q = repr_node q m in |
| 158 |
let q, rigid = match q with ReprVar (q, rigid) -> q, rigid | ReprConstant _ -> assert false in |
| 159 |
let qv = match qv with QVar qv -> repr_node qv m | QConstant qc -> ReprConstant qc in |
| 160 |
let enforce_eq q1 q2 g = |
| 161 |
let ans = QGraph.enforce_eliminates_to q1 q2 (QGraph.enforce_eliminates_to q2 q1 g) in |
| 162 |
let () = QGraph.check_rigid_paths ans in |
| 163 |
ans |
| 164 |
in |
| 165 |
match q, qv with |
| 166 |
| q, ReprVar (qv, _qvrigd) -> |
| 167 |
if QVar.equal q qv then Some m |
| 168 |
else if rigid then None |
| 169 |
else |
| 170 |
let above_prop = |
| 171 |
if is_above_prop m q |
| 172 |
then QSet.add qv (QSet.remove q m.above_prop) |
| 173 |
else m.above_prop in |
| 174 |
Some { qmap = QMap.add q (Equiv (QVar qv)) m.qmap; above_prop; |
| 175 |
elims = enforce_eq (QVar qv) (QVar q) m.elims; initial_elims = m.initial_elims } |
| 176 |
| q, ReprConstant qc -> |
| 177 |
if qc == QSProp && (is_above_prop m q || eliminates_to_prop m q) then None |
| 178 |
else if rigid then None |
| 179 |
else |
| 180 |
let qv = QConstant qc in |
| 181 |
Some { m with qmap = QMap.add q (Equiv qv) m.qmap; |
| 182 |
above_prop = QSet.remove q m.above_prop; |
| 183 |
elims = enforce_eq qv (QVar q) m.elims } |
| 184 |
|
| 185 |
let set_above_prop q m = |
| 186 |
let q = repr_node q m in |
| 187 |
let q, rigid = match q with ReprVar (q, rigid) -> q, rigid | ReprConstant _ -> assert false in |
| 188 |
if rigid then None |
| 189 |
else Some { m with above_prop = QSet.add q m.above_prop } |
| 190 |
|
| 191 |
let unify_quality ~fail c q1 q2 local = match q1, q2 with |
| 192 |
| QConstant QType, QConstant QType |
| 193 |
| QConstant QProp, QConstant QProp |
| 194 |
| QConstant QSProp, QConstant QSProp -> local |
| 195 |
| QConstant QProp, QVar q when c == Conversion.CUMUL -> |
| 196 |
begin match set_above_prop q local with |
| 197 |
| Some local -> local |
| 198 |
| None -> fail () |
| 199 |
end |
| 200 |
| QVar qv1, QVar qv2 -> begin match set qv1 q2 local with |
| 201 |
| Some local -> local |
| 202 |
| None -> match set qv2 q1 local with |
| 203 |
| Some local -> local |
| 204 |
| None -> fail () |
| 205 |
end |
| 206 |
| QVar q, (QConstant (QType | QProp | QSProp) as qv) |
| 207 |
| (QConstant (QType | QProp | QSProp) as qv), QVar q -> |
| 208 |
begin match set q qv local with |
| 209 |
| Some local -> local |
| 210 |
| None -> fail () |
| 211 |
end |
| 212 |
| (QConstant QType, QConstant (QProp | QSProp)) -> fail () |
| 213 |
| (QConstant QProp, QConstant QType) -> |
| 214 |
begin match c with |
| 215 |
| CONV -> fail () |
| 216 |
| CUMUL -> local |
| 217 |
end |
| 218 |
| (QConstant QSProp, QConstant (QType | QProp)) -> fail () |
| 219 |
| (QConstant QProp, QConstant QSProp) -> fail () |
| 220 |
|
| 221 |
let nf_quality m = function |
| 222 |
| QConstant _ as q -> q |
| 223 |
| QVar q -> repr q m |
| 224 |
|
| 225 |
let add_qvars m qmap qs = |
| 226 |
let g = m.initial_elims in |
| 227 |
let filter v = match QMap.find_opt v qmap with |
| 228 |
| None | Some (Canonical _) -> true |
| 229 |
| Some (Equiv _) -> false |
| 230 |
in |
| 231 |
|
| 232 |
|
| 233 |
let qs = QVar.Set.filter filter qs in |
| 234 |
let fold v g = try QGraph.add_quality (QVar v) g with QGraph.AlreadyDeclared -> g in |
| 235 |
QVar.Set.fold fold qs g |
| 236 |
|
| 237 |
let union ~fail s1 s2 = |
| 238 |
let extra = ref [] in |
| 239 |
let qmap = QMap.union (fun qk q1 q2 -> |
| 240 |
match q1, q2 with |
| 241 |
| Equiv q, (Canonical _) | (Canonical _), Equiv q -> Some (Equiv q) |
| 242 |
| Canonical { rigid = r1 }, Canonical { rigid = r2 } -> |
| 243 |
|
| 244 |
Some (Canonical { rigid = r1 || r2 }) |
| 245 |
| Equiv q1, Equiv q2 -> |
| 246 |
let () = if not (Quality.equal q1 q2) then extra := (q1,q2) :: !extra in |
| 247 |
Some (Equiv q1)) |
| 248 |
s1.qmap s2.qmap |
| 249 |
in |
| 250 |
let extra = !extra in |
| 251 |
let qs = QVar.Set.union (QGraph.qvar_domain s1.elims) (QGraph.qvar_domain s2.elims) in |
| 252 |
let filter v = match QMap.find_opt v qmap with |
| 253 |
| None | Some (Canonical _) -> true |
| 254 |
| Some (Equiv _) -> false |
| 255 |
in |
| 256 |
let above_prop = QSet.filter filter @@ QSet.union s1.above_prop s2.above_prop in |
| 257 |
let elims = add_qvars s2 qmap qs in |
| 258 |
let s = { qmap; above_prop; |
| 259 |
elims; initial_elims = elims } in |
| 260 |
List.fold_left (fun s (q1,q2) -> |
| 261 |
let q1 = nf_quality s q1 and q2 = nf_quality s q2 in |
| 262 |
unify_quality ~fail:(fun () -> fail s q1 q2) CONV q1 q2 s) |
| 263 |
s |
| 264 |
extra |
| 265 |
|
| 266 |
let add ~check_fresh ~rigid q m = |
| 267 |
if check_fresh then assert (not (QMap.mem q m.qmap)); |
| 268 |
let add_quality g = |
| 269 |
try QGraph.add_quality (QVar q) g |
| 270 |
with QGraph.AlreadyDeclared as e -> if check_fresh then raise e else g |
| 271 |
in |
| 272 |
{ qmap = QMap.add q (Canonical { rigid }) m.qmap; |
| 273 |
above_prop = m.above_prop; |
| 274 |
elims = add_quality m.elims; |
| 275 |
initial_elims = add_quality m.initial_elims } |
| 276 |
|
| 277 |
let of_elims elims = |
| 278 |
let qs = QGraph.qvar_domain elims in |
| 279 |
let initial_elims = |
| 280 |
QSet.fold (fun v -> QGraph.add_quality (QVar v)) qs (QGraph.initial_graph) in |
| 281 |
let initial_elims = QGraph.update_rigids elims initial_elims in |
| 282 |
{ empty with elims; initial_elims } |
| 283 |
|
| 284 |
|
| 285 |
let undefined m = |
| 286 |
let filter _ v = match v with |
| 287 |
| Canonical _ -> true |
| 288 |
| Equiv _ -> false |
| 289 |
in |
| 290 |
let mq = QMap.filter filter m.qmap in |
| 291 |
QMap.domain mq |
| 292 |
|
| 293 |
let collapse_above_prop ~to_prop m = |
| 294 |
QMap.fold (fun q v m -> |
| 295 |
match v with |
| 296 |
| Equiv _ -> m |
| 297 |
| Canonical _ -> |
| 298 |
if not @@ is_above_prop m q then m else |
| 299 |
if to_prop then Option.get (set q qprop m) |
| 300 |
else Option.get (set q qtype m) |
| 301 |
) |
| 302 |
m.qmap m |
| 303 |
|
| 304 |
let collapse ?(except=QSet.empty) m = |
| 305 |
QMap.fold (fun q v m -> |
| 306 |
match v with |
| 307 |
| Equiv _ -> m |
| 308 |
| Canonical { rigid } -> if rigid || QSet.mem q except then m |
| 309 |
else Option.get (set q qtype m)) |
| 310 |
m.qmap m |
| 311 |
|
| 312 |
let pr prqvar_opt ({ qmap; elims } as m) = |
| 313 |
let open Pp in |
| 314 |
) |
| 315 |
let prqvar q = match prqvar_opt q with |
| 316 |
| None -> QVar.raw_pr q |
| 317 |
| Some qid -> Libnames.pr_qualid qid |
| 318 |
in |
| 319 |
*) |
| 320 |
let prbody u = function |
| 321 |
| Canonical { rigid } -> |
| 322 |
if is_above_prop m u then str " >= Prop" |
| 323 |
else if rigid then |
| 324 |
str " (rigid)" |
| 325 |
else mt () |
| 326 |
| Equiv q -> |
| 327 |
let q = Quality.pr prqvar q in |
| 328 |
str " := " ++ q |
| 329 |
in |
| 330 |
|
| 331 |
let prqvar_name q = match prqvar_opt q with |
| 332 |
| None -> mt () |
| 333 |
| Some qid -> str " (named " ++ Libnames.pr_qualid qid ++ str ")" |
| 334 |
in |
| 335 |
let prqvar_full (q1, q2) = QVar.raw_pr q1 ++ prbody q1 q2 ++ prqvar_name q1 in |
| 336 |
hov 0 (prlist_with_sep fnl prqvar_full (QMap.bindings qmap) ++ |
| 337 |
str " |=" ++ brk (1, 2) ++ hov 0 (QGraph.pr_qualities (Quality.pr prqvar) elims)) |
| 338 |
|
| 339 |
let elims m = m.elims |
| 340 |
|
| 341 |
let set_elims elims m = { m with elims } |
| 342 |
|
| 343 |
let initial_elims m = m.initial_elims |
| 344 |
|
| 345 |
let merge_constraints f m = |
| 346 |
{ m with elims = f m.elims } |
| 347 |
|
| 348 |
let normalize_elim_constraints m cstrs = |
| 349 |
let subst q = match q with |
| 350 |
| QConstant _ -> q |
| 351 |
| QVar qv -> repr qv m |
| 352 |
in |
| 353 |
let is_instantiated q = is_qconst q || is_qglobal q in |
| 354 |
let can_drop (q1,_,q2) = not (is_instantiated q1 && is_instantiated q2) in |
| 355 |
let subst_cst (q1,c,q2) = (subst q1,c,subst q2) in |
| 356 |
let cstrs = ElimConstraints.map subst_cst cstrs in |
| 357 |
ElimConstraints.filter can_drop cstrs |
| 358 |
end |
| 359 |
|
| 360 |
module UPairSet = UnivMinim.UPairSet |
| 361 |
|
| 362 |
type univ_names = UnivNames.universe_binders * (uinfo QVar.Map.t * uinfo Level.Map.t) |
| 363 |
|
| 364 |
|
| 365 |
type t = |
| 366 |
{ names : univ_names; |
| 367 |
local : PContextSet.t; |
| 368 |
univ_variables : UnivFlex.t; |
| 369 |
|
| 370 |
sort_variables : QState.t; |
| 371 |
|
| 372 |
universes : UGraph.t; |
| 373 |
initial_universes : UGraph.t; |
| 374 |
|
| 375 |
minim_extra : UnivMinim.extra; |
| 376 |
} |
| 377 |
|
| 378 |
let empty = |
| 379 |
{ names = UnivNames.empty_binders, (QMap.empty, Level.Map.empty); |
| 380 |
local = PContextSet.empty; |
| 381 |
univ_variables = UnivFlex.empty; |
| 382 |
sort_variables = QState.empty; |
| 383 |
universes = UGraph.initial_universes; |
| 384 |
initial_universes = UGraph.initial_universes; |
| 385 |
minim_extra = UnivMinim.empty_extra; } |
| 386 |
|
| 387 |
let make ~qualities univs = |
| 388 |
{ empty with |
| 389 |
universes = univs; |
| 390 |
initial_universes = univs ; |
| 391 |
sort_variables = QState.of_elims qualities |
| 392 |
} |
| 393 |
|
| 394 |
let is_empty uctx = |
| 395 |
PContextSet.is_empty uctx.local && |
| 396 |
UnivFlex.is_empty uctx.univ_variables |
| 397 |
|
| 398 |
let id_of_level uctx l = |
| 399 |
try (Level.Map.find l (snd (snd uctx.names))).uname |
| 400 |
with Not_found -> None |
| 401 |
|
| 402 |
let id_of_qvar uctx l = |
| 403 |
try (QVar.Map.find l (fst (snd uctx.names))).uname |
| 404 |
with Not_found -> None |
| 405 |
|
| 406 |
let is_rigid_qvar uctx q = QState.is_rigid uctx.sort_variables q |
| 407 |
|
| 408 |
let get_uname info = match info.uname with |
| 409 |
| None -> raise Not_found |
| 410 |
| Some id -> id |
| 411 |
|
| 412 |
let qualid_of_qvar_names (bind, (qrev,_)) l = |
| 413 |
try Some (Libnames.qualid_of_ident (get_uname (QVar.Map.find l qrev))) |
| 414 |
with Not_found -> |
| 415 |
UnivNames.qualid_of_quality bind l |
| 416 |
|
| 417 |
let qualid_of_level_names (bind, (_,urev)) l = |
| 418 |
try Some (Libnames.qualid_of_ident (get_uname (Level.Map.find l urev))) |
| 419 |
with Not_found -> |
| 420 |
UnivNames.qualid_of_level bind l |
| 421 |
|
| 422 |
let qualid_of_level uctx l = qualid_of_level_names uctx.names l |
| 423 |
|
| 424 |
let pr_uctx_qvar_names names l = |
| 425 |
match qualid_of_qvar_names names l with |
| 426 |
| Some qid -> Libnames.pr_qualid qid |
| 427 |
| None -> QVar.raw_pr l |
| 428 |
|
| 429 |
let pr_uctx_level_names names l = |
| 430 |
match qualid_of_level_names names l with |
| 431 |
| Some qid -> Libnames.pr_qualid qid |
| 432 |
| None -> Level.raw_pr l |
| 433 |
|
| 434 |
let pr_uctx_level uctx l = pr_uctx_level_names uctx.names l |
| 435 |
|
| 436 |
let pr_uctx_qvar uctx l = pr_uctx_qvar_names uctx.names l |
| 437 |
|
| 438 |
let merge_univ_constraints uctx cstrs g = |
| 439 |
try UGraph.merge_constraints cstrs g |
| 440 |
with UGraph.UniverseInconsistency (_, i) -> |
| 441 |
let printers = (pr_uctx_qvar uctx, pr_uctx_level uctx) in |
| 442 |
raise (UGraph.UniverseInconsistency (Some printers, i)) |
| 443 |
|
| 444 |
type constraint_source = |
| 445 |
| Internal |
| 446 |
| Rigid |
| 447 |
| Static |
| 448 |
|
| 449 |
let merge_elim_constraints ?(src = Internal) uctx cstrs g = |
| 450 |
try |
| 451 |
let g = QGraph.merge_constraints cstrs g in |
| 452 |
match src with |
| 453 |
| Static -> g |
| 454 |
| Internal -> |
| 455 |
let () = if not (ElimConstraints.is_empty cstrs) then QGraph.check_rigid_paths g in |
| 456 |
g |
| 457 |
| Rigid -> |
| 458 |
let fold (q1, _, q2) accu = QGraph.add_rigid_path q1 q2 accu in |
| 459 |
Sorts.ElimConstraints.fold fold cstrs g |
| 460 |
with QGraph.(EliminationError (QualityInconsistency (_, i))) -> |
| 461 |
let printer = pr_uctx_qvar uctx in |
| 462 |
raise (QGraph.(EliminationError (QualityInconsistency (Some printer, i)))) |
| 463 |
|
| 464 |
let uname_union s t = |
| 465 |
if s == t then s |
| 466 |
else |
| 467 |
UNameMap.merge (fun k l r -> |
| 468 |
match l, r with |
| 469 |
| Some _, _ -> l |
| 470 |
| _, _ -> r) s t |
| 471 |
|
| 472 |
let names_union ((qbind,ubind),(qrev,urev)) ((qbind',ubind'),(qrev',urev')) = |
| 473 |
let qbind = uname_union qbind qbind' |
| 474 |
and ubind = uname_union ubind ubind' |
| 475 |
and qrev = QVar.Map.union (fun _ l _ -> Some l) qrev qrev' |
| 476 |
and urev = Level.Map.lunion urev urev' in |
| 477 |
((qbind,ubind),(qrev,urev)) |
| 478 |
|
| 479 |
let union uctx uctx' = |
| 480 |
if uctx == uctx' then uctx |
| 481 |
else if is_empty uctx' then uctx |
| 482 |
else |
| 483 |
let local = PContextSet.union uctx.local uctx'.local in |
| 484 |
let names = names_union uctx.names uctx'.names in |
| 485 |
let newus = Level.Set.diff (PContextSet.levels uctx'.local) |
| 486 |
(PContextSet.levels uctx.local) in |
| 487 |
let newus = Level.Set.diff newus (UnivFlex.domain uctx.univ_variables) in |
| 488 |
let extra = UnivMinim.extra_union uctx.minim_extra uctx'.minim_extra in |
| 489 |
let declarenew g = |
| 490 |
Level.Set.fold (fun u g -> UGraph.add_universe u ~strict:false g) newus g |
| 491 |
in |
| 492 |
let fail_union s q1 q2 = |
| 493 |
if UGraph.type_in_type uctx.universes then s |
| 494 |
else CErrors.user_err |
| 495 |
Pp.(str "Could not merge universe contexts: could not unify" ++ spc() ++ |
| 496 |
Quality.raw_pr q1 ++ strbrk " and " ++ Quality.raw_pr q2 ++ str ".") |
| 497 |
in |
| 498 |
{ names; |
| 499 |
local = local; |
| 500 |
univ_variables = |
| 501 |
UnivFlex.biased_union uctx.univ_variables uctx'.univ_variables; |
| 502 |
|
| 503 |
sort_variables = QState.union ~fail:fail_union uctx.sort_variables uctx'.sort_variables; |
| 504 |
initial_universes = declarenew uctx.initial_universes; |
| 505 |
universes = |
| 506 |
(if local == uctx.local then uctx.universes |
| 507 |
else |
| 508 |
let cstrsr = PContextSet.univ_constraints uctx'.local in |
| 509 |
merge_univ_constraints uctx cstrsr (declarenew uctx.universes)); |
| 510 |
minim_extra = extra} |
| 511 |
|
| 512 |
let context_set uctx = uctx.local |
| 513 |
|
| 514 |
let universe_context_set uctx = |
| 515 |
let us, (_, ucst) = uctx.local in |
| 516 |
us, ucst |
| 517 |
|
| 518 |
let sort_context_set uctx = |
| 519 |
let us, csts = uctx.local in |
| 520 |
(QState.undefined uctx.sort_variables, us), csts |
| 521 |
|
| 522 |
let constraints uctx = snd uctx.local |
| 523 |
|
| 524 |
let compute_instance_binders uctx inst = |
| 525 |
let (qrev, urev) = snd uctx.names in |
| 526 |
let qinst, uinst = Instance.to_array inst in |
| 527 |
let qmap = function |
| 528 |
| QVar q -> |
| 529 |
begin try Name (get_uname (QVar.Map.find q qrev)) |
| 530 |
with Not_found -> Anonymous |
| 531 |
end |
| 532 |
| QConstant _ -> assert false |
| 533 |
in |
| 534 |
let umap lvl = |
| 535 |
try Name (get_uname (Level.Map.find lvl urev)) |
| 536 |
with Not_found -> Anonymous |
| 537 |
in |
| 538 |
{quals = Array.map qmap qinst; univs = Array.map umap uinst} |
| 539 |
|
| 540 |
let context uctx = |
| 541 |
let qvars = QState.undefined uctx.sort_variables in |
| 542 |
let (uvars, (qcst, ucst)) = uctx.local in |
| 543 |
UContext.of_context_set (compute_instance_binders uctx) ((qvars, qcst), (uvars, ucst)) |
| 544 |
|
| 545 |
type named_universes_entry = universes_entry * UnivNames.universe_binders |
| 546 |
|
| 547 |
let check_mono_sort_constraints uctx = |
| 548 |
let (uvar, (qcst, ucst)) = uctx.local in |
| 549 |
|
| 550 |
let () = assert (Sorts.ElimConstraints.is_empty qcst) in |
| 551 |
(uvar, ucst) |
| 552 |
|
| 553 |
let univ_entry ~poly uctx = |
| 554 |
let (binders, _) = uctx.names in |
| 555 |
let entry = |
| 556 |
if PolyFlags.univ_poly poly then Polymorphic_entry (context uctx) |
| 557 |
else |
| 558 |
let uctx = check_mono_sort_constraints uctx in |
| 559 |
Monomorphic_entry uctx |
| 560 |
in |
| 561 |
entry, binders |
| 562 |
|
| 563 |
type universe_opt_subst = UnivFlex.t |
| 564 |
|
| 565 |
let subst uctx = uctx.univ_variables |
| 566 |
let ugraph uctx = uctx.universes |
| 567 |
|
| 568 |
let elim_graph uctx = QState.elims uctx.sort_variables |
| 569 |
let initial_elim_graph uctx = QState.initial_elims uctx.sort_variables |
| 570 |
|
| 571 |
let is_above_prop uctx qv = QState.is_above_prop uctx.sort_variables qv |
| 572 |
|
| 573 |
let is_algebraic l uctx = UnivFlex.is_algebraic l uctx.univ_variables |
| 574 |
|
| 575 |
let of_names (ubind,(revqbind,revubind)) = |
| 576 |
let revqbind = QVar.Map.map (fun id -> { uname = Some id; uloc = None }) revqbind in |
| 577 |
let revubind = Level.Map.map (fun id -> { uname = Some id; uloc = None }) revubind in |
| 578 |
let qgraph = QVar.Map.fold (fun v _ -> QGraph.add_quality (QVar v)) revqbind QGraph.initial_graph in |
| 579 |
{ empty with names = (ubind,(revqbind,revubind)); |
| 580 |
sort_variables = QState.of_elims qgraph; } |
| 581 |
|
| 582 |
let universe_of_name uctx s = |
| 583 |
UNameMap.find s (snd (fst uctx.names)) |
| 584 |
|
| 585 |
let quality_of_name uctx s = |
| 586 |
Id.Map.find s (fst (fst uctx.names)) |
| 587 |
|
| 588 |
let name_level level id uctx = |
| 589 |
let ((qbind,ubind),(qrev,urev)) = uctx.names in |
| 590 |
assert(not(Id.Map.mem id ubind)); |
| 591 |
let ubind = Id.Map.add id level ubind in |
| 592 |
let urev = Level.Map.add level { uname = Some id; uloc = None } urev in |
| 593 |
{ uctx with names = ((qbind,ubind),(qrev,urev)) } |
| 594 |
|
| 595 |
let universe_binders uctx = |
| 596 |
let named, _ = uctx.names in |
| 597 |
named |
| 598 |
|
| 599 |
let nf_qvar uctx q = QState.repr q uctx.sort_variables |
| 600 |
|
| 601 |
let instantiate_variable l (b : Universe.t) v = |
| 602 |
v := UnivFlex.define l b !v |
| 603 |
|
| 604 |
exception UniversesDiffer |
| 605 |
|
| 606 |
let { Goptions.get = weak_constraints } = |
| 607 |
Goptions.declare_bool_option_and_ref |
| 608 |
~key:["Cumulativity";"Weak";"Constraints"] |
| 609 |
~value:true |
| 610 |
() |
| 611 |
|
| 612 |
let level_inconsistency cst l r = |
| 613 |
let mk u = Sorts.sort_of_univ @@ Universe.make u in |
| 614 |
raise (UGraph.UniverseInconsistency (None, (cst, mk l, mk r, None))) |
| 615 |
|
| 616 |
let nf_universe uctx u = |
| 617 |
UnivSubst.(subst_univs_universe (UnivFlex.normalize_univ_variable uctx.univ_variables)) u |
| 618 |
|
| 619 |
let nf_level uctx u = |
| 620 |
UnivSubst.(level_subst_of (UnivFlex.normalize_univ_variable uctx.univ_variables)) u |
| 621 |
|
| 622 |
let nf_instance uctx u = Instance.subst_fn (nf_qvar uctx, nf_level uctx) u |
| 623 |
|
| 624 |
let nf_quality uctx q = Quality.subst (nf_qvar uctx) q |
| 625 |
|
| 626 |
let nf_sort uctx s = |
| 627 |
let normalize u = nf_universe uctx u in |
| 628 |
let qnormalize q = QState.repr q uctx.sort_variables in |
| 629 |
Sorts.subst_fn (qnormalize, normalize) s |
| 630 |
|
| 631 |
let nf_relevance uctx r = match r with |
| 632 |
| Relevant | Irrelevant -> r |
| 633 |
| RelevanceVar q -> |
| 634 |
match nf_qvar uctx q with |
| 635 |
| QConstant QSProp -> Sorts.Irrelevant |
| 636 |
| QConstant QProp | QConstant QType -> Sorts.Relevant |
| 637 |
| QVar q' -> |
| 638 |
|
| 639 |
|
| 640 |
if QState.is_above_prop uctx.sort_variables q' then Relevant |
| 641 |
else if QVar.equal q q' then r |
| 642 |
else Sorts.RelevanceVar q' |
| 643 |
|
| 644 |
let nf_universes uctx c = |
| 645 |
let lsubst = uctx.univ_variables in |
| 646 |
let nf_univ u = UnivFlex.normalize_univ_variable lsubst u in |
| 647 |
let rec self () c = match Constr.kind c with |
| 648 |
| Evar (evk, args) -> |
| 649 |
let args' = SList.Smart.map (self ()) args in |
| 650 |
if args == args' then c else Constr.mkEvar (evk, args') |
| 651 |
| _ -> UnivSubst.map_universes_opt_subst_with_binders ignore self (nf_relevance uctx) (nf_qvar uctx) nf_univ () c |
| 652 |
in |
| 653 |
self () c |
| 654 |
|
| 655 |
type small_universe = USet | UProp | USProp |
| 656 |
|
| 657 |
let is_uset = function USet -> true | UProp | USProp -> false |
| 658 |
|
| 659 |
type sort_classification = |
| 660 |
| USmall of small_universe |
| 661 |
| ULevel of Level.t |
| 662 |
| UMax of Universe.t * Level.Set.t |
| 663 |
| UAlgebraic of Universe.t |
| 664 |
|
| 665 |
let classify s = match s with |
| 666 |
| Prop -> USmall UProp |
| 667 |
| SProp -> USmall USProp |
| 668 |
| Set -> USmall USet |
| 669 |
| Type u | QSort (_, u) -> |
| 670 |
if Universe.is_levels u then match Universe.level u with |
| 671 |
| None -> UMax (u, Universe.levels u) |
| 672 |
| Some u -> ULevel u |
| 673 |
else UAlgebraic u |
| 674 |
|
| 675 |
type local = { |
| 676 |
local_cst : PConstraints.t; |
| 677 |
local_above_prop : Level.Set.t; |
| 678 |
local_weak : UPairSet.t; |
| 679 |
local_sorts : QState.t; |
| 680 |
} |
| 681 |
|
| 682 |
let add_univ_local cst local = |
| 683 |
{ local with local_cst = PConstraints.add_univ cst local.local_cst } |
| 684 |
|
| 685 |
|
| 686 |
let enforce_leq_up u v local = |
| 687 |
let elim_csts = PConstraints.qualities local.local_cst in |
| 688 |
let univ_csts = UnivSubst.enforce_leq u (Universe.make v) @@ |
| 689 |
PConstraints.univs local.local_cst in |
| 690 |
{ local with local_cst = PConstraints.make elim_csts univ_csts } |
| 691 |
|
| 692 |
let get_constraint = function |
| 693 |
| Conversion.CONV -> UnivConstraint.Eq |
| 694 |
| Conversion.CUMUL -> UnivConstraint.Le |
| 695 |
|
| 696 |
let warn_template = |
| 697 |
CWarnings.create_warning ~from:[CWarnings.CoreCategories.fragile] ~default:Disabled ~name:"bad-template-constraint" () |
| 698 |
|
| 699 |
let do_warn_template = CWarnings.create_in warn_template |
| 700 |
Pp.(fun (uctx,csts) -> |
| 701 |
str "Adding constraints involving global template univs:" ++ spc() ++ |
| 702 |
UnivConstraints.pr (pr_uctx_level uctx) csts ) |
| 703 |
|
| 704 |
let warn_template uctx csts = |
| 705 |
match CWarnings.warning_status warn_template with |
| 706 |
| Disabled -> () |
| 707 |
| Enabled | AsError -> |
| 708 |
let is_template u = Level.Set.mem u (template_default_univs()) in |
| 709 |
let csts = UnivConstraints.filter (fun (u,_,v as cst) -> |
| 710 |
not (Level.is_set u) && not (Level.is_set v) && |
| 711 |
(is_template u || is_template v) && |
| 712 |
not (UGraph.check_constraint uctx.universes cst)) csts in |
| 713 |
if not @@ UnivConstraints.is_empty csts then |
| 714 |
do_warn_template (uctx,csts) |
| 715 |
|
| 716 |
let unify_quality univs c s1 s2 l = |
| 717 |
let fail () = if UGraph.type_in_type univs then l.local_sorts |
| 718 |
else sort_inconsistency (get_constraint c) s1 s2 |
| 719 |
in |
| 720 |
{ l with |
| 721 |
local_sorts = QState.unify_quality ~fail |
| 722 |
c (Sorts.quality s1) (Sorts.quality s2) l.local_sorts; |
| 723 |
} |
| 724 |
|
| 725 |
let process_constraints uctx cstrs = |
| 726 |
let open UnivSubst in |
| 727 |
let open UnivProblem in |
| 728 |
let univs = uctx.universes in |
| 729 |
let vars = ref uctx.univ_variables in |
| 730 |
let normalize u = UnivFlex.normalize_univ_variable !vars u in |
| 731 |
let qnormalize sorts q = QState.repr q sorts in |
| 732 |
let normalize_sort sorts s = |
| 733 |
Sorts.subst_fn ((qnormalize sorts), subst_univs_universe normalize) s |
| 734 |
in |
| 735 |
let nf_constraint sorts = function |
| 736 |
| QElimTo (a, b) -> QElimTo (Quality.subst (qnormalize sorts) a, Quality.subst (qnormalize sorts) b) |
| 737 |
| QLeq (a, b) -> QLeq (Quality.subst (qnormalize sorts) a, Quality.subst (qnormalize sorts) b) |
| 738 |
| QEq (a, b) -> QEq (Quality.subst (qnormalize sorts) a, Quality.subst (qnormalize sorts) b) |
| 739 |
| ULub (u, v) -> ULub (level_subst_of normalize u, level_subst_of normalize v) |
| 740 |
| UWeak (u, v) -> UWeak (level_subst_of normalize u, level_subst_of normalize v) |
| 741 |
| UEq (u, v) -> UEq (normalize_sort sorts u, normalize_sort sorts v) |
| 742 |
| ULe (u, v) -> ULe (normalize_sort sorts u, normalize_sort sorts v) |
| 743 |
in |
| 744 |
let is_local l = UnivFlex.mem l !vars in |
| 745 |
let equalize_small l s local = |
| 746 |
let ls = match l with |
| 747 |
| USProp -> sprop |
| 748 |
| UProp -> prop |
| 749 |
| USet -> set |
| 750 |
in |
| 751 |
if UGraph.check_eq_sort Sorts.Quality.equal univs ls s then local |
| 752 |
else if is_uset l then match classify s with |
| 753 |
| USmall _ -> sort_inconsistency Eq set s |
| 754 |
| ULevel r -> |
| 755 |
if is_local r then |
| 756 |
let () = instantiate_variable r Universe.type0 vars in |
| 757 |
add_univ_local (Level.set, Eq, r) local |
| 758 |
else |
| 759 |
sort_inconsistency Eq set s |
| 760 |
| UMax (u, _)| UAlgebraic u -> |
| 761 |
if univ_level_mem Level.set u then |
| 762 |
let inst = univ_level_rem Level.set u u in |
| 763 |
enforce_leq_up inst Level.set local |
| 764 |
else |
| 765 |
sort_inconsistency Eq ls s |
| 766 |
else sort_inconsistency Eq ls s |
| 767 |
in |
| 768 |
let equalize_variables fo l' r' local = |
| 769 |
if Level.equal l' r' then local |
| 770 |
else |
| 771 |
let () = |
| 772 |
if is_local l' then |
| 773 |
instantiate_variable l' (Universe.make r') vars |
| 774 |
else if is_local r' then |
| 775 |
instantiate_variable r' (Universe.make l') vars |
| 776 |
else if not (UnivProblem.check_eq_level univs l' r') then |
| 777 |
|
| 778 |
|
| 779 |
if Level.is_set l' || Level.is_set r' then |
| 780 |
level_inconsistency Eq l' r' |
| 781 |
else if fo then |
| 782 |
raise UniversesDiffer |
| 783 |
in |
| 784 |
add_univ_local (l', Eq, r') local |
| 785 |
in |
| 786 |
let equalize_algebraic l ru local = |
| 787 |
let alg = UnivFlex.is_algebraic l uctx.univ_variables in |
| 788 |
let inst = univ_level_rem l ru ru in |
| 789 |
if alg && not (Level.Set.mem l (Universe.levels inst)) then |
| 790 |
let () = instantiate_variable l inst vars in |
| 791 |
local |
| 792 |
else |
| 793 |
if univ_level_mem l ru then |
| 794 |
enforce_leq_up inst l local |
| 795 |
else sort_inconsistency Eq (sort_of_univ (Universe.make l)) (sort_of_univ ru) |
| 796 |
in |
| 797 |
let equalize_universes l r local = match classify l, classify r with |
| 798 |
| USmall l', (USmall _ | ULevel _ | UMax _ | UAlgebraic _) -> |
| 799 |
equalize_small l' r local |
| 800 |
| (ULevel _ | UMax _ | UAlgebraic _), USmall r' -> |
| 801 |
equalize_small r' l local |
| 802 |
| ULevel l', ULevel r' -> |
| 803 |
equalize_variables false l' r' local |
| 804 |
| ULevel l', (UAlgebraic r | UMax (r, _)) | (UAlgebraic r | UMax (r, _)), ULevel l' -> |
| 805 |
equalize_algebraic l' r local |
| 806 |
| (UAlgebraic _ | UMax _), (UAlgebraic _ | UMax _) -> |
| 807 |
|
| 808 |
if UGraph.check_eq_sort Sorts.Quality.equal univs l r then local |
| 809 |
else sort_inconsistency Eq l r |
| 810 |
in |
| 811 |
let unify_universes cst local = |
| 812 |
let cst = nf_constraint local.local_sorts cst in |
| 813 |
if UnivProblem.is_trivial cst then local |
| 814 |
else |
| 815 |
|
| 816 |
|
| 817 |
let mk q = Sorts.make q Universe.type0 in |
| 818 |
match cst with |
| 819 |
| QEq (a, b) -> unify_quality univs CONV (mk a) (mk b) local |
| 820 |
| QLeq (a, b) -> unify_quality univs CUMUL (mk a) (mk b) local |
| 821 |
| QElimTo (a, b) -> { local with local_cst = PConstraints.add_quality (a, ElimTo, b) local.local_cst } |
| 822 |
| ULe (l, r) -> |
| 823 |
let local = unify_quality univs CUMUL l r local in |
| 824 |
let l = normalize_sort local.local_sorts l in |
| 825 |
let r = normalize_sort local.local_sorts r in |
| 826 |
begin match classify r with |
| 827 |
| UAlgebraic _ | UMax _ -> |
| 828 |
if UGraph.check_leq_sort Sorts.Quality.equal univs l r then local |
| 829 |
else |
| 830 |
sort_inconsistency Le l r |
| 831 |
~explain:(Pp.str "(cannot handle algebraic on the right)") |
| 832 |
| USmall r' -> |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if UGraph.type_in_type univs then local |
| 837 |
else begin match classify l with |
| 838 |
| UAlgebraic _ -> |
| 839 |
|
| 840 |
sort_inconsistency Le l r |
| 841 |
| USmall l' -> |
| 842 |
if UGraph.check_leq_sort Sorts.Quality.equal univs l r then local |
| 843 |
else sort_inconsistency Le l r |
| 844 |
| ULevel l' -> |
| 845 |
if is_uset r' && is_local l' then |
| 846 |
|
| 847 |
let () = instantiate_variable l' Universe.type0 vars in |
| 848 |
add_univ_local (l', Eq, Level.set) local |
| 849 |
else |
| 850 |
sort_inconsistency Le l r |
| 851 |
| UMax (_, levels) -> |
| 852 |
if is_uset r' then |
| 853 |
let fold l' local = |
| 854 |
let l = sort_of_univ @@ Universe.make l' in |
| 855 |
if Level.is_set l' || is_local l' then |
| 856 |
equalize_variables false l' Level.set local |
| 857 |
else sort_inconsistency Le l r |
| 858 |
in |
| 859 |
Level.Set.fold fold levels local |
| 860 |
else |
| 861 |
sort_inconsistency Le l r |
| 862 |
end |
| 863 |
| ULevel r' -> |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
match classify l with |
| 873 |
| USmall UProp -> |
| 874 |
{ local with local_above_prop = Level.Set.add r' local.local_above_prop } |
| 875 |
| USmall USProp -> |
| 876 |
if UGraph.type_in_type univs then local |
| 877 |
else sort_inconsistency Le l r |
| 878 |
| USmall USet -> |
| 879 |
add_univ_local (Level.set, Le, r') local |
| 880 |
| ULevel l' -> |
| 881 |
add_univ_local (l', Le, r') local |
| 882 |
| UAlgebraic l -> |
| 883 |
enforce_leq_up l r' local |
| 884 |
| UMax (_, l) -> |
| 885 |
Univ.Level.Set.fold (fun l' accu -> add_univ_local (l', Le, r') accu) l local |
| 886 |
end |
| 887 |
| ULub (l, r) -> |
| 888 |
equalize_variables true l r local |
| 889 |
| UWeak (l, r) -> |
| 890 |
if weak_constraints () |
| 891 |
then { local with local_weak = UPairSet.add (l, r) local.local_weak } |
| 892 |
else local |
| 893 |
| UEq (l, r) -> |
| 894 |
let local = unify_quality univs CONV l r local in |
| 895 |
let l = normalize_sort local.local_sorts l in |
| 896 |
let r = normalize_sort local.local_sorts r in |
| 897 |
equalize_universes l r local |
| 898 |
in |
| 899 |
let unify_universes cst local = |
| 900 |
if not (UGraph.type_in_type univs) then unify_universes cst local |
| 901 |
else try unify_universes cst local with UGraph.UniverseInconsistency _ -> local |
| 902 |
in |
| 903 |
let local = { |
| 904 |
local_cst = PConstraints.empty; |
| 905 |
local_weak = uctx.minim_extra.UnivMinim.weak_constraints; |
| 906 |
local_above_prop = uctx.minim_extra.UnivMinim.above_prop; |
| 907 |
local_sorts = uctx.sort_variables; |
| 908 |
} in |
| 909 |
let local = UnivProblem.Set.fold unify_universes cstrs local in |
| 910 |
let extra = { UnivMinim.above_prop = local.local_above_prop; UnivMinim.weak_constraints = local.local_weak } in |
| 911 |
let () = warn_template uctx (PConstraints.univs local.local_cst) in |
| 912 |
!vars, extra, local.local_cst, local.local_sorts |
| 913 |
|
| 914 |
let add_constraints ?src uctx cstrs = |
| 915 |
let univs, local = uctx.local in |
| 916 |
let vars, extra, local', sorts = process_constraints uctx cstrs in |
| 917 |
{ uctx with |
| 918 |
local = (univs, PConstraints.union local local'); |
| 919 |
univ_variables = vars; |
| 920 |
universes = merge_univ_constraints uctx (PConstraints.univs local') uctx.universes; |
| 921 |
sort_variables = |
| 922 |
QState.merge_constraints (merge_elim_constraints ?src uctx (PConstraints.qualities local')) sorts ; |
| 923 |
minim_extra = extra; } |
| 924 |
|
| 925 |
let problem_of_univ_constraints cstrs = |
| 926 |
UnivConstraints.fold (fun (l,d,r) acc -> |
| 927 |
let l = Universe.make l and r = sort_of_univ @@ Universe.make r in |
| 928 |
let cstr' = let open UnivProblem in |
| 929 |
match d with |
| 930 |
| Lt -> |
| 931 |
ULe (sort_of_univ @@ Universe.super l, r) |
| 932 |
| Le -> ULe (sort_of_univ l, r) |
| 933 |
| Eq -> UEq (sort_of_univ l, r) |
| 934 |
in UnivProblem.Set.add cstr' acc) |
| 935 |
cstrs UnivProblem.Set.empty |
| 936 |
|
| 937 |
let problem_of_elim_constraints cstrs = |
| 938 |
ElimConstraints.fold (fun (l, k, r) pbs -> |
| 939 |
let open ElimConstraint in |
| 940 |
match k with |
| 941 |
| ElimTo -> UnivProblem.Set.add (QElimTo (l, r)) pbs) |
| 942 |
cstrs UnivProblem.Set.empty |
| 943 |
|
| 944 |
let add_univ_constraints uctx cstrs = |
| 945 |
let cstrs = problem_of_univ_constraints cstrs in |
| 946 |
add_constraints ~src:Static uctx cstrs |
| 947 |
|
| 948 |
let add_poly_constraints ?src uctx (qcstrs, ucstrs) = |
| 949 |
let lvl_pbs = problem_of_univ_constraints ucstrs in |
| 950 |
let elim_pbs = problem_of_elim_constraints qcstrs in |
| 951 |
let uctx = add_constraints ?src uctx (UnivProblem.Set.union lvl_pbs elim_pbs) in |
| 952 |
let local = on_snd (fun cst -> PConstraints.union cst (PConstraints.of_qualities qcstrs)) uctx.local in |
| 953 |
let sort_variables = QState.merge_constraints (fun cst -> merge_elim_constraints ?src uctx qcstrs cst) uctx.sort_variables in |
| 954 |
{ uctx with local; sort_variables } |
| 955 |
|
| 956 |
let check_elim_constraints uctx csts = |
| 957 |
Sorts.ElimConstraints.for_all (fun (l,k,r) -> |
| 958 |
let l = nf_quality uctx l in |
| 959 |
let r = nf_quality uctx r in |
| 960 |
match l,k,r with |
| 961 |
| _, ElimTo, _ -> Inductive.eliminates_to (QState.elims uctx.sort_variables) l r) |
| 962 |
csts |
| 963 |
|
| 964 |
let check_eq_quality uctx q1 q2 = |
| 965 |
Sorts.Quality.equal q1 q2 || Sorts.Quality.equal (nf_quality uctx q1) (nf_quality uctx q2) |
| 966 |
|
| 967 |
let check_constraint uctx (c:UnivProblem.t) = |
| 968 |
match c with |
| 969 |
| QEq (a,b) -> |
| 970 |
let a = nf_quality uctx a in |
| 971 |
let b = nf_quality uctx b in |
| 972 |
Quality.equal a b |
| 973 |
| QLeq (a,b) -> |
| 974 |
let a = nf_quality uctx a in |
| 975 |
let b = nf_quality uctx b in |
| 976 |
Quality.equal a b || |
| 977 |
begin |
| 978 |
match a, b with |
| 979 |
| QConstant QProp, QConstant QType -> true |
| 980 |
| QConstant QProp, QVar q -> QState.is_above_prop uctx.sort_variables q |
| 981 |
| (QConstant _ | QVar _), _ -> false |
| 982 |
end |
| 983 |
| QElimTo (a, b) -> |
| 984 |
let a = nf_quality uctx a in |
| 985 |
let b = nf_quality uctx b in |
| 986 |
Inductive.eliminates_to (QState.elims uctx.sort_variables) a b |
| 987 |
| ULe (u,v) -> UGraph.check_leq_sort (fun q1 q2 -> check_eq_quality uctx q1 q2) uctx.universes u v |
| 988 |
| UEq (u,v) -> UGraph.check_eq_sort (fun q1 q2 -> check_eq_quality uctx q1 q2) uctx.universes u v |
| 989 |
| ULub (u,v) -> UGraph.check_eq_level uctx.universes u v |
| 990 |
| UWeak _ -> true |
| 991 |
|
| 992 |
let check_constraints uctx csts = |
| 993 |
UnivProblem.Set.for_all (check_constraint uctx) csts |
| 994 |
|
| 995 |
let constrain_variables diff uctx = |
| 996 |
let (us, (qcst, ucst)) = uctx.local in |
| 997 |
let (us, ucst), vars = UnivFlex.constrain_variables diff uctx.univ_variables (us, ucst) in |
| 998 |
{ uctx with local = (us, (qcst, ucst)); univ_variables = vars } |
| 999 |
|
| 1000 |
type ('a, 'b, 'c, 'd) gen_universe_decl = { |
| 1001 |
univdecl_qualities : 'a; |
| 1002 |
univdecl_extensible_qualities : bool; |
| 1003 |
univdecl_elim_constraints : 'b; |
| 1004 |
univdecl_instance : 'c; |
| 1005 |
univdecl_extensible_instance : bool; |
| 1006 |
univdecl_univ_constraints : 'd; |
| 1007 |
univdecl_extensible_constraints : bool; } |
| 1008 |
|
| 1009 |
type universe_decl = |
| 1010 |
(QVar.t list, Sorts.ElimConstraints.t, Level.t list, Univ.UnivConstraints.t) gen_universe_decl |
| 1011 |
|
| 1012 |
let default_univ_decl = |
| 1013 |
{ univdecl_qualities = []; |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
univdecl_extensible_qualities = true; |
| 1018 |
univdecl_elim_constraints = ElimConstraints.empty; |
| 1019 |
univdecl_instance = []; |
| 1020 |
univdecl_extensible_instance = true; |
| 1021 |
univdecl_univ_constraints = UnivConstraints.empty; |
| 1022 |
univdecl_extensible_constraints = true } |
| 1023 |
|
| 1024 |
let univ_decl_csts decl = |
| 1025 |
PConstraints.make decl.univdecl_elim_constraints decl.univdecl_univ_constraints |
| 1026 |
|
| 1027 |
let pr_error_unbound_universes quals univs names = |
| 1028 |
let open Pp in |
| 1029 |
let nqs = QVar.Set.cardinal quals in |
| 1030 |
let prqvar q = |
| 1031 |
let info = QVar.Map.find_opt q (fst (snd names)) in |
| 1032 |
h (pr_uctx_qvar_names names q ++ (match info with |
| 1033 |
| None | Some {uloc=None} -> mt () |
| 1034 |
| Some {uloc=Some loc} -> spc() ++ str"(" ++ Loc.pr loc ++ str")")) |
| 1035 |
in |
| 1036 |
let nus = Level.Set.cardinal univs in |
| 1037 |
let prlev u = |
| 1038 |
let info = Level.Map.find_opt u (snd (snd names)) in |
| 1039 |
h (pr_uctx_level_names names u ++ (match info with |
| 1040 |
| None | Some {uloc=None} -> mt () |
| 1041 |
| Some {uloc=Some loc} -> spc() ++ str"(" ++ Loc.pr loc ++ str")")) |
| 1042 |
in |
| 1043 |
let ppqs = if nqs > 0 then |
| 1044 |
str (if nqs = 1 then "Quality" else "Qualities") ++ spc () ++ |
| 1045 |
prlist_with_sep spc prqvar (QVar.Set.elements quals) |
| 1046 |
else mt() |
| 1047 |
in |
| 1048 |
let ppus = if nus > 0 then |
| 1049 |
let universe_s = CString.plural nus "universe" in |
| 1050 |
let universe_s = if nqs = 0 then CString.capitalize_ascii universe_s else universe_s in |
| 1051 |
str universe_s ++ spc () ++ |
| 1052 |
prlist_with_sep spc prlev (Level.Set.elements univs) |
| 1053 |
else mt() |
| 1054 |
in |
| 1055 |
(hv 0 |
| 1056 |
(ppqs ++ |
| 1057 |
(if nqs > 0 && nus > 0 then strbrk " and " else mt()) ++ |
| 1058 |
ppus ++ |
| 1059 |
spc () ++ str (CString.conjugate_verb_to_be (nus + nqs)) ++ str" unbound.")) |
| 1060 |
|
| 1061 |
exception UnboundUnivs of QVar.Set.t * Level.Set.t * univ_names |
| 1062 |
|
| 1063 |
|
| 1064 |
let error_unbound_universes qs us uctx = |
| 1065 |
let exception Found of Loc.t in |
| 1066 |
let loc = |
| 1067 |
try |
| 1068 |
Level.Set.iter (fun u -> |
| 1069 |
match Level.Map.find_opt u (snd (snd uctx)) with |
| 1070 |
| None -> () |
| 1071 |
| Some info -> match info.uloc with |
| 1072 |
| None -> () |
| 1073 |
| Some loc -> raise_notrace (Found loc)) |
| 1074 |
us; |
| 1075 |
QVar.Set.iter (fun s -> |
| 1076 |
match QVar.Map.find_opt s (fst (snd uctx)) with |
| 1077 |
| None -> () |
| 1078 |
| Some info -> match info.uloc with |
| 1079 |
| None -> () |
| 1080 |
| Some loc -> raise_notrace (Found loc)) |
| 1081 |
qs; |
| 1082 |
None |
| 1083 |
with Found loc -> Some loc |
| 1084 |
in |
| 1085 |
Loc.raise ?loc (UnboundUnivs (qs,us,uctx)) |
| 1086 |
|
| 1087 |
let () = CErrors.register_handler (function |
| 1088 |
| UnboundUnivs (qs,us,uctx) -> Some (pr_error_unbound_universes qs us uctx) |
| 1089 |
| _ -> None) |
| 1090 |
|
| 1091 |
let universe_context_inst decl qvars levels names = |
| 1092 |
let leftqs = List.fold_left (fun acc l -> QSet.remove l acc) qvars decl.univdecl_qualities in |
| 1093 |
let leftus = List.fold_left (fun acc l -> Level.Set.remove l acc) levels decl.univdecl_instance in |
| 1094 |
let () = |
| 1095 |
let unboundqs = if decl.univdecl_extensible_qualities then QSet.empty else leftqs in |
| 1096 |
let unboundus = if decl.univdecl_extensible_instance then Level.Set.empty else leftus in |
| 1097 |
if not (QSet.is_empty unboundqs && Level.Set.is_empty unboundus) |
| 1098 |
then error_unbound_universes unboundqs unboundus names |
| 1099 |
in |
| 1100 |
let leftqs = UContext.sort_qualities |
| 1101 |
(Array.map_of_list (fun q -> Quality.QVar q) (QVar.Set.elements leftqs)) |
| 1102 |
in |
| 1103 |
let leftus = UContext.sort_levels (Array.of_list (Level.Set.elements leftus)) in |
| 1104 |
let instq = Array.append |
| 1105 |
(Array.map_of_list (fun q -> QVar q) decl.univdecl_qualities) |
| 1106 |
leftqs |
| 1107 |
in |
| 1108 |
let instu = Array.append (Array.of_list decl.univdecl_instance) leftus in |
| 1109 |
let inst = Instance.of_array (instq,instu) in |
| 1110 |
inst |
| 1111 |
|
| 1112 |
let check_universe_context_set ~prefix levels names = |
| 1113 |
let left = |
| 1114 |
List.fold_left (fun left l -> Level.Set.remove l left) |
| 1115 |
levels prefix |
| 1116 |
in |
| 1117 |
if not (Level.Set.is_empty left) |
| 1118 |
then error_unbound_universes QVar.Set.empty left names |
| 1119 |
|
| 1120 |
let check_univ_implication uctx cstrs cstrs' = |
| 1121 |
let gr = uctx.initial_universes in |
| 1122 |
let grext = merge_univ_constraints uctx cstrs gr in |
| 1123 |
let cstrs' = UnivConstraints.filter (fun c -> not (UGraph.check_constraint grext c)) cstrs' in |
| 1124 |
if UnivConstraints.is_empty cstrs' then () |
| 1125 |
else CErrors.user_err |
| 1126 |
Pp.(str "Universe constraints are not implied by the ones declared: " ++ |
| 1127 |
UnivConstraints.pr (pr_uctx_level uctx) cstrs') |
| 1128 |
|
| 1129 |
let check_elim_implication uctx cstrs cstrs' = |
| 1130 |
let g = initial_elim_graph uctx in |
| 1131 |
let grext = merge_elim_constraints ~src:Rigid uctx cstrs g in |
| 1132 |
let cstrs' = ElimConstraints.filter (fun c -> not (QGraph.check_constraint grext c)) cstrs' in |
| 1133 |
if ElimConstraints.is_empty cstrs' then () |
| 1134 |
else CErrors.user_err |
| 1135 |
Pp.(str "Elimination constraints are not implied by the ones declared: " ++ |
| 1136 |
ElimConstraints.pr (pr_uctx_qvar uctx) cstrs') |
| 1137 |
|
| 1138 |
let check_implication uctx (elim_csts,univ_csts) (elim_csts',univ_csts') = |
| 1139 |
check_univ_implication uctx univ_csts univ_csts'; |
| 1140 |
check_elim_implication uctx elim_csts elim_csts' |
| 1141 |
|
| 1142 |
let check_template_univ_decl uctx ~template_qvars decl = |
| 1143 |
let () = |
| 1144 |
match List.filter (fun q -> not @@ QSet.mem q template_qvars) decl.univdecl_qualities with |
| 1145 |
| (_ :: _) as qvars -> |
| 1146 |
CErrors.user_err |
| 1147 |
Pp.(str "Qualities " ++ prlist_with_sep spc (pr_uctx_qvar uctx) qvars ++ |
| 1148 |
str " cannot be template.") |
| 1149 |
| [] -> |
| 1150 |
if not (QVar.Set.equal template_qvars (QState.undefined uctx.sort_variables)) |
| 1151 |
then CErrors.anomaly Pp.(str "Bugged template univ declaration.") |
| 1152 |
in |
| 1153 |
|
| 1154 |
|
| 1155 |
let () = assert (Sorts.ElimConstraints.is_empty decl.univdecl_elim_constraints) in |
| 1156 |
let levels, csts = uctx.local in |
| 1157 |
let () = |
| 1158 |
let prefix = decl.univdecl_instance in |
| 1159 |
if not decl.univdecl_extensible_instance |
| 1160 |
then check_universe_context_set ~prefix levels uctx.names |
| 1161 |
in |
| 1162 |
if decl.univdecl_extensible_constraints then |
| 1163 |
PContextSet.univ_context_set uctx.local |
| 1164 |
else |
| 1165 |
let () = check_implication uctx (univ_decl_csts decl) csts in |
| 1166 |
(levels, decl.univdecl_univ_constraints) |
| 1167 |
|
| 1168 |
let check_mono_univ_decl uctx decl = |
| 1169 |
|
| 1170 |
let () = |
| 1171 |
if not (List.is_empty decl.univdecl_qualities) |
| 1172 |
|| not (QSet.is_empty (QState.undefined uctx.sort_variables)) |
| 1173 |
then CErrors.user_err Pp.(str "Monomorphic declarations may not have sort variables.") |
| 1174 |
in |
| 1175 |
let levels, csts = uctx.local in |
| 1176 |
let () = |
| 1177 |
let prefix = decl.univdecl_instance in |
| 1178 |
if not decl.univdecl_extensible_instance |
| 1179 |
then check_universe_context_set ~prefix levels uctx.names |
| 1180 |
in |
| 1181 |
if decl.univdecl_extensible_constraints then check_mono_sort_constraints uctx |
| 1182 |
else |
| 1183 |
let () = assert (Sorts.ElimConstraints.is_empty (fst csts)) in |
| 1184 |
let () = check_implication uctx (univ_decl_csts decl) csts in |
| 1185 |
levels, decl.univdecl_univ_constraints |
| 1186 |
|
| 1187 |
let check_poly_univ_decl uctx decl = |
| 1188 |
|
| 1189 |
let levels, (elim_csts,univ_csts) = uctx.local in |
| 1190 |
let qvars = QState.undefined uctx.sort_variables in |
| 1191 |
let inst = universe_context_inst decl qvars levels uctx.names in |
| 1192 |
let nas = compute_instance_binders uctx inst in |
| 1193 |
let univ_csts = if decl.univdecl_extensible_constraints then univ_csts |
| 1194 |
else begin |
| 1195 |
check_univ_implication uctx |
| 1196 |
decl.univdecl_univ_constraints |
| 1197 |
univ_csts; |
| 1198 |
decl.univdecl_univ_constraints |
| 1199 |
end |
| 1200 |
in |
| 1201 |
let elim_csts = if decl.univdecl_extensible_constraints then elim_csts |
| 1202 |
else begin |
| 1203 |
check_elim_implication uctx |
| 1204 |
decl.univdecl_elim_constraints |
| 1205 |
elim_csts; |
| 1206 |
decl.univdecl_elim_constraints |
| 1207 |
end |
| 1208 |
in |
| 1209 |
let uctx = UContext.make nas (inst, (elim_csts,univ_csts)) in |
| 1210 |
uctx |
| 1211 |
|
| 1212 |
let check_univ_decl ~poly uctx decl = |
| 1213 |
let (binders, _) = uctx.names in |
| 1214 |
let entry = |
| 1215 |
if PolyFlags.univ_poly poly then Polymorphic_entry (check_poly_univ_decl uctx decl) |
| 1216 |
else Monomorphic_entry (check_mono_univ_decl uctx decl) |
| 1217 |
in |
| 1218 |
entry, binders |
| 1219 |
|
| 1220 |
let restrict_universe_context (univs, univ_csts) keep = |
| 1221 |
let removed = Level.Set.diff univs keep in |
| 1222 |
if Level.Set.is_empty removed then univs, univ_csts |
| 1223 |
else |
| 1224 |
let allunivs = UnivConstraints.fold (fun (u,_,v) all -> Level.Set.add u (Level.Set.add v all)) univ_csts univs in |
| 1225 |
let g = UGraph.initial_universes in |
| 1226 |
let g = Level.Set.fold (fun v g -> |
| 1227 |
if Level.is_set v then g else |
| 1228 |
UGraph.add_universe v ~strict:false g) allunivs g in |
| 1229 |
let g = UGraph.merge_constraints univ_csts g in |
| 1230 |
let allkept = Level.Set.union (UGraph.domain UGraph.initial_universes) (Level.Set.diff allunivs removed) in |
| 1231 |
let univ_csts = UGraph.constraints_for ~kept:allkept g in |
| 1232 |
let univ_csts = UnivConstraints.filter (fun (l,d,r) -> not (Level.is_set l && d == Le)) univ_csts in |
| 1233 |
(Level.Set.inter univs keep, univ_csts) |
| 1234 |
|
| 1235 |
let restrict_universe_pcontext (us, (qcst, ucst)) keep = |
| 1236 |
let (us, ucst) = restrict_universe_context (us, ucst) keep in |
| 1237 |
(us, (qcst, ucst)) |
| 1238 |
|
| 1239 |
let restrict uctx vars = |
| 1240 |
let vars = Id.Map.fold (fun na l vars -> Level.Set.add l vars) |
| 1241 |
(snd (fst uctx.names)) vars |
| 1242 |
in |
| 1243 |
let uctx' = restrict_universe_pcontext uctx.local vars in |
| 1244 |
{ uctx with local = uctx' } |
| 1245 |
|
| 1246 |
let restrict_even_binders uctx vars = |
| 1247 |
let uctx' = restrict_universe_pcontext uctx.local vars in |
| 1248 |
{ uctx with local = uctx' } |
| 1249 |
|
| 1250 |
let restrict_univ_constraints uctx csts = |
| 1251 |
let levels, (elim_csts,univ_csts) = uctx.local in |
| 1252 |
let uctx' = { uctx with local = (levels,(elim_csts,UnivConstraints.empty)); universes = uctx.initial_universes } in |
| 1253 |
add_univ_constraints uctx' csts |
| 1254 |
|
| 1255 |
let restrict_elim_constraints ?src uctx csts = |
| 1256 |
let levels, (elim_csts,univ_csts) = uctx.local in |
| 1257 |
let g = initial_elim_graph uctx in |
| 1258 |
|
| 1259 |
let sort_variables = QState.set_elims g uctx.sort_variables in |
| 1260 |
let sort_variables = QState.merge_constraints (fun cst -> merge_elim_constraints ?src uctx elim_csts cst) sort_variables in |
| 1261 |
{ uctx with local = (levels, (csts, univ_csts)); sort_variables } |
| 1262 |
|
| 1263 |
type rigid = |
| 1264 |
| UnivRigid |
| 1265 |
| UnivFlexible of bool |
| 1266 |
|
| 1267 |
let univ_rigid = UnivRigid |
| 1268 |
let univ_flexible = UnivFlexible false |
| 1269 |
let univ_flexible_alg = UnivFlexible true |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
let merge_universe_context ?loc ~sideff rigid uctx (levels, ucst) = |
| 1275 |
let declare g = |
| 1276 |
Level.Set.fold (fun u g -> |
| 1277 |
try UGraph.add_universe ~strict:false u g |
| 1278 |
with UGraph.AlreadyDeclared when sideff -> g) |
| 1279 |
levels g |
| 1280 |
in |
| 1281 |
let names = |
| 1282 |
let fold u accu = |
| 1283 |
let update = function |
| 1284 |
| None -> Some { uname = None; uloc = loc } |
| 1285 |
| Some info -> match info.uloc with |
| 1286 |
| None -> Some { info with uloc = loc } |
| 1287 |
| Some _ -> Some info |
| 1288 |
in |
| 1289 |
Level.Map.update u update accu |
| 1290 |
in |
| 1291 |
(fst uctx.names, (fst (snd uctx.names), Level.Set.fold fold levels (snd (snd uctx.names)))) |
| 1292 |
in |
| 1293 |
let initial = declare uctx.initial_universes in |
| 1294 |
let univs = declare uctx.universes in |
| 1295 |
let universes = merge_univ_constraints uctx ucst univs in |
| 1296 |
let uctx = |
| 1297 |
match rigid with |
| 1298 |
| UnivRigid -> uctx |
| 1299 |
| UnivFlexible b -> |
| 1300 |
assert (not sideff); |
| 1301 |
let uvars' = UnivFlex.add_levels levels ~algebraic:b uctx.univ_variables in |
| 1302 |
{ uctx with univ_variables = uvars' } |
| 1303 |
in |
| 1304 |
let (us, (qcst, ucst0)) = uctx.local in |
| 1305 |
let local = (Univ.Level.Set.union us levels, (qcst, Univ.UnivConstraints.union ucst0 ucst)) in |
| 1306 |
{ uctx with names; local; universes; |
| 1307 |
initial_universes = initial } |
| 1308 |
|
| 1309 |
let merge_sort_variables ?loc ?(sort_rigid=false) ?src ~sideff uctx (qvars, csts) = |
| 1310 |
let sort_variables = |
| 1311 |
QVar.Set.fold (fun qv qstate -> QState.add ~check_fresh:(not sideff) ~rigid:sort_rigid qv qstate) |
| 1312 |
qvars |
| 1313 |
uctx.sort_variables |
| 1314 |
in |
| 1315 |
let names = |
| 1316 |
let fold u accu = |
| 1317 |
let update = function |
| 1318 |
| None -> Some { uname = None; uloc = loc } |
| 1319 |
| Some info -> match info.uloc with |
| 1320 |
| None -> Some { info with uloc = loc } |
| 1321 |
| Some _ -> Some info |
| 1322 |
in |
| 1323 |
QVar.Map.update u update accu |
| 1324 |
in |
| 1325 |
let qrev = QVar.Set.fold fold qvars (fst (snd uctx.names)) in |
| 1326 |
(fst uctx.names, (qrev, snd (snd uctx.names))) |
| 1327 |
in |
| 1328 |
let sort_variables = QState.merge_constraints (merge_elim_constraints ?src uctx csts) sort_variables in |
| 1329 |
let (us, (qcst, ucst)) = uctx.local in |
| 1330 |
let local = (us, (Sorts.ElimConstraints.union qcst csts, ucst)) in |
| 1331 |
{ uctx with local; sort_variables; names } |
| 1332 |
|
| 1333 |
let merge_sort_context ?loc ?sort_rigid ?src ~sideff rigid uctx ((qvars, levels), (qcst, ucst)) = |
| 1334 |
let uctx = merge_sort_variables ?loc ?sort_rigid ?src ~sideff uctx (qvars, qcst) in |
| 1335 |
merge_universe_context ?loc ~sideff rigid uctx (levels, ucst) |
| 1336 |
|
| 1337 |
let demote_global_univs (lvl_set, univ_csts) uctx = |
| 1338 |
let (local_univs, local_constraints) = uctx.local in |
| 1339 |
let local_univs = Level.Set.diff local_univs lvl_set in |
| 1340 |
let univ_variables = Level.Set.fold UnivFlex.remove lvl_set uctx.univ_variables in |
| 1341 |
let update_ugraph g = |
| 1342 |
let g = Level.Set.fold (fun u g -> |
| 1343 |
try UGraph.add_universe u ~strict:true g |
| 1344 |
with UGraph.AlreadyDeclared -> g) |
| 1345 |
lvl_set |
| 1346 |
g |
| 1347 |
in |
| 1348 |
UGraph.merge_constraints univ_csts g |
| 1349 |
in |
| 1350 |
let initial_universes = update_ugraph uctx.initial_universes in |
| 1351 |
let universes = update_ugraph uctx.universes in |
| 1352 |
{ uctx with local = (local_univs, local_constraints); univ_variables; universes; initial_universes } |
| 1353 |
|
| 1354 |
let demote_global_univ_entry entry uctx = match entry with |
| 1355 |
| Monomorphic_entry ucst -> |
| 1356 |
demote_global_univs ucst uctx |
| 1357 |
| Polymorphic_entry _ -> uctx |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
let emit_side_effects eff u = |
| 1362 |
let uctx = Safe_typing.universes_of_private eff in |
| 1363 |
demote_global_univs uctx u |
| 1364 |
|
| 1365 |
let merge_seff uctx uctx' = |
| 1366 |
let levels = PContextSet.levels uctx' in |
| 1367 |
let declare g = |
| 1368 |
Level.Set.fold (fun u g -> |
| 1369 |
try UGraph.add_universe ~strict:false u g |
| 1370 |
with UGraph.AlreadyDeclared -> g) |
| 1371 |
levels g |
| 1372 |
in |
| 1373 |
let initial_universes = declare uctx.initial_universes in |
| 1374 |
let univs = declare uctx.universes in |
| 1375 |
let universes = merge_univ_constraints uctx (PContextSet.univ_constraints uctx') univs in |
| 1376 |
{ uctx with universes; initial_universes } |
| 1377 |
|
| 1378 |
let update_sigma_univs uctx univs = |
| 1379 |
let eunivs = |
| 1380 |
{ uctx with |
| 1381 |
initial_universes = univs; |
| 1382 |
universes = univs } |
| 1383 |
in |
| 1384 |
merge_seff eunivs eunivs.local |
| 1385 |
|
| 1386 |
let add_qnames ?loc s l ((qnames,unames), (qnames_rev,unames_rev)) = |
| 1387 |
if Id.Map.mem s qnames |
| 1388 |
then user_err ?loc |
| 1389 |
Pp.(str "Quality " ++ Id.print s ++ str" already bound."); |
| 1390 |
((Id.Map.add s l qnames, unames), |
| 1391 |
(QVar.Map.add l { uname = Some s; uloc = loc } qnames_rev, unames_rev)) |
| 1392 |
|
| 1393 |
let add_names ?loc s l ((qnames,unames), (qnames_rev,unames_rev)) = |
| 1394 |
if UNameMap.mem s unames |
| 1395 |
then user_err ?loc |
| 1396 |
Pp.(str "Universe " ++ Id.print s ++ str" already bound."); |
| 1397 |
((qnames,UNameMap.add s l unames), |
| 1398 |
(qnames_rev, Level.Map.add l { uname = Some s; uloc = loc } unames_rev)) |
| 1399 |
|
| 1400 |
let add_qloc l loc (names, (qnames_rev,unames_rev) as orig) = |
| 1401 |
match loc with |
| 1402 |
| None -> orig |
| 1403 |
| Some _ -> (names, (QVar.Map.add l { uname = None; uloc = loc } qnames_rev, unames_rev)) |
| 1404 |
|
| 1405 |
let add_loc l loc (names, (qnames_rev,unames_rev) as orig) = |
| 1406 |
match loc with |
| 1407 |
| None -> orig |
| 1408 |
| Some _ -> (names, (qnames_rev, Level.Map.add l { uname = None; uloc = loc } unames_rev)) |
| 1409 |
|
| 1410 |
let add_universe ?loc name strict uctx u = |
| 1411 |
let initial_universes = UGraph.add_universe ~strict u uctx.initial_universes in |
| 1412 |
let universes = UGraph.add_universe ~strict u uctx.universes in |
| 1413 |
let local = PContextSet.add_level u uctx.local in |
| 1414 |
let names = |
| 1415 |
match name with |
| 1416 |
| Some n -> add_names ?loc n u uctx.names |
| 1417 |
| None -> add_loc u loc uctx.names |
| 1418 |
in |
| 1419 |
{ uctx with names; local; initial_universes; universes } |
| 1420 |
|
| 1421 |
let new_sort_variable ?loc ?(sort_rigid = false) ?name uctx = |
| 1422 |
let q = UnivGen.fresh_sort_quality () in |
| 1423 |
|
| 1424 |
let sort_variables = QState.add ~check_fresh:false ~rigid:(sort_rigid || Option.has_some name) |
| 1425 |
q uctx.sort_variables |
| 1426 |
in |
| 1427 |
let names = match name with |
| 1428 |
| Some n -> add_qnames ?loc n q uctx.names |
| 1429 |
| None -> add_qloc q loc uctx.names |
| 1430 |
in |
| 1431 |
{ uctx with sort_variables; names }, q |
| 1432 |
|
| 1433 |
let new_univ_variable ?loc rigid name uctx = |
| 1434 |
let u = UnivGen.fresh_level () in |
| 1435 |
let uctx = |
| 1436 |
match rigid with |
| 1437 |
| UnivRigid -> uctx |
| 1438 |
| UnivFlexible algebraic -> |
| 1439 |
let univ_variables = UnivFlex.add u ~algebraic uctx.univ_variables in |
| 1440 |
{ uctx with univ_variables } |
| 1441 |
in |
| 1442 |
let uctx = add_universe ?loc name false uctx u in |
| 1443 |
uctx, u |
| 1444 |
|
| 1445 |
let add_forgotten_univ uctx u = add_universe None true uctx u |
| 1446 |
|
| 1447 |
let make_with_initial_binders ~qualities univs binders = |
| 1448 |
let uctx = make ~qualities univs in |
| 1449 |
List.fold_left |
| 1450 |
(fun uctx { CAst.loc; v = id } -> |
| 1451 |
fst (new_univ_variable ?loc univ_rigid (Some id) uctx)) |
| 1452 |
uctx binders |
| 1453 |
|
| 1454 |
let from_env ?(binders=[]) env = |
| 1455 |
make_with_initial_binders |
| 1456 |
~qualities:(Environ.qualities env) |
| 1457 |
(Environ.universes env) |
| 1458 |
binders |
| 1459 |
|
| 1460 |
let make_nonalgebraic_variable uctx u = |
| 1461 |
{ uctx with univ_variables = UnivFlex.make_nonalgebraic_variable uctx.univ_variables u } |
| 1462 |
|
| 1463 |
let make_flexible_nonalgebraic uctx = |
| 1464 |
{ uctx with univ_variables = UnivFlex.make_all_undefined_nonalgebraic uctx.univ_variables } |
| 1465 |
|
| 1466 |
let subst_univs_context_with_def def usubst (uctx, (elim_csts,univ_csts)) = |
| 1467 |
(Level.Set.diff uctx def, PConstraints.make elim_csts @@ |
| 1468 |
UnivSubst.subst_univs_constraints usubst univ_csts) |
| 1469 |
|
| 1470 |
let normalize_univ_variables uctx = |
| 1471 |
let normalized_variables, def, subst = |
| 1472 |
UnivFlex.normalize_univ_variables uctx.univ_variables |
| 1473 |
in |
| 1474 |
let uctx_local = subst_univs_context_with_def def subst uctx.local in |
| 1475 |
let univs = UGraph.merge_constraints (snd (snd uctx_local)) uctx.initial_universes in |
| 1476 |
{ uctx with |
| 1477 |
local = uctx_local; |
| 1478 |
univ_variables = normalized_variables; |
| 1479 |
universes = univs } |
| 1480 |
|
| 1481 |
let normalize_quality_variables uctx = |
| 1482 |
let (lvls, (elim_cstrs, lvl_cstrs)) = uctx.local in |
| 1483 |
let elim_cstrs = QState.normalize_elim_constraints uctx.sort_variables elim_cstrs in |
| 1484 |
{ uctx with local = (lvls, (elim_cstrs, lvl_cstrs)) } |
| 1485 |
|
| 1486 |
let normalize_variables uctx = |
| 1487 |
let uctx = normalize_univ_variables uctx in |
| 1488 |
normalize_quality_variables uctx |
| 1489 |
|
| 1490 |
let fix_undefined_variables uctx = |
| 1491 |
{ uctx with univ_variables = UnivFlex.fix_undefined_variables uctx.univ_variables } |
| 1492 |
|
| 1493 |
let collapse_above_prop_sort_variables ~to_prop uctx = |
| 1494 |
let sorts = QState.collapse_above_prop ~to_prop uctx.sort_variables in |
| 1495 |
normalize_quality_variables { uctx with sort_variables = sorts } |
| 1496 |
|
| 1497 |
let collapse_sort_variables ?except uctx = |
| 1498 |
let sorts = QState.collapse ?except uctx.sort_variables in |
| 1499 |
normalize_quality_variables { uctx with sort_variables = sorts } |
| 1500 |
|
| 1501 |
let minimize uctx = |
| 1502 |
let open UnivMinim in |
| 1503 |
let (us, (qcst, ucst)) = uctx.local in |
| 1504 |
let (vars', (us', ucst')) = |
| 1505 |
normalize_context_set uctx.universes (us, ucst) uctx.univ_variables |
| 1506 |
uctx.minim_extra |
| 1507 |
in |
| 1508 |
if Univ.ContextSet.equal (us', ucst') (us, ucst) then uctx |
| 1509 |
else |
| 1510 |
let universes = UGraph.merge_constraints ucst' uctx.initial_universes in |
| 1511 |
{ names = uctx.names; |
| 1512 |
local = (us', (qcst, ucst')); |
| 1513 |
univ_variables = vars'; |
| 1514 |
sort_variables = uctx.sort_variables; |
| 1515 |
universes = universes; |
| 1516 |
initial_universes = uctx.initial_universes; |
| 1517 |
minim_extra = UnivMinim.empty_extra; } |
| 1518 |
|
| 1519 |
let universe_context_inst_decl decl qvars levels names = |
| 1520 |
let leftqs = List.fold_left (fun acc l -> QSet.remove l acc) qvars decl.univdecl_qualities in |
| 1521 |
let leftus = List.fold_left (fun acc l -> Level.Set.remove l acc) levels decl.univdecl_instance in |
| 1522 |
let () = |
| 1523 |
let unboundqs = if decl.univdecl_extensible_qualities then QSet.empty else leftqs in |
| 1524 |
let unboundus = if decl.univdecl_extensible_instance then Level.Set.empty else leftus in |
| 1525 |
if not (QSet.is_empty unboundqs && Level.Set.is_empty unboundus) |
| 1526 |
then error_unbound_universes unboundqs unboundus names |
| 1527 |
in |
| 1528 |
let instq = Array.map_of_list (fun q -> QVar q) decl.univdecl_qualities in |
| 1529 |
let instu = Array.of_list decl.univdecl_instance in |
| 1530 |
let inst = Instance.of_array (instq,instu) in |
| 1531 |
inst |
| 1532 |
|
| 1533 |
let check_univ_decl_rev uctx decl = |
| 1534 |
let levels, (elim_csts,univ_csts as csts) = uctx.local in |
| 1535 |
let qvars = QState.undefined uctx.sort_variables in |
| 1536 |
let inst = universe_context_inst_decl decl qvars levels uctx.names in |
| 1537 |
let nas = compute_instance_binders uctx inst in |
| 1538 |
let () = check_implication uctx csts (univ_decl_csts decl) |
| 1539 |
in |
| 1540 |
let uctx = fix_undefined_variables uctx in |
| 1541 |
let uctx, univ_csts = |
| 1542 |
if decl.univdecl_extensible_constraints |
| 1543 |
then uctx, univ_csts |
| 1544 |
else restrict_univ_constraints uctx decl.univdecl_univ_constraints, |
| 1545 |
univ_csts |
| 1546 |
in |
| 1547 |
let uctx, elim_csts = |
| 1548 |
if decl.univdecl_extensible_constraints |
| 1549 |
then uctx, elim_csts |
| 1550 |
else restrict_elim_constraints ~src:Rigid uctx decl.univdecl_elim_constraints, |
| 1551 |
elim_csts |
| 1552 |
in |
| 1553 |
let uctx' = UContext.make nas (inst, (elim_csts,univ_csts)) in |
| 1554 |
uctx, uctx' |
| 1555 |
|
| 1556 |
let check_uctx_impl ~fail uctx uctx' = |
| 1557 |
let levels, (elim_csts,univ_csts) = uctx'.local in |
| 1558 |
let qvars_diff = |
| 1559 |
QVar.Set.diff |
| 1560 |
(QState.undefined uctx'.sort_variables) |
| 1561 |
(QState.undefined uctx.sort_variables) |
| 1562 |
in |
| 1563 |
let levels_diff = Level.Set.diff levels (fst uctx.local) in |
| 1564 |
let () = if not @@ (QVar.Set.is_empty qvars_diff && Level.Set.is_empty levels_diff) then |
| 1565 |
error_unbound_universes qvars_diff levels_diff uctx'.names |
| 1566 |
in |
| 1567 |
let () = |
| 1568 |
let grext = ugraph uctx in |
| 1569 |
let cstrs' = UnivConstraints.filter (fun c -> not (UGraph.check_constraint grext c)) univ_csts in |
| 1570 |
if UnivConstraints.is_empty cstrs' then () |
| 1571 |
else fail (UnivConstraints.pr (pr_uctx_level uctx) cstrs') |
| 1572 |
in |
| 1573 |
let () = |
| 1574 |
let grext = elim_graph uctx in |
| 1575 |
let cstrs' = ElimConstraints.filter (fun c -> not (QGraph.check_constraint grext c)) elim_csts in |
| 1576 |
if ElimConstraints.is_empty cstrs' then () |
| 1577 |
else fail (ElimConstraints.pr (pr_uctx_qvar uctx) cstrs') |
| 1578 |
in |
| 1579 |
() |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
let pr_weak prl {minim_extra={UnivMinim.weak_constraints=weak; above_prop}} = |
| 1584 |
let open Pp in |
| 1585 |
v 0 ( |
| 1586 |
prlist_with_sep cut (fun (u,v) -> h (prl u ++ str " ~ " ++ prl v)) (UPairSet.elements weak) |
| 1587 |
++ if UPairSet.is_empty weak || Level.Set.is_empty above_prop then mt() else cut () ++ |
| 1588 |
prlist_with_sep cut (fun u -> h (str "Prop <= " ++ prl u)) (Level.Set.elements above_prop)) |
| 1589 |
|
| 1590 |
let pr_sort_opt_subst uctx = QState.pr (qualid_of_qvar_names uctx.names) uctx.sort_variables |
| 1591 |
|
| 1592 |
let pr ctx = |
| 1593 |
let open Pp in |
| 1594 |
let prl = pr_uctx_level ctx in |
| 1595 |
let prq = pr_uctx_qvar ctx in |
| 1596 |
if is_empty ctx then mt () |
| 1597 |
else |
| 1598 |
v 0 |
| 1599 |
(str"UNIVERSES:"++brk(0,1)++ |
| 1600 |
h (PContextSet.pr prq prl (context_set ctx)) ++ fnl () ++ |
| 1601 |
UnivFlex.pr prl (subst ctx) ++ fnl() ++ |
| 1602 |
str"SORTS:"++brk(0,1)++ |
| 1603 |
h (pr_sort_opt_subst ctx) ++ fnl() ++ |
| 1604 |
str "WEAK CONSTRAINTS:"++brk(0,1)++ |
| 1605 |
h (pr_weak prl ctx) ++ fnl ()) |
| 1606 |
|
| 1607 |
module Internal = |
| 1608 |
struct |
| 1609 |
|
| 1610 |
let reboot env uctx = |
| 1611 |
let uctx_global = from_env env in |
| 1612 |
{ uctx_global with univ_variables = uctx.univ_variables; sort_variables = uctx.sort_variables } |
| 1613 |
|
| 1614 |
end |