Debian Codegraph @

engine/eConstr.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 module CVars = Vars
12
13 open CErrors
14 open Util
15 open Names
16 open Constr
17 open Context
18
19 module NamedDecl = Context.Named.Declaration
20
21 module ERelevance = struct
22 include Evd.MiniEConstr.ERelevance
23
24 let equal sigma r1 r2 = Sorts.relevance_equal (kind sigma r1) (kind sigma r2)
25
26 let is_irrelevant = Evd.is_relevance_irrelevant
27
28 let relevant = make Relevant
29 let irrelevant = make Irrelevant
30 end
31
32 module ESorts = struct
33 include Evd.MiniEConstr.ESorts
34
35 let equal sigma s1 s2 =
36 Sorts.equal (kind sigma s1) (kind sigma s2)
37
38 let is_small sigma s = Sorts.is_small (kind sigma s)
39 let is_prop sigma s = Sorts.is_prop (kind sigma s)
40 let is_sprop sigma s = Sorts.is_sprop (kind sigma s)
41 let is_set sigma s = Sorts.is_set (kind sigma s)
42
43 let prop = make Sorts.prop
44 let sprop = make Sorts.sprop
45 let set = make Sorts.set
46 let type1 = make Sorts.type1
47
48 let super sigma s =
49 make (Sorts.super (kind sigma s))
50
51 let relevance_of_sort s =
52 let r = Sorts.relevance_of_sort (unsafe_to_sorts s) in
53 ERelevance.make r
54
55 let quality sigma s = Sorts.quality (kind sigma s)
56
57 let quality_or_set sigma s =
58 if is_set sigma s then UnivGen.QualityOrSet.set
59 else UnivGen.QualityOrSet.of_quality @@ Sorts.quality (kind sigma s)
60
61 end
62
63 module EInstance = struct
64 include Evd.MiniEConstr.EInstance
65
66 let equal sigma i1 i2 =
67 UVars.Instance.equal (kind sigma i1) (kind sigma i2)
68
69 let length u = UVars.Instance.length (unsafe_to_instance u)
70 end
71
72 module Expand :
73 sig
74
75 type t
76 type kind = (t, t, ESorts.t, EInstance.t, ERelevance.t) Constr.kind_of_term
77 type handle
78 val make : Evd.econstr -> handle * t
79 val repr : Evd.evar_map -> handle -> t -> Evd.econstr
80 val liftn_handle : int -> handle -> handle
81 val kind : Evd.evar_map -> handle -> t -> handle * kind
82 val expand_instance : skip:bool -> Evd.undefined Evd.evar_info -> handle -> t SList.t -> t SList.t
83 val iter : Evd.evar_map -> (handle -> t -> unit) -> handle -> kind -> unit
84 val iter_with_binders : Evd.evar_map -> ('a -> 'a) -> ('a -> handle -> t -> unit) -> 'a -> handle -> kind -> unit
85
86 end
87 =
88 struct
89 include Evd.Expand
90 type t = Evd.econstr
91 type kind = (t, t, ESorts.t, EInstance.t, ERelevance.t) Constr.kind_of_term
92
93 let make c = (empty_handle, c)
94 let repr = expand
95
96 let iter sigma f h knd = match knd with
97 | Evar (evk, args) ->
98 let evi = Evd.find_undefined sigma evk in
99 let args = expand_instance ~skip:false evi h args in
100 (* Despite the type, the sparse list contains no default element *)
101 SList.Skip.iter (f h) args
102 | Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
103 | Construct _ | Int _ | Float _ | String _ -> ()
104 | Cast (c, _, t) -> f h c; f h t
105 | Prod (_, t, c) -> f h t; f (liftn_handle 1 h) c
106 | Lambda (_, t, c) -> f h t; f (liftn_handle 1 h) c
107 | LetIn (_, b, t, c) -> f h b; f h t; f (liftn_handle 1 h) c
108 | App (c, l) -> f h c; Array.Fun1.iter f h l
109 | Case (_, _, pms, (p, _), iv, c, bl) ->
110 Array.Fun1.iter f h pms;
111 f (liftn_handle (Array.length (fst p)) h) (snd p);
112 iter_invert (f h) iv;
113 f h c;
114 Array.Fun1.iter (fun h (ctx, b) -> f (liftn_handle (Array.length ctx) h) b) h bl
115 | Proj (_p, _r, c) -> f h c
116 | Fix (_, (_, tl, bl)) ->
117 Array.Fun1.iter f h tl;
118 Array.Fun1.iter f (liftn_handle (Array.length tl) h) bl
119 | CoFix (_, (_, tl, bl)) ->
120 Array.Fun1.iter f h tl;
121 Array.Fun1.iter f (liftn_handle (Array.length tl) h) bl
122 | Array(_u, t, def, ty) ->
123 Array.iter (f h) t; f h def; f h ty
124
125 let iter_with_binders sigma g f l h knd = match knd with
126 | Evar (evk, args) ->
127 let evi = Evd.find_undefined sigma evk in
128 let args = expand_instance ~skip:false evi h args in
129 (* Despite the type, the sparse list contains no default element *)
130 SList.Skip.iter (f l h) args
131 | Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
132 | Construct _ | Int _ | Float _ | String _ -> ()
133 | Cast (c, _, t) -> f l h c; f l h t
134 | Prod (_, t, c) -> f l h t; f (g l) (liftn_handle 1 h) c
135 | Lambda (_, t, c) -> f l h t; f (g l) (liftn_handle 1 h) c
136 | LetIn (_, b, t, c) -> f l h b; f l h t; f (g l) (liftn_handle 1 h) c
137 | App (c, args) -> f l h c; Array.iter (fun c -> f l h c) args
138 | Case (_, _, pms, (p, _), iv, c, bl) ->
139 Array.iter (fun c -> f l h c) pms;
140 f (iterate g (Array.length (fst p)) l) (liftn_handle (Array.length (fst p)) h) (snd p);
141 iter_invert (fun c -> f l h c) iv;
142 f l h c;
143 Array.iter (fun (ctx, b) -> f (iterate g (Array.length ctx) l) (liftn_handle (Array.length ctx) h) b) bl
144 | Proj (_p, _r, c) -> f l h c
145 | Fix (_, (_, tl, bl)) ->
146 Array.iter (fun c -> f l h c) tl;
147 Array.iter (f (iterate g (Array.length tl) l) (liftn_handle (Array.length tl) h)) bl
148 | CoFix (_, (_, tl, bl)) ->
149 Array.iter (fun c -> f l h c) tl;
150 Array.iter (f (iterate g (Array.length tl) l) (liftn_handle (Array.length tl) h)) bl
151 | Array(_u, t, def, ty) ->
152 Array.iter (fun c -> f l h c) t; f l h def; f l h ty
153
154 end
155
156 include (Evd.MiniEConstr : module type of Evd.MiniEConstr
157 with module ERelevance := ERelevance
158 and module ESorts := ESorts
159 and module EInstance := EInstance)
160
161 type types = t
162 type constr = t
163 type existential = t pexistential
164 type case_return = (t, ERelevance.t) pcase_return
165 type case_branch = (t, ERelevance.t) pcase_branch
166 type case_invert = t pcase_invert
167 type case = (t, t, EInstance.t, ERelevance.t) pcase
168 type rec_declaration = (t, t, ERelevance.t) prec_declaration
169 type fixpoint = (t, t, ERelevance.t) pfixpoint
170 type cofixpoint = (t, t, ERelevance.t) pcofixpoint
171 type unsafe_judgment = (constr, types) Environ.punsafe_judgment
172 type unsafe_type_judgment = (types, ESorts.t) Environ.punsafe_type_judgment
173 type named_declaration = (constr, types, ERelevance.t) Context.Named.Declaration.pt
174 type rel_declaration = (constr, types, ERelevance.t) Context.Rel.Declaration.pt
175 type compacted_declaration = (constr, types, ERelevance.t) Context.Compacted.Declaration.pt
176 type named_context = (constr, types, ERelevance.t) Context.Named.pt
177 type compacted_context = compacted_declaration list
178 type rel_context = (constr, types, ERelevance.t) Context.Rel.pt
179 type 'a binder_annot = ('a, ERelevance.t) Context.pbinder_annot
180
181 let annotR x = Context.make_annot x ERelevance.relevant
182 let nameR x = annotR (Name x)
183 let anonR = annotR Anonymous
184
185 type 'a puniverses = 'a * EInstance.t
186
187 let in_punivs a = (a, EInstance.empty)
188
189 let mkSProp = of_kind (Sort (ESorts.make Sorts.sprop))
190 let mkProp = of_kind (Sort (ESorts.make Sorts.prop))
191 let mkSet = of_kind (Sort (ESorts.make Sorts.set))
192 let mkType u = of_kind (Sort (ESorts.make (Sorts.sort_of_univ u)))
193 let mkRel n = of_kind (Rel n)
194 let mkVar id = of_kind (Var id)
195 let mkMeta n = of_kind (Meta n)
196 let mkEvar e = of_kind (Evar e)
197 let mkSort s = of_kind (Sort s)
198 let mkCast (b, k, t) = of_kind (Cast (b, k, t))
199 let mkProd (na, t, u) = of_kind (Prod (na, t, u))
200 let mkLambda (na, t, c) = of_kind (Lambda (na, t, c))
201 let mkLetIn (na, b, t, c) = of_kind (LetIn (na, b, t, c))
202 let mkApp (f, arg) = of_kind (App (f, arg))
203 let mkConstU pc = of_kind (Const pc)
204 let mkIndU pi = of_kind (Ind pi)
205 let mkConstructU pc = of_kind (Construct pc)
206 let mkConstructUi ((ind,u),i) = of_kind (Construct ((ind,i),u))
207 let mkCase (ci, u, pms, c, iv, r, p) = of_kind (Case (ci, u, pms, c, iv, r, p))
208 let mkFix f = of_kind (Fix f)
209 let mkCoFix f = of_kind (CoFix f)
210 let mkProj (p, r, c) = of_kind (Proj (p, r, c))
211 let mkArrow t1 r t2 = of_kind (Prod (make_annot Anonymous r, t1, t2))
212 let mkArrowR t1 t2 = mkArrow t1 ERelevance.relevant t2
213 let mkInt i = of_kind (Int i)
214 let mkFloat f = of_kind (Float f)
215 let mkString s = of_kind (String s)
216 let mkArray (u,t,def,ty) = of_kind (Array (u,t,def,ty))
217
218 let mkRef (gr,u) = let open GlobRef in match gr with
219 | ConstRef c -> mkConstU (c,u)
220 | IndRef ind -> mkIndU (ind,u)
221 | ConstructRef c -> mkConstructU (c,u)
222 | VarRef x -> mkVar x
223
224 let mkLEvar = Evd.MiniEConstr.mkLEvar
225
226 let type1 = mkSort ESorts.type1
227
228 let applist (f, arg) = mkApp (f, Array.of_list arg)
229 let applistc f arg = mkApp (f, Array.of_list arg)
230
231 let isRel sigma c = match kind sigma c with Rel _ -> true | _ -> false
232 let isVar sigma c = match kind sigma c with Var _ -> true | _ -> false
233 let isInd sigma c = match kind sigma c with Ind _ -> true | _ -> false
234 let isEvar sigma c = match kind sigma c with Evar _ -> true | _ -> false
235 let isMeta sigma c = match kind sigma c with Meta _ -> true | _ -> false
236 let isSort sigma c = match kind sigma c with Sort _ -> true | _ -> false
237 let isCast sigma c = match kind sigma c with Cast _ -> true | _ -> false
238 let isApp sigma c = match kind sigma c with App _ -> true | _ -> false
239 let isLambda sigma c = match kind sigma c with Lambda _ -> true | _ -> false
240 let isLetIn sigma c = match kind sigma c with LetIn _ -> true | _ -> false
241 let isProd sigma c = match kind sigma c with Prod _ -> true | _ -> false
242 let isConst sigma c = match kind sigma c with Const _ -> true | _ -> false
243 let isConstruct sigma c = match kind sigma c with Construct _ -> true | _ -> false
244 let isFix sigma c = match kind sigma c with Fix _ -> true | _ -> false
245 let isCoFix sigma c = match kind sigma c with CoFix _ -> true | _ -> false
246 let isCase sigma c = match kind sigma c with Case _ -> true | _ -> false
247 let isProj sigma c = match kind sigma c with Proj _ -> true | _ -> false
248
249 let rec isType sigma c = match kind sigma c with
250 | Sort s -> (match ESorts.kind sigma s with
251 | Sorts.Type _ -> true
252 | _ -> false )
253 | Cast (c,_,_) -> isType sigma c
254 | _ -> false
255
256 let isVarId sigma id c =
257 match kind sigma c with Var id' -> Id.equal id id' | _ -> false
258 let isRelN sigma n c =
259 match kind sigma c with Rel n' -> Int.equal n n' | _ -> false
260
261 let isRef sigma c = match kind sigma c with
262 | Const _ | Ind _ | Construct _ | Var _ -> true
263 | _ -> false
264
265 let isRefX env sigma x c =
266 let open GlobRef in
267 match x, kind sigma c with
268 | ConstRef c, Const (c', _) -> Environ.QConstant.equal env c c'
269 | IndRef i, Ind (i', _) -> Environ.QInd.equal env i i'
270 | ConstructRef i, Construct (i', _) -> Environ.QConstruct.equal env i i'
271 | VarRef id, Var id' -> Id.equal id id'
272 | _ -> false
273
274 let is_lib_ref env sigma x c =
275 match Rocqlib.lib_ref_opt x with
276 | Some x -> isRefX env sigma x c
277 | None -> false
278
279 let destRel sigma c = match kind sigma c with
280 | Rel p -> p
281 | _ -> raise DestKO
282
283 let destVar sigma c = match kind sigma c with
284 | Var p -> p
285 | _ -> raise DestKO
286
287 let destInd sigma c = match kind sigma c with
288 | Ind p -> p
289 | _ -> raise DestKO
290
291 let destEvar sigma c = match kind sigma c with
292 | Evar p -> p
293 | _ -> raise DestKO
294
295 let destMeta sigma c = match kind sigma c with
296 | Meta p -> p
297 | _ -> raise DestKO
298
299 let destSort sigma c = match kind sigma c with
300 | Sort p -> p
301 | _ -> raise DestKO
302
303 let destCast sigma c = match kind sigma c with
304 | Cast (c, k, t) -> (c, k, t)
305 | _ -> raise DestKO
306
307 let destApp sigma c = match kind sigma c with
308 | App (f, a) -> (f, a)
309 | _ -> raise DestKO
310
311 let destLambda sigma c = match kind sigma c with
312 | Lambda (na, t, c) -> (na, t, c)
313 | _ -> raise DestKO
314
315 let destLetIn sigma c = match kind sigma c with
316 | LetIn (na, b, t, c) -> (na, b, t, c)
317 | _ -> raise DestKO
318
319 let destProd sigma c = match kind sigma c with
320 | Prod (na, t, c) -> (na, t, c)
321 | _ -> raise DestKO
322
323 let destConst sigma c = match kind sigma c with
324 | Const p -> p
325 | _ -> raise DestKO
326
327 let destConstruct sigma c = match kind sigma c with
328 | Construct p -> p
329 | _ -> raise DestKO
330
331 let destFix sigma c = match kind sigma c with
332 | Fix p -> p
333 | _ -> raise DestKO
334
335 let destCoFix sigma c = match kind sigma c with
336 | CoFix p -> p
337 | _ -> raise DestKO
338
339 let destCase sigma c = match kind sigma c with
340 | Case (ci, u, pms, t, iv, c, p) -> (ci, u, pms, t, iv, c, p)
341 | _ -> raise DestKO
342
343 let destProj sigma c = match kind sigma c with
344 | Proj (p, r, c) -> (p, r, c)
345 | _ -> raise DestKO
346
347 let destRef sigma c = let open GlobRef in match kind sigma c with
348 | Var x -> VarRef x, EInstance.empty
349 | Const (c,u) -> ConstRef c, u
350 | Ind (ind,u) -> IndRef ind, u
351 | Construct (c,u) -> ConstructRef c, u
352 | _ -> raise DestKO
353
354 let decompose_app sigma c =
355 match kind sigma c with
356 | App (f,cl) -> (f, cl)
357 | _ -> (c,[||])
358
359 let decompose_app_list sigma c =
360 match kind sigma c with
361 | App (f,cl) -> (f, Array.to_list cl)
362 | _ -> (c,[])
363
364 let decompose_lambda sigma c =
365 let rec lamdec_rec l c = match kind sigma c with
366 | Lambda (x,t,c) -> lamdec_rec ((x,t)::l) c
367 | Cast (c,_,_) -> lamdec_rec l c
368 | _ -> l,c
369 in
370 lamdec_rec [] c
371
372 let decompose_lambda_decls sigma c =
373 let open Rel.Declaration in
374 let rec lamdec_rec l c =
375 match kind sigma c with
376 | Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) c
377 | LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) c
378 | Cast (c,_,_) -> lamdec_rec l c
379 | _ -> l,c
380 in
381 lamdec_rec Context.Rel.empty c
382
383 let decompose_lambda_n sigma n =
384 if n < 0 then
385 anomaly Pp.(str "decompose_lambda_n: integer parameter must be positive");
386 let rec lamdec_rec l n c =
387 if Int.equal n 0 then (l, c)
388 else
389 match kind sigma c with
390 | Lambda (x, t, c) -> lamdec_rec ((x, t) :: l) (n - 1) c
391 | Cast (c, _, _) -> lamdec_rec l n c
392 | _ ->
393 anomaly Pp.(str "decompose_lambda_n: not enough abstractions")
394 in
395 lamdec_rec [] n
396
397 let decompose_lambda_n_assum sigma n c =
398 let open Rel.Declaration in
399 if n < 0 then
400 anomaly Pp.(str "decompose_lambda_n_assum: integer parameter must be positive.");
401 let rec lamdec_rec l n c =
402 if Int.equal n 0 then l,c
403 else
404 match kind sigma c with
405 | Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
406 | LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) n c
407 | Cast (c,_,_) -> lamdec_rec l n c
408 | c -> anomaly Pp.(str "decompose_lambda_n_assum: not enough abstractions.")
409 in
410 lamdec_rec Context.Rel.empty n c
411
412 let decompose_lambda_n_decls sigma n =
413 let open Rel.Declaration in
414 if n < 0 then
415 anomaly Pp.(str "decompose_lambda_n_decls: integer parameter must be positive.");
416 let rec lamdec_rec l n c =
417 if Int.equal n 0 then l,c
418 else
419 match kind sigma c with
420 | Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
421 | LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) (n-1) c
422 | Cast (c,_,_) -> lamdec_rec l n c
423 | c -> anomaly Pp.(str "decompose_lambda_n_decls: not enough abstractions.")
424 in
425 lamdec_rec Context.Rel.empty n
426
427 let rec to_lambda sigma n prod =
428 if Int.equal n 0 then
429 prod
430 else
431 match kind sigma prod with
432 | Prod (na,ty,bd) -> mkLambda (na,ty,to_lambda sigma (n-1) bd)
433 | Cast (c,_,_) -> to_lambda sigma n c
434 | _ -> anomaly Pp.(str "Not enough products.")
435
436 let decompose_prod sigma c =
437 let rec proddec_rec l c = match kind sigma c with
438 | Prod (x,t,c) -> proddec_rec ((x,t)::l) c
439 | Cast (c,_,_) -> proddec_rec l c
440 | _ -> l,c
441 in
442 proddec_rec [] c
443
444 let decompose_prod_n sigma n =
445 if n < 0 then
446 anomaly Pp.(str "decompose_prod_n: integer parameter must be positive");
447 let rec proddec_rec l n c =
448 if Int.equal n 0 then (l, c)
449 else
450 match kind sigma c with
451 | Prod (x, t, c) -> proddec_rec ((x, t) :: l) (n - 1) c
452 | Cast (c, _, _) -> proddec_rec l n c
453 | _ ->
454 anomaly Pp.(str "decompose_prod_n: not enough products")
455 in
456 proddec_rec [] n
457
458 let decompose_prod_decls sigma c =
459 let open Rel.Declaration in
460 let rec proddec_rec l c =
461 match kind sigma c with
462 | Prod (x,t,c) -> proddec_rec (Context.Rel.add (LocalAssum (x,t)) l) c
463 | LetIn (x,b,t,c) -> proddec_rec (Context.Rel.add (LocalDef (x,b,t)) l) c
464 | Cast (c,_,_) -> proddec_rec l c
465 | _ -> l,c
466 in
467 proddec_rec Context.Rel.empty c
468
469 let decompose_prod_n_decls sigma n c =
470 let open Rel.Declaration in
471 if n < 0 then
472 anomaly Pp.(str "decompose_prod_n_decls: integer parameter must be positive.");
473 let rec prodec_rec l n c =
474 if Int.equal n 0 then l,c
475 else
476 match kind sigma c with
477 | Prod (x,t,c) -> prodec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
478 | LetIn (x,b,t,c) -> prodec_rec (Context.Rel.add (LocalDef (x,b,t)) l) (n-1) c
479 | Cast (c,_,_) -> prodec_rec l n c
480 | c -> anomaly Pp.(str "decompose_prod_n_decls: not enough assumptions.")
481 in
482 prodec_rec Context.Rel.empty n c
483
484 let prod_decls sigma t = fst (decompose_prod_decls sigma t)
485
486 let existential_type = Evd.existential_type
487
488 let lift n c = of_constr (Vars.lift n (unsafe_to_constr c))
489
490 let of_branches : Constr.case_branch array -> case_branch array =
491 match Evd.MiniEConstr.(unsafe_eq, unsafe_relevance_eq) with
492 | Refl, Refl -> fun x -> x
493
494 let unsafe_to_branches : case_branch array -> Constr.case_branch array =
495 match Evd.MiniEConstr.(unsafe_eq, unsafe_relevance_eq) with
496 | Refl, Refl -> fun x -> x
497
498 let of_return : Constr.case_return -> case_return =
499 match Evd.MiniEConstr.(unsafe_eq, unsafe_relevance_eq) with
500 | Refl, Refl -> fun x -> x
501
502 let unsafe_to_return : case_return -> Constr.case_return =
503 match Evd.MiniEConstr.(unsafe_eq, unsafe_relevance_eq) with
504 | Refl, Refl -> fun x -> x
505
506 let of_binder_annot : 'a Constr.binder_annot -> 'a binder_annot =
507 match Evd.MiniEConstr.unsafe_relevance_eq with
508 | Refl -> fun x -> x
509
510 let to_binder_annot sigma (x:_ binder_annot) : _ Constr.binder_annot =
511 let Refl = unsafe_relevance_eq in
512 Context.map_annot_relevance (ERelevance.kind sigma) x
513
514 let to_rel_decl sigma (d:rel_declaration) : Constr.rel_declaration =
515 let Refl = unsafe_eq in
516 let Refl = unsafe_relevance_eq in
517 Context.Rel.Declaration.map_constr_with_relevance (ERelevance.kind sigma) (to_constr sigma) d
518
519 let to_rel_context sigma (ctx:rel_context) : Constr.rel_context =
520 let Refl = unsafe_eq in
521 let Refl = unsafe_relevance_eq in
522 List.Smart.map (to_rel_decl sigma) ctx
523
524 let to_named_decl sigma (d:named_declaration) : Constr.named_declaration =
525 let Refl = unsafe_eq in
526 let Refl = unsafe_relevance_eq in
527 Context.Named.Declaration.map_constr_with_relevance (ERelevance.kind sigma) (to_constr sigma) d
528
529 let to_named_context sigma (ctx:named_context) : Constr.named_context =
530 let Refl = unsafe_eq in
531 let Refl = unsafe_relevance_eq in
532 List.Smart.map (to_named_decl sigma) ctx
533
534 let map_branches f br =
535 let f c = unsafe_to_constr (f (of_constr c)) in
536 of_branches (Constr.map_branches f (unsafe_to_branches br))
537 let map_return_predicate f p =
538 let f c = unsafe_to_constr (f (of_constr c)) in
539 of_return (Constr.map_return_predicate f (unsafe_to_return p))
540
541 let map_instance sigma f evk args =
542 let rec map ctx args = match ctx, SList.view args with
543 | [], None -> SList.empty
544 | decl :: ctx, Some (Some c, rem) ->
545 let c' = f c in
546 let rem' = map ctx rem in
547 if c' == c && rem' == rem then args
548 else if Constr.isVarId (NamedDecl.get_id decl) c' then SList.default rem'
549 else SList.cons c' rem'
550 | decl :: ctx, Some (None, rem) ->
551 let c = Constr.mkVar (NamedDecl.get_id decl) in
552 let c' = f c in
553 let rem' = map ctx rem in
554 if c' == c && rem' == rem then args
555 else SList.cons c' rem'
556 | [], Some _ | _ :: _, None -> assert false
557 in
558 let EvarInfo evi = Evd.find sigma evk in
559 let ctx = Evd.evar_filtered_context evi in
560 map ctx args
561
562 let map sigma f c =
563 let f c = unsafe_to_constr (f (of_constr c)) in
564 let c = unsafe_to_constr @@ whd_evar sigma c in
565 match Constr.kind c with
566 | Evar (evk, args) ->
567 let args' = map_instance sigma f evk args in
568 if args' == args then of_constr c else of_constr @@ Constr.mkEvar (evk, args')
569 | _ -> of_constr (Constr.map f c)
570
571 let map_with_binders sigma g f l c =
572 let f l c = unsafe_to_constr (f l (of_constr c)) in
573 let c = unsafe_to_constr @@ whd_evar sigma c in
574 match Constr.kind c with
575 | Evar (evk, args) ->
576 let args' = map_instance sigma (fun c -> f l c) evk args in
577 if args' == args then of_constr c else of_constr @@ Constr.mkEvar (evk, args')
578 | _ -> of_constr (Constr.map_with_binders g f l c)
579
580 let map_existential sigma f ((evk, args) as ev : existential) =
581 let f c = unsafe_to_constr (f (of_constr c)) in
582 let args : Constr.t SList.t = match Evd.MiniEConstr.unsafe_eq with Refl -> args in
583 let args' = map_instance sigma f evk args in
584 if args' == args then ev
585 else
586 let args' : t SList.t = match Evd.MiniEConstr.unsafe_eq with Refl -> args' in
587 (evk, args')
588
589 let iter sigma f c =
590 let f c = f (of_constr c) in
591 let c = unsafe_to_constr @@ whd_evar sigma c in
592 match Constr.kind c with
593 | Evar ((evk, _) as ev) ->
594 let args = Evd.expand_existential0 sigma ev in
595 List.iter (fun c -> f c) args
596 | _ -> Constr.iter f c
597
598 let expand_case env _sigma (ci, u, pms, p, iv, c, bl) =
599 let u = EInstance.unsafe_to_instance u in
600 let pms = unsafe_to_constr_array pms in
601 let p = unsafe_to_return p in
602 let iv = unsafe_to_case_invert iv in
603 let c = unsafe_to_constr c in
604 let bl = unsafe_to_branches bl in
605 let (ci, (p,r), iv, c, bl) = Inductive.expand_case env (ci, u, pms, p, iv, c, bl) in
606 let p = of_constr p in
607 let r = ERelevance.make r in
608 let c = of_constr c in
609 let iv = of_case_invert iv in
610 let bl = of_constr_array bl in
611 (ci, (p,r), iv, c, bl)
612
613 let annotate_case env sigma (ci, u, pms, p, iv, c, bl as case) =
614 let (_, (p,r), _, _, bl) = expand_case env sigma case in
615 let p =
616 (* Too bad we need to fetch this data in the environment, should be in the
617 case_info instead. *)
618 let (_, mip) = Inductive.lookup_mind_specif env ci.ci_ind in
619 decompose_lambda_n_decls sigma (mip.Declarations.mind_nrealdecls + 1) p
620 in
621 let mk_br c n = decompose_lambda_n_decls sigma n c in
622 let bl = Array.map2 mk_br bl ci.ci_cstr_ndecls in
623 (ci, u, pms, (p,r), iv, c, bl)
624
625 let expand_branch env _sigma u pms (ind, i) (nas, _br) =
626 let open Declarations in
627 let u = EInstance.unsafe_to_instance u in
628 let pms = unsafe_to_constr_array pms in
629 let (mib, mip) = Inductive.lookup_mind_specif env ind in
630 let paramdecl = Vars.subst_instance_context u mib.mind_params_ctxt in
631 let paramsubst = Vars.subst_of_rel_context_instance paramdecl pms in
632 let (ctx, _) = mip.mind_nf_lc.(i - 1) in
633 let (ctx, _) = List.chop mip.mind_consnrealdecls.(i - 1) ctx in
634 let nas =
635 let gen : type a b. (a,b) eq -> (_,a) Context.pbinder_annot array ->
636 (_,b) Context.pbinder_annot array =
637 fun Refl x -> x
638 in
639 gen unsafe_relevance_eq nas
640 in
641 let ans = Inductive.instantiate_context u paramsubst nas ctx in
642 let ans : rel_context =
643 match Evd.MiniEConstr.(unsafe_eq, unsafe_relevance_eq) with
644 | Refl, Refl -> ans
645 in
646 ans
647
648 let contract_case env _sigma (ci, (p,r), iv, c, bl) =
649 let p = unsafe_to_constr p in
650 let r = ERelevance.unsafe_to_relevance r in
651 let iv = unsafe_to_case_invert iv in
652 let c = unsafe_to_constr c in
653 let bl = unsafe_to_constr_array bl in
654 let (ci, u, pms, p, iv, c, bl) = Inductive.contract_case env (ci, (p,r), iv, c, bl) in
655 let u = EInstance.make u in
656 let pms = of_constr_array pms in
657 let p = of_return p in
658 let iv = of_case_invert iv in
659 let c = of_constr c in
660 let bl = of_branches bl in
661 (ci, u, pms, p, iv, c, bl)
662
663 let iter_with_full_binders env sigma g f n c =
664 let open Context.Rel.Declaration in
665 match kind sigma c with
666 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
667 | Construct _ | Int _ | Float _ | String _) -> ()
668 | Cast (c,_,t) -> f n c; f n t
669 | Prod (na,t,c) -> f n t; f (g (LocalAssum (na, t)) n) c
670 | Lambda (na,t,c) -> f n t; f (g (LocalAssum (na, t)) n) c
671 | LetIn (na,b,t,c) -> f n b; f n t; f (g (LocalDef (na, b, t)) n) c
672 | App (c,l) -> f n c; Array.Fun1.iter f n l
673 | Evar ((_,l) as ev) ->
674 let l = Evd.expand_existential sigma ev in
675 List.iter (fun c -> f n c) l
676 | Case (ci,u,pms,p,iv,c,bl) ->
677 let (ci, _, pms, (p,_), iv, c, bl) = annotate_case env sigma (ci, u, pms, p, iv, c, bl) in
678 let f_ctx (ctx, c) = f (List.fold_right g ctx n) c in
679 Array.Fun1.iter f n pms; f_ctx p; iter_invert (f n) iv; f n c; Array.iter f_ctx bl
680 | Proj (_,_,c) -> f n c
681 | Fix (_,(lna,tl,bl)) ->
682 Array.iter (f n) tl;
683 let n' = Array.fold_left2_i (fun i n na t -> g (LocalAssum (na, lift i t)) n) n lna tl in
684 Array.iter (f n') bl
685 | CoFix (_,(lna,tl,bl)) ->
686 Array.iter (f n) tl;
687 let n' = Array.fold_left2_i (fun i n na t -> g (LocalAssum (na,lift i t)) n) n lna tl in
688 Array.iter (f n') bl
689 | Array (_u,t,def,ty) -> Array.Fun1.iter f n t; f n def; f n ty
690
691 let iter_with_binders sigma g f n c =
692 let f l c = f l (of_constr c) in
693 let c = unsafe_to_constr @@ whd_evar sigma c in
694 match Constr.kind c with
695 | Evar ((evk, _) as ev) ->
696 let args = Evd.expand_existential0 sigma ev in
697 List.iter (fun c -> f n c) args
698 | _ -> Constr.iter_with_binders g f n c
699
700 let fold sigma f acc c =
701 let f acc c = f acc (of_constr c) in
702 let c = unsafe_to_constr @@ whd_evar sigma c in
703 match Constr.kind c with
704 | Evar ((evk, _) as ev) ->
705 let args = Evd.expand_existential0 sigma ev in
706 List.fold_left f acc args
707 | _ -> Constr.fold f acc c
708
709 let fold_with_binders sigma g f e acc c =
710 let f e acc c = f e acc (of_constr c) in
711 let c = unsafe_to_constr @@ whd_evar sigma c in
712 match Constr.kind c with
713 | Evar ((evk, _) as ev) ->
714 let args = Evd.expand_existential0 sigma ev in
715 List.fold_left (fun acc c -> f e acc c) acc args
716 | _ -> Constr.fold_constr_with_binders g f e acc c
717
718 let compare_gen k eq_inst eq_sort eq_constr eq_evars nargs c1 c2 =
719 (c1 == c2) || Constr.compare_head_gen_with k k eq_inst eq_sort eq_constr eq_evars nargs c1 c2
720
721 let eq_existential sigma eq (evk1, args1) (evk2, args2) =
722 if Evar.equal evk1 evk2 then
723 let args1 = Evd.expand_existential sigma (evk1, args1) in
724 let args2 = Evd.expand_existential sigma (evk2, args2) in
725 List.equal eq args1 args2
726 else false
727
728 let eq_constr sigma c1 c2 =
729 let kind c = kind sigma c in
730 let eq_inst _ i1 i2 = EInstance.equal sigma i1 i2 in
731 let eq_sorts s1 s2 = ESorts.equal sigma s1 s2 in
732 let eq_existential eq e1 e2 = eq_existential sigma (eq 0) e1 e2 in
733 let rec eq_constr nargs c1 c2 =
734 compare_gen kind eq_inst eq_sorts (eq_existential eq_constr) eq_constr nargs c1 c2
735 in
736 eq_constr 0 c1 c2
737
738 let eq_constr_nounivs sigma c1 c2 =
739 let kind c = kind sigma c in
740 let eq_existential eq e1 e2 = eq_existential sigma (eq 0) e1 e2 in
741 let rec eq_constr nargs c1 c2 =
742 compare_gen kind (fun _ _ _ -> true) (fun _ _ -> true) (eq_existential eq_constr) eq_constr nargs c1 c2
743 in
744 eq_constr 0 c1 c2
745
746 let compare_constr sigma cmp c1 c2 =
747 let kind c = kind sigma c in
748 let eq_inst _ i1 i2 = EInstance.equal sigma i1 i2 in
749 let eq_sorts s1 s2 = ESorts.equal sigma s1 s2 in
750 let eq_existential eq e1 e2 = eq_existential sigma (eq 0) e1 e2 in
751 let cmp nargs c1 c2 = cmp c1 c2 in
752 compare_gen kind eq_inst eq_sorts (eq_existential cmp) cmp 0 c1 c2
753
754 let cmp_inductives cv_pb (mind,ind as spec) nargs u1 u2 cstrs =
755 let open UnivProblem in
756 match mind.Declarations.mind_variance with
757 | None -> enforce_eq_instances_univs false u1 u2 cstrs
758 | Some variances ->
759 let num_param_arity = Conversion.inductive_cumulativity_arguments spec in
760 if not (Int.equal num_param_arity nargs) then enforce_eq_instances_univs false u1 u2 cstrs
761 else compare_cumulative_instances cv_pb variances u1 u2 cstrs
762
763 let cmp_constructors (mind, ind, cns as spec) nargs u1 u2 cstrs =
764 let open UnivProblem in
765 match mind.Declarations.mind_variance with
766 | None -> enforce_eq_instances_univs false u1 u2 cstrs
767 | Some _ ->
768 let num_cnstr_args = Conversion.constructor_cumulativity_arguments spec in
769 if not (Int.equal num_cnstr_args nargs)
770 then enforce_eq_instances_univs false u1 u2 cstrs
771 else
772 let qs1, us1 = UVars.Instance.to_array u1
773 and qs2, us2 = UVars.Instance.to_array u2 in
774 let cstrs = enforce_eq_qualities qs1 qs2 cstrs in
775 Array.fold_left2 (fun cstrs u1 u2 -> UnivProblem.(Set.add (UWeak (u1,u2)) cstrs))
776 cstrs us1 us2
777
778 let eq_universes env sigma cstrs cv_pb refargs l l' =
779 if EInstance.is_empty l then (assert (EInstance.is_empty l'); true)
780 else
781 let l = EInstance.kind sigma l
782 and l' = EInstance.kind sigma l' in
783 let open GlobRef in
784 let open UnivProblem in
785 match refargs with
786 | Some (ConstRef c, 1) when Environ.is_array_type env c ->
787 cstrs := compare_cumulative_instances cv_pb [|UVars.Variance.Irrelevant|] l l' !cstrs;
788 true
789 | None | Some (ConstRef _, _) ->
790 cstrs := enforce_eq_instances_univs true l l' !cstrs; true
791 | Some (VarRef _, _) -> assert false (* variables don't have instances *)
792 | Some (IndRef ind, nargs) ->
793 let mind = Environ.lookup_mind (fst ind) env in
794 cstrs := cmp_inductives cv_pb (mind,snd ind) nargs l l' !cstrs;
795 true
796 | Some (ConstructRef ((mi,ind),ctor), nargs) ->
797 let mind = Environ.lookup_mind mi env in
798 cstrs := cmp_constructors (mind,ind,ctor) nargs l l' !cstrs;
799 true
800
801 let test_constr_universes env sigma leq ?(nargs=0) m n =
802 let open UnivProblem in
803 let kind c = kind sigma c in
804 if m == n then Some Set.empty
805 else
806 let cstrs = ref Set.empty in
807 let cv_pb = if leq then Conversion.CUMUL else Conversion.CONV in
808 let eq_universes refargs l l' = eq_universes env sigma cstrs Conversion.CONV refargs l l'
809 and leq_universes refargs l l' = eq_universes env sigma cstrs cv_pb refargs l l' in
810 let eq_sorts s1 s2 =
811 let s1 = ESorts.kind sigma s1 in
812 let s2 = ESorts.kind sigma s2 in
813 if Sorts.equal s1 s2 then true
814 else (cstrs := Set.add
815 (UEq (s1, s2)) !cstrs;
816 true)
817 in
818 let leq_sorts s1 s2 =
819 let s1 = ESorts.kind sigma s1 in
820 let s2 = ESorts.kind sigma s2 in
821 if Sorts.equal s1 s2 then true
822 else
823 (cstrs := Set.add
824 (ULe (s1, s2)) !cstrs;
825 true)
826 in
827 let eq_existential eq e1 e2 = eq_existential sigma (eq 0) e1 e2 in
828 let rec eq_constr' nargs m n = compare_gen kind eq_universes eq_sorts (eq_existential eq_constr') eq_constr' nargs m n in
829 let res =
830 if leq then
831 let rec compare_leq nargs m n =
832 Constr.compare_head_gen_leq_with kind kind leq_universes leq_sorts (eq_existential eq_constr')
833 eq_constr' leq_constr' nargs m n
834 and leq_constr' nargs m n = m == n || compare_leq nargs m n in
835 compare_leq nargs m n
836 else
837 Constr.compare_head_gen_with kind kind eq_universes eq_sorts (eq_existential eq_constr') eq_constr' nargs m n
838 in
839 if res then Some !cstrs else None
840
841 let eq_constr_universes env sigma ?nargs m n =
842 test_constr_universes env sigma false ?nargs m n
843 let leq_constr_universes env sigma ?nargs m n =
844 test_constr_universes env sigma true ?nargs m n
845
846 let compare_head_gen_proj env sigma equ eqs eqev eqc' nargs m n =
847 let kind c = kind sigma c in
848 match kind m, kind n with
849 | Proj (p, _, c), App (f, args)
850 | App (f, args), Proj (p, _, c) ->
851 (match kind f with
852 | Const (p', u) when Environ.QConstant.equal env (Projection.constant p) p' ->
853 let npars = Projection.npars p in
854 if Array.length args == npars + 1 then
855 eqc' 0 c args.(npars)
856 else false
857 | _ -> false)
858 | _ -> Constr.compare_head_gen_with kind kind equ eqs eqev eqc' nargs m n
859
860 let eq_constr_universes_proj env sigma m n =
861 let open UnivProblem in
862 if m == n then Some Set.empty
863 else
864 let cstrs = ref Set.empty in
865 let eq_universes ref l l' = eq_universes env sigma cstrs Conversion.CONV ref l l' in
866 let eq_sorts s1 s2 =
867 let s1 = ESorts.kind sigma s1 in
868 let s2 = ESorts.kind sigma s2 in
869 if Sorts.equal s1 s2 then true
870 else
871 (cstrs := Set.add
872 (UEq (s1, s2)) !cstrs;
873 true)
874 in
875 let eq_existential eq e1 e2 = eq_existential sigma (eq 0) e1 e2 in
876 let rec eq_constr' nargs m n =
877 m == n || compare_head_gen_proj env sigma eq_universes eq_sorts (eq_existential eq_constr') eq_constr' nargs m n
878 in
879 let res = eq_constr' 0 m n in
880 if res then Some !cstrs else None
881
882 let add_universes_of_instance sigma (qs,us) u =
883 let u = EInstance.kind sigma u in
884 let qs', us' = UVars.Instance.levels u in
885 let qs = Sorts.Quality.(Set.fold (fun q qs -> match q with
886 | QVar q -> Sorts.QVar.Set.add q qs
887 | QConstant _ -> qs)
888 qs' qs)
889 in
890 qs, Univ.Level.Set.union us us'
891
892 let add_relevance sigma (qs,us as v) r =
893 let open Sorts in
894 (* NB this normalizes above_prop to Relevant which makes it disappear *)
895 match ERelevance.kind sigma r with
896 | Irrelevant | Relevant -> v
897 | RelevanceVar q -> QVar.Set.add q qs, us
898
899 let univs_and_qvars_visitor sigma =
900 let open Univ in
901 let visit_sort (qs,us as acc) s =
902 match ESorts.kind sigma s with
903 | Sorts.Type u ->
904 qs, Universe.levels ~init:us u
905 | Sorts.QSort (q,u) ->
906 Sorts.QVar.Set.add q qs, Universe.levels ~init:us u
907 | Sorts.(SProp | Prop | Set) -> acc
908 in
909 let visit_instance acc u = add_universes_of_instance sigma acc u in
910 let visit_relevance acc r = add_relevance sigma acc r in
911 {
912 Vars.visit_sort = visit_sort;
913 visit_instance = visit_instance;
914 visit_relevance = visit_relevance;
915 }
916
917 let universes_of_constr ?(init=Sorts.QVar.Set.empty,Univ.Level.Set.empty) sigma c =
918 let visit = univs_and_qvars_visitor sigma in
919 let rec aux s c =
920 let kc = kind sigma c in
921 let s = Vars.visit_kind_univs visit s kc in
922 match kc with
923 | Evar (k, args) ->
924 let concl = Evd.evar_concl (Evd.find_undefined sigma k) in
925 fold sigma aux (aux s concl) c
926 | _ -> fold sigma aux s c
927 in
928 aux init c
929
930 open Context
931 open Environ
932
933 let cast_list : type a b. (a,b) eq -> a list -> b list =
934 fun Refl x -> x
935
936 let cast_vect : type a b. (a,b) eq -> a array -> b array =
937 fun Refl x -> x
938
939 let cast_rel_decl :
940 type a b c d. (a,b) eq -> (c,d) eq -> (a, a, c) Rel.Declaration.pt -> (b, b, d) Rel.Declaration.pt =
941 fun Refl Refl x -> x
942
943 let cast_rel_context :
944 type a b c d. (a,b) eq -> (c,d) eq -> (a, a, c) Rel.pt -> (b, b, d) Rel.pt =
945 fun Refl Refl x -> x
946
947 let cast_rec_decl :
948 type a b c d. (a,b) eq -> (c,d) eq -> (a, a, c) Constr.prec_declaration -> (b, b, d) Constr.prec_declaration =
949 fun Refl Refl x -> x
950
951 let cast_named_decl :
952 type a b c d. (a,b) eq -> (c,d) eq -> (a, a, c) Named.Declaration.pt -> (b, b, d) Named.Declaration.pt =
953 fun Refl Refl x -> x
954
955 let cast_named_context :
956 type a b c d. (a,b) eq -> (c,d) eq -> (a, a, c) Named.pt -> (b, b, d) Named.pt =
957 fun Refl Refl x -> x
958
959
960 module Vars =
961 struct
962 exception LocalOccur
963 let to_constr = unsafe_to_constr
964 let to_rel_decl = unsafe_to_rel_decl
965
966 type instance = t array
967 type instance_list = t list
968 type substl = t list
969
970 (** Operations that commute with evar-normalization *)
971 let exliftn s c = of_constr (exliftn s (to_constr c))
972 let lift = lift
973 let liftn n m c = of_constr (Vars.liftn n m (to_constr c))
974
975 let substnl subst n c = of_constr (Vars.substnl (cast_list unsafe_eq subst) n (to_constr c))
976 let substl subst c = of_constr (Vars.substl (cast_list unsafe_eq subst) (to_constr c))
977 let subst1 c r = of_constr (Vars.subst1 (to_constr c) (to_constr r))
978
979 let substnl_decl subst n d = of_rel_decl (Vars.substnl_decl (cast_list unsafe_eq subst) n (to_rel_decl d))
980 let substl_decl subst d = of_rel_decl (Vars.substl_decl (cast_list unsafe_eq subst) (to_rel_decl d))
981 let subst1_decl c d = of_rel_decl (Vars.subst1_decl (to_constr c) (to_rel_decl d))
982
983 type substituend = Vars.substituend
984 let make_substituend c = Vars.make_substituend (unsafe_to_constr c)
985 let lift_substituend n s = of_constr (Vars.lift_substituend n s)
986
987 let replace_vars = replace_vars
988
989 (* (subst_var str t) substitute (Var str) by (Rel 1) in t *)
990 let subst_var sigma str t = replace_vars sigma [(str, mkRel 1)] t
991
992 (* (subst_vars [id1;...;idn] t) substitute (Var idj) by (Rel j) in t *)
993 let substn_vars sigma p vars c =
994 let _,subst =
995 List.fold_left (fun (n,l) var -> ((n+1),(var, mkRel n)::l)) (p,[]) vars
996 in replace_vars sigma (List.rev subst) c
997
998 let subst_vars sigma subst c = substn_vars sigma 1 subst c
999
1000 let subst_univs_level_constr subst c =
1001 of_constr (Vars.subst_univs_level_constr subst (to_constr c))
1002
1003 let subst_instance_context subst ctx =
1004 let subst = EInstance.unsafe_to_instance subst in
1005 cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq) (Vars.subst_instance_context subst (cast_rel_context unsafe_eq unsafe_relevance_eq ctx))
1006
1007 let subst_instance_constr subst c =
1008 let subst = EInstance.unsafe_to_instance subst in
1009 of_constr (Vars.subst_instance_constr subst (to_constr c))
1010
1011 let subst_instance_relevance subst r =
1012 let subst = EInstance.unsafe_to_instance subst in
1013 let r = ERelevance.unsafe_to_relevance r in
1014 let r = UVars.subst_instance_relevance subst r in
1015 ERelevance.make r
1016
1017 (** Operations that dot NOT commute with evar-normalization *)
1018 let noccurn sigma n term =
1019 let rec occur_rec n h c =
1020 let (h, knd) = Expand.kind sigma h c in
1021 match knd with
1022 | Rel m -> if Int.equal m n then raise LocalOccur
1023 | Evar (evk, l) ->
1024 let evi = Evd.find_undefined sigma evk in
1025 let l = Expand.expand_instance ~skip:true evi h l in
1026 SList.Skip.iter (fun c -> occur_rec n h c) l
1027 | _ -> Expand.iter_with_binders sigma succ occur_rec n h knd
1028 in
1029 let h, term = Expand.make term in
1030 try occur_rec n h term; true with LocalOccur -> false
1031
1032 let noccur_between sigma n m term =
1033 let rec occur_rec n h c =
1034 let (h, knd) = Expand.kind sigma h c in
1035 match knd with
1036 | Rel p -> if n<=p && p<n+m then raise LocalOccur
1037 | Evar (evk, l) ->
1038 let evi = Evd.find_undefined sigma evk in
1039 let l = Expand.expand_instance ~skip:true evi h l in
1040 SList.Skip.iter (fun c -> occur_rec n h c) l
1041 | _ -> Expand.iter_with_binders sigma succ occur_rec n h knd
1042 in
1043 let h, term = Expand.make term in
1044 try occur_rec n h term; true with LocalOccur -> false
1045
1046 let closedn sigma n c =
1047 let rec closed_rec n h c =
1048 let (h, knd) = Expand.kind sigma h c in
1049 match knd with
1050 | Rel m -> if m>n then raise LocalOccur
1051 | Evar (evk, l) ->
1052 let evi = Evd.find_undefined sigma evk in
1053 let l = Expand.expand_instance ~skip:true evi h l in
1054 SList.Skip.iter (fun c -> closed_rec n h c) l
1055 | _ -> Expand.iter_with_binders sigma succ closed_rec n h knd
1056 in
1057 let h, c = Expand.make c in
1058 try closed_rec n h c; true with LocalOccur -> false
1059
1060 let closed0 sigma c = closedn sigma 0 c
1061
1062 let subst_of_rel_context_instance ctx subst =
1063 cast_list (sym unsafe_eq)
1064 (Vars.subst_of_rel_context_instance (cast_rel_context unsafe_eq unsafe_relevance_eq ctx) (cast_vect unsafe_eq subst))
1065 let subst_of_rel_context_instance_list ctx subst =
1066 cast_list (sym unsafe_eq)
1067 (Vars.subst_of_rel_context_instance_list (cast_rel_context unsafe_eq unsafe_relevance_eq ctx) (cast_list unsafe_eq subst))
1068
1069 let liftn_rel_context n k ctx =
1070 cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq)
1071 (Vars.liftn_rel_context n k (cast_rel_context unsafe_eq unsafe_relevance_eq ctx))
1072
1073 let lift_rel_context n ctx =
1074 cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq)
1075 (Vars.lift_rel_context n (cast_rel_context unsafe_eq unsafe_relevance_eq ctx))
1076
1077 let substnl_rel_context subst n ctx =
1078 cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq)
1079 (Vars.substnl_rel_context (cast_list unsafe_eq subst) n (cast_rel_context unsafe_eq unsafe_relevance_eq ctx))
1080
1081 let substl_rel_context subst ctx =
1082 cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq)
1083 (Vars.substl_rel_context (cast_list unsafe_eq subst) (cast_rel_context unsafe_eq unsafe_relevance_eq ctx))
1084
1085 let smash_rel_context ctx =
1086 cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq)
1087 (Vars.smash_rel_context (cast_rel_context unsafe_eq unsafe_relevance_eq ctx))
1088
1089 let esubst : (int -> 'a -> t) -> 'a Esubst.subs -> t -> t =
1090 match unsafe_eq with
1091 | Refl -> Vars.esubst
1092
1093 end
1094
1095 (* Constructs either [forall x:t, c] or [let x:=b:t in c] *)
1096 let mkProd_or_LetIn decl c =
1097 let open Context.Rel.Declaration in
1098 match decl with
1099 | LocalAssum (na,t) -> mkProd (na, t, c)
1100 | LocalDef (na,b,t) -> mkLetIn (na, b, t, c)
1101
1102 (* Constructs either [forall x:t, c] or [c] in which [x] is replaced by [b] *)
1103 let mkProd_wo_LetIn decl c =
1104 let open Context.Rel.Declaration in
1105 match decl with
1106 | LocalAssum (na,t) -> mkProd (na, t, c)
1107 | LocalDef (_,b,_) -> Vars.subst1 b c
1108
1109 let mkLambda_or_LetIn decl c =
1110 let open Context.Rel.Declaration in
1111 match decl with
1112 | LocalAssum (na,t) -> mkLambda (na, t, c)
1113 | LocalDef (na,b,t) -> mkLetIn (na, b, t, c)
1114
1115 let mkLambda_wo_LetIn decl c =
1116 let open Context.Rel.Declaration in
1117 match decl with
1118 | LocalAssum (na,t) -> mkLambda (na, t, c)
1119 | LocalDef (_,b,_) -> Vars.subst1 b c
1120
1121 let mkNamedProd sigma id typ c = mkProd (map_annot Name.mk_name id, typ, Vars.subst_var sigma id.binder_name c)
1122 let mkNamedLambda sigma id typ c = mkLambda (map_annot Name.mk_name id, typ, Vars.subst_var sigma id.binder_name c)
1123 let mkNamedLetIn sigma id c1 t c2 = mkLetIn (map_annot Name.mk_name id, c1, t, Vars.subst_var sigma id.binder_name c2)
1124
1125 let mkNamedProd_or_LetIn sigma decl c =
1126 let open Context.Named.Declaration in
1127 match decl with
1128 | LocalAssum (id,t) -> mkNamedProd sigma id t c
1129 | LocalDef (id,b,t) -> mkNamedLetIn sigma id b t c
1130
1131 let mkNamedLambda_or_LetIn sigma decl c =
1132 let open Context.Named.Declaration in
1133 match decl with
1134 | LocalAssum (id,t) -> mkNamedLambda sigma id t c
1135 | LocalDef (id,b,t) -> mkNamedLetIn sigma id b t c
1136
1137 let mkNamedProd_wo_LetIn sigma decl c =
1138 let open Context.Named.Declaration in
1139 match decl with
1140 | LocalAssum (id,t) -> mkNamedProd sigma id t c
1141 | LocalDef (id,b,t) -> Vars.subst1 b c
1142
1143 let it_mkProd init = List.fold_left (fun c (n,t) -> mkProd (n, t, c)) init
1144 let it_mkLambda init = List.fold_left (fun c (n,t) -> mkLambda (n, t, c)) init
1145
1146 let it_mkProd_or_LetIn t ctx = List.fold_left (fun c d -> mkProd_or_LetIn d c) t ctx
1147 let it_mkLambda_or_LetIn t ctx = List.fold_left (fun c d -> mkLambda_or_LetIn d c) t ctx
1148
1149 let it_mkProd_wo_LetIn t ctx = List.fold_left (fun c d -> mkProd_wo_LetIn d c) t ctx
1150 let it_mkLambda_wo_LetIn t ctx = List.fold_left (fun c d -> mkLambda_wo_LetIn d c) t ctx
1151
1152 let it_mkNamedProd_or_LetIn sigma t ctx = List.fold_left (fun c d -> mkNamedProd_or_LetIn sigma d c) t ctx
1153 let it_mkNamedLambda_or_LetIn sigma t ctx = List.fold_left (fun c d -> mkNamedLambda_or_LetIn sigma d c) t ctx
1154
1155 let it_mkNamedProd_wo_LetIn sigma t ctx = List.fold_left (fun c d -> mkNamedProd_wo_LetIn sigma d c) t ctx
1156
1157 let rec isArity sigma c =
1158 match kind sigma c with
1159 | Prod (_,_,c) -> isArity sigma c
1160 | LetIn (_,_,_,c) -> isArity sigma c
1161 | Cast (c,_,_) -> isArity sigma c
1162 | Sort _ -> true
1163 | _ -> false
1164
1165 type arity = rel_context * ESorts.t
1166
1167 let mkArity (ctx, s) = it_mkProd_or_LetIn (mkSort s) ctx
1168
1169 let destArity sigma =
1170 let open Context.Rel.Declaration in
1171 let rec prodec_rec l c =
1172 match kind sigma c with
1173 | Prod (x,t,c) -> prodec_rec (LocalAssum (x,t) :: l) c
1174 | LetIn (x,b,t,c) -> prodec_rec (LocalDef (x,b,t) :: l) c
1175 | Cast (c,_,_) -> prodec_rec l c
1176 | Sort s -> l,s
1177 | _ -> anomaly ~label:"destArity" (Pp.str "not an arity.")
1178 in
1179 prodec_rec []
1180
1181 let push_rel d e = push_rel (cast_rel_decl unsafe_eq unsafe_relevance_eq d) e
1182 let push_rel_context d e = push_rel_context (cast_rel_context unsafe_eq unsafe_relevance_eq d) e
1183 let push_rec_types d e = push_rec_types (cast_rec_decl unsafe_eq unsafe_relevance_eq d) e
1184 let push_named d e = push_named (cast_named_decl unsafe_eq unsafe_relevance_eq d) e
1185 let push_named_context d e = push_named_context (cast_named_context unsafe_eq unsafe_relevance_eq d) e
1186 let push_named_context_val d e = push_named_context_val (cast_named_decl unsafe_eq unsafe_relevance_eq d) e
1187
1188 let rel_context e = cast_rel_context (sym unsafe_eq) (sym unsafe_relevance_eq) (rel_context e)
1189 let named_context e = cast_named_context (sym unsafe_eq) (sym unsafe_relevance_eq) (named_context e)
1190
1191 let val_of_named_context e = val_of_named_context (cast_named_context unsafe_eq unsafe_relevance_eq e)
1192 let named_context_of_val e = cast_named_context (sym unsafe_eq) (sym unsafe_relevance_eq) (named_context_of_val e)
1193
1194 let of_existential : Constr.existential -> existential =
1195 let gen : type a b. (a,b) eq -> 'c * b SList.t -> 'c * a SList.t = fun Refl x -> x in
1196 gen unsafe_eq
1197
1198 let lookup_rel i e = cast_rel_decl (sym unsafe_eq) (sym unsafe_relevance_eq) (lookup_rel i e)
1199 let lookup_named n e = cast_named_decl (sym unsafe_eq) (sym unsafe_relevance_eq) (lookup_named n e)
1200 let lookup_named_val n e = cast_named_decl (sym unsafe_eq) (sym unsafe_relevance_eq) (lookup_named_ctxt n e)
1201
1202 let map_rel_context_in_env f env sign =
1203 let rec aux env acc = function
1204 | d::sign ->
1205 aux (push_rel d env) (Context.Rel.Declaration.map_constr (f env) d :: acc) sign
1206 | [] ->
1207 acc
1208 in
1209 aux env [] (List.rev sign)
1210
1211 let match_named_context_val :
1212 named_context_val -> (named_declaration * named_context_val) option =
1213 match unsafe_eq, unsafe_relevance_eq with
1214 | Refl, Refl -> match_named_context_val
1215
1216 let identity_subst_val : named_context_val -> t SList.t = fun ctx ->
1217 SList.defaultn (List.length ctx.Environ.env_named_ctx) SList.empty
1218
1219 let fresh_global ?loc ?rigid ?names env sigma reference =
1220 let (evd,t) = Evd.fresh_global ?loc ?rigid ?names env sigma reference in
1221 evd, t
1222
1223 let lookup_constant = Evd.MiniEConstr.lookup_constant
1224
1225 let constant_value_in env sigma (kn, u) =
1226 let u = EInstance.unsafe_to_instance u in
1227 let cb = lookup_constant env sigma kn in
1228 match cb.const_body with
1229 | Def l_body ->
1230 of_constr (CVars.subst_instance_constr u l_body)
1231 | OpaqueDef _ -> raise (NotEvaluableConst Opaque)
1232 | Undef _ -> raise (NotEvaluableConst NoBody)
1233 | Primitive p -> raise (NotEvaluableConst (IsPrimitive (u ,p)))
1234 | Symbol b ->
1235 let r = Environ.lookup_rewrite_rules kn env in
1236 raise (NotEvaluableConst (HasRules (u, b, r)))
1237
1238 (** Kind of type *)
1239
1240 type kind_of_type =
1241 | SortType of ESorts.t
1242 | CastType of types * t
1243 | ProdType of Name.t binder_annot * t * t
1244 | LetInType of Name.t binder_annot * t * t * t
1245 | AtomicType of t * t array
1246
1247 let kind_of_type sigma t = match kind sigma t with
1248 | Sort s -> SortType s
1249 | Cast (c,_,t) -> CastType (c, t)
1250 | Prod (na,t,c) -> ProdType (na, t, c)
1251 | LetIn (na,b,t,c) -> LetInType (na, b, t, c)
1252 | App (c,l) -> AtomicType (c, l)
1253 | (Rel _ | Meta _ | Var _ | Evar _ | Const _
1254 | Proj _ | Case _ | Fix _ | CoFix _ | Ind _)
1255 -> AtomicType (t,[||])
1256 | (Lambda _ | Construct _ | Int _ | Float _ | String _ | Array _) -> failwith "Not a type"
1257
1258 module Unsafe =
1259 struct
1260 let to_relevance = ERelevance.unsafe_to_relevance
1261 let to_sorts = ESorts.unsafe_to_sorts
1262 let to_instance = EInstance.unsafe_to_instance
1263 let to_constr = unsafe_to_constr
1264 let to_constr_array = unsafe_to_constr_array
1265 let to_binder_annot : 'a binder_annot -> 'a Constr.binder_annot =
1266 match unsafe_relevance_eq with Refl -> fun x -> x
1267 let to_rel_decl = unsafe_to_rel_decl
1268 let to_named_decl = unsafe_to_named_decl
1269 let to_named_context =
1270 let gen : type a b c d. (a, b) eq -> (c,d) eq -> (a,a,c) Context.Named.pt -> (b,b,d) Context.Named.pt
1271 = fun Refl Refl x -> x
1272 in
1273 gen unsafe_eq unsafe_relevance_eq
1274 let to_rel_context =
1275 let gen : type a b c d. (a, b) eq -> (c,d) eq -> (a,a,c) Context.Rel.pt -> (b,b,d) Context.Rel.pt
1276 = fun Refl Refl x -> x
1277 in
1278 gen unsafe_eq unsafe_relevance_eq
1279 let to_case_invert = unsafe_to_case_invert
1280
1281 let eq = unsafe_eq
1282
1283 let relevance_eq = unsafe_relevance_eq
1284 end
1285
1286 module UnsafeMonomorphic = struct
1287 let mkConst c = of_kind (Const (in_punivs c))
1288 let mkInd i = of_kind (Ind (in_punivs i))
1289 let mkConstruct c = of_kind (Construct (in_punivs c))
1290 end
1291
1292 include UnsafeMonomorphic