Debian Codegraph @

kernel/cooking.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 by Jean-Christophe Filliâtre out of V6.3 file constants.ml
12 as part of the rebuilding of Coq around a purely functional
13 abstract type-checker, Nov 1999 *)
14
15 (* This module implements kernel-level discharching of local
16 declarations over global constants and inductive types *)
17
18 open Util
19 open Names
20 open Term
21 open Constr
22 open UVars
23 open Context
24
25 module NamedDecl = Context.Named.Declaration
26
27 (** {6 Data needed to abstract over the section variables and section universes } *)
28
29 (** The generalization to be done on a specific judgment:
30 [a:T,b:U,c:V(a) ⊢ w(a,c):W(a,c)
31 ~~>
32 ⊢ λxz.w(a,c)[a,c:=x,z]:(Πx:T.z:T(a).W(a,c))[a,c:=x,z]]
33 so, an abstr_info is both the context x:T,z:V(x) to generalize
34 (skipping y which does not occur), and the substitution [a↦x,c↦z]
35 where in practice, x and z are canonical (hence implicit) de
36 Bruijn indices, that is, only the instantiation [a,c] is kept *)
37
38 type abstr_info = {
39 abstr_ctx : Constr.named_context;
40 (** Context over which to generalize (e.g. x:T,z:V(x)) *)
41 abstr_auctx : UVars.AbstractContext.t;
42 (** Universe context over which to generalize *)
43 abstr_ausubst : Instance.t;
44 (** Universe substitution represented as an instance *)
45 }
46
47 (** The instantiation to apply to generalized declarations so that
48 they behave as if not generalized: this is the a1..an instance to
49 apply to a declaration c in the following transformation:
50 [a1:T1..an:Tn, C:U(a1..an) ⊢ v(a1..an,C):V(a1..an,C)
51 ~~>
52 C:Πx1..xn.U(x1..xn), a1:T1..an:Tn ⊢ v(a1..an,Ca1..an):V(a1..an,Ca1..an)]
53 note that the data looks close to the one for substitution above
54 (because the substitution are represented by their domain) but
55 here, local definitions of the context have been dropped *)
56
57 type abstr_inst_info = {
58 abstr_rev_inst : Id.t list;
59 (** The variables to reapply (excluding "let"s of the context), in reverse order *)
60 abstr_uinst : UVars.Instance.t;
61 (** Abstracted universe variables to reapply *)
62 }
63
64 (** The collection of instantiations to apply to generalized
65 declarations so that they behave as if not generalized.
66 This accounts for the permutation (lambda-lifting) of global and
67 local declarations.
68 Using the notations above, a expand_info is a map [c ↦ a1..an]
69 over all generalized global declarations of the section *)
70
71 type 'a entry_map = 'a Cmap_env.t * 'a Mindmap_env.t
72 type expand_info = abstr_inst_info entry_map
73
74 (** The collection of instantiations to be done on generalized
75 declarations + the generalization to be done on a specific
76 judgment:
77 [a1:T1,a2:T2,C:U(a1) ⊢ v(a1,a2,C):V(a1,a2,C)
78 ~~>
79 c:Πx.U(x) ⊢ λx1x2.(v(a1,a2,cx1)[a1,a2:=x1,x2]):Πx1x2.(V(a1,a2,ca1)[a1,a2:=x1,x2])]
80 so, a cooking_info is the map [c ↦ x1..xn],
81 the context x:T,y:U to generalize, and the substitution [x,y] *)
82
83 type cooking_info = {
84 expand_info : expand_info;
85 abstr_info : abstr_info;
86 }
87
88 let empty_cooking_info = {
89 expand_info = (Cmap_env.empty, Mindmap_env.empty);
90 abstr_info = {
91 abstr_ctx = [];
92 abstr_auctx = AbstractContext.empty;
93 abstr_ausubst = Instance.empty;
94 };
95 }
96
97 (*s Cooking the constants. *)
98
99 type my_global_reference =
100 | ConstRef of Constant.t
101 | IndRef of inductive
102 | ConstructRef of constructor
103
104 module RefHash =
105 struct
106 type t = my_global_reference
107 let equal gr1 gr2 = match gr1, gr2 with
108 | ConstRef c1, ConstRef c2 -> Constant.UserOrd.equal c1 c2
109 | IndRef i1, IndRef i2 -> Ind.UserOrd.equal i1 i2
110 | ConstructRef c1, ConstructRef c2 -> Construct.UserOrd.equal c1 c2
111 | _ -> false
112 open Hashset.Combine
113 let hash = function
114 | ConstRef c -> combinesmall 1 (Constant.UserOrd.hash c)
115 | IndRef i -> combinesmall 2 (Ind.UserOrd.hash i)
116 | ConstructRef c -> combinesmall 3 (Construct.UserOrd.hash c)
117 end
118
119 module RefTable = Hashtbl.Make(RefHash)
120 type internal_abstr_inst_info = UVars.Instance.t * int list * int
121
122 type cooking_cache = {
123 cache : internal_abstr_inst_info RefTable.t;
124 info : cooking_info;
125 rel_ctx : rel_context Lazy.t;
126 }
127
128 let instantiate_my_gr gr u =
129 match gr with
130 | ConstRef c -> mkConstU (c, u)
131 | IndRef i -> mkIndU (i, u)
132 | ConstructRef c -> mkConstructU (c, u)
133
134 let discharge_inst top_abst_subst sub_abst_rev_inst =
135 let rec aux k relargs top_abst_subst sub_abst_rev_inst =
136 match top_abst_subst, sub_abst_rev_inst with
137 | decl :: top_abst_subst, id' :: sub_abst_rev_inst' ->
138 if Id.equal (NamedDecl.get_id decl) id' then
139 aux (k+1) (k :: relargs) top_abst_subst sub_abst_rev_inst'
140 else
141 aux (k+1) relargs top_abst_subst sub_abst_rev_inst
142 | _, [] -> relargs
143 | [], _ -> assert false in
144 aux 1 [] top_abst_subst sub_abst_rev_inst
145
146 let rec find_var k id = function
147 | [] -> raise Not_found
148 | decl :: subst ->
149 if Id.equal id (NamedDecl.get_id decl) then k+1
150 else find_var (k+1) id subst
151
152 let share cache top_abst_subst r (cstl,knl) =
153 try RefTable.find cache r
154 with Not_found ->
155 let {abstr_uinst;abstr_rev_inst} =
156 match r with
157 | IndRef (kn,_i) -> Mindmap_env.find kn knl
158 | ConstructRef ((kn,_i),_j) -> Mindmap_env.find kn knl
159 | ConstRef cst -> Cmap_env.find cst cstl
160 in
161 let inst = (abstr_uinst, discharge_inst top_abst_subst abstr_rev_inst, List.length abstr_rev_inst) in
162 RefTable.add cache r inst;
163 inst
164
165 let make_inst k abstr_inst_rel =
166 Array.map_of_list (fun n -> mkRel (k+n)) abstr_inst_rel
167
168 let share_univs cache top_abst_subst k r u l =
169 let (abstr_uinst,abstr_inst_rel,_) = share cache top_abst_subst r l in
170 mkApp (instantiate_my_gr r (Instance.append abstr_uinst u), make_inst k abstr_inst_rel)
171
172 let discharge_proj_repr r p = (* To merge with discharge_proj *)
173 let nnewpars = List.count NamedDecl.is_local_assum r.abstr_info.abstr_ctx in
174 let map npars = npars + nnewpars in
175 Projection.Repr.map_npars map p
176
177 let discharge_proj (_,_,abstr_inst_length) p =
178 let map npars = npars + abstr_inst_length in
179 Projection.map_npars map p
180
181 let is_empty_modlist (cm, mm) =
182 Cmap_env.is_empty cm && Mindmap_env.is_empty mm
183
184 let expand_constr cache modlist top_abst_subst c =
185 let share_univs = share_univs cache top_abst_subst in
186 let rec substrec k c =
187 match kind c with
188 | Case (ci, u, pms, p, iv, t, br) ->
189 begin match share cache top_abst_subst (IndRef ci.ci_ind) modlist with
190 | (abstr_uinst, abstr_inst_rel, abstr_inst_length) ->
191 let u = Instance.append abstr_uinst u in
192 let pms = Array.append (make_inst k abstr_inst_rel) pms in
193 let ci = { ci with ci_npar = ci.ci_npar + abstr_inst_length } in
194 Constr.map_with_binders succ substrec k (mkCase (ci,u,pms,p,iv,t,br))
195 | exception Not_found ->
196 Constr.map_with_binders succ substrec k c
197 end
198
199 | Ind (ind,u) ->
200 (try
201 share_univs k (IndRef ind) u modlist
202 with
203 | Not_found -> Constr.map_with_binders succ substrec k c)
204
205 | Construct (cstr,u) ->
206 (try
207 share_univs k (ConstructRef cstr) u modlist
208 with
209 | Not_found -> Constr.map_with_binders succ substrec k c)
210
211 | Const (cst,u) ->
212 (try
213 share_univs k (ConstRef cst) u modlist
214 with
215 | Not_found -> Constr.map_with_binders succ substrec k c)
216
217 | Proj (p, r, c') ->
218 let ind = Names.Projection.inductive p in
219 let p' =
220 try
221 let exp = share cache top_abst_subst (IndRef ind) modlist in
222 discharge_proj exp p
223 with Not_found -> p in
224 let c'' = substrec k c' in
225 if p == p' && c' == c'' then c else mkProj (p', r, c'')
226
227 | Var id ->
228 (try
229 mkRel (find_var k id top_abst_subst)
230 with Not_found -> c)
231
232 | _ -> Constr.map_with_binders succ substrec k c
233
234 in
235 if is_empty_modlist modlist && List.is_empty top_abst_subst then c
236 else substrec 0 c
237
238 (** Cooking is made of 4 steps:
239 1. expansion of global constants by applying them to the section
240 subcontext they depend on
241 2. substitution of named universe variables by de Bruijn universe variables
242 3. substitution of named term variables by de Bruijn term variables
243 3. generalization of terms over the section subcontext they depend on
244 (note that the generalization over universe variable is implicit) *)
245
246 (** The main expanding/substitution functions, performing the three first steps *)
247 let expand_subst0 cache expand_info abstr_ctx abstr_ausubst c =
248 let c = expand_constr cache expand_info abstr_ctx c in
249 let c = Vars.subst_univs_level_constr (make_instance_subst abstr_ausubst) c in
250 c
251
252 let expand_subst cache c =
253 expand_subst0 cache.cache cache.info.expand_info cache.info.abstr_info.abstr_ctx cache.info.abstr_info.abstr_ausubst c
254
255 (** Adding the final abstraction step, term case *)
256 let abstract_as_type cache t =
257 let ctx = Lazy.force cache.rel_ctx in
258 it_mkProd_wo_LetIn (expand_subst cache t) ctx
259
260 (** Adding the final abstraction step, type case *)
261 let abstract_as_body cache c =
262 let ctx = Lazy.force cache.rel_ctx in
263 it_mkLambda_or_LetIn (expand_subst cache c) ctx
264
265 (** Adding the final abstraction step, sort case (for universes) *)
266 let abstract_as_sort cache s =
267 destSort (expand_subst cache (mkSort s))
268
269 (** Absorb a named context in the transformation which turns a
270 judgment [G, Δ ⊢ ΠΩ.J] into [⊢ ΠG.ΠΔ.((ΠΩ.J)[σ][τ])], that is,
271 produces the context [Δ(Ω[σ][τ])] and substitutions [σ'] and [τ]
272 that turns a judgment [G, Δ, Ω[σ][τ] ⊢ J] into [⊢ ΠG.ΠΔ.((ΠΩ.J)[σ][τ])]
273 via [⊢ ΠG.ΠΔ.Π(Ω[σ][τ]).(J[σ'][τ])] *)
274 let abstract_named_context expand_info abstr_ausubst hyps =
275 let fold decl abstr_ctx =
276 let cache = RefTable.create 13 in
277 let decl = match decl with
278 | NamedDecl.LocalDef (id, b, t) ->
279 let id = Context.map_annot_relevance (UVars.subst_sort_level_relevance (make_instance_subst abstr_ausubst)) id in
280 let b = expand_subst0 cache expand_info abstr_ctx abstr_ausubst b in
281 let t = expand_subst0 cache expand_info abstr_ctx abstr_ausubst t in
282 NamedDecl.LocalDef (id, b, t)
283 | NamedDecl.LocalAssum (id, t) ->
284 let id = Context.map_annot_relevance (UVars.subst_sort_level_relevance (make_instance_subst abstr_ausubst)) id in
285 let t = expand_subst0 cache expand_info abstr_ctx abstr_ausubst t in
286 NamedDecl.LocalAssum (id, t)
287 in
288 decl :: abstr_ctx
289 in
290 Context.Named.fold_outside fold hyps ~init:[]
291
292 let create_cache info =
293 let cache = RefTable.create 13 in
294 let abstr_info = info.abstr_info in
295 let named_ctx = lazy (abstract_named_context info.expand_info abstr_info.abstr_ausubst abstr_info.abstr_ctx) in
296 let rel_ctx = lazy (List.map NamedDecl.to_rel_decl (Lazy.force named_ctx)) in
297 { cache; info; rel_ctx }
298
299 (** Turn a named context [Δ] (hyps) and a universe named context
300 [G] (uctx) into a rel context and abstract universe context
301 respectively; computing also the substitution [σ] and universe
302 substitution [τ] such that if [G, Δ ⊢ J] is valid then
303 [⊢ ΠG.ΠΔ.(J[σ][τ])] is too, that is, it produces the substitutions
304 which turns named binders into de Bruijn binders; computing also
305 the instance to apply to take the generalization into account;
306 collecting the information needed to do such as a transformation
307 of judgment into a [cooking_info] *)
308 let make_cooking_info ~recursive expand_info hyps uctx =
309 let abstr_rev_inst = List.rev (Named.instance_list (fun id -> id) hyps) in
310 let abstr_ausubst, abstr_auctx = abstract_universes uctx in
311 let abstr_info = { abstr_ctx = hyps; abstr_auctx; abstr_ausubst } in
312 let abstr_inst_info = {
313 abstr_rev_inst = abstr_rev_inst;
314 abstr_uinst = abstr_ausubst;
315 } in
316 let info = { expand_info; abstr_info } in
317 let info = match recursive with
318 | None -> info
319 | Some ind ->
320 let (cmap, imap) = info.expand_info in
321 { info with expand_info = (cmap, Mindmap_env.add ind abstr_inst_info imap) }
322 in
323 info, abstr_inst_info
324
325 let names_info info =
326 let fold accu id = Id.Set.add (NamedDecl.get_id id) accu in
327 List.fold_left fold Id.Set.empty info.abstr_info.abstr_ctx
328
329 let rel_context_of_cooking_cache cache =
330 Lazy.force cache.rel_ctx
331
332 let universe_context_of_cooking_info info =
333 info.abstr_info.abstr_auctx
334
335 let instance_of_cooking_info info =
336 Named.instance mkVar info.abstr_info.abstr_ctx
337
338 let instance_of_cooking_cache { info; _ } =
339 instance_of_cooking_info info
340
341 let discharge_abstract_universe_context abstr auctx =
342 (** Given a substitution [abstr.abstr_ausubst := u₀ ... uₙ₋₁] together with an abstract
343 context [abstr.abstr_ctx := 0 ... n - 1 |= C{0, ..., n - 1}] of the same length,
344 and another abstract context relative to the former context
345 [auctx := 0 ... m - 1 |= C'{u₀, ..., uₙ₋₁, 0, ..., m - 1}],
346 construct the lifted abstract universe context
347 [0 ... n - 1 n ... n + m - 1 |=
348 C{0, ... n - 1} ∪
349 C'{0, ..., n - 1, n, ..., n + m - 1} ]
350 together with the substitution
351 [u₀ ↦ Var(0), ... ,uₙ₋₁ ↦ Var(n - 1), Var(0) ↦ Var(n), ..., Var(m - 1) ↦ Var (n + m - 1)].
352 *)
353 let n = AbstractContext.size abstr.abstr_auctx in
354 if (UVars.eq_sizes n (0,0)) then
355 (** Optimization: still need to take the union for the constraints between globals *)
356 abstr, n, AbstractContext.union abstr.abstr_auctx auctx
357 else
358 let subst = abstr.abstr_ausubst in
359 let suff = UVars.make_abstract_instance auctx in
360 let ainst = Instance.append subst suff in
361 let substf = make_instance_subst ainst in
362 let auctx = UVars.subst_univs_level_abstract_universe_context substf auctx in
363 let auctx' = AbstractContext.union abstr.abstr_auctx auctx in
364 { abstr with abstr_ausubst = ainst }, n, auctx'
365
366 let lift_mono_univs info ctx =
367 assert (AbstractContext.is_empty info.abstr_info.abstr_auctx); (* No monorphic constants in a polymorphic section *)
368 info, ctx
369
370 let lift_poly_univs info auctx =
371 (** The constant under consideration is quantified over a
372 universe context [auctx]; it has to be quantified further over
373 [abstr.abstr_auctx] leading to a new abstraction recipe valid
374 under the quantification; that is if we had a judgment
375 [G, Δ ⊢ ΠG'.J] to be turned, thanks to [abstr] =
376 [{ctx:=Δ;uctx:=G;subst:=σ;usubst:=τ}], into
377 [⊢ ΠG.ΠΔ.(ΠG'.J)[σ][τ])], we build the new
378 [{ctx:=Δ;uctx:=G(G'[ττ']);subst:=σ;usubst:=ττ'}], for some [τ']
379 built by [discharge_abstract_universe_context], that works on
380 [J], that is, that allows to turn [GG'[ττ'], Δ ⊢ J] into
381 [⊢ ΠG.ΠΔ.(ΠG'.J)[σ][τ]] via [⊢ ΠG(G'[ττ']).ΠΔ.(J[σ][ττ'])] *)
382 let abstr_info, n, auctx = discharge_abstract_universe_context info.abstr_info auctx in
383 { info with abstr_info }, n, auctx
384
385 let lift_private_mono_univs info a =
386 let () = assert (AbstractContext.is_empty info.abstr_info.abstr_auctx) in
387 let () = assert (Instance.is_empty info.abstr_info.abstr_ausubst) in
388 a
389
390 let lift_private_poly_univs info (inst, cstrs) =
391 let cstrs = UVars.subst_univs_constraints (snd @@ make_instance_subst info.abstr_info.abstr_ausubst) cstrs in
392 (inst, cstrs)
393
394 let lift_quality info q =
395 UVars.subst_sort_level_quality (make_instance_subst info.abstr_info.abstr_ausubst) q
396
397 let lift_relevance info relevance =
398 UVars.subst_sort_level_relevance (make_instance_subst info.abstr_info.abstr_ausubst) relevance