Debian Codegraph @

kernel/constr.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 (* File initially created by Gérard Huet and Thierry Coquand in 1984 *)
12 (* Extension to inductive constructions by Christine Paulin for Coq V5.6 *)
13 (* Extension to mutual inductive constructions by Christine Paulin for
14 Coq V5.10.2 *)
15 (* Extension to co-inductive constructions by Eduardo Gimenez *)
16 (* Optimization of substitution functions by Chet Murthy *)
17 (* Optimization of lifting functions by Bruno Barras, Mar 1997 *)
18 (* Hash-consing by Bruno Barras in Feb 1998 *)
19 (* Restructuration of Coq of the type-checking kernel by Jean-Christophe
20 Filliâtre, 1999 *)
21 (* Abstraction of the syntax of terms and iterators by Hugo Herbelin, 2000 *)
22 (* Cleaning and lightening of the kernel by Bruno Barras, Nov 2001 *)
23
24 (* This file defines the internal syntax of the Calculus of
25 Inductive Constructions (CIC) terms together with constructors,
26 destructors, iterators and basic functions *)
27
28 open Util
29 open Names
30 open UVars
31 open Context
32
33 type existential_key = Evar.t
34 type metavariable = int
35
36 (* This defines the strategy to use for verifiying a Cast *)
37 type cast_kind = VMcast | NATIVEcast | DEFAULTcast
38
39 (* This defines Cases annotations *)
40 type case_style = LetStyle | IfStyle | LetPatternStyle | MatchStyle | RegularStyle
41 type case_printing =
42 { style : case_style }
43
44 (* INVARIANT:
45 * - Array.length ci_cstr_ndecls = Array.length ci_cstr_nargs
46 * - forall (i : 0 .. pred (Array.length ci_cstr_ndecls)),
47 * ci_cstr_ndecls.(i) >= ci_cstr_nargs.(i)
48 *)
49 type case_info =
50 { ci_ind : inductive; (* inductive type to which belongs the value that is being matched *)
51 ci_npar : int; (* number of parameters of the above inductive type *)
52 ci_cstr_ndecls : int array; (* For each constructor, the corresponding integer determines
53 the number of values that can be bound in a match-construct.
54 NOTE: parameters of the inductive type are therefore excluded from the count *)
55 ci_cstr_nargs : int array; (* for each constructor, the corresponding integers determines
56 the number of values that can be applied to the constructor,
57 in addition to the parameters of the related inductive type
58 NOTE: "lets" are therefore excluded from the count
59 NOTE: parameters of the inductive type are also excluded from the count *)
60 ci_pp_info : case_printing (* not interpreted by the kernel *)
61 }
62
63 (********************************************************************)
64 (* Constructions as implemented *)
65 (********************************************************************)
66
67 (* [constr array] is an instance matching definitional [named_context] in
68 the same order (i.e. last argument first) *)
69 type 'constr pexistential = existential_key * 'constr SList.t
70 type ('constr, 'types, 'r) prec_declaration =
71 (Name.t,'r) pbinder_annot array * 'types array * 'constr array
72 type ('constr, 'types, 'r) pfixpoint =
73 (int array * int) * ('constr, 'types, 'r) prec_declaration
74 type ('constr, 'types, 'r) pcofixpoint =
75 int * ('constr, 'types, 'r) prec_declaration
76 type 'a puniverses = 'a UVars.puniverses
77 type pconstant = Constant.t puniverses
78 type pinductive = inductive puniverses
79 type pconstructor = constructor puniverses
80
81 type 'constr pcase_invert =
82 | NoInvert
83 | CaseInvert of { indices : 'constr array }
84
85 type ('constr,'r) pcase_branch = (Name.t,'r) Context.pbinder_annot array * 'constr
86 type ('types,'r) pcase_return = ((Name.t,'r) Context.pbinder_annot array * 'types) * 'r
87
88 type ('constr, 'types, 'univs, 'r) pcase =
89 case_info * 'univs * 'constr array * ('types,'r) pcase_return * 'constr pcase_invert * 'constr * ('constr, 'r) pcase_branch array
90
91 (* [Var] is used for named variables and [Rel] for variables as
92 de Bruijn indices. *)
93 type ('constr, 'types, 'sort, 'univs, 'r) kind_of_term =
94 | Rel of int
95 | Var of Id.t
96 | Meta of metavariable
97 | Evar of 'constr pexistential
98 | Sort of 'sort
99 | Cast of 'constr * cast_kind * 'types
100 | Prod of (Name.t,'r) pbinder_annot * 'types * 'types
101 | Lambda of (Name.t,'r) pbinder_annot * 'types * 'constr
102 | LetIn of (Name.t,'r) pbinder_annot * 'constr * 'types * 'constr
103 | App of 'constr * 'constr array
104 | Const of (Constant.t * 'univs)
105 | Ind of (inductive * 'univs)
106 | Construct of (constructor * 'univs)
107 | Case of case_info * 'univs * 'constr array * ('types,'r) pcase_return * 'constr pcase_invert * 'constr * ('constr,'r) pcase_branch array
108 | Fix of ('constr, 'types, 'r) pfixpoint
109 | CoFix of ('constr, 'types, 'r) pcofixpoint
110 | Proj of Projection.t * 'r * 'constr
111 | Int of Uint63.t
112 | Float of Float64.t
113 | String of Pstring.t
114 | Array of 'univs * 'constr array * 'constr * 'types
115
116 (* constr is the fixpoint of the previous type. *)
117 type t = T of (t, t, Sorts.t, Instance.t, Sorts.relevance) kind_of_term [@@unboxed]
118 type constr = t
119 type types = constr
120
121 type existential = existential_key * constr SList.t
122
123 type case_invert = constr pcase_invert
124 type case_return = (types,Sorts.relevance) pcase_return
125 type case_branch = (constr,Sorts.relevance) pcase_branch
126 type case = (constr, types, Instance.t, Sorts.relevance) pcase
127 type rec_declaration = (constr, types, Sorts.relevance) prec_declaration
128 type fixpoint = (constr, types, Sorts.relevance) pfixpoint
129 type cofixpoint = (constr, types, Sorts.relevance) pcofixpoint
130 type 'a binder_annot = ('a,Sorts.relevance) Context.pbinder_annot
131
132 (************************************************************************)
133 (* kind_of_term = constructions as seen by the user *)
134 (************************************************************************)
135
136 (* User view of [constr]. For [App], it is ensured there is at
137 least one argument and the function is not itself an applicative
138 term *)
139
140 let kind (T c) = c
141
142 let rec kind_nocast_gen kind c =
143 match kind c with
144 | Cast (c, _, _) -> kind_nocast_gen kind c
145 | App (h, outer) as k ->
146 (match kind_nocast_gen kind h with
147 | App (h, inner) -> App (h, Array.append inner outer)
148 | _ -> k)
149 | k -> k
150
151 let kind_nocast c = kind_nocast_gen kind c
152
153 (**********************************************************************)
154 (* Non primitive term destructors *)
155 (**********************************************************************)
156
157 (* Destructor operations : partial functions
158 Raise [DestKO] if the const has not the expected form *)
159
160 exception DestKO
161
162 let isMeta c = match kind c with Meta _ -> true | _ -> false
163
164 (* Destructs a type *)
165 let isSort c = match kind c with
166 | Sort _ -> true
167 | _ -> false
168
169 let rec isprop c = match kind c with
170 | Sort (Sorts.Prop | Sorts.Set) -> true
171 | Cast (c,_,_) -> isprop c
172 | _ -> false
173
174 let rec is_Prop c = match kind c with
175 | Sort Sorts.Prop -> true
176 | Cast (c,_,_) -> is_Prop c
177 | _ -> false
178
179 let rec is_Set c = match kind c with
180 | Sort Sorts.Set -> true
181 | Cast (c,_,_) -> is_Set c
182 | _ -> false
183
184 let rec is_Type c = match kind c with
185 | Sort (Sorts.Type _) -> true
186 | Cast (c,_,_) -> is_Type c
187 | _ -> false
188
189 let is_small = Sorts.is_small
190 let iskind c = isprop c || is_Type c
191
192 (* Tests if an evar *)
193 let isEvar c = match kind c with Evar _ -> true | _ -> false
194 let isEvar_or_Meta c = match kind c with
195 | Evar _ | Meta _ -> true
196 | _ -> false
197
198 let isCast c = match kind c with Cast _ -> true | _ -> false
199 (* Tests if a de Bruijn index *)
200 let isRel c = match kind c with Rel _ -> true | _ -> false
201 let isRelN n c =
202 match kind c with Rel n' -> Int.equal n n' | _ -> false
203 (* Tests if a variable *)
204 let isVar c = match kind c with Var _ -> true | _ -> false
205 let isVarId id c = match kind c with Var id' -> Id.equal id id' | _ -> false
206 (* Tests if an inductive *)
207 let isInd c = match kind c with Ind _ -> true | _ -> false
208 let isProd c = match kind c with | Prod _ -> true | _ -> false
209 let isLambda c = match kind c with | Lambda _ -> true | _ -> false
210 let isLetIn c = match kind c with LetIn _ -> true | _ -> false
211 let isApp c = match kind c with App _ -> true | _ -> false
212 let isConst c = match kind c with Const _ -> true | _ -> false
213 let isConstruct c = match kind c with Construct _ -> true | _ -> false
214 let isCase c = match kind c with Case _ -> true | _ -> false
215 let isProj c = match kind c with Proj _ -> true | _ -> false
216 let isFix c = match kind c with Fix _ -> true | _ -> false
217 let isCoFix c = match kind c with CoFix _ -> true | _ -> false
218
219 let isRef c = match kind c with
220 | Const _ | Ind _ | Construct _ | Var _ -> true
221 | _ -> false
222
223 (* Destructs a de Bruijn index *)
224 let destRel c = match kind c with
225 | Rel n -> n
226 | _ -> raise DestKO
227
228 (* Destructs an existential variable *)
229 let destMeta c = match kind c with
230 | Meta n -> n
231 | _ -> raise DestKO
232
233 (* Destructs a variable *)
234 let destVar c = match kind c with
235 | Var id -> id
236 | _ -> raise DestKO
237
238 let destSort c = match kind c with
239 | Sort s -> s
240 | _ -> raise DestKO
241
242 (* Destructs a casted term *)
243 let destCast c = match kind c with
244 | Cast (t1,k,t2) -> (t1,k,t2)
245 | _ -> raise DestKO
246
247 (* Destructs the product (x:t1)t2 *)
248 let destProd c = match kind c with
249 | Prod (x,t1,t2) -> (x,t1,t2)
250 | _ -> raise DestKO
251
252 (* Destructs the abstraction [x:t1]t2 *)
253 let destLambda c = match kind c with
254 | Lambda (x,t1,t2) -> (x,t1,t2)
255 | _ -> raise DestKO
256
257 (* Destructs the let [x:=b:t1]t2 *)
258 let destLetIn c = match kind c with
259 | LetIn (x,b,t1,t2) -> (x,b,t1,t2)
260 | _ -> raise DestKO
261
262 (* Destructs an application *)
263 let destApp c = match kind c with
264 | App (f,a) -> (f, a)
265 | _ -> raise DestKO
266
267 (* Destructs a constant *)
268 let destConst c = match kind c with
269 | Const kn -> kn
270 | _ -> raise DestKO
271
272 (* Destructs an existential variable *)
273 let destEvar c = match kind c with
274 | Evar (_kn, _a as r) -> r
275 | _ -> raise DestKO
276
277 (* Destructs a (co)inductive type named kn *)
278 let destInd c = match kind c with
279 | Ind (_kn, _a as r) -> r
280 | _ -> raise DestKO
281
282 (* Destructs a constructor *)
283 let destConstruct c = match kind c with
284 | Construct (_kn, _a as r) -> r
285 | _ -> raise DestKO
286
287 (* Destructs a term <p>Case c of lc1 | lc2 .. | lcn end *)
288 let destCase c = match kind c with
289 | Case (ci,u,params,p,iv,c,v) -> (ci,u,params,p,iv,c,v)
290 | _ -> raise DestKO
291
292 let destProj c = match kind c with
293 | Proj (p, r, c) -> (p, r, c)
294 | _ -> raise DestKO
295
296 let destFix c = match kind c with
297 | Fix fix -> fix
298 | _ -> raise DestKO
299
300 let destCoFix c = match kind c with
301 | CoFix cofix -> cofix
302 | _ -> raise DestKO
303
304 let destRef c = let open GlobRef in match kind c with
305 | Var x -> VarRef x, UVars.Instance.empty
306 | Const (c,u) -> ConstRef c, u
307 | Ind (ind,u) -> IndRef ind, u
308 | Construct (c,u) -> ConstructRef c, u
309 | _ -> raise DestKO
310
311 let destArray c = match kind c with
312 | Array (u,ar,def,ty) -> u,ar,def,ty
313 | _ -> raise DestKO
314
315 (******************************************************************)
316 (* Flattening and unflattening of embedded applications and casts *)
317 (******************************************************************)
318
319 let decompose_app_list c =
320 match kind c with
321 | App (f,cl) -> (f, Array.to_list cl)
322 | _ -> (c,[])
323
324 let decompose_app c =
325 match kind c with
326 | App (f,cl) -> (f, cl)
327 | _ -> (c,[||])
328
329 (*********************)
330 (* Term constructors *)
331 (*********************)
332
333 (* Constructs a de Bruijn index with number n *)
334 let rels = Array.init 17 (fun i -> T (Rel i))
335
336 let mkRel n = if 0<=n && n<=16 then rels.(n) else T (Rel n)
337
338 let mkSProp = T (Sort Sorts.sprop)
339 let mkProp = T (Sort Sorts.prop)
340 let mkSet = T (Sort Sorts.set)
341
342 (* Enforces:
343 - applicative terms have at least one argument and the
344 function is not itself an applicative term
345 - stacks of VM or native casts are collapsed
346 - small rels are shared
347 - small sorts are shared
348 *)
349 let of_kind = function
350 | Rel n when 0 <= n && n < Array.length rels -> rels.(n)
351 | App (f, [||]) -> f
352 | App (f, a) as k -> begin match kind f with
353 | App (g, cl) -> T (App (g, Array.append cl a))
354 | _ -> T k
355 end
356 | Cast (c, knd, t) as k -> begin match kind c with
357 | Cast (c, knd', _) when (knd == VMcast || knd == NATIVEcast) && knd == knd' ->
358 T (Cast (c, knd, t))
359 | _ -> T k
360 end
361 | Sort Sorts.SProp -> mkSProp
362 | Sort Sorts.Prop -> mkProp
363 | Sort Sorts.Set -> mkSet
364 | k -> T k
365
366 (* Construct a type *)
367 let mkType u = of_kind @@ Sort (Sorts.sort_of_univ u)
368 let mkSort s = of_kind @@ Sort s
369
370 (* Constructs the product (x:t1)t2 *)
371 let mkProd (x,t1,t2) = of_kind @@ Prod (x,t1,t2)
372
373 (* Constructs the abstraction [x:t1]t2 *)
374 let mkLambda (x,t1,t2) = of_kind @@ Lambda (x,t1,t2)
375
376 (* Constructs [x=c_1:t]c_2 *)
377 let mkLetIn (x,c1,t,c2) = of_kind @@ LetIn (x,c1,t,c2)
378
379 let mkApp (f, a) = of_kind (App (f, a))
380
381 (* Constructs the term t1::t2, i.e. the term t1 casted with the type t2 *)
382 (* (that means t2 is declared as the type of t1) *)
383 let mkCast (t1,k,t2) = of_kind @@ Cast (t1,k,t2)
384
385 let map_puniverses f (x,u) = (f x, u)
386 let in_punivs a = (a, UVars.Instance.empty)
387
388 (* Constructs a constant *)
389 let mkConst c = of_kind @@ Const (in_punivs c)
390 let mkConstU c = of_kind @@ Const c
391
392 (* Constructs an applied projection *)
393 let mkProj (p,r,c) = of_kind @@ Proj (p,r,c)
394
395 (* Constructs an existential variable *)
396 let mkEvar e = of_kind @@ Evar e
397
398 (* Constructs the ith (co)inductive type of the block named kn *)
399 let mkInd m = of_kind @@ Ind (in_punivs m)
400 let mkIndU m = of_kind @@ Ind m
401
402 (* Constructs the jth constructor of the ith (co)inductive type of the
403 block named kn. *)
404 let mkConstruct c = of_kind @@ Construct (in_punivs c)
405 let mkConstructU c = of_kind @@ Construct c
406 let mkConstructUi ((ind,u),i) = of_kind @@ Construct ((ind,i),u)
407
408 (* Constructs the term <p>Case c of c1 | c2 .. | cn end *)
409 let mkCase (ci, u, params, p, iv, c, ac) = of_kind @@ Case (ci, u, params, p, iv, c, ac)
410
411 (* If recindxs = [|i1,...in|]
412 funnames = [|f1,...fn|]
413 typarray = [|t1,...tn|]
414 bodies = [|b1,...bn|]
415 then
416
417 mkFix ((recindxs,i),(funnames,typarray,bodies))
418
419 constructs the ith function of the block
420
421 Fixpoint f1 [ctx1] : t1 := b1
422 with f2 [ctx2] : t2 := b2
423 ...
424 with fn [ctxn] : tn := bn.
425
426 where the length of the jth context is ij.
427 *)
428
429 let mkFix fix = of_kind @@ Fix fix
430
431 (* If funnames = [|f1,...fn|]
432 typarray = [|t1,...tn|]
433 bodies = [|b1,...bn|]
434 then
435
436 mkCoFix (i,(funnames,typsarray,bodies))
437
438 constructs the ith function of the block
439
440 CoFixpoint f1 : t1 := b1
441 with f2 : t2 := b2
442 ...
443 with fn : tn := bn.
444 *)
445 let mkCoFix cofix= of_kind @@ CoFix cofix
446
447 (* Constructs an existential variable named "?n" *)
448 let mkMeta n = of_kind @@ Meta n
449
450 (* Constructs a Variable named id *)
451 let mkVar id = of_kind @@ Var id
452
453 let mkRef (gr,u) = let open GlobRef in match gr with
454 | ConstRef c -> mkConstU (c,u)
455 | IndRef ind -> mkIndU (ind,u)
456 | ConstructRef c -> mkConstructU (c,u)
457 | VarRef x -> mkVar x
458
459 (* Constructs a primitive integer *)
460 let mkInt i = of_kind @@ Int i
461
462 (* Constructs an array *)
463 let mkArray (u,t,def,ty) = of_kind @@ Array (u,t,def,ty)
464
465 (* Constructs a primitive float number *)
466 let mkFloat f = of_kind @@ Float f
467
468 (* Constructs a primitive string. *)
469 let mkString s = of_kind @@ String s
470
471 module UnsafeMonomorphic = struct
472 let mkConst = mkConst
473 let mkInd = mkInd
474 let mkConstruct = mkConstruct
475 end
476
477 (****************************************************************************)
478 (* Functions to recur through subterms *)
479 (****************************************************************************)
480
481 (* [fold f acc c] folds [f] on the immediate subterms of [c]
482 starting from [acc] and proceeding from left to right according to
483 the usual representation of the constructions; it is not recursive *)
484
485 let fold_invert f acc = function
486 | NoInvert -> acc
487 | CaseInvert {indices} ->
488 Array.fold_left f acc indices
489
490 let fold f acc c = match kind c with
491 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
492 | Construct _ | Int _ | Float _ | String _) -> acc
493 | Cast (c,_,t) -> f (f acc c) t
494 | Prod (_,t,c) -> f (f acc t) c
495 | Lambda (_,t,c) -> f (f acc t) c
496 | LetIn (_,b,t,c) -> f (f (f acc b) t) c
497 | App (c,l) -> Array.fold_left f (f acc c) l
498 | Proj (_p,_r,c) -> f acc c
499 | Evar (_,l) -> SList.Skip.fold f acc l
500 | Case (_,_,pms,((_,p),_),iv,c,bl) ->
501 Array.fold_left (fun acc (_, b) -> f acc b) (f (fold_invert f (f (Array.fold_left f acc pms) p) iv) c) bl
502 | Fix (_,(_lna,tl,bl)) ->
503 Array.fold_left2 (fun acc t b -> f (f acc t) b) acc tl bl
504 | CoFix (_,(_lna,tl,bl)) ->
505 Array.fold_left2 (fun acc t b -> f (f acc t) b) acc tl bl
506 | Array(_u,t,def,ty) ->
507 f (f (Array.fold_left f acc t) def) ty
508
509 (* [iter f c] iters [f] on the immediate subterms of [c]; it is
510 not recursive and the order with which subterms are processed is
511 not specified *)
512
513 let iter_invert f = function
514 | NoInvert -> ()
515 | CaseInvert {indices;} ->
516 Array.iter f indices
517
518 let iter f c = match kind c with
519 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
520 | Construct _ | Int _ | Float _ | String _) -> ()
521 | Cast (c,_,t) -> f c; f t
522 | Prod (_,t,c) -> f t; f c
523 | Lambda (_,t,c) -> f t; f c
524 | LetIn (_,b,t,c) -> f b; f t; f c
525 | App (c,l) -> f c; Array.iter f l
526 | Proj (_p,_r,c) -> f c
527 | Evar (_,l) -> SList.Skip.iter f l
528 | Case (_,_,pms,p,iv,c,bl) ->
529 Array.iter f pms; f (snd @@ fst p); iter_invert f iv; f c; Array.iter (fun (_, b) -> f b) bl
530 | Fix (_,(_,tl,bl)) -> Array.iter f tl; Array.iter f bl
531 | CoFix (_,(_,tl,bl)) -> Array.iter f tl; Array.iter f bl
532 | Array(_u,t,def,ty) -> Array.iter f t; f def; f ty
533
534 (* [iter_with_binders g f n c] iters [f n] on the immediate
535 subterms of [c]; it carries an extra data [n] (typically a lift
536 index) which is processed by [g] (which typically add 1 to [n]) at
537 each binder traversal; it is not recursive and the order with which
538 subterms are processed is not specified *)
539
540 let iter_with_binders g f n c = match kind c with
541 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
542 | Construct _ | Int _ | Float _ | String _) -> ()
543 | Cast (c,_,t) -> f n c; f n t
544 | Prod (_,t,c) -> f n t; f (g n) c
545 | Lambda (_,t,c) -> f n t; f (g n) c
546 | LetIn (_,b,t,c) -> f n b; f n t; f (g n) c
547 | App (c,l) -> f n c; Array.Fun1.iter f n l
548 | Evar (_,l) -> SList.Skip.iter (fun c -> f n c) l
549 | Case (_,_,pms,(p,_),iv,c,bl) ->
550 Array.Fun1.iter f n pms;
551 f (iterate g (Array.length (fst p)) n) (snd p);
552 iter_invert (f n) iv;
553 f n c;
554 Array.Fun1.iter (fun n (ctx, b) -> f (iterate g (Array.length ctx) n) b) n bl
555 | Proj (_p,_r,c) -> f n c
556 | Fix (_,(_,tl,bl)) ->
557 Array.Fun1.iter f n tl;
558 Array.Fun1.iter f (iterate g (Array.length tl) n) bl
559 | CoFix (_,(_,tl,bl)) ->
560 Array.Fun1.iter f n tl;
561 Array.Fun1.iter f (iterate g (Array.length tl) n) bl
562 | Array(_u,t,def,ty) ->
563 Array.iter (f n) t; f n def; f n ty
564
565 (* [fold_constr_with_binders g f n acc c] folds [f n] on the immediate
566 subterms of [c] starting from [acc] and proceeding from left to
567 right according to the usual representation of the constructions as
568 [fold_constr] but it carries an extra data [n] (typically a lift
569 index) which is processed by [g] (which typically add 1 to [n]) at
570 each binder traversal; it is not recursive *)
571
572 let fold_constr_with_binders g f n acc c =
573 match kind c with
574 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
575 | Construct _ | Int _ | Float _ | String _) -> acc
576 | Cast (c,_, t) -> f n (f n acc c) t
577 | Prod (_na,t,c) -> f (g n) (f n acc t) c
578 | Lambda (_na,t,c) -> f (g n) (f n acc t) c
579 | LetIn (_na,b,t,c) -> f (g n) (f n (f n acc b) t) c
580 | App (c,l) -> Array.fold_left (f n) (f n acc c) l
581 | Proj (_p,_r,c) -> f n acc c
582 | Evar (_,l) -> SList.Skip.fold (f n) acc l
583 | Case (_,_,pms,(p,_),iv,c,bl) ->
584 let fold_ctx n accu (nas, c) =
585 f (iterate g (Array.length nas) n) accu c
586 in
587 Array.fold_left (fold_ctx n) (f n (fold_invert (f n) (fold_ctx n (Array.fold_left (f n) acc pms) p) iv) c) bl
588 | Fix (_,(_,tl,bl)) ->
589 let n' = iterate g (Array.length tl) n in
590 let fd = Array.map2 (fun t b -> (t,b)) tl bl in
591 Array.fold_left (fun acc (t,b) -> f n' (f n acc t) b) acc fd
592 | CoFix (_,(_,tl,bl)) ->
593 let n' = iterate g (Array.length tl) n in
594 let fd = Array.map2 (fun t b -> (t,b)) tl bl in
595 Array.fold_left (fun acc (t,b) -> f n' (f n acc t) b) acc fd
596 | Array(_u,t,def,ty) ->
597 f n (f n (Array.fold_left (f n) acc t) def) ty
598
599 (* [map f c] maps [f] on the immediate subterms of [c]; it is
600 not recursive and the order with which subterms are processed is
601 not specified *)
602
603 let map_under_context f d =
604 let (nas, p) = d in
605 let p' = f p in
606 if p' == p then d else (nas, p')
607
608 let map_branches f bl =
609 let bl' = Array.map (map_under_context f) bl in
610 if Array.for_all2 (==) bl' bl then bl else bl'
611
612 let map_return_predicate f (p,r as v) =
613 let p' = map_under_context f p in
614 if p == p' then v else p', r
615
616 let map_under_context_with_binders g f l d =
617 let (nas, p) = d in
618 let l = iterate g (Array.length nas) l in
619 let p' = f l p in
620 if p' == p then d else (nas, p')
621
622 let map_branches_with_binders g f l bl =
623 let bl' = Array.map (map_under_context_with_binders g f l) bl in
624 if Array.for_all2 (==) bl' bl then bl else bl'
625
626 let map_return_predicate_with_binders g f l (p,r as v) =
627 let p' = map_under_context_with_binders g f l p in
628 if p == p' then v else p',r
629
630 let map_invert f = function
631 | NoInvert -> NoInvert
632 | CaseInvert {indices;} as orig ->
633 let indices' = Array.Smart.map f indices in
634 if indices == indices' then orig
635 else CaseInvert {indices=indices';}
636
637 let map f c = match kind c with
638 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
639 | Construct _ | Int _ | Float _ | String _) -> c
640 | Cast (b,k,t) ->
641 let b' = f b in
642 let t' = f t in
643 if b'==b && t' == t then c
644 else mkCast (b', k, t')
645 | Prod (na,t,b) ->
646 let b' = f b in
647 let t' = f t in
648 if b'==b && t' == t then c
649 else mkProd (na, t', b')
650 | Lambda (na,t,b) ->
651 let b' = f b in
652 let t' = f t in
653 if b'==b && t' == t then c
654 else mkLambda (na, t', b')
655 | LetIn (na,b,t,k) ->
656 let b' = f b in
657 let t' = f t in
658 let k' = f k in
659 if b'==b && t' == t && k'==k then c
660 else mkLetIn (na, b', t', k')
661 | App (b,l) ->
662 let b' = f b in
663 let l' = Array.Smart.map f l in
664 if b'==b && l'==l then c
665 else mkApp (b', l')
666 | Proj (p,r,t) ->
667 let t' = f t in
668 if t' == t then c
669 else mkProj (p, r, t')
670 | Evar (e,l) ->
671 let l' = SList.Smart.map f l in
672 if l'==l then c
673 else mkEvar (e, l')
674 | Case (ci,u,pms,p,iv,b,bl) ->
675 let pms' = Array.Smart.map f pms in
676 let b' = f b in
677 let iv' = map_invert f iv in
678 let p' = map_return_predicate f p in
679 let bl' = map_branches f bl in
680 if b'==b && iv'==iv && p'==p && bl'==bl && pms'==pms then c
681 else mkCase (ci, u, pms', p', iv', b', bl')
682 | Fix (ln,(lna,tl,bl)) ->
683 let tl' = Array.Smart.map f tl in
684 let bl' = Array.Smart.map f bl in
685 if tl'==tl && bl'==bl then c
686 else mkFix (ln,(lna,tl',bl'))
687 | CoFix(ln,(lna,tl,bl)) ->
688 let tl' = Array.Smart.map f tl in
689 let bl' = Array.Smart.map f bl in
690 if tl'==tl && bl'==bl then c
691 else mkCoFix (ln,(lna,tl',bl'))
692 | Array(u,t,def,ty) ->
693 let t' = Array.Smart.map f t in
694 let def' = f def in
695 let ty' = f ty in
696 if def'==def && t==t' && ty==ty' then c
697 else mkArray(u,t',def',ty')
698
699 (* Like {!map} but with an accumulator. *)
700
701 let fold_map_invert f acc = function
702 | NoInvert -> acc, NoInvert
703 | CaseInvert {indices;} as orig ->
704 let acc, indices' = Array.Smart.fold_left_map f acc indices in
705 if indices==indices' then acc, orig
706 else acc, CaseInvert {indices=indices';}
707
708 let fold_map_under_context f accu d =
709 let (nas, p) = d in
710 let accu, p' = f accu p in
711 if p' == p then accu, d else accu, (nas, p')
712
713 let fold_map_branches f accu bl =
714 let accu, bl' = Array.Smart.fold_left_map (fold_map_under_context f) accu bl in
715 if Array.for_all2 (==) bl' bl then accu, bl else accu, bl'
716
717 let fold_map_return_predicate f accu (p,r as v) =
718 let accu, p' = fold_map_under_context f accu p in
719 let v = if p == p' then v else p', r in
720 accu, v
721
722 let fold_map f accu c = match kind c with
723 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
724 | Construct _ | Int _ | Float _ | String _) -> accu, c
725 | Cast (b,k,t) ->
726 let accu, b' = f accu b in
727 let accu, t' = f accu t in
728 if b'==b && t' == t then accu, c
729 else accu, mkCast (b', k, t')
730 | Prod (na,t,b) ->
731 let accu, b' = f accu b in
732 let accu, t' = f accu t in
733 if b'==b && t' == t then accu, c
734 else accu, mkProd (na, t', b')
735 | Lambda (na,t,b) ->
736 let accu, b' = f accu b in
737 let accu, t' = f accu t in
738 if b'==b && t' == t then accu, c
739 else accu, mkLambda (na, t', b')
740 | LetIn (na,b,t,k) ->
741 let accu, b' = f accu b in
742 let accu, t' = f accu t in
743 let accu, k' = f accu k in
744 if b'==b && t' == t && k'==k then accu, c
745 else accu, mkLetIn (na, b', t', k')
746 | App (b,l) ->
747 let accu, b' = f accu b in
748 let accu, l' = Array.Smart.fold_left_map f accu l in
749 if b'==b && l'==l then accu, c
750 else accu, mkApp (b', l')
751 | Proj (p,r,t) ->
752 let accu, t' = f accu t in
753 if t' == t then accu, c
754 else accu, mkProj (p, r, t')
755 | Evar (e,l) ->
756 let accu, l' = SList.Smart.fold_left_map f accu l in
757 if l'==l then accu, c
758 else accu, mkEvar (e, l')
759 | Case (ci,u,pms,p,iv,b,bl) ->
760 let accu, pms' = Array.Smart.fold_left_map f accu pms in
761 let accu, p' = fold_map_return_predicate f accu p in
762 let accu, iv' = fold_map_invert f accu iv in
763 let accu, b' = f accu b in
764 let accu, bl' = fold_map_branches f accu bl in
765 if pms'==pms && p'==p && iv'==iv && b'==b && bl'==bl then accu, c
766 else accu, mkCase (ci, u, pms', p', iv', b', bl')
767 | Fix (ln,(lna,tl,bl)) ->
768 let accu, tl' = Array.Smart.fold_left_map f accu tl in
769 let accu, bl' = Array.Smart.fold_left_map f accu bl in
770 if tl'==tl && bl'==bl then accu, c
771 else accu, mkFix (ln,(lna,tl',bl'))
772 | CoFix(ln,(lna,tl,bl)) ->
773 let accu, tl' = Array.Smart.fold_left_map f accu tl in
774 let accu, bl' = Array.Smart.fold_left_map f accu bl in
775 if tl'==tl && bl'==bl then accu, c
776 else accu, mkCoFix (ln,(lna,tl',bl'))
777 | Array(u,t,def,ty) ->
778 let accu, t' = Array.Smart.fold_left_map f accu t in
779 let accu, def' = f accu def in
780 let accu, ty' = f accu ty in
781 if def'==def && t==t' && ty==ty' then accu, c
782 else accu, mkArray(u,t',def',ty')
783
784 (* [map_with_binders g f n c] maps [f n] on the immediate
785 subterms of [c]; it carries an extra data [n] (typically a lift
786 index) which is processed by [g] (which typically add 1 to [n]) at
787 each binder traversal; it is not recursive and the order with which
788 subterms are processed is not specified *)
789
790 let map_with_binders g f l c0 = match kind c0 with
791 | (Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _
792 | Construct _ | Int _ | Float _ | String _) -> c0
793 | Cast (c, k, t) ->
794 let c' = f l c in
795 let t' = f l t in
796 if c' == c && t' == t then c0
797 else mkCast (c', k, t')
798 | Prod (na, t, c) ->
799 let t' = f l t in
800 let c' = f (g l) c in
801 if t' == t && c' == c then c0
802 else mkProd (na, t', c')
803 | Lambda (na, t, c) ->
804 let t' = f l t in
805 let c' = f (g l) c in
806 if t' == t && c' == c then c0
807 else mkLambda (na, t', c')
808 | LetIn (na, b, t, c) ->
809 let b' = f l b in
810 let t' = f l t in
811 let c' = f (g l) c in
812 if b' == b && t' == t && c' == c then c0
813 else mkLetIn (na, b', t', c')
814 | App (c, al) ->
815 let c' = f l c in
816 let al' = Array.Fun1.Smart.map f l al in
817 if c' == c && al' == al then c0
818 else mkApp (c', al')
819 | Proj (p, r, t) ->
820 let t' = f l t in
821 if t' == t then c0
822 else mkProj (p, r, t')
823 | Evar (e, al) ->
824 let al' = SList.Smart.map (fun c -> f l c) al in
825 if al' == al then c0
826 else mkEvar (e, al')
827 | Case (ci, u, pms, p, iv, c, bl) ->
828 let pms' = Array.Fun1.Smart.map f l pms in
829 let p' = map_return_predicate_with_binders g f l p in
830 let iv' = map_invert (f l) iv in
831 let c' = f l c in
832 let bl' = map_branches_with_binders g f l bl in
833 if pms' == pms && p' == p && iv' == iv && c' == c && bl' == bl then c0
834 else mkCase (ci, u, pms', p', iv', c', bl')
835 | Fix (ln, (lna, tl, bl)) ->
836 let tl' = Array.Fun1.Smart.map f l tl in
837 let l' = iterate g (Array.length tl) l in
838 let bl' = Array.Fun1.Smart.map f l' bl in
839 if tl' == tl && bl' == bl then c0
840 else mkFix (ln,(lna,tl',bl'))
841 | CoFix(ln,(lna,tl,bl)) ->
842 let tl' = Array.Fun1.Smart.map f l tl in
843 let l' = iterate g (Array.length tl) l in
844 let bl' = Array.Fun1.Smart.map f l' bl in
845 mkCoFix (ln,(lna,tl',bl'))
846 | Array(u,t,def,ty) ->
847 let t' = Array.Fun1.Smart.map f l t in
848 let def' = f l def in
849 let ty' = f l ty in
850 if def'==def && t==t' && ty==ty' then c0
851 else mkArray(u,t',def',ty')
852
853 (*********************)
854 (* Lifting *)
855 (*********************)
856
857 (* The generic lifting function *)
858 let rec exliftn el c =
859 let open Esubst in
860 match kind c with
861 | Rel i ->
862 let j = reloc_rel i el in
863 if Int.equal i j then c else mkRel j
864 | _ -> map_with_binders el_lift exliftn el c
865
866 (* Lifting the binding depth across k bindings *)
867
868 let liftn n k c =
869 let open Esubst in
870 match el_liftn (pred k) (el_shft n el_id) with
871 | ELID -> c
872 | el -> exliftn el c
873
874 let lift n = liftn n 1
875
876 type 'univs instance_compare_fn = (GlobRef.t * int) option ->
877 'univs -> 'univs -> bool
878
879 type 'constr constr_compare_fn = int -> 'constr -> 'constr -> bool
880
881 (* [compare_head_gen_evar k1 k2 u s e eq leq c1 c2] compare [c1] and
882 [c2] (using [k1] to expose the structure of [c1] and [k2] to expose
883 the structure [c2]) using [eq] to compare the immediate subterms of
884 [c1] of [c2] for conversion if needed, [leq] for cumulativity, [u]
885 to compare universe instances, and [s] to compare sorts; Cast's,
886 application associativity, binders name and Cases annotations are
887 not taken into account. Note that as [kind1] and [kind2] are
888 potentially different, we cannot use, in recursive case, the
889 optimisation that physically equal arrays are equals (hence the
890 calls to {!Array.equal_norefl}). *)
891
892 let eq_invert eq iv1 iv2 =
893 match iv1, iv2 with
894 | NoInvert, NoInvert -> true
895 | NoInvert, CaseInvert _ | CaseInvert _, NoInvert -> false
896 | CaseInvert {indices}, CaseInvert iv2 ->
897 Array.equal eq indices iv2.indices
898
899 let eq_under_context eq (_nas1, p1) (_nas2, p2) =
900 eq p1 p2
901
902 let compare_head_gen_leq_with kind1 kind2 leq_universes leq_sorts eq_evars eq leq nargs t1 t2 =
903 match kind_nocast_gen kind1 t1, kind_nocast_gen kind2 t2 with
904 | Cast _, _ | _, Cast _ -> assert false (* kind_nocast *)
905 | Rel n1, Rel n2 -> Int.equal n1 n2
906 | Meta m1, Meta m2 -> Int.equal m1 m2
907 | Var id1, Var id2 -> Id.equal id1 id2
908 | Int i1, Int i2 -> Uint63.equal i1 i2
909 | Float f1, Float f2 -> Float64.equal f1 f2
910 | String s1, String s2 -> Pstring.equal s1 s2
911 | Sort s1, Sort s2 -> leq_sorts s1 s2
912 | Prod (_,t1,c1), Prod (_,t2,c2) -> eq 0 t1 t2 && leq 0 c1 c2
913 | Lambda (_,t1,c1), Lambda (_,t2,c2) -> eq 0 t1 t2 && eq 0 c1 c2
914 | LetIn (_,b1,t1,c1), LetIn (_,b2,t2,c2) -> eq 0 b1 b2 && eq 0 t1 t2 && leq nargs c1 c2
915 | App (c1, l1), App (c2, l2) ->
916 let len = Array.length l1 in
917 Int.equal len (Array.length l2) &&
918 leq (nargs+len) c1 c2 && Array.equal_norefl (eq 0) l1 l2
919 | Proj (p1,_,c1), Proj (p2,_,c2) ->
920 Projection.CanOrd.equal p1 p2 && eq 0 c1 c2
921 | Evar (e1,l1), Evar (e2,l2) -> eq_evars (e1, l1) (e2, l2)
922 | Const (c1,u1), Const (c2,u2) ->
923 (* The args length currently isn't used but may as well pass it. *)
924 Constant.CanOrd.equal c1 c2 && leq_universes (Some (GlobRef.ConstRef c1, nargs)) u1 u2
925 | Ind (c1,u1), Ind (c2,u2) -> Ind.CanOrd.equal c1 c2 && leq_universes (Some (GlobRef.IndRef c1, nargs)) u1 u2
926 | Construct (c1,u1), Construct (c2,u2) ->
927 Construct.CanOrd.equal c1 c2 && leq_universes (Some (GlobRef.ConstructRef c1, nargs)) u1 u2
928 | Case (ci1,u1,pms1,(p1,_r1),iv1,c1,bl1), Case (ci2,u2,pms2,(p2,_r2),iv2,c2,bl2) ->
929 (* Ignore _r1/_r2: implied by comparing p1/p2 *)
930 (** FIXME: what are we doing with u1 = u2 ? *)
931 Ind.CanOrd.equal ci1.ci_ind ci2.ci_ind && leq_universes (Some (GlobRef.IndRef ci1.ci_ind, 0)) u1 u2 &&
932 Array.equal (eq 0) pms1 pms2 && eq_under_context (eq 0) p1 p2 &&
933 eq_invert (eq 0) iv1 iv2 &&
934 eq 0 c1 c2 && Array.equal (eq_under_context (eq 0)) bl1 bl2
935 | Fix ((ln1, i1),(_,tl1,bl1)), Fix ((ln2, i2),(_,tl2,bl2)) ->
936 Int.equal i1 i2 && Array.equal Int.equal ln1 ln2
937 && Array.equal_norefl (eq 0) tl1 tl2 && Array.equal_norefl (eq 0) bl1 bl2
938 | CoFix(ln1,(_,tl1,bl1)), CoFix(ln2,(_,tl2,bl2)) ->
939 Int.equal ln1 ln2 && Array.equal_norefl (eq 0) tl1 tl2 && Array.equal_norefl (eq 0) bl1 bl2
940 | Array(u1,t1,def1,ty1), Array(u2,t2,def2,ty2) ->
941 leq_universes None u1 u2 &&
942 Array.equal_norefl (eq 0) t1 t2 &&
943 eq 0 def1 def2 && eq 0 ty1 ty2
944 | (Rel _ | Meta _ | Var _ | Sort _ | Prod _ | Lambda _ | LetIn _ | App _
945 | Proj _ | Evar _ | Const _ | Ind _ | Construct _ | Case _ | Fix _
946 | CoFix _ | Int _ | Float _ | String _ | Array _), _ -> false
947
948 (* [compare_head_gen_leq u s eq leq c1 c2] compare [c1] and [c2] using [eq] to compare
949 the immediate subterms of [c1] of [c2] for conversion if needed, [leq] for cumulativity,
950 [u] to compare universe instances and [s] to compare sorts; Cast's,
951 application associativity, binders name and Cases annotations are
952 not taken into account *)
953
954 let compare_head_gen_leq leq_universes leq_sorts eq_evars eq leq t1 t2 =
955 compare_head_gen_leq_with kind kind leq_universes leq_sorts eq_evars eq leq t1 t2
956
957 (* [compare_head_gen u s f c1 c2] compare [c1] and [c2] using [f] to
958 compare the immediate subterms of [c1] of [c2] if needed, [u] to
959 compare universe instances and [s] to compare sorts; Cast's,
960 application associativity, binders name and Cases annotations are
961 not taken into account.
962
963 [compare_head_gen_with] is a variant taking kind-of-term functions,
964 to expose subterms of [c1] and [c2], as arguments. *)
965
966 let compare_head_gen_with kind1 kind2 eq_universes eq_sorts eq_evars eq t1 t2 =
967 compare_head_gen_leq_with kind1 kind2 eq_universes eq_sorts eq_evars eq eq t1 t2
968
969 let compare_head_gen eq_universes eq_sorts eq_evars eq t1 t2 =
970 compare_head_gen_leq eq_universes eq_sorts eq_evars eq eq t1 t2
971
972 let compare_head = compare_head_gen (fun _ -> UVars.Instance.equal) Sorts.equal
973
974 (*******************************)
975 (* alpha conversion functions *)
976 (*******************************)
977
978 (* alpha conversion : ignore print names and casts *)
979
980 let eq_existential eq (evk1, args1) (evk2, args2) =
981 Evar.equal evk1 evk2 && SList.equal eq args1 args2
982
983 let rec eq_constr nargs m n =
984 (m == n) || compare_head_gen (fun _ -> Instance.equal) Sorts.equal (eq_existential (eq_constr 0)) eq_constr nargs m n
985
986 let equal n m = eq_constr 0 m n (* to avoid tracing a recursive fun *)
987
988 let eq_constr_univs univs m n =
989 if m == n then true
990 else
991 let eq_universes _ = UGraph.check_eq_instances Sorts.Quality.equal univs in
992 let eq_sorts s1 s2 = s1 == s2 || UGraph.check_eq_sort Sorts.Quality.equal univs s1 s2 in
993 let rec eq_constr' nargs m n =
994 m == n || compare_head_gen eq_universes eq_sorts (eq_existential (eq_constr' 0)) eq_constr' nargs m n
995 in compare_head_gen eq_universes eq_sorts (eq_existential (eq_constr' 0)) eq_constr' 0 m n
996
997 let leq_constr_univs univs m n =
998 if m == n then true
999 else
1000 let eq_universes _ = UGraph.check_eq_instances Sorts.Quality.equal univs in
1001 let eq_sorts s1 s2 = s1 == s2 ||
1002 UGraph.check_eq_sort Sorts.Quality.equal univs s1 s2 in
1003 let leq_sorts s1 s2 = s1 == s2 ||
1004 UGraph.check_leq_sort Sorts.Quality.equal univs s1 s2 in
1005 let rec eq_constr' nargs m n =
1006 m == n || compare_head_gen eq_universes eq_sorts (eq_existential (eq_constr' 0)) eq_constr' nargs m n
1007 in
1008 let rec compare_leq nargs m n =
1009 compare_head_gen_leq eq_universes leq_sorts (eq_existential (eq_constr' 0)) eq_constr' leq_constr' nargs m n
1010 and leq_constr' nargs m n = m == n || compare_leq nargs m n in
1011 compare_leq 0 m n
1012
1013 let rec eq_constr_nounivs m n =
1014 (m == n) || compare_head_gen (fun _ _ _ -> true) (fun _ _ -> true) (eq_existential eq_constr_nounivs) (fun _ -> eq_constr_nounivs) 0 m n
1015
1016 let compare_invert f iv1 iv2 =
1017 match iv1, iv2 with
1018 | NoInvert, NoInvert -> 0
1019 | NoInvert, CaseInvert _ -> -1
1020 | CaseInvert _, NoInvert -> 1
1021 | CaseInvert iv1, CaseInvert iv2 ->
1022 Array.compare f iv1.indices iv2.indices
1023
1024 let constr_ord_int f t1 t2 =
1025 let open! Compare in
1026 let fix_cmp (a1, i1) (a2, i2) =
1027 compare [(Array.compare Int.compare,a1,a2); (Int.compare,i1,i2)]
1028 in
1029 let ctx_cmp f (_n1, p1) (_n2, p2) = f p1 p2 in
1030 match kind t1, kind t2 with
1031 | Cast (c1,_,_), _ -> f c1 t2
1032 | _, Cast (c2,_,_) -> f t1 c2
1033 (* Why this special case? *)
1034 | App (c1,l1), _ when isCast c1 -> let c1 = pi1 (destCast c1) in f (mkApp (c1,l1)) t2
1035 | _, App (c2,l2) when isCast c2 -> let c2 = pi1 (destCast c2) in f t1 (mkApp (c2,l2))
1036 | Rel n1, Rel n2 -> Int.compare n1 n2
1037 | Rel _, _ -> -1 | _, Rel _ -> 1
1038 | Var id1, Var id2 -> Id.compare id1 id2
1039 | Var _, _ -> -1 | _, Var _ -> 1
1040 | Meta m1, Meta m2 -> Int.compare m1 m2
1041 | Meta _, _ -> -1 | _, Meta _ -> 1
1042 | Evar (e1,l1), Evar (e2,l2) ->
1043 compare [(Evar.compare, e1, e2); (SList.compare f, l1, l2)]
1044 | Evar _, _ -> -1 | _, Evar _ -> 1
1045 | Sort s1, Sort s2 -> Sorts.compare s1 s2
1046 | Sort _, _ -> -1 | _, Sort _ -> 1
1047 | Prod (_,t1,c1), Prod (_,t2,c2)
1048 | Lambda (_,t1,c1), Lambda (_,t2,c2) -> compare [(f,t1,t2); (f,c1,c2)]
1049 | Prod _, _ -> -1 | _, Prod _ -> 1
1050 | Lambda _, _ -> -1 | _, Lambda _ -> 1
1051 | LetIn (_,b1,t1,c1), LetIn (_,b2,t2,c2) -> compare [(f,b1,b2); (f,t1,t2); (f,c1,c2)]
1052 | LetIn _, _ -> -1 | _, LetIn _ -> 1
1053 | App (c1,l1), App (c2,l2) -> compare [(f,c1,c2); (Array.compare f, l1, l2)]
1054 | App _, _ -> -1 | _, App _ -> 1
1055 | Const (c1,_u1), Const (c2,_u2) -> Constant.CanOrd.compare c1 c2
1056 | Const _, _ -> -1 | _, Const _ -> 1
1057 | Ind (ind1, _u1), Ind (ind2, _u2) -> Ind.CanOrd.compare ind1 ind2
1058 | Ind _, _ -> -1 | _, Ind _ -> 1
1059 | Construct (ct1,_u1), Construct (ct2,_u2) -> Construct.CanOrd.compare ct1 ct2
1060 | Construct _, _ -> -1 | _, Construct _ -> 1
1061 | Case (_,_u1,pms1,(p1,_r1),iv1,c1,bl1), Case (_,_u2,pms2,(p2,_r2),iv2,c2,bl2) ->
1062 compare [
1063 (Array.compare f, pms1, pms2);
1064 (ctx_cmp f, p1, p2);
1065 (compare_invert f, iv1, iv2);
1066 (f, c1, c2);
1067 (Array.compare (ctx_cmp f), bl1, bl2);
1068 ]
1069 | Case _, _ -> -1 | _, Case _ -> 1
1070 | Fix (ln1,(_,tl1,bl1)), Fix (ln2,(_,tl2,bl2)) ->
1071 compare [(fix_cmp, ln1, ln2); (Array.compare f, tl1, tl2); (Array.compare f, bl1, bl2)]
1072 | Fix _, _ -> -1 | _, Fix _ -> 1
1073 | CoFix(ln1,(_,tl1,bl1)), CoFix(ln2,(_,tl2,bl2)) ->
1074 compare [(Int.compare, ln1, ln2); (Array.compare f, tl1, tl2); (Array.compare f, bl1, bl2)]
1075 | CoFix _, _ -> -1 | _, CoFix _ -> 1
1076 | Proj (p1,_r1,c1), Proj (p2,_r2,c2) -> compare [(Projection.CanOrd.compare, p1, p2); (f, c1, c2)]
1077 | Proj _, _ -> -1 | _, Proj _ -> 1
1078 | Int i1, Int i2 -> Uint63.compare i1 i2
1079 | Int _, _ -> -1 | _, Int _ -> 1
1080 | Float f1, Float f2 -> Float64.total_compare f1 f2
1081 | Float _, _ -> -1 | _, Float _ -> 1
1082 | String s1, String s2 -> Pstring.compare s1 s2
1083 | String _, _ -> -1 | _, String _ -> 1
1084 | Array(_u1,t1,def1,ty1), Array(_u2,t2,def2,ty2) ->
1085 compare [(Array.compare f, t1, t2); (f, def1, def2); (f, ty1, ty2)]
1086 (*| Array _, _ -> -1 | _, Array _ -> 1*)
1087
1088 let rec compare m n=
1089 constr_ord_int compare m n
1090
1091 (*******************)
1092 (* hash-consing *)
1093 (*******************)
1094
1095 (* Hash-consing of [constr] does not use the module [Hashcons] because
1096 [Hashcons] is not efficient on deep tree-like data
1097 structures. Indeed, [Hashcons] is based the (very efficient)
1098 generic hash function [Hashtbl.hash], which computes the hash key
1099 through a depth bounded traversal of the data structure to be
1100 hashed. As a consequence, for a deep [constr] like the natural
1101 number 1000 (S (S (... (S O)))), the same hash is assigned to all
1102 the sub [constr]s greater than the maximal depth handled by
1103 [Hashtbl.hash]. This entails a huge number of collisions in the
1104 hash table and leads to cubic hash-consing in this worst-case.
1105
1106 In order to compute a hash key that is independent of the data
1107 structure depth while being constant-time, an incremental hashing
1108 function must be devised. A standard implementation creates a cache
1109 of the hashing function by decorating each node of the hash-consed
1110 data structure with its hash key. In that case, the hash function
1111 can deduce the hash key of a toplevel data structure by a local
1112 computation based on the cache held on its substructures.
1113 Unfortunately, this simple implementation introduces a space
1114 overhead that is damageable for the hash-consing of small [constr]s
1115 (the most common case). One can think of an heterogeneous
1116 distribution of caches on smartly chosen nodes, but this is forbidden
1117 by the use of generic equality in Rocq source code. (Indeed, this forces
1118 each [constr] to have a unique canonical representation.)
1119
1120 Given that hash-consing proceeds inductively, we can nonetheless
1121 computes the hash key incrementally during hash-consing by changing
1122 a little the signature of the hash-consing function: it now returns
1123 both the hash-consed term and its hash key. This simple solution is
1124 implemented in the following code: it does not introduce a space
1125 overhead in [constr], that's why the efficiency is unchanged for
1126 small [constr]s. Besides, it does handle deep [constr]s without
1127 introducing an unreasonable number of collisions in the hash table.
1128 Some benchmarks make us think that this implementation of
1129 hash-consing is linear in the size of the hash-consed data
1130 structure for our daily use of Rocq.
1131 *)
1132
1133 let array_eqeq t1 t2 =
1134 t1 == t2 ||
1135 (Int.equal (Array.length t1) (Array.length t2) &&
1136 let rec aux i =
1137 (Int.equal i (Array.length t1)) || (t1.(i) == t2.(i) && aux (i + 1))
1138 in aux 0)
1139
1140 let invert_eqeq iv1 iv2 =
1141 match iv1, iv2 with
1142 | NoInvert, NoInvert -> true
1143 | NoInvert, CaseInvert _ | CaseInvert _, NoInvert -> false
1144 | CaseInvert {indices=i1}, CaseInvert {indices=i2} ->
1145 i1 == i2
1146
1147 let hasheq_ctx (nas1, c1) (nas2, c2) =
1148 array_eqeq nas1 nas2 && c1 == c2
1149
1150 let hasheq_kind t1 t2 =
1151 match t1, t2 with
1152 | Rel n1, Rel n2 -> n1 == n2
1153 | Meta m1, Meta m2 -> m1 == m2
1154 | Var id1, Var id2 -> id1 == id2
1155 | Sort s1, Sort s2 -> s1 == s2
1156 | Cast (c1,k1,t1), Cast (c2,k2,t2) -> c1 == c2 && k1 == k2 && t1 == t2
1157 | Prod (n1,t1,c1), Prod (n2,t2,c2) -> n1 == n2 && t1 == t2 && c1 == c2
1158 | Lambda (n1,t1,c1), Lambda (n2,t2,c2) -> n1 == n2 && t1 == t2 && c1 == c2
1159 | LetIn (n1,b1,t1,c1), LetIn (n2,b2,t2,c2) ->
1160 n1 == n2 && b1 == b2 && t1 == t2 && c1 == c2
1161 | App (c1,l1), App (c2,l2) -> c1 == c2 && array_eqeq l1 l2
1162 | Proj (p1,r1,c1), Proj(p2,r2,c2) -> p1 == p2 && r1 == r2 && c1 == c2
1163 | Evar (e1,l1), Evar (e2,l2) -> e1 == e2 && SList.equal (==) l1 l2
1164 | Const (c1,u1), Const (c2,u2) -> c1 == c2 && u1 == u2
1165 | Ind (ind1,u1), Ind (ind2,u2) -> ind1 == ind2 && u1 == u2
1166 | Construct (cstr1,u1), Construct (cstr2,u2) -> cstr1 == cstr2 && u1 == u2
1167 | Case (ci1,u1,pms1,(p1,r1),iv1,c1,bl1), Case (ci2,u2,pms2,(p2,r2),iv2,c2,bl2) ->
1168 (** FIXME: use deeper equality for contexts *)
1169 u1 == u2 && array_eqeq pms1 pms2 &&
1170 ci1 == ci2 && hasheq_ctx p1 p2 && r1 == r2 &&
1171 invert_eqeq iv1 iv2 && c1 == c2 && Array.equal hasheq_ctx bl1 bl2
1172 | Fix ((ln1, i1),(lna1,tl1,bl1)), Fix ((ln2, i2),(lna2,tl2,bl2)) ->
1173 Int.equal i1 i2
1174 && Array.equal Int.equal ln1 ln2
1175 && array_eqeq lna1 lna2
1176 && array_eqeq tl1 tl2
1177 && array_eqeq bl1 bl2
1178 | CoFix(ln1,(lna1,tl1,bl1)), CoFix(ln2,(lna2,tl2,bl2)) ->
1179 Int.equal ln1 ln2
1180 && array_eqeq lna1 lna2
1181 && array_eqeq tl1 tl2
1182 && array_eqeq bl1 bl2
1183 | Int i1, Int i2 -> i1 == i2
1184 | Float f1, Float f2 -> Float64.equal f1 f2
1185 | String s1, String s2 -> Pstring.equal s1 s2
1186 | Array(u1,t1,def1,ty1), Array(u2,t2,def2,ty2) ->
1187 u1 == u2 && def1 == def2 && ty1 == ty2 && array_eqeq t1 t2
1188 | (Rel _ | Meta _ | Var _ | Sort _ | Cast _ | Prod _ | Lambda _ | LetIn _
1189 | App _ | Proj _ | Evar _ | Const _ | Ind _ | Construct _ | Case _
1190 | Fix _ | CoFix _ | Int _ | Float _ | String _ | Array _), _ -> false
1191
1192 let hasheq t1 t2 = hasheq_kind (kind t1) (kind t2)
1193
1194 (** Note that the following Make has the side effect of creating
1195 once and for all the table we'll use for hash-consing all constr *)
1196
1197 module HashsetTerm =
1198 Hashset.Make(struct type t = constr let eq = hasheq end)
1199
1200 module HashsetTermArray =
1201 Hashset.Make(struct type t = constr array let eq = array_eqeq end)
1202
1203 let term_table = HashsetTerm.create 19991
1204 (* The associative table to hashcons terms. *)
1205
1206 let term_array_table = HashsetTermArray.create 4999
1207 (* The associative table to hashcons term arrays. *)
1208
1209 open Hashset.Combine
1210
1211 let hash_cast_kind = function
1212 | VMcast -> 0
1213 | NATIVEcast -> 1
1214 | DEFAULTcast -> 2
1215
1216 (* Exported hashing fonction on constr, used mainly in plugins.
1217 Slight differences from [snd (hash_term t)] above: it ignores binders. *)
1218
1219 let rec hash t =
1220 match kind t with
1221 | Var i -> combinesmall 1 (Id.hash i)
1222 | Sort s -> combinesmall 2 (Sorts.hash s)
1223 | Cast (c, k, t) ->
1224 let hc = hash c in
1225 let ht = hash t in
1226 combinesmall 3 (combine3 hc (hash_cast_kind k) ht)
1227 | Prod (_, t, c) -> combinesmall 4 (combine (hash t) (hash c))
1228 | Lambda (_, t, c) -> combinesmall 5 (combine (hash t) (hash c))
1229 | LetIn (_, b, t, c) ->
1230 combinesmall 6 (combine3 (hash b) (hash t) (hash c))
1231 | App (c,l) -> begin match kind c with
1232 | Cast (c, _, _) -> hash (mkApp (c,l)) (* WTF *)
1233 | _ -> combinesmall 7 (combine (hash_term_array l) (hash c))
1234 end
1235 | Evar (e,l) ->
1236 combinesmall 8 (combine (Evar.hash e) (hash_term_list l))
1237 | Const (c,u) ->
1238 combinesmall 9 (combine (Constant.CanOrd.hash c) (Instance.hash u))
1239 | Ind (ind,u) ->
1240 combinesmall 10 (combine (Ind.CanOrd.hash ind) (Instance.hash u))
1241 | Construct (c,u) ->
1242 combinesmall 11 (combine (Construct.CanOrd.hash c) (Instance.hash u))
1243 | Case (_ , u, pms, (p,r), iv, c, bl) ->
1244 combinesmall 12 (combine5 (hash c) (hash_invert iv) (hash_term_array pms) (Instance.hash u)
1245 (combine3 (hash_under_context p) (Sorts.relevance_hash r) (hash_branches bl)))
1246 | Fix (_ln ,(_, tl, bl)) ->
1247 combinesmall 13 (combine (hash_term_array bl) (hash_term_array tl))
1248 | CoFix(_ln, (_, tl, bl)) ->
1249 combinesmall 14 (combine (hash_term_array bl) (hash_term_array tl))
1250 | Meta n -> combinesmall 15 n
1251 | Rel n -> combinesmall 16 n
1252 | Proj (p,r, c) ->
1253 combinesmall 17 (combine3 (Projection.CanOrd.hash p) (Sorts.relevance_hash r) (hash c))
1254 | Int i -> combinesmall 18 (Uint63.hash i)
1255 | Float f -> combinesmall 19 (Float64.hash f)
1256 | String s -> combinesmall 20 (Pstring.hash s)
1257 | Array(u,t,def,ty) ->
1258 combinesmall 21 (combine4 (Instance.hash u) (hash_term_array t) (hash def) (hash ty))
1259
1260 and hash_invert = function
1261 | NoInvert -> 0
1262 | CaseInvert {indices;} ->
1263 combinesmall 1 (hash_term_array indices)
1264
1265 and hash_term_array t =
1266 Array.fold_left (fun acc t -> combine acc (hash t)) 0 t
1267
1268 and hash_term_list t =
1269 SList.Skip.fold (fun acc t -> combine (hash t) acc) 0 t
1270
1271 and hash_under_context (_, t) = hash t
1272
1273 and hash_branches bl =
1274 Array.fold_left (fun acc t -> combine acc (hash_under_context t)) 0 bl
1275
1276 module CaseinfoHash =
1277 struct
1278 type t = case_info
1279 open Hashset.Combine
1280 let hash_pp_info info =
1281 let h1 = match info.style with
1282 | LetStyle -> 0
1283 | IfStyle -> 1
1284 | LetPatternStyle -> 2
1285 | MatchStyle -> 3
1286 | RegularStyle -> 4 in
1287 h1
1288 let hash ~hind ci =
1289 let h1 = hind in
1290 let h2 = Int.hash ci.ci_npar in
1291 let h3 = Array.fold_left combine 0 ci.ci_cstr_ndecls in
1292 let h4 = Array.fold_left combine 0 ci.ci_cstr_nargs in
1293 let h5 = hash_pp_info ci.ci_pp_info in
1294 combine5 h1 h2 h3 h4 h5
1295 let hashcons ci =
1296 let hind, ind = hcons_ind ci.ci_ind in
1297 hash ~hind ci, { ci with ci_ind = ind }
1298 let pp_info_equal info1 info2 =
1299 info1.style == info2.style
1300 let eq ci ci' =
1301 ci.ci_ind == ci'.ci_ind &&
1302 Int.equal ci.ci_npar ci'.ci_npar &&
1303 Array.equal Int.equal ci.ci_cstr_ndecls ci'.ci_cstr_ndecls && (* we use [Array.equal] on purpose *)
1304 Array.equal Int.equal ci.ci_cstr_nargs ci'.ci_cstr_nargs && (* we use [Array.equal] on purpose *)
1305 pp_info_equal ci.ci_pp_info ci'.ci_pp_info (* we use (=) on purpose *)
1306 end
1307
1308 module Hcaseinfo = Hashcons.Make(CaseinfoHash)
1309
1310 let hcons_caseinfo = Hashcons.simple_hcons Hcaseinfo.generate Hcaseinfo.hcons ()
1311
1312 module Hannotinfo = struct
1313 type t = Name.t binder_annot
1314 let eq = eq_annot (fun na1 na2 -> na1 == na2) Sorts.relevance_equal
1315 let hashcons {binder_name=na;binder_relevance=r} =
1316 let hna, na = Name.hcons na in
1317 let h = Hashset.Combine.combinesmall (Sorts.relevance_hash r) hna in
1318 h, {binder_name=na;binder_relevance=r}
1319 end
1320 module Hannot = Hashcons.Make(Hannotinfo)
1321
1322 let hcons_annot = Hashcons.simple_hcons Hannot.generate Hannot.hcons ()
1323
1324 let dbg = CDebug.create ~name:"hcons" ()
1325
1326 module GenHCons(C:sig
1327 type t
1328 val kind : t -> (t, t, Sorts.t, Instance.t, Sorts.relevance) kind_of_term
1329 val self : t -> constr
1330 val refcount : t -> int
1331
1332 val via_hconstr : bool
1333
1334 module Tbl : sig
1335 val find_opt : t -> (int * constr) option
1336 val add : t -> int * constr -> unit
1337 end
1338 end) = struct
1339 open C
1340
1341 let steps = ref 0
1342
1343 let hashcons_array2 hcons a a' =
1344 assert (Array.length a = Array.length a');
1345 CArray.Smart.fold_left_map_i (fun i h _ ->
1346 let hx, x = hcons (Array.unsafe_get a' i) in
1347 combine hx h, x)
1348 0
1349 a
1350
1351 let rec hash_term (t : t) : int * (constr,constr,_,_,_) kind_of_term =
1352 match kind t with
1353 | Var i ->
1354 let hi, i = Id.hcons i in
1355 (combinesmall 1 hi, Var i)
1356 | Sort s ->
1357 let hs, s = Sorts.hcons s in
1358 (combinesmall 2 hs, Sort s)
1359 | Cast (c, k, t) ->
1360 let hc, c = sh_rec c in
1361 let ht, t = sh_rec t in
1362 (combinesmall 3 (combine3 hc (hash_cast_kind k) ht), Cast (c, k, t))
1363 | Prod (na,t,c) ->
1364 let hna, na = hcons_annot na
1365 and ht, t = sh_rec t
1366 and hc, c = sh_rec c in
1367 (combinesmall 4 (combine3 hna ht hc), Prod (na, t, c))
1368 | Lambda (na,t,c) ->
1369 let hna, na = hcons_annot na
1370 and ht, t = sh_rec t
1371 and hc, c = sh_rec c in
1372 (combinesmall 5 (combine3 hna ht hc), Lambda (na, t, c))
1373 | LetIn (na,b,t,c) ->
1374 let hna, na = hcons_annot na
1375 and hb, b = sh_rec b
1376 and ht, t = sh_rec t
1377 and hc, c = sh_rec c in
1378 (combinesmall 6 (combine4 hna hb ht hc), LetIn (na, b, t, c))
1379 | App (c,l) ->
1380 let _, cl = destApp (self t) in
1381 let hc, c = sh_rec c in
1382 let hl, l = hash_term_array cl l in
1383 (combinesmall 7 (combine hl hc), App (c,l))
1384 | Evar _ -> assert false
1385 | Const (c,u) ->
1386 let hc, c' = hcons_con c in
1387 let hu, u' = Instance.hcons u in
1388 (combinesmall 9 (combine hc hu), Const (c', u'))
1389 | Ind (ind,u) ->
1390 let hind, ind' = hcons_ind ind in
1391 let hu, u' = Instance.hcons u in
1392 (combinesmall 10 (combine hind hu), Ind (ind', u'))
1393 | Construct (c,u) ->
1394 let hc, c' = hcons_construct c in
1395 let hu, u' = Instance.hcons u in
1396 (combinesmall 11 (combine hc hu), Construct (c', u'))
1397 | Case (ci,u,pms,(p,r),iv,c,bl) ->
1398 (** FIXME: use a dedicated hashconsing structure *)
1399 let hcons_ctx (lna, c) =
1400 let hna, lna = hashcons_array2 hcons_annot lna lna in
1401 let hc, c = sh_rec c in
1402 combine hna hc, (lna, c)
1403 in
1404 (* XXX use hci? *)
1405 let _hci, ci = hcons_caseinfo ci in
1406 let hu, u = Instance.hcons u in
1407 let _,_,cpms,_,civ,_,cbl = destCase (self t) in
1408 let hpms,pms = hash_term_array cpms pms in
1409 let hp, p = hcons_ctx p in
1410 let hiv, iv = sh_invert civ iv in
1411 let hc, c = sh_rec c in
1412 let hbl, cbl = hashcons_array2 hcons_ctx cbl bl in
1413 let hbl = combine (combine hc (combine hiv (combine hpms (combine hu hp)))) hbl in
1414 (combinesmall 12 hbl, Case (ci, u, pms, (p,r), iv, c, cbl))
1415 | Fix (ln,(lna,tl,bl)) ->
1416 let _, (_,ctl,cbl) = destFix (self t) in
1417 let hbl,bl = hash_term_array cbl bl in
1418 let htl,tl = hash_term_array ctl tl in
1419 let hna, lna = Hashcons.hashcons_array hcons_annot lna in
1420 let h = combine3 hna hbl htl in
1421 (combinesmall 13 h, Fix (ln,(lna,tl,bl)))
1422 | CoFix(ln,(lna,tl,bl)) ->
1423 let _, (_,ctl,cbl) = destCoFix (self t) in
1424 let hbl,bl = hash_term_array cbl bl in
1425 let htl,tl = hash_term_array ctl tl in
1426 let hna, lna = Hashcons.hashcons_array hcons_annot lna in
1427 let h = combine3 hna hbl htl in
1428 (combinesmall 14 h, CoFix (ln,(lna,tl,bl)))
1429 | Meta n as t ->
1430 (combinesmall 15 n, t)
1431 | Rel n as t ->
1432 (combinesmall 16 n, t)
1433 | Proj (p,r,c) ->
1434 let hc, c' = sh_rec c in
1435 let hp, p' = Projection.hcons p in
1436 (combinesmall 17 (combine hp hc), Proj (p', r, c'))
1437 | Int i as t ->
1438 let (h,l) = Uint63.to_int2 i in
1439 (combinesmall 18 (combine h l), t)
1440 | Float f as t -> (combinesmall 19 (Float64.hash f), t)
1441 | String s as t -> (combinesmall 20 (Pstring.hash s), t)
1442 | Array (u,ar,def,ty) ->
1443 let _,car,_,_ = destArray (self t) in
1444 let hu, u = Instance.hcons u in
1445 let ht, t = hash_term_array car ar in
1446 let hdef, def = sh_rec def in
1447 let hty, ty = sh_rec ty in
1448 let h = combine4 hu ht hdef hty in
1449 (combinesmall 21 h, Array(u,t,def,ty))
1450
1451 and sh_invert civ iv = match civ, iv with
1452 | NoInvert, NoInvert -> 0, NoInvert
1453 | CaseInvert {indices=cindices}, CaseInvert {indices;} ->
1454 let ha, indices = hash_term_array cindices indices in
1455 combinesmall 1 ha, CaseInvert {indices;}
1456 | (NoInvert | CaseInvert _), _ -> assert false
1457
1458 and sh_rec_main t =
1459 let (h, y) = hash_term t in
1460 (h, HashsetTerm.repr h (T y) term_table)
1461
1462 and sh_rec t =
1463 incr steps;
1464 if refcount t = 1 then sh_rec_main t
1465 else match Tbl.find_opt t with
1466 | Some res -> res
1467 | None ->
1468 let res = sh_rec_main t in
1469 Tbl.add t res;
1470 res
1471
1472 (* Note : During hash-cons of arrays, we modify them *in place* *)
1473
1474 and hash_term_array ct t =
1475 let h, ct = hashcons_array2 sh_rec ct t in
1476 (h, HashsetTermArray.repr h ct term_array_table)
1477
1478 let hcons t = NewProfile.profile "Constr.hcons" (fun () -> sh_rec t) ()
1479
1480 let hcons t =
1481 steps := 0;
1482 let t = hcons t in
1483 dbg Pp.(fun () ->
1484 let open Hashset in
1485 let stats = HashsetTerm.stats term_table in
1486 v 0 (
1487 str "via hconstr = " ++ bool via_hconstr ++ spc() ++
1488 str "steps = " ++ int !steps ++ spc() ++
1489 str "num_bindings = " ++ int stats.num_bindings ++ spc() ++
1490 str "num_buckets = " ++ int stats.num_buckets ++ spc() ++
1491 str "max_bucket_length = " ++ int stats.max_bucket_length
1492 )
1493 );
1494 t
1495
1496 end
1497
1498 module HCons = GenHCons(struct
1499 type t = constr
1500 let kind = kind
1501 let self x = x
1502 let refcount _ = 1
1503
1504 let via_hconstr = false
1505
1506 module Tbl = struct
1507 let find_opt _ = None
1508 let add _ _ : unit = assert false
1509 end
1510 end)
1511
1512 (* Make sure our statically allocated Rels (1 to 16) are considered
1513 as canonical, and hence hash-consed to themselves *)
1514 let () = ignore (HCons.hash_term_array rels rels)
1515
1516 let hcons = HCons.hcons
1517
1518 (* let hcons_types = hcons_constr *)
1519
1520 type rel_declaration = (constr, types, Sorts.relevance) Context.Rel.Declaration.pt
1521 type named_declaration = (constr, types, Sorts.relevance) Context.Named.Declaration.pt
1522 type compacted_declaration = (constr, types, Sorts.relevance) Context.Compacted.Declaration.pt
1523 type rel_context = rel_declaration list
1524 type named_context = named_declaration list
1525 type compacted_context = compacted_declaration list
1526
1527 (** Minimalistic constr printer, typically for debugging *)
1528
1529 let debug_print_fix pr_constr ((t,i),(lna,tl,bl)) =
1530 let open Pp in
1531 let fixl = Array.mapi (fun i na -> (na.binder_name,t.(i),tl.(i),bl.(i))) lna in
1532 hov 1
1533 (str"fix " ++ int i ++ spc() ++ str"{" ++
1534 v 0 (prlist_with_sep spc (fun (na,i,ty,bd) ->
1535 Name.print na ++ str"/" ++ int i ++ str":" ++ pr_constr ty ++
1536 cut() ++ str":=" ++ pr_constr bd) (Array.to_list fixl)) ++
1537 str"}")
1538
1539 let pr_puniverses p u =
1540 if UVars.Instance.is_empty u then p
1541 else Pp.(p ++ str"(*" ++ UVars.Instance.pr Sorts.QVar.raw_pr Univ.Level.raw_pr u ++ str"*)")
1542
1543 let rec debug_print c =
1544 let open Pp in
1545 match kind c with
1546 | Rel n -> str "#"++int n
1547 | Meta n -> str "Meta(" ++ int n ++ str ")"
1548 | Var id -> Id.print id
1549 | Sort s -> Sorts.debug_print s
1550 | Cast (c,_, t) -> hov 1
1551 (str"(" ++ debug_print c ++ cut() ++
1552 str":" ++ debug_print t ++ str")")
1553 | Prod ({binder_name=Name id;_},t,c) -> hov 1
1554 (str"forall " ++ Id.print id ++ str":" ++ debug_print t ++ str"," ++
1555 spc() ++ debug_print c)
1556 | Prod ({binder_name=Anonymous;_},t,c) -> hov 0
1557 (str"(" ++ debug_print t ++ str " ->" ++ spc() ++
1558 debug_print c ++ str")")
1559 | Lambda (na,t,c) -> hov 1
1560 (str"fun " ++ Name.print na.binder_name ++ str":" ++
1561 debug_print t ++ str" =>" ++ spc() ++ debug_print c)
1562 | LetIn (na,b,t,c) -> hov 0
1563 (str"let " ++ Name.print na.binder_name ++ str":=" ++ debug_print b ++
1564 str":" ++ brk(1,2) ++ debug_print t ++ cut() ++
1565 debug_print c)
1566 | App (c,l) -> hov 1
1567 (str"(" ++ debug_print c ++ spc() ++
1568 prlist_with_sep spc debug_print (Array.to_list l) ++ str")")
1569 | Evar (e,l) ->
1570 let pro = function None -> str "?" | Some c -> debug_print c in
1571 hov 1
1572 (str"Evar#" ++ int (Evar.repr e) ++ str"{" ++
1573 prlist_with_sep spc pro (SList.to_list l) ++str"}")
1574 | Const (c,u) -> str"Cst(" ++ pr_puniverses (Constant.debug_print c) u ++ str")"
1575 | Ind ((sp,i),u) -> str"Ind(" ++ pr_puniverses (MutInd.print sp ++ str"," ++ int i) u ++ str")"
1576 | Construct (((sp,i),j),u) ->
1577 str"Constr(" ++ pr_puniverses (MutInd.print sp ++ str"," ++ int i ++ str"," ++ int j) u ++ str")"
1578 | Proj (p,_r,c) ->
1579 str"Proj(" ++ Projection.debug_print p ++ str"," ++ debug_print c ++ str")"
1580 | Case (_ci,_u,pms,(p,_),iv,c,bl) ->
1581 let pr_ctx (nas, c) =
1582 hov 2 (hov 0 (prvect (fun na -> Name.print na.binder_name ++ spc ()) nas ++ str "|-") ++ spc () ++
1583 debug_print c)
1584 in
1585 v 0 (hv 0 (str"Case" ++ brk (1,1) ++
1586 debug_print c ++ spc () ++ str "params" ++ brk (1,1) ++ prvect (fun x -> spc () ++ debug_print x) pms ++
1587 spc () ++ str"return"++ brk (1,1) ++ pr_ctx p ++ debug_invert iv ++ spc () ++ str"with") ++
1588 prvect (fun b -> spc () ++ pr_ctx b) bl ++
1589 spc () ++ str"end")
1590 | Fix f -> debug_print_fix debug_print f
1591 | CoFix(i,(lna,tl,bl)) ->
1592 let fixl = Array.mapi (fun i na -> (na,tl.(i),bl.(i))) lna in
1593 hov 1
1594 (str"cofix " ++ int i ++ spc() ++ str"{" ++
1595 v 0 (prlist_with_sep spc (fun (na,ty,bd) ->
1596 Name.print na.binder_name ++ str":" ++ debug_print ty ++
1597 cut() ++ str":=" ++ debug_print bd) (Array.to_list fixl)) ++
1598 str"}")
1599 | Int i -> str"Int("++str (Uint63.to_string i) ++ str")"
1600 | Float i -> str"Float("++str (Float64.to_string i) ++ str")"
1601 | String s -> str"String("++str (Printf.sprintf "%S" (Pstring.to_string s)) ++ str")"
1602 | Array(u,t,def,ty) -> str"Array(" ++ prlist_with_sep pr_comma debug_print (Array.to_list t) ++ str" | "
1603 ++ debug_print def ++ str " : " ++ debug_print ty
1604 ++ str")@{" ++ UVars.Instance.pr Sorts.QVar.raw_pr Univ.Level.raw_pr u ++ str"}"
1605
1606 and debug_invert = let open Pp in function
1607 | NoInvert -> mt()
1608 | CaseInvert {indices;} ->
1609 spc() ++ str"Invert {indices=" ++
1610 prlist_with_sep spc debug_print (Array.to_list indices) ++ str "} "