Debian Codegraph @

kernel/sorts.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 open Univ
12
13 module QGlobal = struct
14 open Names
15
16 type t = {
17 library : DirPath.t;
18 id : Id.t
19 }
20
21 let make library id = { library ; id }
22
23 let repr x = (x.library, x.id)
24
25 let equal u1 u2 =
26 Id.equal u1.id u2.id &&
27 DirPath.equal u1.library u2.library
28
29 let hash u = Hashset.Combine.combine (Id.hash u.id) (DirPath.hash u.library)
30
31 let compare u1 u2 =
32 let c = Id.compare u1.id u2.id in
33 if c <> 0 then c
34 else
35 DirPath.compare u1.library u2.library
36
37 let to_string { library = d ; id } =
38 DirPath.to_string d ^ "." ^ Id.to_string id
39 end
40
41 module QVar =
42 struct
43 type repr =
44 | Var of int
45 | Unif of string * int
46 | Global of QGlobal.t
47 type t = repr
48
49 let make_var n = Var n
50
51 let make_unif s n = Unif (s,n)
52
53 let make_global id = Global id
54
55 let var_index = function
56 | Var q -> Some q
57 | Unif _ -> None
58 | Global _ -> None
59
60 let hash = function
61 | Var q -> Hashset.Combine.combinesmall 1 q
62 | Unif (s,q) -> Hashset.Combine.(combinesmall 2 (combine (CString.hash s) q))
63 | Global id -> Hashset.Combine.combinesmall 3 (QGlobal.hash id)
64
65 module Hstruct = struct
66 type nonrec t = t
67
68 open Hashset.Combine
69
70 let hashcons = function
71 | Var qv as q -> combinesmall 1 qv, q
72 | Unif (s,i) as q ->
73 let hs, s' = CString.hcons s in
74 combinesmall 2 (combine hs i), if s == s' then q else Unif (s',i)
75 | Global id as q -> combinesmall 3 (QGlobal.hash id), q
76
77 let eq a b =
78 match a, b with
79 | Var a, Var b -> Int.equal a b
80 | Unif (sa, ia), Unif (sb, ib) -> sa == sb && Int.equal ia ib
81 | Global ida, Global idb -> QGlobal.equal ida idb
82 | (Var _ | Unif _| Global _), _ -> false
83 end
84
85 module Hasher = Hashcons.Make(Hstruct)
86
87 let hcons = Hashcons.simple_hcons Hasher.generate Hasher.hcons ()
88
89 let compare a b = match a, b with
90 | Var a, Var b -> Int.compare a b
91 | Unif (s1,i1), Unif (s2,i2) ->
92 let c = Int.compare i1 i2 in
93 if c <> 0 then c
94 else CString.compare s1 s2
95 | Global ida, Global idb -> QGlobal.compare ida idb
96 | Var _, _ -> -1
97 | _, Var _ -> 1
98 | Unif _, _ -> -1
99 | _, Unif _ -> 1
100
101 let equal a b = match a, b with
102 | Var a, Var b -> Int.equal a b
103 | Unif (s1,i1), Unif (s2,i2) ->
104 Int.equal i1 i2 && CString.equal s1 s2
105 | Global ida, Global idb -> QGlobal.equal ida idb
106 | (Var _| Unif _ | Global _), _ -> false
107
108 let to_string = function
109 | Var q -> Printf.sprintf "β%d" q
110 | Unif (s,q) ->
111 let s = if CString.is_empty s then "" else s^"." in
112 Printf.sprintf "%sα%d" s q
113 | Global id -> Printf.sprintf "γ%s" (QGlobal.to_string id)
114
115 let raw_pr q = Pp.str (to_string q)
116
117 let repr x = x
118 let of_repr x = x
119
120 let is_unif = function
121 | Unif _ -> true
122 | (Var _ | Global _) -> false
123
124 let is_global = function
125 | Global _ -> true
126 | (Unif _ | Var _) -> false
127
128 module Self = struct type nonrec t = t let compare = compare end
129 module Set =
130 struct
131 include CSet.Make(Self)
132 let pr prl s =
133 let open Pp in
134 hov 1 (str"{" ++ prlist_with_sep spc prl (elements s) ++ str"}")
135 end
136 module Map = CMap.Make(Self)
137 end
138
139 module Quality = struct
140 type constant = QProp | QSProp | QType
141 type t = QVar of QVar.t | QConstant of constant
142
143 let var i = QVar (QVar.make_var i)
144 let global sg = QVar (QVar.make_global sg)
145
146 let is_var x =
147 match x with
148 | QVar _ -> true
149 | QConstant _ -> false
150
151 let var_index = function
152 | QVar q -> QVar.var_index q
153 | QConstant _ -> None
154
155 module Constants = struct
156 let equal a b = match a, b with
157 | QProp, QProp | QSProp, QSProp | QType, QType -> true
158 | (QProp | QSProp | QType), _ -> false
159
160 let compare a b = match a, b with
161 | QProp, QProp -> 0
162 | QProp, _ -> -1
163 | _, QProp -> 1
164 | QSProp, QSProp -> 0
165 | QSProp, _ -> -1
166 | _, QSProp -> 1
167 | QType, QType -> 0
168
169 let pr = function
170 | QProp -> Pp.str "Prop"
171 | QSProp -> Pp.str "SProp"
172 | QType -> Pp.str "Type"
173
174 let hash = function
175 | QSProp -> 0
176 | QProp -> 1
177 | QType -> 2
178
179 let all = [QSProp; QProp; QType]
180 end
181
182 let equal a b = match a, b with
183 | QVar a, QVar b -> QVar.equal a b
184 | QConstant a, QConstant b -> Constants.equal a b
185 | (QVar _ | QConstant _), _ -> false
186
187 let compare a b = match a, b with
188 | QVar a, QVar b -> QVar.compare a b
189 | QVar _, _ -> -1
190 | _, QVar _ -> 1
191 | QConstant a, QConstant b -> Constants.compare a b
192
193 let pr prv = function
194 | QVar v -> prv v
195 | QConstant q -> Constants.pr q
196
197 let raw_pr q = pr QVar.raw_pr q
198
199 let all_constants = List.map (fun q -> QConstant q) Constants.all
200 let all = var (-1) :: all_constants
201
202 let hash = let open Hashset.Combine in function
203 | QConstant q -> Constants.hash q
204 | QVar q -> combinesmall 3 (QVar.hash q)
205
206 let subst f = function
207 | QConstant _ as q -> q
208 | QVar qv as q ->
209 match f qv with
210 | QConstant _ as q -> q
211 | QVar qv' as q' ->
212 if qv == qv' then q else q'
213
214 let subst_fn m v =
215 match QVar.Map.find_opt v m with
216 | Some v -> v
217 | None -> QVar v
218
219 module Hstruct = struct
220 type nonrec t = t
221
222 let hashcons = function
223 | QConstant c as q -> Constants.hash c, q
224 | QVar qv as q ->
225 let hqv, qv' = QVar.hcons qv in
226 Hashset.Combine.combinesmall 3 hqv, if qv == qv' then q else QVar qv'
227
228 let eq a b =
229 match a, b with
230 | QVar a, QVar b -> a == b
231 | QVar _, _ -> false
232 | (QConstant _), _ -> equal a b
233 end
234
235 module Hasher = Hashcons.Make(Hstruct)
236
237 let hcons = Hashcons.simple_hcons Hasher.generate Hasher.hcons ()
238
239 let qsprop = snd @@ hcons (QConstant QSProp)
240 let qprop = snd @@ hcons (QConstant QProp)
241 let qtype = snd @@ hcons (QConstant QType)
242
243 let is_qsprop = equal qsprop
244 let is_qprop = equal qprop
245 let is_qtype = equal qtype
246 let is_qvar q = match q with QVar _ -> true | _ -> false
247 let is_qconst q = match q with QConstant _ -> true | _ -> false
248 let is_qglobal q = match q with QVar (QVar.Global _) -> true | _ -> false
249 let is_impredicative q = is_qsprop q || is_qprop q
250
251 module Self = struct type nonrec t = t let compare = compare end
252 module Set = CSet.Make(Self)
253 module Map = CMap.Make(Self)
254
255 type 'q pattern =
256 | PQVar of 'q | PQConstant of constant
257
258 let pattern_match ps s qusubst =
259 match ps, s with
260 | PQConstant qc, QConstant qc' -> if Constants.equal qc qc' then Some qusubst else None
261 | PQVar qio, q -> Some (Partial_subst.maybe_add_quality qio q qusubst)
262 | PQConstant _, QVar _ -> None
263 end
264
265 module ElimConstraint = struct
266 type kind = ElimTo
267
268 let eq_kind : kind -> kind -> bool = (=)
269 let compare_kind : kind -> kind -> int = compare
270
271 let hash_kind = function
272 | ElimTo -> 0
273
274 let pr_kind = function
275 | ElimTo -> Pp.str "->"
276
277 type t = Quality.t * kind * Quality.t
278
279 let equal (a,k,b) (a',k',b') =
280 eq_kind k k' && Quality.equal a a' && Quality.equal b b'
281
282 let compare (a,k,b) (a',k',b') =
283 let c = compare_kind k k' in
284 if c <> 0 then c
285 else
286 let c = Quality.compare a a' in
287 if c <> 0 then c
288 else Quality.compare b b'
289
290 let pr prq (a,k,b) =
291 let open Pp in
292 hov 1 (Quality.pr prq a ++ spc() ++ pr_kind k ++ spc() ++ Quality.pr prq b)
293
294 let raw_pr x = pr QVar.raw_pr x
295
296 module Hstruct = struct
297 type nonrec t = t
298
299 let hashcons (q1,k,q2) =
300 let hq1, q1 = Quality.hcons q1 in
301 let hq2, q2 = Quality.hcons q2 in
302 Hashset.Combine.(combinesmall (hash_kind k) (combine hq1 hq2)), (q1,k,q2)
303
304 let eq (q1,k,q2) (q1',k',q2') =
305 eq_kind k k' && q1 == q1' && q2 == q2'
306 end
307
308 module Hasher = Hashcons.Make(Hstruct)
309
310 let hcons = Hashcons.simple_hcons Hasher.generate Hasher.hcons ()
311 end
312
313 module ElimConstraints = struct include Stdlib.Set.Make(ElimConstraint)
314 let pr prq c =
315 let open Pp in
316 v 0 (prlist_with_sep spc (fun (u1,op,u2) ->
317 hov 0 (Quality.pr prq u1 ++ spc() ++ ElimConstraint.pr_kind op ++ spc() ++ Quality.pr prq u2))
318 (elements c))
319
320 module HConstraints = CSet.Hashcons(ElimConstraint)(struct
321 type t = ElimConstraint.t
322 let hcons = ElimConstraint.hcons
323 end)
324
325 let hcons = Hashcons.simple_hcons HConstraints.generate HConstraints.hcons ()
326 end
327
328 module QContextSet =
329 struct
330 type t = QVar.Set.t * ElimConstraints.t
331 let empty = (QVar.Set.empty, ElimConstraints.empty)
332 let union (q1, c1) (q2, c2) = (QVar.Set.union q1 q2, ElimConstraints.union c1 c2)
333 let filter_constant_qualities (q, c) =
334 let filter (q1, _, q2) = not (Quality.is_qconst q1 && Quality.is_qconst q2) in
335 (q, ElimConstraints.filter filter c)
336 end
337
338 type t =
339 | SProp
340 | Prop
341 | Set
342 | Type of Universe.t
343 | QSort of QVar.t * Universe.t
344
345 let sprop = SProp
346 let prop = Prop
347 let set = Set
348 let type1 = Type Universe.type1
349 let qsort q u = QSort (q, u)
350
351 let sort_of_univ u =
352 if Universe.is_type0 u then set else Type u
353
354 let univ_of_sort s =
355 match s with
356 | SProp | Prop | Set -> Universe.type0
357 | Type u | QSort (_, u) -> u
358
359 let make q u =
360 let open Quality in
361 match q with
362 | QVar q -> qsort q u
363 | QConstant QSProp -> sprop
364 | QConstant QProp -> prop
365 | QConstant QType -> sort_of_univ u
366
367 let compare s1 s2 =
368 if s1 == s2 then 0 else
369 match s1, s2 with
370 | SProp, SProp -> 0
371 | SProp, (Prop | Set | Type _ | QSort _) -> -1
372 | (Prop | Set | Type _ | QSort _), SProp -> 1
373 | Prop, Prop -> 0
374 | Prop, (Set | Type _ | QSort _) -> -1
375 | Set, Prop -> 1
376 | Set, Set -> 0
377 | Set, (Type _ | QSort _) -> -1
378 | Type _, QSort _ -> -1
379 | Type u1, Type u2 -> Universe.compare u1 u2
380 | Type _, (Prop | Set) -> 1
381 | QSort (q1, u1), QSort (q2, u2) ->
382 let c = QVar.compare q1 q2 in
383 if Int.equal c 0 then Universe.compare u1 u2 else c
384 | QSort _, (Prop | Set | Type _) -> 1
385
386 let equal s1 s2 = Int.equal (compare s1 s2) 0
387
388 let super = function
389 | SProp | Prop | Set -> Type (Universe.type1)
390 | Type u | QSort (_, u) -> Type (Universe.super u)
391
392 let is_sprop = function
393 | SProp -> true
394 | Prop | Set | Type _ | QSort _ -> false
395
396 let is_prop = function
397 | Prop -> true
398 | SProp | Set | Type _ | QSort _-> false
399
400 let is_set = function
401 | Set -> true
402 | SProp | Prop | Type _ | QSort _ -> false
403
404 let is_small = function
405 | SProp | Prop | Set -> true
406 | Type _ | QSort _ -> false
407
408 let levels s = match s with
409 | SProp | Prop -> Level.Set.empty
410 | Set -> Level.Set.singleton Level.set
411 | Type u | QSort (_, u) -> Universe.levels u
412
413 let subst_fn (fq,fu) = function
414 | SProp | Prop | Set as s -> s
415 | Type v as s ->
416 let v' = fu v in
417 if v' == v then s else sort_of_univ v'
418 | QSort (q, v) as s ->
419 let open Quality in
420 match fq q with
421 | QVar q' ->
422 let v' = fu v in
423 if q' == q && v' == v then s
424 else qsort q' v'
425 | QConstant QSProp -> sprop
426 | QConstant QProp -> prop
427 | QConstant QType -> sort_of_univ (fu v)
428
429 let quality = let open Quality in function
430 | Set | Type _ -> qtype
431 | Prop -> qprop
432 | SProp -> qsprop
433 | QSort (q, _) -> QVar q
434
435 open Hashset.Combine
436
437 let hash = function
438 | SProp -> combinesmall 1 0
439 | Prop -> combinesmall 1 1
440 | Set -> combinesmall 1 2
441 | Type u ->
442 let h = Univ.Universe.hash u in
443 combinesmall 2 h
444 | QSort (q, u) ->
445 let h = Univ.Universe.hash u in
446 let h' = QVar.hash q in
447 combinesmall 3 (combine h h')
448
449 module HSorts =
450 Hashcons.Make(
451 struct
452 type nonrec t = t
453
454 let hashcons = function
455 | Type u as c ->
456 let hu, u' = Universe.hcons u in
457 combinesmall 2 hu, if u' == u then c else Type u'
458 | QSort (q, u) as c ->
459 let hq, q' = QVar.hcons q in
460 let hu, u' = Universe.hcons u in
461 combinesmall 3 (combine hu hq), if u' == u && q' == q then c else QSort (q', u')
462 | SProp | Prop | Set as s -> hash s, s
463 let eq s1 s2 = match (s1,s2) with
464 | SProp, SProp | Prop, Prop | Set, Set -> true
465 | (Type u1, Type u2) -> u1 == u2
466 | QSort (q1, u1), QSort (q2, u2) -> q1 == q2 && u1 == u2
467 | (SProp | Prop | Set | Type _ | QSort _), _ -> false
468 end)
469
470 let hcons = Hashcons.simple_hcons HSorts.generate HSorts.hcons ()
471
472 (** On binders: is this variable proof relevant *)
473 type relevance = Relevant | Irrelevant | RelevanceVar of QVar.t
474
475 let relevance_equal r1 r2 = match r1,r2 with
476 | Relevant, Relevant | Irrelevant, Irrelevant -> true
477 | RelevanceVar q1, RelevanceVar q2 -> QVar.equal q1 q2
478 | (Relevant | Irrelevant | RelevanceVar _), _ -> false
479
480 let relevance_hash = function
481 | Relevant -> 0
482 | Irrelevant -> 1
483 | RelevanceVar q -> Hashset.Combine.combinesmall 2 (QVar.hash q)
484
485 let relevance_subst_fn f = function
486 | Relevant | Irrelevant as r -> r
487 | RelevanceVar qv as r ->
488 let open Quality in
489 match f qv with
490 | QConstant QSProp -> Irrelevant
491 | QConstant (QProp | QType) -> Relevant
492 | QVar qv' ->
493 if qv' == qv then r else RelevanceVar qv'
494
495 let relevance_of_sort = function
496 | SProp -> Irrelevant
497 | Prop | Set | Type _ -> Relevant
498 | QSort (q, _) -> RelevanceVar q
499
500 let debug_print = function
501 | SProp -> Pp.(str "SProp")
502 | Prop -> Pp.(str "Prop")
503 | Set -> Pp.(str "Set")
504 | Type u -> Pp.(str "Type(" ++ Univ.Universe.raw_pr u ++ str ")")
505 | QSort (q, u) -> Pp.(str "QSort(" ++ QVar.raw_pr q ++ str ","
506 ++ spc() ++ Univ.Universe.raw_pr u ++ str ")")
507
508 let pr prv pru = function
509 | SProp -> Pp.(str "SProp")
510 | Prop -> Pp.(str "Prop")
511 | Set -> Pp.(str "Set")
512 | Type u -> Pp.(str "Type@{" ++ pru u ++ str "}")
513 | QSort (q, u) -> Pp.(str "Type@{" ++ prv q ++ str "|"
514 ++ spc() ++ pru u ++ str "}")
515
516 let raw_pr = pr QVar.raw_pr Univ.Universe.raw_pr
517
518 type ('q, 'u) pattern =
519 | PSProp | PSSProp | PSSet | PSType of 'u | PSQSort of 'q * 'u
520
521 let extract_level u =
522 match Universe.level u with
523 | Some l -> l
524 | None -> CErrors.anomaly Pp.(str "Tried to extract level of an algebraic universe")
525
526 let extract_sort_level = function
527 | Type u
528 | QSort (_, u) -> extract_level u
529 | Prop | SProp | Set -> Univ.Level.set
530
531 let pattern_match ps s qusubst =
532 match ps, s with
533 | PSProp, Prop -> Some qusubst
534 | PSSProp, SProp -> Some qusubst
535 | PSSet, Set -> Some qusubst
536 | PSType uio, Set -> Some (Partial_subst.maybe_add_univ uio Univ.Level.set qusubst)
537 | PSType uio, Type u -> Some (Partial_subst.maybe_add_univ uio (extract_level u) qusubst)
538 | PSQSort (qio, uio), s -> Some (qusubst |> Partial_subst.maybe_add_quality qio (quality s) |> Partial_subst.maybe_add_univ uio (extract_sort_level s))
539 | (PSProp | PSSProp | PSSet | PSType _), _ -> None