Debian Codegraph @

kernel/univ.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created in Caml by Gérard Huet for CoC 4.8 [Dec 1988] *)
12 (* Functional code by Jean-Christophe Filliâtre for Coq V7.0 [1999] *)
13 (* Extension with algebraic universes by HH for Coq V7.0 [Sep 2001] *)
14 (* Additional support for sort-polymorphic inductive types by HH [Mar 2006] *)
15 (* Support for universe polymorphism by MS [2014] *)
16
17 (* Revisions by Bruno Barras, Hugo Herbelin, Pierre Letouzey, Matthieu
18 Sozeau, Pierre-Marie Pédrot *)
19
20 open Pp
21 open Util
22
23 (* Universes are stratified by a partial ordering $\le$.
24 Let $\~{}$ be the associated equivalence. We also have a strict ordering
25 $<$ between equivalence classes, and we maintain that $<$ is acyclic,
26 and contained in $\le$ in the sense that $[U]<[V]$ implies $U\le V$.
27
28 At every moment, we have a finite number of universes, and we
29 maintain the ordering in the presence of assertions $U<V$ and $U\le V$.
30
31 The equivalence $\~{}$ is represented by a tree structure, as in the
32 union-find algorithm. The assertions $<$ and $\le$ are represented by
33 adjacency lists *)
34
35 module UGlobal = struct
36 open Names
37
38 type t = {
39 library : DirPath.t;
40 process : string;
41 uid : int;
42 }
43
44 let make library process uid = { library; process; uid }
45
46 let repr x = (x.library, x.process, x.uid)
47
48 let equal u1 u2 =
49 Int.equal u1.uid u2.uid &&
50 DirPath.equal u1.library u2.library &&
51 String.equal u1.process u2.process
52
53 let hash u = Hashset.Combine.combine3 u.uid (String.hash u.process) (DirPath.hash u.library)
54
55 let compare u1 u2 =
56 let c = Int.compare u1.uid u2.uid in
57 if c <> 0 then c
58 else
59 let c = DirPath.compare u1.library u2.library in
60 if c <> 0 then c
61 else String.compare u1.process u2.process
62
63 let to_string { library = d; process = s; uid = n } =
64 DirPath.to_string d ^
65 (if CString.is_empty s then "" else "." ^ s) ^
66 "." ^ string_of_int n
67
68 end
69
70 module RawLevel =
71 struct
72
73 type t =
74 | Set
75 | Level of UGlobal.t
76 | Var of int
77
78 (* Hash-consing *)
79
80 let equal x y =
81 x == y ||
82 match x, y with
83 | Set, Set -> true
84 | Level l, Level l' -> UGlobal.equal l l'
85 | Var n, Var n' -> Int.equal n n'
86 | _ -> false
87
88 let compare u v =
89 match u, v with
90 | Set, Set -> 0
91 | Set, _ -> -1
92 | _, Set -> 1
93 | Level l1, Level l2 -> UGlobal.compare l1 l2
94 | Level _, _ -> -1
95 | _, Level _ -> 1
96 | Var n, Var m -> Int.compare n m
97
98 let hequal x y =
99 x == y ||
100 match x, y with
101 | Set, Set -> true
102 | UGlobal.(Level { library = d; process = s; uid = n }, Level { library = d'; process = s'; uid = n' }) ->
103 n == n' && s==s' && d == d'
104 | Var n, Var n' -> n == n'
105 | _ -> false
106
107 open Hashset.Combine
108
109 let hcons = function
110 | Set as x -> combinesmall 1 2, x
111 | UGlobal.(Level { library = d; process = s; uid = n }) as x ->
112 let hs, s' = CString.hcons s in
113 let hd, d' = Names.DirPath.hcons d in
114 let x = if s' == s && d' == d then x else Level (UGlobal.make d' s' n) in
115 combinesmall 3 (combine3 n hs hd), x
116 | Var n as x -> combinesmall 2 n, x
117
118 let hash = function
119 | Set -> combinesmall 1 2
120 | Var n -> combinesmall 2 n
121 | Level l -> combinesmall 3 (UGlobal.hash l)
122
123 end
124
125 module Level = struct
126
127 type raw_level = RawLevel.t =
128 | Set
129 | Level of UGlobal.t
130 | Var of int
131
132 (** Embed levels with their hash value *)
133 type t = {
134 hash : int;
135 data : RawLevel.t }
136
137 let equal x y =
138 x == y || Int.equal x.hash y.hash && RawLevel.equal x.data y.data
139
140 let hash x = x.hash
141
142 let data x = x.data
143
144 (** Hashcons on levels + their hash *)
145
146 module Self = struct
147 type nonrec t = t
148 let eq x y = x.hash == y.hash && RawLevel.hequal x.data y.data
149 let hashcons x =
150 let _, data' = RawLevel.hcons x.data in
151 x.hash, if x.data == data' then x else { x with data = data' }
152 end
153
154 let hcons =
155 let module H = Hashcons.Make(Self) in
156 Hashcons.simple_hcons H.generate H.hcons ()
157
158 let make l = snd @@ hcons { hash = RawLevel.hash l; data = l }
159
160 let set = make Set
161
162 let is_small x =
163 match data x with
164 | Level _ -> false
165 | Var _ -> false
166 | Set -> true
167
168 let is_set x =
169 match data x with
170 | Set -> true
171 | _ -> false
172
173 let compare u v =
174 if u == v then 0
175 else RawLevel.compare (data u) (data v)
176
177 let to_string x =
178 match data x with
179 | Set -> "Set"
180 | Level l -> UGlobal.to_string l
181 | Var n -> "Var(" ^ string_of_int n ^ ")"
182
183 let raw_pr u = str (to_string u)
184
185 let pr = raw_pr
186
187 let vars = Array.init 20 (fun i -> make (Var i))
188
189 let var n =
190 if n < 20 then vars.(n) else make (Var n)
191
192 let var_index u =
193 match data u with
194 | Var n -> Some n | _ -> None
195
196 let make qid = make (Level qid)
197
198 let name u =
199 match data u with
200 | Level l -> Some l
201 | _ -> None
202
203 (** Level maps *)
204
205 module Map = struct
206
207 module Self = struct type nonrec t = t let hash = hash let compare = compare end
208 module M = HMap.Make (Self)
209 include M
210
211 let lunion l r =
212 union (fun _k l _r -> Some l) l r
213
214 let diff ext orig =
215 fold (fun u v acc ->
216 if mem u orig then acc
217 else add u v acc)
218 ext empty
219
220 let pr prl f m =
221 h (prlist_with_sep fnl (fun (u, v) ->
222 prl u ++ f v) (bindings m))
223
224 end
225
226 module Set = struct
227 include Map.Set
228
229 let pr prl s =
230 hov 1 (str"{" ++ prlist_with_sep spc prl (elements s) ++ str"}")
231
232
233 module Huniverse_set =
234 Hashcons.Make(
235 struct
236 type nonrec t = t
237 let hashcons s =
238 fold (fun x (h,acc) ->
239 let hx, x = hcons x in
240 Hashset.Combine.combine h hx, add x acc)
241 s
242 (0,empty)
243 let eq s s' = Map.Set.equal s s'
244 end)
245
246 let hcons = Hashcons.simple_hcons Huniverse_set.generate Huniverse_set.hcons ()
247 end
248
249 end
250
251 (* An algebraic universe [universe] is either a universe variable
252 [Level.t] or a formal universe known to be greater than some
253 universe variables and strictly greater than some (other) universe
254 variables
255
256 Universes variables denote universes initially present in the term
257 to type-check and non variable algebraic universes denote the
258 universes inferred while type-checking: it is either the successor
259 of a universe present in the initial term to type-check or the
260 maximum of two algebraic universes
261 *)
262
263 module Universe =
264 struct
265 (* Invariants: non empty, sorted and without duplicates *)
266
267 module Expr =
268 struct
269 type t = Level.t * int
270
271 (* Hashing of expressions *)
272 module ExprHash =
273 struct
274 type t = Level.t * int
275 let hashcons (b,n as x) =
276 let hb, b' = Level.hcons b in
277 n + hb, if b' == b then x else (b',n)
278 let eq l1 l2 =
279 l1 == l2 ||
280 match l1,l2 with
281 | (b,n), (b',n') -> b == b' && n == n'
282
283 let hash (x, n) = n + Level.hash x
284
285 end
286
287 module H = Hashcons.Make(ExprHash)
288
289 let hcons =
290 Hashcons.simple_hcons H.generate H.hcons ()
291
292 let make l = (l, 0)
293
294 let compare u v =
295 if u == v then 0
296 else
297 let (x, n) = u and (x', n') = v in
298 let c = Int.compare n n' in
299 if Int.equal 0 c then Level.compare x x'
300 else c
301
302 let _, set = hcons (Level.set, 0)
303 let _, type1 = hcons (Level.set, 1)
304
305 let is_small = function
306 | (l,0) -> Level.is_small l
307 | _ -> false
308
309 let equal x y = x == y ||
310 (let (u,n) = x and (v,n') = y in
311 Int.equal n n' && Level.equal u v)
312
313 let successor (u,n as e) =
314 if is_small e then type1
315 else (u, n + 1)
316
317 let pr_with f (v, n) =
318 if Int.equal n 0 then f v
319 else f v ++ str"+" ++ int n
320
321 let is_level = function
322 | (_v, 0) -> true
323 | _ -> false
324
325 let level = function
326 | (v,0) -> Some v
327 | _ -> None
328
329 let get_level (v,_n) = v
330
331 let map f (v, n as x) =
332 let v' = f v in
333 if v' == v then x
334 else (v', n)
335
336 end
337
338 type t = Expr.t list
339
340 let tip l = [l]
341
342 let rec hash = function
343 | [] -> 0
344 | e :: l -> Hashset.Combine.combinesmall (Expr.ExprHash.hash e) (hash l)
345
346 let equal x y = x == y || List.equal Expr.equal x y
347
348 let compare x y = if x == y then 0 else List.compare Expr.compare x y
349
350 module Huniv = Hashcons.Hlist(Expr)
351
352 let hcons = Hashcons.simple_hcons Huniv.generate Huniv.hcons ()
353
354 module Self = struct type nonrec t = t let compare = compare end
355 module Map = CMap.Make(Self)
356 module Set = CSet.Make(Self)
357
358 let make l = tip (Expr.make l)
359 let maken l n = tip (l, n)
360 let tip x = tip x
361
362 let pr f l = match l with
363 | [u] -> Expr.pr_with f u
364 | _ ->
365 str "max(" ++ hov 0
366 (prlist_with_sep pr_comma (Expr.pr_with f) l) ++
367 str ")"
368
369 let raw_pr l = pr Level.raw_pr l
370
371 let is_level l = match l with
372 | [l] -> Expr.is_level l
373 | _ -> false
374
375 let rec is_levels l = match l with
376 | l :: r -> Expr.is_level l && is_levels r
377 | [] -> true
378
379 let level l = match l with
380 | [l] -> Expr.level l
381 | _ -> None
382
383 let levels ?(init=Level.Set.empty) l =
384 let fold acc x =
385 let l = Expr.get_level x in
386 Level.Set.add l acc
387 in
388 List.fold_left fold init l
389
390 let is_small u =
391 match u with
392 | [l] -> Expr.is_small l
393 | _ -> false
394
395 (* The level of sets *)
396 let type0 = tip Expr.set
397
398 (* When typing [Prop] and [Set], there is no constraint on the level,
399 hence the definition of [type1_univ], the type of [Prop] *)
400 let type1 = tip Expr.type1
401
402 let is_type0 x = equal type0 x
403
404 (* Returns the formal universe that lies just above the universe variable u.
405 Used to type the sort u. *)
406 let super l =
407 if is_small l then type1
408 else
409 List.Smart.map (fun x -> Expr.successor x) l
410
411 (* Returns the formal universe that is greater than the universes u and v.
412 Used to type the products. *)
413 let rec sup (l1:t) (l2:t) : t =
414 match l1, l2 with
415 | [], _ -> l2
416 | _, [] -> l1
417 | h1 :: t1, h2 :: t2 ->
418 let cmp = Level.compare (fst h1) (fst h2) in
419 if Int.equal cmp 0 then
420 if (snd h1 : int) < snd h2
421 then sup t1 l2
422 else sup l1 t2
423 else if cmp <= 0
424 then h1 :: (sup t1 l2)
425 else h2 :: (sup l1 t2)
426
427 let exists = List.exists
428
429 let for_all = List.for_all
430 let repr x : t = x
431 let unrepr l =
432 assert (not (List.is_empty l));
433 List.fold_right (fun a acc -> sup [a] acc) l []
434
435 let map f u =
436 let f = Expr.map f in
437 let u' = List.Smart.map f u in
438 if u == u' then u
439 else unrepr u'
440 end
441
442 module UnivConstraint = struct
443 type kind = AcyclicGraph.constraint_type = Lt | Le | Eq
444
445 let eq_kind c1 c2 =
446 match c1, c2 with
447 | Lt, Lt | Le, Le | Eq, Eq -> true
448 | _ -> false
449
450 let compare_kind c1 c2 =
451 match c1, c2 with
452 | Lt, Lt -> 0
453 | Lt, _ -> -1
454 | Le, Lt -> 1
455 | Le, Le -> 0
456 | Le, Eq -> -1
457 | Eq, Eq -> 0
458 | Eq, _ -> 1
459
460 let pr_kind op =
461 let op_str = match op with
462 | Lt -> " < "
463 | Le -> " <= "
464 | Eq -> " = "
465 in str op_str
466
467 let hash_kind = function
468 | Lt -> 0
469 | Le -> 1
470 | Eq -> 2
471
472 type t = Level.t * kind * Level.t
473
474 let compare (u, c, v) (u', c', v') =
475 let i = compare_kind c c' in
476 if not (Int.equal i 0) then i
477 else
478 let i' = Level.compare u u' in
479 if not (Int.equal i' 0) then i'
480 else Level.compare v v'
481
482 module Hstruct = struct
483 type nonrec t = t
484
485 let hashcons (l1,k,l2) =
486 let hl1, l1 = Level.hcons l1 in
487 let hl2, l2 = Level.hcons l2 in
488 Hashset.Combine.(combinesmall (hash_kind k) (combine hl1 hl2)), (l1,k,l2)
489
490 let eq (l1,k,l2) (l1',k',l2') =
491 eq_kind k k' && l1 == l1' && l2 == l2'
492 end
493
494 module Hasher = Hashcons.Make(Hstruct)
495
496 let hcons = Hashcons.simple_hcons Hasher.generate Hasher.hcons ()
497 end
498
499 module UnivConstraints =
500 struct
501 module S = Set.Make(UnivConstraint)
502 include S
503
504 let pr prl c =
505 v 0 (prlist_with_sep spc (fun (u1,op,u2) ->
506 hov 0 (prl u1 ++ UnivConstraint.pr_kind op ++ prl u2))
507 (elements c))
508
509 module Hconstraints = CSet.Hashcons(UnivConstraint)(struct
510 type t = UnivConstraint.t
511 let hcons = UnivConstraint.hcons
512 end)
513
514 let hcons = Hashcons.simple_hcons Hconstraints.generate Hconstraints.hcons ()
515 end
516
517 (** UnivConstraints functions. *)
518
519 type 'a constraint_function = 'a -> 'a -> UnivConstraints.t -> UnivConstraints.t
520
521 let enforce_eq_level u v c =
522 (* We discard trivial constraints like u=u *)
523 if Level.equal u v then c
524 else UnivConstraints.add (u,UnivConstraint.Eq,v) c
525
526 let enforce_leq_level u v c =
527 if Level.equal u v then c else UnivConstraints.add (u,UnivConstraint.Le,v) c
528
529 (* Miscellaneous functions to remove or test local univ assumed to
530 occur in a universe *)
531
532 let univ_level_mem u v =
533 List.exists (fun (l, n) -> Int.equal n 0 && Level.equal u l) v
534
535 let univ_level_rem u v min =
536 match Universe.level v with
537 | Some u' -> if Level.equal u u' then min else v
538 | None -> List.filter (fun (l, n) -> not (Int.equal n 0 && Level.equal u l)) v
539
540 (* Is u mentioned in v (or equals to v) ? *)
541
542 module ContextSet =
543 struct
544 type t = Level.Set.t * UnivConstraints.t
545
546 let empty = (Level.Set.empty, UnivConstraints.empty)
547 let is_empty (univs, cst) = Level.Set.is_empty univs && UnivConstraints.is_empty cst
548
549 let equal (univs, cst as x) (univs', cst' as y) =
550 x == y || (Level.Set.equal univs univs' && UnivConstraints.equal cst cst')
551
552 let of_set s = (s, UnivConstraints.empty)
553 let singleton l = of_set (Level.Set.singleton l)
554
555 let union (univs, cst as x) (univs', cst' as y) =
556 if x == y then x
557 else Level.Set.union univs univs', UnivConstraints.union cst cst'
558
559 let append (univs, cst) (univs', cst') =
560 let univs = Level.Set.fold Level.Set.add univs univs' in
561 let cst = UnivConstraints.fold UnivConstraints.add cst cst' in
562 (univs, cst)
563
564 let diff (univs, cst) (univs', cst') =
565 Level.Set.diff univs univs', UnivConstraints.diff cst cst'
566
567 let add_universe u (univs, cst) =
568 Level.Set.add u univs, cst
569
570 let add_constraints cst' (univs, cst) =
571 univs, UnivConstraints.union cst cst'
572
573 let pr prl (univs, cst as ctx) =
574 if is_empty ctx then mt() else
575 hov 0 (h (Level.Set.pr prl univs ++ str " |=") ++ brk(1,2) ++ h (UnivConstraints.pr prl cst))
576
577 let constraints (_univs, cst) = cst
578 let levels (univs, _cst) = univs
579
580 let size (univs,_) = Level.Set.cardinal univs
581
582 let hcons (v,c) =
583 let hv, v = Level.Set.hcons v in
584 let hc, c = UnivConstraints.hcons c in
585 Hashset.Combine.combine hv hc, (v, c)
586
587 end