| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Univ |
| 12 |
open UVars |
| 13 |
open UnivConstraint |
| 14 |
|
| 15 |
module G = AcyclicGraph.Make(struct |
| 16 |
type t = Level.t |
| 17 |
module Set = Level.Set |
| 18 |
module Map = Level.Map |
| 19 |
|
| 20 |
let equal = Level.equal |
| 21 |
let compare = Level.compare |
| 22 |
|
| 23 |
let raw_pr = Level.raw_pr |
| 24 |
|
| 25 |
let anomaly_err u = Pp.(str"Universe " ++ Level.raw_pr u ++ str" undefined.") |
| 26 |
end) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
type t = { |
| 32 |
graph: G.t; |
| 33 |
type_in_type: bool; |
| 34 |
above_prop_qvars: Sorts.QVar.Set.t; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
type path_explanation = G.explanation Lazy.t |
| 41 |
|
| 42 |
type explanation = |
| 43 |
| Path of path_explanation |
| 44 |
| Other of Pp.t |
| 45 |
|
| 46 |
type univ_variable_printers = (Sorts.QVar.t -> Pp.t) * (Level.t -> Pp.t) |
| 47 |
type univ_inconsistency = univ_variable_printers option * (UnivConstraint.kind * Sorts.t * Sorts.t * explanation option) |
| 48 |
|
| 49 |
exception UniverseInconsistency of univ_inconsistency |
| 50 |
|
| 51 |
type 'a check_function = t -> 'a -> 'a -> bool |
| 52 |
|
| 53 |
let set_type_in_type b g = {g with type_in_type=b} |
| 54 |
|
| 55 |
let type_in_type g = g.type_in_type |
| 56 |
|
| 57 |
let check_smaller_expr g (u,n) (v,m) = |
| 58 |
let diff = n - m in |
| 59 |
match diff with |
| 60 |
| 0 -> G.check_leq g.graph u v |
| 61 |
| 1 -> G.check_lt g.graph u v |
| 62 |
| x when x < 0 -> G.check_leq g.graph u v |
| 63 |
| _ -> false |
| 64 |
|
| 65 |
let exists_bigger g ul l = |
| 66 |
Universe.exists (fun ul' -> |
| 67 |
check_smaller_expr g ul ul') l |
| 68 |
|
| 69 |
let real_check_leq g u v = |
| 70 |
Universe.for_all (fun ul -> exists_bigger g ul v) u |
| 71 |
|
| 72 |
let check_leq g u v = |
| 73 |
type_in_type g || Universe.equal u v || (real_check_leq g u v) |
| 74 |
|
| 75 |
let check_eq g u v = |
| 76 |
type_in_type g || Universe.equal u v || |
| 77 |
(real_check_leq g u v && real_check_leq g v u) |
| 78 |
|
| 79 |
let check_eq_level g u v = |
| 80 |
u == v || type_in_type g || G.check_eq g.graph u v |
| 81 |
|
| 82 |
let empty_universes = { |
| 83 |
graph=G.empty; |
| 84 |
type_in_type=false; |
| 85 |
above_prop_qvars=Sorts.QVar.Set.empty; |
| 86 |
} |
| 87 |
|
| 88 |
let initial_universes = |
| 89 |
let big_rank = 1000000 in |
| 90 |
let g = G.empty in |
| 91 |
let g = G.add ~rank:big_rank Level.set g in |
| 92 |
{empty_universes with graph=g} |
| 93 |
|
| 94 |
let initial_universes_with g = {g with graph=initial_universes.graph} |
| 95 |
|
| 96 |
let enforce_constraint (u,d,v) g = |
| 97 |
match d with |
| 98 |
| Le -> G.enforce_leq u v g |
| 99 |
| Lt -> G.enforce_lt u v g |
| 100 |
| Eq -> G.enforce_eq u v g |
| 101 |
|
| 102 |
let enforce_constraint0 cst g = match enforce_constraint cst g.graph with |
| 103 |
| None -> None |
| 104 |
| Some g' -> |
| 105 |
if g' == g.graph then Some g |
| 106 |
else Some { g with graph = g' } |
| 107 |
|
| 108 |
let enforce_constraint cst g = match enforce_constraint0 cst g with |
| 109 |
| None -> |
| 110 |
if not (type_in_type g) then |
| 111 |
let (u, c, v) = cst in |
| 112 |
let e = lazy (G.get_explanation cst g.graph) in |
| 113 |
let mk u = Sorts.sort_of_univ @@ Universe.make u in |
| 114 |
raise (UniverseInconsistency (None, (c, mk u, mk v, Some (Path e)))) |
| 115 |
else g |
| 116 |
| Some g -> g |
| 117 |
|
| 118 |
let merge_constraints csts g = UnivConstraints.fold enforce_constraint csts g |
| 119 |
|
| 120 |
let check_constraint { graph = g; type_in_type; _ } (u,d,v) = |
| 121 |
type_in_type |
| 122 |
|| match d with |
| 123 |
| Le -> G.check_leq g u v |
| 124 |
| Lt -> G.check_lt g u v |
| 125 |
| Eq -> G.check_eq g u v |
| 126 |
|
| 127 |
let check_constraints csts g = UnivConstraints.for_all (check_constraint g) csts |
| 128 |
|
| 129 |
let is_above_prop ugraph q = |
| 130 |
Sorts.QVar.Set.mem q ugraph.above_prop_qvars |
| 131 |
|
| 132 |
let check_type_in_type_qualities q1 q2 = |
| 133 |
let open Sorts.Quality in |
| 134 |
if Sorts.Quality.equal q1 q2 then true |
| 135 |
else |
| 136 |
match q1, q2 with |
| 137 |
| QConstant (QSProp | QProp), _ | _, QConstant (QSProp | QProp) -> true |
| 138 |
| (QConstant _ | QVar _), _ -> false |
| 139 |
|
| 140 |
let check_eq_sort qeq univs s1 s2 = |
| 141 |
if type_in_type univs then |
| 142 |
check_eq univs (Sorts.univ_of_sort s1) (Sorts.univ_of_sort s2) || |
| 143 |
check_type_in_type_qualities (Sorts.quality s1) (Sorts.quality s2) |
| 144 |
else |
| 145 |
let u1 = Sorts.univ_of_sort s1 in |
| 146 |
let u2 = Sorts.univ_of_sort s2 in |
| 147 |
let q1 = Sorts.quality s1 in |
| 148 |
let q2 = Sorts.quality s2 in |
| 149 |
qeq q1 q2 && check_eq univs u1 u2 |
| 150 |
|
| 151 |
let check_leq_sort qeq univs s1 s2 = |
| 152 |
if type_in_type univs then |
| 153 |
check_leq univs (Sorts.univ_of_sort s1) (Sorts.univ_of_sort s2) || |
| 154 |
check_type_in_type_qualities (Sorts.quality s1) (Sorts.quality s2) |
| 155 |
else |
| 156 |
match s1, s2 with |
| 157 |
| (SProp, SProp) | (Prop, Prop) | (Set, Set) -> true |
| 158 |
| (Prop, (Set | Type _)) -> true |
| 159 |
| (Prop, QSort (q, _)) -> is_above_prop univs q |
| 160 |
| (Type _ | Set), (Set | Type _) -> check_leq univs (Sorts.univ_of_sort s1) (Sorts.univ_of_sort s2) |
| 161 |
| (QSort (s1, u1), QSort (s2, u2)) -> qeq (Sorts.Quality.QVar s1) (Sorts.Quality.QVar s2) && check_leq univs u1 u2 |
| 162 |
| (QSort (q, u1), Type u2) -> is_above_prop univs q && check_leq univs u1 u2 |
| 163 |
| ((SProp | Prop | Set | Type _ | QSort _), _) -> false |
| 164 |
|
| 165 |
let leq_expr (u,m) (v,n) = |
| 166 |
let d = match m - n with |
| 167 |
| 1 -> Lt |
| 168 |
| diff -> assert (diff <= 0); Le |
| 169 |
in |
| 170 |
(u,d,v) |
| 171 |
|
| 172 |
let enforce_leq_alg u v g = |
| 173 |
let open Util in |
| 174 |
let enforce_one (u,v) = function |
| 175 |
| Inr _ as orig -> orig |
| 176 |
| Inl (cstrs,g) as orig -> |
| 177 |
if check_smaller_expr g u v then orig |
| 178 |
else |
| 179 |
(let c = leq_expr u v in |
| 180 |
match enforce_constraint0 c g with |
| 181 |
| Some g -> Inl (UnivConstraints.add c cstrs,g) |
| 182 |
| None -> Inr (c, g)) |
| 183 |
in |
| 184 |
|
| 185 |
let c = List.map (fun u -> List.map (fun v -> (u,v)) (Universe.repr v)) (Universe.repr u) in |
| 186 |
let c = List.cartesians enforce_one (Inl (UnivConstraints.empty,g)) c in |
| 187 |
|
| 188 |
let order x y = match x, y with |
| 189 |
| Inr _, Inr _ -> 0 |
| 190 |
| Inl _, Inr _ -> -1 |
| 191 |
| Inr _, Inl _ -> 1 |
| 192 |
| Inl (c,_), Inl (c',_) -> |
| 193 |
Int.compare (UnivConstraints.cardinal c) (UnivConstraints.cardinal c') |
| 194 |
in |
| 195 |
match List.min order c with |
| 196 |
| Inl x -> x |
| 197 |
| Inr ((u, c, v), g) -> |
| 198 |
let e = lazy (G.get_explanation (u, c, v) g.graph) in |
| 199 |
let mk u = Sorts.sort_of_univ @@ Universe.make u in |
| 200 |
let e = UniverseInconsistency (None, (c, mk u, mk v, Some (Path e))) in |
| 201 |
raise e |
| 202 |
|
| 203 |
exception AlreadyDeclared = G.AlreadyDeclared |
| 204 |
let add_universe u ~strict g = |
| 205 |
let graph = G.add u g.graph in |
| 206 |
let d = if strict then Lt else Le in |
| 207 |
enforce_constraint (Level.set, d, u) { g with graph } |
| 208 |
|
| 209 |
let check_declared_universes g l = |
| 210 |
G.check_declared g.graph l |
| 211 |
|
| 212 |
let constraints_of_universes g = |
| 213 |
let add cst accu = UnivConstraints.add cst accu in |
| 214 |
G.constraints_of g.graph add UnivConstraints.empty |
| 215 |
let constraints_for ~kept g = |
| 216 |
let add cst accu = UnivConstraints.add cst accu in |
| 217 |
G.constraints_for ~kept g.graph add UnivConstraints.empty |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
let check_eq_instances qeq univs t1 t2 = |
| 222 |
let qt1, ut1 = Instance.to_array t1 in |
| 223 |
let qt2, ut2 = Instance.to_array t2 in |
| 224 |
CArray.equal qeq qt1 qt2 |
| 225 |
&& CArray.equal (check_eq_level univs) ut1 ut2 |
| 226 |
|
| 227 |
let domain g = G.domain g.graph |
| 228 |
let choose p g u = G.choose p g.graph u |
| 229 |
|
| 230 |
let check_universes_invariants g = G.check_invariants ~required_canonical:Level.is_set g.graph |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
let pr_pmap sep pr map = |
| 235 |
let cmp (u,_) (v,_) = Level.compare u v in |
| 236 |
Pp.prlist_with_sep sep pr (List.sort cmp (Level.Map.bindings map)) |
| 237 |
|
| 238 |
let pr_arc prl = let open Pp in |
| 239 |
function |
| 240 |
| u, G.Node ltle -> |
| 241 |
if Level.Map.is_empty ltle then mt () |
| 242 |
else |
| 243 |
prl u ++ str " " ++ |
| 244 |
v 0 |
| 245 |
(pr_pmap spc (fun (v, strict) -> |
| 246 |
(if strict then str "< " else str "<= ") ++ prl v) |
| 247 |
ltle) ++ |
| 248 |
fnl () |
| 249 |
| u, G.Alias v -> |
| 250 |
prl u ++ str " = " ++ prl v ++ fnl () |
| 251 |
|
| 252 |
type node = G.node = |
| 253 |
| Alias of Level.t |
| 254 |
| Node of bool Level.Map.t |
| 255 |
|
| 256 |
let repr g = G.repr g.graph |
| 257 |
|
| 258 |
let pr_universes prl g = pr_pmap Pp.mt (pr_arc prl) g |
| 259 |
|
| 260 |
open Pp |
| 261 |
|
| 262 |
let explain_universe_inconsistency default_prq default_prl (printers, (o,u,v,p) : univ_inconsistency) = |
| 263 |
let prq, prl = match printers with |
| 264 |
| Some (prq, prl) -> prq, prl |
| 265 |
| None -> default_prq, default_prl |
| 266 |
in |
| 267 |
let pr_uni u = match u with |
| 268 |
| Sorts.Set -> str "Set" |
| 269 |
| Sorts.Prop -> str "Prop" |
| 270 |
| Sorts.SProp -> str "SProp" |
| 271 |
| Sorts.Type u -> Universe.pr prl u |
| 272 |
| Sorts.QSort (q, u) -> str "Type@{" ++ prq q ++ str " | " ++ Universe.pr prl u ++ str"}" |
| 273 |
in |
| 274 |
let pr_rel = function |
| 275 |
| Eq -> str"=" | Lt -> str"<" | Le -> str"<=" |
| 276 |
in |
| 277 |
let reason = match p with |
| 278 |
| None -> mt() |
| 279 |
| Some (Other p) -> spc() ++ p |
| 280 |
| Some (Path p) -> |
| 281 |
let pstart, p = Lazy.force p in |
| 282 |
if p = [] then mt () |
| 283 |
else |
| 284 |
str " because" ++ spc() ++ prl pstart ++ |
| 285 |
prlist (fun (r,v) -> spc() ++ pr_rel r ++ str" " ++ prl v) p |
| 286 |
in |
| 287 |
str "Cannot enforce" ++ spc() ++ pr_uni u ++ spc() ++ |
| 288 |
pr_rel o ++ spc() ++ pr_uni v ++ reason |
| 289 |
|
| 290 |
module Internal = struct |
| 291 |
let add_template_qvars qvars g = |
| 292 |
assert (Sorts.QVar.Set.is_empty g.above_prop_qvars); |
| 293 |
{g with above_prop_qvars=qvars} |
| 294 |
|
| 295 |
let is_above_prop = is_above_prop |
| 296 |
end |