Debian Codegraph @

kernel/conversion.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created under Benjamin Werner account by Bruno Barras to implement
12 a call-by-value conversion algorithm and a lazy reduction machine
13 with sharing, Nov 1996 *)
14 (* Addition of zeta-reduction (let-in contraction) by Hugo Herbelin, Oct 2000 *)
15 (* Irreversibility of opacity by Bruno Barras *)
16 (* Cleaning and lightening of the kernel by Bruno Barras, Nov 2001 *)
17 (* Equal inductive types by Jacek Chrzaszcz as part of the module
18 system, Aug 2002 *)
19
20 open CErrors
21 open Util
22 open Names
23 open Constr
24 open Declarations
25 open Environ
26 open CClosure
27 open Esubst
28
29 let rec is_empty_stack = function
30 [] -> true
31 | Zupdate _::s -> is_empty_stack s
32 | Zshift _::s -> is_empty_stack s
33 | _ -> false
34
35 (* Compute the lift to be performed on a term placed in a given stack *)
36 let el_stack el stk =
37 let n =
38 List.fold_left
39 (fun i z ->
40 match z with
41 Zshift n -> i+n
42 | _ -> i)
43 0
44 stk in
45 el_shft n el
46
47 let compare_stack_shape stk1 stk2 =
48 let rec compare_rec bal stk1 stk2 =
49 match (stk1,stk2) with
50 ([],[]) -> Int.equal bal 0
51 | ((Zupdate _|Zshift _)::s1, _) -> compare_rec bal s1 stk2
52 | (_, (Zupdate _|Zshift _)::s2) -> compare_rec bal stk1 s2
53 | (Zapp l1::s1, _) -> compare_rec (bal+Array.length l1) s1 stk2
54 | (_, Zapp l2::s2) -> compare_rec (bal-Array.length l2) stk1 s2
55 | (Zproj _::s1, Zproj _::s2) ->
56 Int.equal bal 0 && compare_rec 0 s1 s2
57 | (ZcaseT(_c1,_,_,_,_,_)::s1, ZcaseT(_c2,_,_,_,_,_)::s2) ->
58 Int.equal bal 0 (* && c1.ci_ind = c2.ci_ind *) && compare_rec 0 s1 s2
59 | (Zfix(_,a1)::s1, Zfix(_,a2)::s2) ->
60 Int.equal bal 0 && compare_rec 0 a1 a2 && compare_rec 0 s1 s2
61 | Zprimitive(op1,_,rargs1, _kargs1)::s1, Zprimitive(op2,_,rargs2, _kargs2)::s2 ->
62 bal=0 && op1=op2 && List.length rargs1=List.length rargs2 &&
63 compare_rec 0 s1 s2
64 | [], _ :: _
65 | (Zproj _ | ZcaseT _ | Zfix _ | Zprimitive _) :: _, _ -> false
66 in
67 compare_rec 0 stk1 stk2
68
69 type lft_fconstr = lift * fconstr
70
71 type lft_constr_stack_elt =
72 Zlapp of (lift * fconstr) array
73 | Zlproj of Projection.Repr.t * lift
74 | Zlfix of (lift * fconstr) * lft_constr_stack
75 | Zlcase of case_info * lift * UVars.Instance.t * constr array * case_return * case_branch array * usubs
76 | Zlprimitive of
77 CPrimitives.t * pconstant * lft_fconstr list * lft_fconstr next_native_args
78 and lft_constr_stack = lft_constr_stack_elt list
79
80 let rec zlapp v = function
81 Zlapp v2 :: s -> zlapp (Array.append v v2) s
82 | s -> Zlapp v :: s
83
84 (** Hand-unrolling of the map function to bypass the call to the generic array
85 allocation. Type annotation is required to tell OCaml that the array does
86 not contain floats. *)
87 let map_lift (l : lift) (v : fconstr array) = match v with
88 | [||] -> assert false
89 | [|c0|] -> [|(l, c0)|]
90 | [|c0; c1|] -> [|(l, c0); (l, c1)|]
91 | [|c0; c1; c2|] -> [|(l, c0); (l, c1); (l, c2)|]
92 | [|c0; c1; c2; c3|] -> [|(l, c0); (l, c1); (l, c2); (l, c3)|]
93 | v -> Array.Fun1.map (fun l t -> (l, t)) l v
94
95 let pure_stack lfts stk =
96 let rec pure_rec lfts stk =
97 match stk with
98 [] -> (lfts,[])
99 | zi::s ->
100 (match (zi,pure_rec lfts s) with
101 (Zupdate _,lpstk) -> lpstk
102 | (Zshift n,(l,pstk)) -> (el_shft n l, pstk)
103 | (Zapp a, (l,pstk)) ->
104 (l,zlapp (map_lift l a) pstk)
105 | (Zproj (p,_), (l,pstk)) ->
106 (l, Zlproj (p,l)::pstk)
107 | (Zfix(fx,a),(l,pstk)) ->
108 let (lfx,pa) = pure_rec l a in
109 (l, Zlfix((lfx,fx),pa)::pstk)
110 | (ZcaseT(ci,u,pms,p,br,e),(l,pstk)) ->
111 (l,Zlcase(ci,l,u,pms,p,br,e)::pstk)
112 | (Zprimitive(op,c,rargs,kargs),(l,pstk)) ->
113 (l,Zlprimitive(op,c,List.map (fun t -> (l,t)) rargs,
114 List.map (fun (k,t) -> (k,(l,t))) kargs)::pstk))
115 in
116 snd (pure_rec lfts stk)
117
118 (********************************************************************)
119 (* Conversion *)
120 (********************************************************************)
121
122 (* Conversion utility functions *)
123
124 (* functions of this type are called from the kernel *)
125 type 'a kernel_conversion_function = env -> 'a -> 'a -> (unit, unit) result
126
127 (* functions of this type can be called from outside the kernel *)
128 type 'a extended_conversion_function =
129 ?l2r:bool -> ?reds:TransparentState.t -> env ->
130 ?evars:evar_handler ->
131 'a -> 'a -> (unit, unit) result
132
133 type payload = ..
134
135 exception NotConvertible
136 exception NotConvertibleTrace of payload
137
138 (* Convertibility of sorts *)
139
140 (* The sort cumulativity is
141
142 Prop <= Set <= Type 1 <= ... <= Type i <= ...
143
144 and this holds whatever Set is predicative or impredicative
145 *)
146
147 type conv_pb =
148 | CONV
149 | CUMUL
150
151 type ('a, 'err) universe_compare = {
152 compare_sorts : conv_pb -> Sorts.t -> Sorts.t -> 'a -> ('a, 'err option) result;
153 compare_instances: flex:bool -> UVars.Instance.t -> UVars.Instance.t -> 'a -> ('a, 'err option) result;
154 compare_cumul_instances : conv_pb -> UVars.Variance.t array ->
155 UVars.Instance.t -> UVars.Instance.t -> 'a -> ('a, 'err option) result;
156 }
157
158 type ('a, 'err) universe_state = 'a * ('a, 'err) universe_compare
159
160 type ('a, 'err) generic_conversion_function = ('a, 'err) universe_state -> constr -> constr -> ('a, 'err option) result
161
162 let sort_cmp_universes pb s0 s1 (u, check) =
163 (check.compare_sorts pb s0 s1 u, check)
164
165 (* [flex] should be true for constants, false for inductive types and
166 constructors. *)
167 let convert_instances ~flex u u' (s, check) =
168 (check.compare_instances ~flex u u' s, check)
169
170 exception MustExpand
171
172 let convert_instances_cumul pb var u u' (s, check) =
173 (check.compare_cumul_instances pb var u u' s, check)
174
175 let get_cumulativity_constraints cv_pb variance u u' =
176 match cv_pb with
177 | CONV ->
178 UVars.enforce_eq_variance_instances variance u u' (UVars.QPairSet.empty, Univ.UnivConstraints.empty)
179 | CUMUL ->
180 UVars.enforce_leq_variance_instances variance u u' (UVars.QPairSet.empty, Univ.UnivConstraints.empty)
181
182 let inductive_cumulativity_arguments (mind,ind) =
183 mind.Declarations.mind_nparams +
184 mind.Declarations.mind_packets.(ind).Declarations.mind_nrealargs
185
186 let convert_inductives_gen cmp_instances cmp_cumul cv_pb (mind,ind) nargs u1 u2 s =
187 match mind.Declarations.mind_variance with
188 | None -> cmp_instances u1 u2 s
189 | Some variances ->
190 let num_param_arity = inductive_cumulativity_arguments (mind,ind) in
191 if not (Int.equal num_param_arity nargs) then
192 (* shortcut, not sure if worth doing, could use perf data *)
193 if UVars.Instance.equal u1 u2 then Result.Ok s else raise MustExpand
194 else
195 cmp_cumul cv_pb variances u1 u2 s
196
197 type 'e conv_tab = {
198 cnv_inf : clos_infos;
199 cnv_typ : bool; (* true if the input terms were well-typed *)
200 lft_tab : clos_tab;
201 rgt_tab : clos_tab;
202 err_ret : 'e -> payload;
203 }
204 (** Invariant: for any tl ∈ lft_tab and tr ∈ rgt_tab, there is no mutable memory
205 location contained both in tl and in tr. *)
206
207 let fail_check (infos : 'err conv_tab) (state, check) = match state with
208 | Result.Ok state -> (state, check)
209 | Result.Error None -> raise NotConvertible
210 | Result.Error (Some err) -> raise (NotConvertibleTrace (infos.err_ret err))
211
212 let convert_inductives cv_pb ind nargs u1 u2 (s, check) =
213 convert_inductives_gen (check.compare_instances ~flex:false) check.compare_cumul_instances
214 cv_pb ind nargs u1 u2 s, check
215
216 let constructor_cumulativity_arguments (mind, ind, ctor) =
217 mind.Declarations.mind_nparams +
218 mind.Declarations.mind_packets.(ind).Declarations.mind_consnrealargs.(ctor - 1)
219
220 let convert_constructors_gen cmp_instances cmp_cumul (mind, ind, cns) nargs u1 u2 s =
221 match mind.Declarations.mind_variance with
222 | None -> cmp_instances u1 u2 s
223 | Some _ ->
224 let num_cnstr_args = constructor_cumulativity_arguments (mind,ind,cns) in
225 if not (Int.equal num_cnstr_args nargs) then
226 if UVars.Instance.equal u1 u2 then Result.Ok s else raise MustExpand
227 else
228 (** By invariant, both constructors have a common supertype,
229 so they are convertible _at that type_. *)
230 (* NB: no variance for qualities *)
231 let variance = Array.make (snd (UVars.Instance.length u1)) UVars.Variance.Irrelevant in
232 cmp_cumul CONV variance u1 u2 s
233
234 let convert_constructors ctor nargs u1 u2 (s, check) =
235 convert_constructors_gen (check.compare_instances ~flex:false) check.compare_cumul_instances
236 ctor nargs u1 u2 s, check
237
238 let conv_table_key infos ~nargs k1 k2 cuniv =
239 if k1 == k2 then cuniv else
240 match k1, k2 with
241 | ConstKey (cst, u), ConstKey (cst', u') when Constant.CanOrd.equal cst cst' ->
242 if UVars.Instance.equal u u' then cuniv
243 else if Int.equal nargs 1 && is_array_type (info_env infos.cnv_inf) cst then cuniv
244 else
245 let flex = evaluable_constant cst (info_env infos.cnv_inf)
246 && RedFlags.red_set (info_flags infos.cnv_inf) (RedFlags.fCONST cst)
247 in fail_check infos @@ convert_instances ~flex u u' cuniv
248 | VarKey id, VarKey id' when Id.equal id id' -> cuniv
249 | RelKey n, RelKey n' when Int.equal n n' -> cuniv
250 | _ -> raise NotConvertible
251
252 let same_args_size sk1 sk2 =
253 let n = CClosure.stack_args_size sk1 in
254 if Int.equal n (CClosure.stack_args_size sk2) then n
255 else raise NotConvertible
256
257 (** The same heap separation invariant must hold for the fconstr arguments
258 passed to each respective side of the conversion function below. *)
259
260 let push_relevance infos r =
261 { infos with cnv_inf = CClosure.push_relevance infos.cnv_inf r }
262
263 let push_relevances infos nas =
264 { infos with cnv_inf = CClosure.push_relevances infos.cnv_inf nas }
265
266 let identity_of_ctx (ctx:Constr.rel_context) =
267 Context.Rel.instance mkRel 0 ctx
268
269 (* ind -> fun args => ind args *)
270 let eta_expand_ind env (ind,u as pind) =
271 let mib = Environ.lookup_mind (fst ind) env in
272 let mip = mib.mind_packets.(snd ind) in
273 let ctx = Vars.subst_instance_context u mip.mind_arity_ctxt in
274 let args = identity_of_ctx ctx in
275 let c = mkApp (mkIndU pind, args) in
276 let c = Term.it_mkLambda_or_LetIn c ctx in
277 inject c
278
279 let eta_expand_constructor env ((ind,ctor),u as pctor) =
280 let mib = Environ.lookup_mind (fst ind) env in
281 let mip = mib.mind_packets.(snd ind) in
282 let ctx = Vars.subst_instance_context u (fst mip.mind_nf_lc.(ctor-1)) in
283 let args = identity_of_ctx ctx in
284 let c = mkApp (mkConstructU pctor, args) in
285 let c = Term.it_mkLambda_or_LetIn c ctx in
286 inject c
287
288 let esubst_of_context ctx args e =
289 let rec aux lft e args ctx = match ctx with
290 | [] -> lft, e
291 | None :: ctx -> aux (lft + 1) (usubs_lift e) (usubs_lift args) ctx
292 | Some c :: ctx ->
293 let c = mk_clos args c in
294 aux lft (usubs_cons c e) (usubs_cons c args) ctx
295 in
296 aux 0 e args (List.rev ctx)
297
298 let irr_flex infos = function
299 | ConstKey (con,u) -> is_irrelevant infos @@ UVars.subst_instance_relevance u @@ Environ.constant_relevance con (info_env infos)
300 | VarKey x -> is_irrelevant infos @@ Context.Named.Declaration.get_relevance (Environ.lookup_named x (info_env infos))
301 | RelKey x -> is_irrelevant infos @@ Context.Rel.Declaration.get_relevance (Environ.lookup_rel x (info_env infos))
302
303 let eq_universes (_,e1) (_,e2) u1 u2 =
304 let subst e u = if UVars.Instance.is_empty e then u else UVars.subst_instance_instance e u in
305 UVars.Instance.equal (subst e1 u1) (subst e2 u2)
306
307 let rec compare_under e1 c1 e2 c2 =
308 match Constr.kind c1, Constr.kind c2 with
309 | Cast (c1, _, _), _ -> compare_under e1 c1 e2 c2
310 | _, Cast (c2, _, _) -> compare_under e1 c1 e2 c2
311 | Rel i, Rel j -> begin match Esubst.expand_rel i (fst e1) with
312 | Inl _ -> false
313 | Inr (k, _) -> begin match Esubst.expand_rel j (fst e2) with
314 | Inl _ -> false
315 | Inr (k', _) -> Int.equal k k'
316 end
317 end
318 | Meta m1, Meta m2 -> Int.equal m1 m2
319 | Var id1, Var id2 -> Id.equal id1 id2
320 | Int i1, Int i2 -> Uint63.equal i1 i2
321 | Float f1, Float f2 -> Float64.equal f1 f2
322 | String s1, String s2 -> Pstring.equal s1 s2
323 | Sort s1, Sort s2 ->
324 let subst_instance_sort u s =
325 if UVars.Instance.is_empty u then s else UVars.subst_instance_sort u s
326 in
327 let s1 = subst_instance_sort (snd e1) s1
328 and s2 = subst_instance_sort (snd e2) s2 in
329 Sorts.equal s1 s2
330 | Prod (_,t1,c1), Prod (_,t2,c2) ->
331 compare_under e1 t1 e2 t2
332 && compare_under (usubs_lift e1) c1 (usubs_lift e2) c2
333 | Lambda (_,t1,c1), Lambda (_,t2,c2) ->
334 compare_under e1 t1 e2 t2
335 && compare_under (usubs_lift e1) c1 (usubs_lift e2) c2
336 | LetIn (_,b1,_,c1), LetIn (_,b2,_,c2) ->
337 (* don't care about types when bodies are equal *)
338 compare_under e1 b1 e2 b2
339 && compare_under (usubs_lift e1) c1 (usubs_lift e2) c2
340 | App (c1, l1), App (c2, l2) ->
341 let len = Array.length l1 in
342 Int.equal len (Array.length l2)
343 && compare_under e1 c1 e2 c2
344 && Array.equal_norefl (fun c1 c2 -> compare_under e1 c1 e2 c2) l1 l2
345 | Proj (p1,_,c1), Proj (p2,_,c2) ->
346 Projection.CanOrd.equal p1 p2 && compare_under e1 c1 e2 c2
347 | Evar _, Evar _ -> false
348 | Const (c1,u1), Const (c2,u2) ->
349 (* The args length currently isn't used but may as well pass it. *)
350 Constant.CanOrd.equal c1 c2 && eq_universes e1 e2 u1 u2
351 | Ind (c1,u1), Ind (c2,u2) -> Ind.CanOrd.equal c1 c2 && eq_universes e1 e2 u1 u2
352 | Construct (c1,u1), Construct (c2,u2) ->
353 Construct.CanOrd.equal c1 c2 && eq_universes e1 e2 u1 u2
354 | Case _, Case _ | Fix _, Fix _ | CoFix _, CoFix _ -> false (* todo some other time *)
355 | Array(_,t1,def1,ty1), Array(_,t2,def2,ty2) ->
356 Array.equal_norefl (fun c1 c2 -> compare_under e1 c1 e2 c2) t1 t2
357 && compare_under e1 def1 e2 def2
358 && compare_under e1 ty1 e2 ty2
359 | (Rel _ | Meta _ | Var _ | Sort _ | Prod _ | Lambda _ | LetIn _ | App _
360 | Proj _ | Evar _ | Const _ | Ind _ | Construct _ | Case _ | Fix _
361 | CoFix _ | Int _ | Float _ | String _ | Array _), _ -> false
362
363
364 let rec fast_test lft1 term1 lft2 term2 = match fterm_of term1, fterm_of term2 with
365 | FLIFT (i, term1), (FLIFT _ | FCLOS _) -> fast_test (el_shft i lft1) term1 lft2 term2
366 | FCLOS _, FLIFT (j, term2) -> fast_test lft1 term1 (el_shft j lft2) term2
367 | FCLOS (c1, (e1,u1)), FCLOS (c2, (e2,u2)) ->
368 eq_lift lft1 lft2 &&
369 compare_under (e1, u1) c1 (e2, u2) c2
370 | _ -> false
371
372 let assert_reduced_constructor s =
373 if not @@ CList.is_empty s then
374 CErrors.anomaly Pp.(str "conversion was given unreduced term (FConstruct).")
375
376 (* Conversion between [lft1]term1 and [lft2]term2 *)
377 let rec ccnv cv_pb l2r infos lft1 lft2 term1 term2 cuniv =
378 let fast = fast_test lft1 term1 lft2 term2 in
379 if fast then cuniv
380 else
381 eqappr cv_pb l2r infos (lft1, (term1,[])) (lft2, (term2,[])) cuniv
382
383 (* Conversion between [lft1](hd1 v1) and [lft2](hd2 v2) *)
384 and eqappr cv_pb l2r infos (lft1,st1) (lft2,st2) cuniv =
385 Control.check_for_interrupt ();
386 (* First head reduce both terms *)
387 let ninfos = infos_with_reds infos.cnv_inf RedFlags.betaiotazeta in
388 let appr1 = whd_stack ninfos infos.lft_tab (fst st1) (snd st1) in
389 let appr2 = whd_stack ninfos infos.rgt_tab (fst st2) (snd st2) in
390 eqwhnf cv_pb l2r infos (lft1, appr1) (lft2, appr2) cuniv
391
392 (* assumes that appr1 and appr2 are in whnf *)
393 and eqwhnf cv_pb l2r infos (lft1, (hd1, v1) as appr1) (lft2, (hd2, v2) as appr2) cuniv =
394 (** We delay the computation of the lifts that apply to the head of the term
395 with [el_stack] inside the branches where they are actually used. *)
396 (** Irrelevant terms are guaranteed to be [FIrrelevant], except for [FFlex],
397 [FRel] and [FLambda]. Those ones are handled specifically below. *)
398 match (fterm_of hd1, fterm_of hd2) with
399 (* case of leaves *)
400 | (FAtom a1, FAtom a2) ->
401 (match kind a1, kind a2 with
402 | (Sort s1, Sort s2) ->
403 if not (is_empty_stack v1 && is_empty_stack v2) then
404 (* May happen because we convert application right to left *)
405 raise NotConvertible;
406 fail_check infos @@ sort_cmp_universes cv_pb s1 s2 cuniv
407 | (Meta n, Meta m) ->
408 if Int.equal n m
409 then convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
410 else raise NotConvertible
411 | _ -> raise NotConvertible)
412 | (FEvar (ev1, args1, env1, _), FEvar (ev2, args2, env2, _)) ->
413 if Evar.equal ev1 ev2 then
414 let el1 = el_stack lft1 v1 in
415 let el2 = el_stack lft2 v2 in
416 let cuniv = convert_stacks l2r infos lft1 lft2 v1 v2 cuniv in
417 convert_list l2r infos el1 el2
418 (List.map (mk_clos env1) args1)
419 (List.map (mk_clos env2) args2) cuniv
420 else raise NotConvertible
421
422 (* 2 index known to be bound to no constant *)
423 | (FRel n, FRel m) ->
424 let el1 = el_stack lft1 v1 in
425 let el2 = el_stack lft2 v2 in
426 let n = reloc_rel n el1 in
427 let m = reloc_rel m el2 in
428 let rn = Range.get (info_relevances infos.cnv_inf) (n - 1) in
429 let rm = Range.get (info_relevances infos.cnv_inf) (m - 1) in
430 if is_irrelevant infos.cnv_inf rn && is_irrelevant infos.cnv_inf rm then
431 let v1 = CClosure.skip_irrelevant_stack infos.cnv_inf v1 in
432 let v2 = CClosure.skip_irrelevant_stack infos.cnv_inf v2 in
433 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
434 else if Int.equal n m then
435 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
436 else
437 raise NotConvertible
438
439 (* 2 constants, 2 local defined vars or 2 defined rels *)
440 | (FFlex fl1, FFlex fl2) ->
441 (try
442 let nargs = same_args_size v1 v2 in
443 let cuniv = conv_table_key infos ~nargs fl1 fl2 cuniv in
444 let () = if irr_flex infos.cnv_inf fl1 then raise NotConvertible (* trigger the fallback *) in
445 let mask = if infos.cnv_typ then match fl1 with
446 | ConstKey _ -> get_ref_mask infos.cnv_inf infos.lft_tab fl1
447 | RelKey _ | VarKey _ -> [||]
448 else [||]
449 in
450 convert_stacks ~mask l2r infos lft1 lft2 v1 v2 cuniv
451 with NotConvertible | NotConvertibleTrace _ ->
452 let r1 = unfold_ref_with_args infos.cnv_inf infos.lft_tab fl1 v1 in
453 let r2 = unfold_ref_with_args infos.cnv_inf infos.rgt_tab fl2 v2 in
454 match r1, r2 with
455 | None, None -> raise NotConvertible
456 | Some (t1, v1), Some (t2, v2) ->
457 (* else the oracle tells which constant is to be expanded *)
458 let oracle = CClosure.oracle_of_infos infos.cnv_inf in
459 let to_er fl =
460 match fl with
461 | ConstKey (c, _) -> Some (Conv_oracle.EvalConstRef c)
462 | VarKey id -> Some (Conv_oracle.EvalVarRef id)
463 | RelKey _ -> None
464 in
465 let ninfos = infos_with_reds infos.cnv_inf RedFlags.betaiotazeta in
466 let () = Control.check_for_interrupt () in
467 if Conv_oracle.oracle_order oracle l2r (to_er fl1) (to_er fl2) then
468 let appr1 = whd_stack ninfos infos.lft_tab t1 v1 in
469 eqwhnf cv_pb l2r infos (lft1, appr1) appr2 cuniv
470 else
471 let appr2 = whd_stack ninfos infos.rgt_tab t2 v2 in
472 eqwhnf cv_pb l2r infos appr1 (lft2, appr2) cuniv
473 | Some (t1, v1), None ->
474 let all = RedFlags.(red_add_transparent all (red_transparent (info_flags infos.cnv_inf))) in
475 let t1 = whd_stack (infos_with_reds infos.cnv_inf all) infos.lft_tab t1 v1 in
476 eqwhnf cv_pb l2r infos (lft1, t1) appr2 cuniv
477 | None, Some (t2, v2) ->
478 let all = RedFlags.(red_add_transparent all (red_transparent (info_flags infos.cnv_inf))) in
479 let t2 = whd_stack (infos_with_reds infos.cnv_inf all) infos.rgt_tab t2 v2 in
480 eqwhnf cv_pb l2r infos appr1 (lft2, t2) cuniv
481 )
482
483 | (FProj (p1,r1,c1), FProj (p2, r2, c2)) ->
484 (* Projections: prefer unfolding to first-order unification,
485 which will happen naturally if the terms c1, c2 are not in constructor
486 form *)
487 (match unfold_projection infos.cnv_inf p1 r1 with
488 | Some s1 ->
489 eqappr cv_pb l2r infos (lft1, (c1, (s1 :: v1))) appr2 cuniv
490 | None ->
491 match unfold_projection infos.cnv_inf p2 r2 with
492 | Some s2 ->
493 eqappr cv_pb l2r infos appr1 (lft2, (c2, (s2 :: v2))) cuniv
494 | None ->
495 if Projection.Repr.CanOrd.equal (Projection.repr p1) (Projection.repr p2)
496 && compare_stack_shape v1 v2 then
497 let el1 = el_stack lft1 v1 in
498 let el2 = el_stack lft2 v2 in
499 let u1 = ccnv CONV l2r infos el1 el2 c1 c2 cuniv in
500 convert_stacks l2r infos lft1 lft2 v1 v2 u1
501 else (* Two projections in WHNF: unfold *)
502 raise NotConvertible)
503
504 | (FProj (p1,r1,c1), t2) ->
505 begin match unfold_projection infos.cnv_inf p1 r1 with
506 | Some s1 ->
507 eqappr cv_pb l2r infos (lft1, (c1, (s1 :: v1))) appr2 cuniv
508 | None ->
509 begin match t2 with
510 | FFlex fl2 ->
511 begin match unfold_ref_with_args infos.cnv_inf infos.rgt_tab fl2 v2 with
512 | Some t2 ->
513 eqappr cv_pb l2r infos appr1 (lft2, t2) cuniv
514 | None -> raise NotConvertible
515 end
516 | _ -> raise NotConvertible
517 end
518 end
519
520 | (t1, FProj (p2,r2,c2)) ->
521 begin match unfold_projection infos.cnv_inf p2 r2 with
522 | Some s2 ->
523 eqappr cv_pb l2r infos appr1 (lft2, (c2, (s2 :: v2))) cuniv
524 | None ->
525 begin match t1 with
526 | FFlex fl1 ->
527 begin match unfold_ref_with_args infos.cnv_inf infos.lft_tab fl1 v1 with
528 | Some t1 ->
529 eqappr cv_pb l2r infos (lft1, t1) appr2 cuniv
530 | None -> raise NotConvertible
531 end
532 | _ -> raise NotConvertible
533 end
534 end
535
536 (* other constructors *)
537 | (FLambda _, FLambda _) ->
538 (* Inconsistency: we tolerate that v1, v2 contain shift and update but
539 we throw them away *)
540 if not (is_empty_stack v1 && is_empty_stack v2) then
541 anomaly (Pp.str "conversion was given ill-typed terms (FLambda).");
542 let (x1,ty1,bd1) = destFLambda mk_clos hd1 in
543 let (_,ty2,bd2) = destFLambda mk_clos hd2 in
544 let el1 = el_stack lft1 v1 in
545 let el2 = el_stack lft2 v2 in
546 let cuniv = ccnv CONV l2r infos el1 el2 ty1 ty2 cuniv in
547 ccnv CONV l2r (push_relevance infos x1) (el_lift el1) (el_lift el2) bd1 bd2 cuniv
548
549 | (FProd (x1, c1, c2, e), FProd (_, c'1, c'2, e')) ->
550 if not (is_empty_stack v1 && is_empty_stack v2) then
551 (* May happen because we convert application right to left *)
552 raise NotConvertible;
553 (* Luo's system *)
554 let el1 = el_stack lft1 v1 in
555 let el2 = el_stack lft2 v2 in
556 let cuniv = ccnv CONV l2r infos el1 el2 c1 c'1 cuniv in
557 let x1 = usubst_binder e x1 in
558 ccnv cv_pb l2r (push_relevance infos x1) (el_lift el1) (el_lift el2) (mk_clos (usubs_lift e) c2) (mk_clos (usubs_lift e') c'2) cuniv
559
560 (* Eta-expansion on the fly *)
561 | (FLambda _, _) ->
562 let () = match v1 with
563 | [] -> ()
564 | _ ->
565 anomaly (Pp.str "conversion was given unreduced term (FLambda).")
566 in
567 let (x1,_ty1,bd1) = destFLambda mk_clos hd1 in
568 let infos = push_relevance infos x1 in
569 eqappr CONV l2r infos
570 (el_lift lft1, (bd1, [])) (el_lift lft2, (hd2, eta_expand_stack infos.cnv_inf x1 v2)) cuniv
571 | (_, FLambda _) ->
572 let () = match v2 with
573 | [] -> ()
574 | _ ->
575 anomaly (Pp.str "conversion was given unreduced term (FLambda).")
576 in
577 let (x2,_ty2,bd2) = destFLambda mk_clos hd2 in
578 let infos = push_relevance infos x2 in
579 eqappr CONV l2r infos
580 (el_lift lft1, (hd1, eta_expand_stack infos.cnv_inf x2 v1)) (el_lift lft2, (bd2, [])) cuniv
581
582 (* only one constant, defined var or defined rel *)
583 | (FFlex fl1, c2) ->
584 begin match unfold_ref_with_args infos.cnv_inf infos.lft_tab fl1 v1 with
585 | Some (def1,v1) ->
586 (** By virtue of the previous case analyses, we know [c2] is rigid.
587 Conversion check to rigid terms eventually implies full weak-head
588 reduction, so instead of repeatedly performing small-step
589 unfoldings, we perform reduction with all flags on. *)
590 let all = RedFlags.(red_add_transparent all (red_transparent (info_flags infos.cnv_inf))) in
591 let r1 = whd_stack (infos_with_reds infos.cnv_inf all) infos.lft_tab def1 v1 in
592 eqwhnf cv_pb l2r infos (lft1, r1) appr2 cuniv
593 | None ->
594 (match c2 with
595 | FConstruct (((ind2,1),u2),_) ->
596 let () = assert_reduced_constructor v2 in
597 (try
598 let v2, v1 =
599 eta_expand_ind_stack (info_env infos.cnv_inf) (ind2,u2) hd2 (snd appr1)
600 in convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
601 with Not_found -> raise NotConvertible)
602 | _ -> raise NotConvertible)
603 end
604
605 | (c1, FFlex fl2) ->
606 begin match unfold_ref_with_args infos.cnv_inf infos.rgt_tab fl2 v2 with
607 | Some (def2, v2) ->
608 (** Symmetrical case of above. *)
609 let all = RedFlags.(red_add_transparent all (red_transparent (info_flags infos.cnv_inf))) in
610 let r2 = whd_stack (infos_with_reds infos.cnv_inf all) infos.rgt_tab def2 v2 in
611 eqwhnf cv_pb l2r infos appr1 (lft2, r2) cuniv
612 | None ->
613 match c1 with
614 | FConstruct (((ind1,1),u1),_) ->
615 let () = assert_reduced_constructor v1 in
616 (try let v1, v2 =
617 eta_expand_ind_stack (info_env infos.cnv_inf) (ind1,u1) hd1 (snd appr2)
618 in convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
619 with Not_found -> raise NotConvertible)
620 | _ -> raise NotConvertible
621 end
622
623 (* Inductive types: MutInd MutConstruct Fix Cofix *)
624 | (FInd (ind1,u1 as pind1), FInd (ind2,u2 as pind2)) ->
625 if Ind.CanOrd.equal ind1 ind2 then
626 if UVars.Instance.is_empty u1 || UVars.Instance.is_empty u2 then
627 let cuniv = fail_check infos @@ convert_instances ~flex:false u1 u2 cuniv in
628 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
629 else
630 let mind = Environ.lookup_mind (fst ind1) (info_env infos.cnv_inf) in
631 let nargs = same_args_size v1 v2 in
632 match fail_check infos @@ convert_inductives cv_pb (mind, snd ind1) nargs u1 u2 cuniv with
633 | cuniv -> convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
634 | exception MustExpand ->
635 let env = info_env infos.cnv_inf in
636 let hd1 = eta_expand_ind env pind1 in
637 let hd2 = eta_expand_ind env pind2 in
638 eqappr cv_pb l2r infos (lft1,(hd1,v1)) (lft2,(hd2,v2)) cuniv
639 else raise NotConvertible
640
641 | (FConstruct (((ind1,j1),u1 as pctor1,args1)), FConstruct (((ind2,j2),u2 as pctor2),args2)) ->
642 let () = assert_reduced_constructor v1 in
643 let () = assert_reduced_constructor v2 in
644 let nargs = Array.length args1 in
645 let () = if not @@ Int.equal nargs (Array.length args2) then raise NotConvertible in
646 let v1 = append_stack args1 v1 in
647 let v2 = append_stack args2 v2 in
648 if Int.equal j1 j2 && Ind.CanOrd.equal ind1 ind2 then
649 if UVars.Instance.is_empty u1 || UVars.Instance.is_empty u2 then
650 let cuniv = fail_check infos @@ convert_instances ~flex:false u1 u2 cuniv in
651 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
652 else
653 let mind = Environ.lookup_mind (fst ind1) (info_env infos.cnv_inf) in
654 match fail_check infos @@ convert_constructors (mind, snd ind1, j1) nargs u1 u2 cuniv with
655 | cuniv -> convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
656 | exception MustExpand ->
657 let env = info_env infos.cnv_inf in
658 let hd1 = eta_expand_constructor env pctor1 in
659 let hd2 = eta_expand_constructor env pctor2 in
660 eqappr cv_pb l2r infos (lft1,(hd1,v1)) (lft2,(hd2,v2)) cuniv
661 else raise NotConvertible
662
663 (* Eta expansion of records *)
664 | (FConstruct (((ind1,j1),u1), _),_) ->
665 let () = assert_reduced_constructor v1 in
666 (* records only have 1 constructor *)
667 let () = if not @@ Int.equal j1 1 then raise NotConvertible in
668 (try
669 let v1, v2 =
670 eta_expand_ind_stack (info_env infos.cnv_inf) (ind1,u1) hd1 (snd appr2)
671 in convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
672 with Not_found -> raise NotConvertible)
673
674 | (_, FConstruct (((ind2,j2),u2),_)) ->
675 let () = assert_reduced_constructor v2 in
676 (* records only have 1 constructor *)
677 let () = if not @@ Int.equal j2 1 then raise NotConvertible in
678 (try
679 let v2, v1 =
680 eta_expand_ind_stack (info_env infos.cnv_inf) (ind2,u2) hd2 (snd appr1)
681 in convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
682 with Not_found -> raise NotConvertible)
683
684 | (FFix (((op1, i1),(na1,tys1,cl1)),e1), FFix(((op2, i2),(_,tys2,cl2)),e2)) ->
685 if Int.equal i1 i2 && Array.equal Int.equal op1 op2
686 then
687 let n = Array.length cl1 in
688 let fty1 = Array.map (mk_clos e1) tys1 in
689 let fty2 = Array.map (mk_clos e2) tys2 in
690 let fcl1 = Array.map (mk_clos (usubs_liftn n e1)) cl1 in
691 let fcl2 = Array.map (mk_clos (usubs_liftn n e2)) cl2 in
692 let el1 = el_stack lft1 v1 in
693 let el2 = el_stack lft2 v2 in
694 let cuniv = convert_vect l2r infos el1 el2 fty1 fty2 cuniv in
695 let cuniv =
696 let na1 = Array.map (usubst_binder e1) na1 in
697 let infos = push_relevances infos na1 in
698 convert_vect l2r infos
699 (el_liftn n el1) (el_liftn n el2) fcl1 fcl2 cuniv
700 in
701 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
702 else raise NotConvertible
703
704 | (FCoFix ((op1,(na1,tys1,cl1)),e1), FCoFix((op2,(_,tys2,cl2)),e2)) ->
705 if Int.equal op1 op2
706 then
707 let n = Array.length cl1 in
708 let fty1 = Array.map (mk_clos e1) tys1 in
709 let fty2 = Array.map (mk_clos e2) tys2 in
710 let fcl1 = Array.map (mk_clos (usubs_liftn n e1)) cl1 in
711 let fcl2 = Array.map (mk_clos (usubs_liftn n e2)) cl2 in
712 let el1 = el_stack lft1 v1 in
713 let el2 = el_stack lft2 v2 in
714 let cuniv = convert_vect l2r infos el1 el2 fty1 fty2 cuniv in
715 let cuniv =
716 let na1 = Array.map (usubst_binder e1) na1 in
717 let infos = push_relevances infos na1 in
718 convert_vect l2r infos
719 (el_liftn n el1) (el_liftn n el2) fcl1 fcl2 cuniv
720 in
721 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
722 else raise NotConvertible
723
724 | FInt i1, FInt i2 ->
725 if Uint63.equal i1 i2 then convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
726 else raise NotConvertible
727
728 | FFloat f1, FFloat f2 ->
729 if Float64.equal f1 f2 then convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
730 else raise NotConvertible
731
732 | FString s1, FString s2 ->
733 if Pstring.equal s1 s2 then convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
734 else raise NotConvertible
735
736 | FCaseInvert (ci1,u1,pms1,p1,iv1,_,br1,e1), FCaseInvert (ci2,u2,pms2,p2,iv2,_,br2,e2) ->
737 (if not (Ind.CanOrd.equal ci1.ci_ind ci2.ci_ind) then raise NotConvertible);
738 let el1 = el_stack lft1 v1 and el2 = el_stack lft2 v2 in
739 let fold c1 c2 cuniv = ccnv CONV l2r infos el1 el2 c1 c2 cuniv in
740 (** FIXME: cache the presence of let-bindings in the case_info *)
741 let mind = Environ.lookup_mind (fst ci1.ci_ind) (info_env infos.cnv_inf) in
742 let mip = mind.Declarations.mind_packets.(snd ci1.ci_ind) in
743 let cuniv =
744 let ind = (mind,snd ci1.ci_ind) in
745 let nargs = inductive_cumulativity_arguments ind in
746 let u1 = CClosure.usubst_instance e1 u1 in
747 let u2 = CClosure.usubst_instance e2 u2 in
748 fail_check infos @@ convert_inductives CONV ind nargs u1 u2 cuniv
749 in
750 let pms1 = mk_clos_vect e1 pms1 in
751 let pms2 = mk_clos_vect e2 pms2 in
752 let cuniv = Array.fold_right2 fold pms1 pms2 cuniv in
753 let cuniv = Array.fold_right2 fold (get_invert iv1) (get_invert iv2) cuniv in
754 let cuniv = convert_return_clause mind mip l2r infos e1 e2 el1 el2 u1 u2 pms1 pms2 p1 p2 cuniv in
755 (* not clear if we need to pass both u1 and u2 as
756 convert_inductives should have enforced that they are
757 equivalent when used to instantiate this inductive's
758 components, but we may as well *)
759 let cuniv = convert_branches mind mip l2r infos e1 e2 el1 el2 u1 u2 pms1 pms2 br1 br2 cuniv in
760 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
761
762 | FArray (u1,t1,ty1), FArray (u2,t2,ty2) ->
763 let len = Parray.length_int t1 in
764 if not (Int.equal len (Parray.length_int t2)) then raise NotConvertible;
765 let cuniv = fail_check infos @@ convert_instances_cumul CONV [|UVars.Variance.Irrelevant|] u1 u2 cuniv in
766 let el1 = el_stack lft1 v1 in
767 let el2 = el_stack lft2 v2 in
768 let cuniv = ccnv CONV l2r infos el1 el2 ty1 ty2 cuniv in
769 let cuniv = Parray.fold_left2 (fun u v1 v2 -> ccnv CONV l2r infos el1 el2 v1 v2 u) cuniv t1 t2 in
770 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
771
772 | (FRel n1, FIrrelevant) ->
773 let n1 = reloc_rel n1 (el_stack lft1 v1) in
774 let r1 = Range.get (info_relevances infos.cnv_inf) (n1 - 1) in
775 if is_irrelevant infos.cnv_inf r1 then
776 let v1 = CClosure.skip_irrelevant_stack infos.cnv_inf v1 in
777 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
778 else raise NotConvertible
779
780 | (FIrrelevant, FRel n2) ->
781 let n2 = reloc_rel n2 (el_stack lft2 v2) in
782 let r2 = Range.get (info_relevances infos.cnv_inf) (n2 - 1) in
783 if is_irrelevant infos.cnv_inf r2 then
784 let v2 = CClosure.skip_irrelevant_stack infos.cnv_inf v2 in
785 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
786 else raise NotConvertible
787
788 | FIrrelevant, FIrrelevant ->
789 convert_stacks l2r infos lft1 lft2 v1 v2 cuniv
790
791 (* Should not happen because both (hd1,v1) and (hd2,v2) are in whnf *)
792 | ( (FLetIn _, _) | (FCaseT _,_) | (FApp _,_) | (FCLOS _,_) | (FLIFT _,_)
793 | (_, FLetIn _) | (_,FCaseT _) | (_,FApp _) | (_,FCLOS _) | (_,FLIFT _)
794 | (FLOCKED,_) | (_,FLOCKED) ) -> assert false
795
796 | (FRel _ | FAtom _ | FInd _ | FFix _ | FCoFix _ | FCaseInvert _
797 | FProd _ | FEvar _ | FInt _ | FFloat _ | FString _
798 | FArray _ | FIrrelevant), _ -> raise NotConvertible
799
800 and convert_stacks ?(mask = [||]) l2r infos lft1 lft2 stk1 stk2 cuniv =
801 let f (l1, t1) (l2, t2) cuniv = ccnv CONV l2r infos l1 l2 t1 t2 cuniv in
802 let rec cmp_rec nargs pstk1 pstk2 cuniv =
803 match (pstk1,pstk2) with
804 | (z1::s1, z2::s2) ->
805 (* Stacks are known to have the same argument size *)
806 let rnargs = match z1 with
807 | Zlapp a -> if nargs < 0 then -1 else nargs + Array.length a
808 | Zlproj _ | Zlfix _ | Zlcase _ | Zlprimitive _ -> -1
809 in
810 let cu1 = cmp_rec rnargs s1 s2 cuniv in
811 (match (z1,z2) with
812 | (Zlapp a1,Zlapp a2) ->
813 if nargs < 0 then
814 Array.fold_right2 f a1 a2 cu1
815 else
816 let rec fold i cu =
817 if i < 0 then cu
818 else if nargs + i < Array.length mask && not mask.(nargs + i) then
819 fold (i - 1) cu (* skip runtime irrelevant argument *)
820 else
821 let cu = f a1.(i) a2.(i) cu in
822 fold (i - 1) cu
823 in
824 fold (Array.length a1 - 1) cu1
825 | (Zlproj (c1,_l1),Zlproj (c2,_l2)) ->
826 if not (Projection.Repr.CanOrd.equal c1 c2) then
827 raise NotConvertible
828 else cu1
829 | (Zlfix(fx1,a1),Zlfix(fx2,a2)) ->
830 let cu2 = f fx1 fx2 cu1 in
831 cmp_rec (-1) a1 a2 cu2
832 | (Zlcase(ci1,l1,u1,pms1,p1,br1,e1),Zlcase(ci2,l2,u2,pms2,p2,br2,e2)) ->
833 if not (Ind.CanOrd.equal ci1.ci_ind ci2.ci_ind) then
834 raise NotConvertible;
835 let cu = cu1 in
836 (** FIXME: cache the presence of let-bindings in the case_info *)
837 let mind = Environ.lookup_mind (fst ci1.ci_ind) (info_env infos.cnv_inf) in
838 let mip = mind.Declarations.mind_packets.(snd ci1.ci_ind) in
839 let cu =
840 if UVars.Instance.is_empty u1 || UVars.Instance.is_empty u2 then
841 convert_instances ~flex:false u1 u2 cu
842 else
843 let u1 = CClosure.usubst_instance e1 u1 in
844 let u2 = CClosure.usubst_instance e2 u2 in
845 match mind.Declarations.mind_variance with
846 | None -> convert_instances ~flex:false u1 u2 cu
847 | Some variances -> convert_instances_cumul CONV variances u1 u2 cu
848 in
849 let cu = fail_check infos cu in
850 let pms1 = mk_clos_vect e1 pms1 in
851 let pms2 = mk_clos_vect e2 pms2 in
852 let fold_params c1 c2 accu = f (l1, c1) (l2, c2) accu in
853 let cu = Array.fold_right2 fold_params pms1 pms2 cu in
854 let cu = convert_return_clause mind mip l2r infos e1 e2 l1 l2 u1 u2 pms1 pms2 p1 p2 cu in
855 convert_branches mind mip l2r infos e1 e2 l1 l2 u1 u2 pms1 pms2 br1 br2 cu
856 | (Zlprimitive(op1,_,rargs1,kargs1),Zlprimitive(op2,_,rargs2,kargs2)) ->
857 if not (CPrimitives.equal op1 op2) then raise NotConvertible else
858 let cu2 = List.fold_right2 f rargs1 rargs2 cu1 in
859 let fk (_,a1) (_,a2) cu = f a1 a2 cu in
860 List.fold_right2 fk kargs1 kargs2 cu2
861 | ((Zlapp _ | Zlproj _ | Zlfix _| Zlcase _| Zlprimitive _), _) -> assert false)
862 | _ -> cuniv in
863 if compare_stack_shape stk1 stk2 then
864 let nargs = if Array.is_empty mask then -1 else 0 in
865 cmp_rec nargs (pure_stack lft1 stk1) (pure_stack lft2 stk2) cuniv
866 else raise NotConvertible
867
868 and convert_vect l2r infos lft1 lft2 v1 v2 cuniv =
869 let lv1 = Array.length v1 in
870 let lv2 = Array.length v2 in
871 if Int.equal lv1 lv2
872 then
873 let rec fold n cuniv =
874 if n >= lv1 then cuniv
875 else
876 let cuniv = ccnv CONV l2r infos lft1 lft2 v1.(n) v2.(n) cuniv in
877 fold (n+1) cuniv in
878 fold 0 cuniv
879 else raise NotConvertible
880
881 and convert_under_context l2r infos e1 e2 lft1 lft2 ctx (nas1, c1) (nas2, c2) cu =
882 let n = Array.length nas1 in
883 let () = assert (Int.equal n (Array.length nas2)) in
884 let n, e1, e2 = match ctx with
885 | None -> (* nolet *)
886 let e1 = usubs_liftn n e1 in
887 let e2 = usubs_liftn n e2 in
888 (n, e1, e2)
889 | Some (ctx, args1, args2) ->
890 let n1, e1 = esubst_of_context ctx args1 e1 in
891 let n2, e2 = esubst_of_context ctx args2 e2 in
892 let () = assert (Int.equal n1 n2) in
893 n1, e1, e2
894 in
895 let lft1 = el_liftn n lft1 in
896 let lft2 = el_liftn n lft2 in
897 let infos = push_relevances infos (Array.map (usubst_binder e1) nas1) in
898 ccnv CONV l2r infos lft1 lft2 (mk_clos e1 c1) (mk_clos e2 c2) cu
899
900 and convert_return_clause mib mip l2r infos e1 e2 l1 l2 u1 u2 pms1 pms2 p1 p2 cu =
901 let ctx =
902 if Int.equal mip.mind_nrealargs mip.mind_nrealdecls then None
903 else
904 let ctx, _ = List.chop mip.mind_nrealdecls mip.mind_arity_ctxt in
905 let pms1 = inductive_subst mib u1 pms1 in
906 let pms2 = inductive_subst mib u2 pms2 in
907 let open Context.Rel.Declaration in
908 (* Add the inductive binder *)
909 let ctx = None :: List.map get_value ctx in
910 Some (ctx, pms1, pms2)
911 in
912 convert_under_context l2r infos e1 e2 l1 l2 ctx (fst p1) (fst p2) cu
913
914 and convert_branches mib mip l2r infos e1 e2 lft1 lft2 u1 u2 pms1 pms2 br1 br2 cuniv =
915 let fold i (ctx, _) cuniv =
916 let ctx =
917 if Int.equal mip.mind_consnrealdecls.(i) mip.mind_consnrealargs.(i) then None
918 else
919 let ctx, _ = List.chop mip.mind_consnrealdecls.(i) ctx in
920 let ctx = List.map Context.Rel.Declaration.get_value ctx in
921 let pms1 = inductive_subst mib u1 pms1 in
922 let pms2 = inductive_subst mib u2 pms2 in
923 Some (ctx, pms1, pms2)
924 in
925 let c1 = br1.(i) in
926 let c2 = br2.(i) in
927 convert_under_context l2r infos e1 e2 lft1 lft2 ctx c1 c2 cuniv
928 in
929 Array.fold_right_i fold mip.mind_nf_lc cuniv
930
931 and convert_list l2r infos lft1 lft2 v1 v2 cuniv = match v1, v2 with
932 | [], [] -> cuniv
933 | c1 :: v1, c2 :: v2 ->
934 let cuniv = ccnv CONV l2r infos lft1 lft2 c1 c2 cuniv in
935 convert_list l2r infos lft1 lft2 v1 v2 cuniv
936 | _, _ -> raise NotConvertible
937
938 let clos_gen_conv (type err) ~typed trans cv_pb l2r evars env graph univs t1 t2 =
939 NewProfile.profile "Conversion" begin fun () ->
940 let reds = RedFlags.red_add_transparent RedFlags.betaiotazeta trans in
941 let infos = create_conv_infos ~univs:graph ~evars reds env in
942 let module Error = struct type payload += Error of err end in
943 let box e = Error.Error e in
944 let infos = {
945 cnv_inf = infos;
946 cnv_typ = typed;
947 lft_tab = create_tab ();
948 rgt_tab = create_tab ();
949 err_ret = box;
950 } in
951 try Result.Ok (ccnv cv_pb l2r infos el_id el_id (inject t1) (inject t2) univs)
952 with
953 | NotConvertible -> Result.Error None
954 | NotConvertibleTrace (Error.Error e) -> Result.Error (Some e)
955 | NotConvertibleTrace _ -> assert false
956 end ()
957
958 let check_eq qeq state u u' =
959 if UGraph.check_eq_sort qeq state u u'
960 then Result.Ok state
961 else Result.Error None
962
963 let check_leq qeq state u u' =
964 if UGraph.check_leq_sort qeq state u u'
965 then Result.Ok state
966 else Result.Error None
967
968 let checked_sort_cmp_universes qeq = (); fun pb s0 s1 state ->
969 match pb with
970 | CUMUL -> check_leq qeq state s0 s1
971 | CONV -> check_eq qeq state s0 s1
972
973 let check_convert_instances qeq = (); fun ~flex:_ u u' state ->
974 if UGraph.check_eq_instances qeq state u u' then Result.Ok state
975 else Result.Error None
976
977 (* general conversion and inference functions *)
978 let check_inductive_instances qeq = (); fun cv_pb variance u1 u2 state ->
979 let qcsts, ucsts = get_cumulativity_constraints cv_pb variance u1 u2 in
980 let check_quality (q1, q2) = qeq q1 q2 in
981 if UVars.QPairSet.for_all check_quality qcsts && UGraph.check_constraints ucsts state
982 then Result.Ok state
983 else Result.Error None
984
985 let checked_universes_gen qeq =
986 { compare_sorts = checked_sort_cmp_universes qeq;
987 compare_instances = check_convert_instances qeq;
988 compare_cumul_instances = check_inductive_instances qeq; }
989
990 let checked_universes = checked_universes_gen Sorts.Quality.equal
991
992 let () =
993 let conv infos tab a b =
994 try
995 let box = Empty.abort in
996 let state = info_univs infos in
997 let qual_equal q1 q2 = CClosure.eq_quality infos q1 q2 in
998 let infos = { cnv_inf = infos; cnv_typ = true; lft_tab = tab; rgt_tab = tab; err_ret = box } in
999 let state', _ = ccnv CONV false infos el_id el_id a b (state, checked_universes_gen qual_equal) in
1000 assert (state==state');
1001 true
1002 with
1003 | NotConvertible -> false
1004 | NotConvertibleTrace _ -> assert false
1005 in
1006 CClosure.set_conv conv
1007
1008 let gen_conv ~typed cv_pb ?(l2r=false) ?(reds=TransparentState.full) env ?(evars=default_evar_handler env) t1 t2 =
1009 let univs = Environ.universes env in
1010 let state = univs in
1011 let b =
1012 if cv_pb = CUMUL then leq_constr_univs univs t1 t2
1013 else eq_constr_univs univs t1 t2
1014 in
1015 if b then Result.Ok ()
1016 else match clos_gen_conv ~typed reds cv_pb l2r evars env univs (state, checked_universes) t1 t2 with
1017 | Result.Ok (_ : 'a * ('a, Empty.t) universe_compare)-> Result.Ok ()
1018 | Result.Error None -> Result.Error ()
1019 | Result.Error (Some e) -> Empty.abort e
1020
1021 let conv = gen_conv ~typed:false CONV
1022 let conv_leq = gen_conv ~typed:false CUMUL
1023
1024 let generic_conv cv_pb ~l2r reds env ?(evars=default_evar_handler env) state t1 t2 =
1025 let graph = Environ.universes env in
1026 match clos_gen_conv ~typed:false reds cv_pb l2r evars env graph state t1 t2 with
1027 | Result.Ok (s, _) -> Result.Ok s
1028 | Result.Error e -> Result.Error e
1029
1030 let default_conv cv_pb env t1 t2 =
1031 gen_conv ~typed:true cv_pb env t1 t2
1032
1033 let default_conv_leq = default_conv CUMUL
1034
1035 type graph_inconsistency = Univ of UGraph.univ_inconsistency | Qual of QGraph.elimination_error