Debian Codegraph @

engine/univGen.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 Sorts
12 open Names
13 open Constr
14 open Univ
15 open UVars
16
17 module QualityOrSet = struct
18 type t = Qual of Quality.t | Set
19
20 let equal a b = match a, b with
21 | Qual a, Qual b -> Quality.equal a b
22 | Set, Set -> true
23 | Qual _, Set | Set, Qual _ -> false
24
25 let compare a b = match a, b with
26 | Qual a, Qual b -> Quality.compare a b
27 | Set, Set -> 0
28 | Qual _, Set -> 1
29 | Set, Qual _ -> -1
30
31 let eliminates_to a b =
32 let to_qual = function
33 | Set -> Quality.qtype
34 | Qual q -> q
35 in Inductive.raw_eliminates_to (to_qual a) (to_qual b)
36
37 let of_quality q = Qual q
38 let of_sort s = match s with
39 | Sorts.Set -> Set
40 | s -> of_quality (Sorts.quality s)
41 let quality q = match q with
42 | Set -> Quality.qtype
43 | Qual q -> q
44
45 let set = Set
46
47 let qtype = Qual Quality.qtype
48 let prop = Qual Quality.qprop
49 let sprop = Qual Quality.qsprop
50
51 let is_type q = match q with
52 | Set -> false
53 | Qual q -> Quality.is_qtype q
54
55 let is_set q = match q with
56 | Set -> true
57 | Qual _ -> false
58
59 let is_prop q = match q with
60 | Set -> false
61 | Qual q -> Quality.is_qprop q
62
63 let is_sprop q = match q with
64 | Set -> false
65 | Qual q -> Quality.is_qsprop q
66
67 let pr prv q = match q with
68 | Set -> Pp.str"Set"
69 | Qual q -> Quality.pr prv q
70
71 let raw_pr = pr Sorts.QVar.raw_pr
72
73 let all_constants = Set :: List.map (fun q -> Qual q) Quality.all_constants
74 let all = Set :: List.map (fun q -> Qual q) Quality.all
75 end
76
77 type sort_context_set = (QVar.Set.t * Univ.Level.Set.t) * PConstraints.t
78
79 type 'a in_sort_context_set = 'a * sort_context_set
80
81 let empty_sort_context = (QVar.Set.empty, Level.Set.empty), PConstraints.empty
82
83 let is_empty_sort_context ((qs,us),csts) =
84 QVar.Set.is_empty qs && Level.Set.is_empty us && PConstraints.is_empty csts
85
86 let sort_context_union ((qs,us),csts) ((qs',us'),csts') =
87 ((QVar.Set.union qs qs', Level.Set.union us us'), PConstraints.union csts csts')
88
89 let diff_sort_context ((qs,us),csts) ((qs',us'),csts') =
90 (QVar.Set.diff qs qs', Level.Set.diff us us'), PConstraints.diff csts csts'
91
92 let pr_sort_context prv prl ((vs, us), cst as ctx) =
93 let open Pp in
94 if is_empty_sort_context ctx then mt ()
95 else
96 let vs =
97 if Sorts.QVar.Set.is_empty vs then mt ()
98 else Sorts.QVar.Set.pr prv vs ++ pr_semicolon ()
99 in
100 hov 0 (h (vs ++ Level.Set.pr prl us ++ str " |=") ++ brk(1,2) ++ h (PConstraints.pr prv prl cst))
101
102 type univ_length_mismatch = {
103 gref : GlobRef.t;
104 actual : int * int;
105 expect : int * int;
106 }
107 (* Due to an OCaml bug ocaml/ocaml#10027 inlining this record will cause
108 compliation with -rectypes to crash. *)
109 exception UniverseLengthMismatch of univ_length_mismatch
110
111 let () = CErrors.register_handler (function
112 | UniverseLengthMismatch { gref; actual=(aq,au); expect=(eq,eu) } ->
113 let ppreal, ppexpected =
114 if aq = 0 && eq = 0 then Pp.(int au, int eu)
115 else Pp.(str "(" ++ int aq ++ str " | " ++ int au ++ str ")"
116 , str "(" ++ int eq ++ str " | " ++ int eu ++ str ")")
117 in
118 Some Pp.(str "Universe instance length for " ++ Nametab.pr_global_env Id.Set.empty gref ++
119 spc() ++ str "is " ++ ppreal ++
120 spc() ++ str "but should be " ++ ppexpected ++ str".")
121 | _ -> None)
122
123 (* Generator of levels *)
124 let new_univ_id =
125 let cnt = ref 0 in
126 fun () -> incr cnt; !cnt
127
128 let new_univ_global () =
129 let s = if Flags.async_proofs_is_worker() then !Flags.async_proofs_worker_id else "" in
130 Univ.UGlobal.make (Global.current_dirpath ()) s (new_univ_id ())
131
132 let fresh_level () =
133 Univ.Level.make (new_univ_global ())
134
135 let new_sort_id =
136 let cnt = ref 0 in
137 fun () -> incr cnt; !cnt
138
139 let new_sort_global id =
140 Sorts.QGlobal.make (Global.current_dirpath ()) id
141
142 let fresh_sort_quality () =
143 let s = if Flags.async_proofs_is_worker() then !Flags.async_proofs_worker_id else "" in
144 Sorts.QVar.make_unif s (new_sort_id ())
145
146 let fresh_instance auctx : _ in_sort_context_set =
147 let qlen, ulen = AbstractContext.size auctx in
148 let qinst = Array.init qlen (fun _ -> Sorts.Quality.QVar (fresh_sort_quality ())) in
149 let uinst = Array.init ulen (fun _ -> fresh_level()) in
150 let qctx = Array.fold_left (fun qctx q -> match q with
151 | Sorts.Quality.QVar q -> Sorts.QVar.Set.add q qctx
152 | _ -> assert false)
153 Sorts.QVar.Set.empty
154 qinst
155 in
156 let uctx = Array.fold_right Level.Set.add uinst Level.Set.empty in
157 let inst = Instance.of_array (qinst,uinst) in
158 inst, ((qctx,uctx), AbstractContext.instantiate inst auctx)
159
160 let existing_instance ?loc ~gref auctx inst =
161 let () =
162 let actual = Instance.length inst
163 and expect = AbstractContext.size auctx in
164 if not (UVars.eq_sizes actual expect) then
165 Loc.raise ?loc (UniverseLengthMismatch { gref; actual; expect })
166 else ()
167 in
168 inst, ((Sorts.QVar.Set.empty,Level.Set.empty), AbstractContext.instantiate inst auctx)
169
170 let fresh_instance_from ?loc ctx = function
171 | Some (gref,inst) -> existing_instance ?loc ~gref ctx inst
172 | None -> fresh_instance ctx
173
174 (** Fresh universe polymorphic construction *)
175
176 let fresh_global_instance ?loc ?names env gr =
177 let auctx = Environ.universes_of_global env gr in
178 let names = Option.map (fun x -> gr, x) names in
179 let u, ctx = fresh_instance_from ?loc auctx names in
180 u, ctx
181
182 let fresh_constant_instance env c =
183 let u, ctx = fresh_global_instance env (GlobRef.ConstRef c) in
184 (c, u), ctx
185
186 let fresh_inductive_instance env ind =
187 let u, ctx = fresh_global_instance env (GlobRef.IndRef ind) in
188 (ind, u), ctx
189
190 let fresh_constructor_instance env c =
191 let u, ctx = fresh_global_instance env (GlobRef.ConstructRef c) in
192 (c, u), ctx
193
194 let fresh_array_instance env =
195 let auctx = CPrimitives.typ_univs CPrimitives.PT_array in
196 let u, ctx = fresh_instance_from auctx None in
197 u, ctx
198
199 let fresh_global_instance ?loc ?names env gr =
200 let u, ctx = fresh_global_instance ?loc ?names env gr in
201 mkRef (gr, u), ctx
202
203 let constr_of_monomorphic_global env gr =
204 if not (Environ.is_polymorphic env gr) then
205 fst (fresh_global_instance env gr)
206 else CErrors.user_err
207 Pp.(str "globalization of polymorphic reference " ++ Nametab.pr_global_env Id.Set.empty gr ++
208 str " would forget universes.")
209
210 let fresh_sort_in_quality =
211 let open QualityOrSet in
212 function
213 | Qual (QConstant QSProp) -> Sorts.sprop, empty_sort_context
214 | Qual (QConstant QProp) -> Sorts.prop, empty_sort_context
215 | Set -> Sorts.set, empty_sort_context
216 | Qual (QConstant QType | QVar _ (* Treat as Type *)) ->
217 let u = fresh_level () in
218 sort_of_univ (Univ.Universe.make u), ((QVar.Set.empty,Level.Set.singleton u), PConstraints.empty)
219
220 let fresh_sort_context_instance ((qs, us), csts) =
221 let ufold u (us, usubst) =
222 let u' = fresh_level () in
223 (Level.Set.add u' us, Level.Map.add u u' usubst)
224 in
225 let qfold q (qs, qsubst) =
226 let q' = fresh_sort_quality () in
227 QVar.Set.add q' qs, QVar.Map.add q (Sorts.Quality.QVar q') qsubst
228 in
229 let us, usubst = Level.Set.fold ufold us (Level.Set.empty, Level.Map.empty) in
230 let qs, qsubst = QVar.Set.fold qfold qs (QVar.Set.empty, QVar.Map.empty) in
231 let csts = subst_poly_constraints (qsubst, usubst) csts in
232 (qsubst, usubst), ((qs, us), csts)