1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
28 (* TODO unify exceptions *)
30 exception WrongUriToInductiveDefinition;;
31 exception Impossible of int;;
32 exception ReferenceToConstant;;
33 exception ReferenceToVariable;;
34 exception ReferenceToCurrentProof;;
35 exception ReferenceToInductiveDefinition;;
39 let debug_print s = if debug then prerr_endline (Lazy.force s)
43 let rec debug_aux t i =
45 let module U = UriManager in
46 CicPp.ppobj (C.Variable ("DEBUG", None, t, [], [])) ^ "\n" ^ i
49 debug_print (lazy (s ^ "\n" ^ List.fold_right debug_aux (t::env) ""))
52 module type Strategy =
56 type config = int * env_term list * NCic.term * stack_term list
58 reduce: (config -> config) ->
59 unwind: (config -> NCic.term) ->
61 val from_stack : stack_term -> config
62 val from_stack_list_for_unwind :
63 unwind: (config -> NCic.term) ->
64 stack_term list -> NCic.term list
65 val from_env : env_term -> config
66 val from_env_for_unwind :
67 unwind: (config -> NCic.term) ->
70 reduce: (config -> config) ->
71 unwind: (config -> NCic.term) ->
72 stack_term -> env_term
74 reduce: (config -> config) ->
75 unwind: (config -> NCic.term) ->
76 int -> env_term list ->
78 val compute_to_stack :
79 reduce: (config -> config) ->
80 unwind: (config -> NCic.term) ->
86 module CallByValueByNameForUnwind =
88 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
89 and stack_term = config
90 and env_term = config * config (* cbv, cbn *)
91 and ens_term = config * config (* cbv, cbn *)
95 let from_stack config = config
96 let from_stack_list_for_unwind ~unwind l = List.map unwind l
97 let from_env (c,_) = c
98 let from_ens (c,_) = c
99 let from_env_for_unwind ~unwind (_,c) = unwind c
100 let from_ens_for_unwind ~unwind (_,c) = unwind c
101 let stack_to_env ~reduce ~unwind config = reduce config, (0,[],[],unwind config,[])
102 let compute_to_env ~reduce ~unwind k e ens t = (k,e,ens,t,[]), (k,e,ens,t,[])
103 let compute_to_stack ~reduce ~unwind config = config
108 module CallByValueByNameForUnwind' =
110 type config = int * env_term list * NCic.term * stack_term list
111 and stack_term = config lazy_t * NCic.term lazy_t (* cbv, cbn *)
112 and env_term = config lazy_t * NCic.term lazy_t (* cbv, cbn *)
113 let to_env ~reduce ~unwind c = lazy (reduce c),lazy (unwind c)
114 let from_stack (c,_) = Lazy.force c
115 let from_stack_list_for_unwind ~unwind:_ l =
116 List.map (function (_,c) -> Lazy.force c) l
117 let from_env (c,_) = Lazy.force c
118 let from_env_for_unwind ~unwind:_ (_,c) = Lazy.force c
119 let stack_to_env ~reduce:_ ~unwind:_ config = config
120 let compute_to_env ~reduce ~unwind k e t =
121 lazy (reduce (k,e,t,[])), lazy (unwind (k,e,t,[]))
122 let compute_to_stack ~reduce ~unwind config =
123 lazy (reduce config), lazy (unwind config)
129 module CallByNameStrategy =
131 type stack_term = Cic.term
132 type env_term = Cic.term
133 type ens_term = Cic.term
134 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
137 let from_stack ~unwind v = v
138 let from_stack_list ~unwind l = l
141 let from_env_for_unwind ~unwind v = v
142 let from_ens_for_unwind ~unwind v = v
143 let stack_to_env ~reduce ~unwind v = v
144 let compute_to_stack ~reduce ~unwind k e ens t = unwind k e ens t
145 let compute_to_env ~reduce ~unwind k e ens t = unwind k e ens t
149 module CallByNameStrategy =
151 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
152 and stack_term = config
153 and env_term = config
154 and ens_term = config
158 let from_stack config = config
159 let from_stack_list_for_unwind ~unwind l = List.map unwind l
162 let from_env_for_unwind ~unwind c = unwind c
163 let from_ens_for_unwind ~unwind c = unwind c
164 let stack_to_env ~reduce ~unwind config = 0,[],[],unwind config,[]
165 let compute_to_env ~reduce ~unwind k e ens t = k,e,ens,t,[]
166 let compute_to_stack ~reduce ~unwind config = config
170 module CallByValueStrategy =
172 type stack_term = Cic.term
173 type env_term = Cic.term
174 type ens_term = Cic.term
175 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
178 let from_stack ~unwind v = v
179 let from_stack_list ~unwind l = l
182 let from_env_for_unwind ~unwind v = v
183 let from_ens_for_unwind ~unwind v = v
184 let stack_to_env ~reduce ~unwind v = v
185 let compute_to_stack ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
186 let compute_to_env ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
190 module CallByValueStrategyByNameOnConstants =
192 type stack_term = Cic.term
193 type env_term = Cic.term
194 type ens_term = Cic.term
195 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
198 let from_stack ~unwind v = v
199 let from_stack_list ~unwind l = l
202 let from_env_for_unwind ~unwind v = v
203 let from_ens_for_unwind ~unwind v = v
204 let stack_to_env ~reduce ~unwind v = v
205 let compute_to_stack ~reduce ~unwind k e ens =
207 Cic.Const _ as t -> unwind k e ens t
208 | t -> reduce (k,e,ens,t,[])
209 let compute_to_env ~reduce ~unwind k e ens =
211 Cic.Const _ as t -> unwind k e ens t
212 | t -> reduce (k,e,ens,t,[])
216 module LazyCallByValueStrategy =
218 type stack_term = Cic.term lazy_t
219 type env_term = Cic.term lazy_t
220 type ens_term = Cic.term lazy_t
221 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
222 let to_env v = lazy v
223 let to_ens v = lazy v
224 let from_stack ~unwind v = Lazy.force v
225 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
226 let from_env v = Lazy.force v
227 let from_ens v = Lazy.force v
228 let from_env_for_unwind ~unwind v = Lazy.force v
229 let from_ens_for_unwind ~unwind v = Lazy.force v
230 let stack_to_env ~reduce ~unwind v = v
231 let compute_to_stack ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
232 let compute_to_env ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
236 module LazyCallByValueStrategyByNameOnConstants =
238 type stack_term = Cic.term lazy_t
239 type env_term = Cic.term lazy_t
240 type ens_term = Cic.term lazy_t
241 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
242 let to_env v = lazy v
243 let to_ens v = lazy v
244 let from_stack ~unwind v = Lazy.force v
245 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
246 let from_env v = Lazy.force v
247 let from_ens v = Lazy.force v
248 let from_env_for_unwind ~unwind v = Lazy.force v
249 let from_ens_for_unwind ~unwind v = Lazy.force v
250 let stack_to_env ~reduce ~unwind v = v
251 let compute_to_stack ~reduce ~unwind k e ens t =
254 Cic.Const _ as t -> unwind k e ens t
255 | t -> reduce (k,e,ens,t,[]))
256 let compute_to_env ~reduce ~unwind k e ens t =
259 Cic.Const _ as t -> unwind k e ens t
260 | t -> reduce (k,e,ens,t,[]))
264 module LazyCallByNameStrategy =
266 type stack_term = Cic.term lazy_t
267 type env_term = Cic.term lazy_t
268 type ens_term = Cic.term lazy_t
269 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
270 let to_env v = lazy v
271 let to_ens v = lazy v
272 let from_stack ~unwind v = Lazy.force v
273 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
274 let from_env v = Lazy.force v
275 let from_ens v = Lazy.force v
276 let from_env_for_unwind ~unwind v = Lazy.force v
277 let from_ens_for_unwind ~unwind v = Lazy.force v
278 let stack_to_env ~reduce ~unwind v = v
279 let compute_to_stack ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
280 let compute_to_env ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
285 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns
288 type stack_term = reduce:bool -> Cic.term
289 type env_term = reduce:bool -> Cic.term
290 type ens_term = reduce:bool -> Cic.term
291 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
293 let value = lazy v in
294 fun ~reduce -> Lazy.force value
296 let value = lazy v in
297 fun ~reduce -> Lazy.force value
298 let from_stack ~unwind v = (v ~reduce:false)
299 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
300 let from_env v = (v ~reduce:true)
301 let from_ens v = (v ~reduce:true)
302 let from_env_for_unwind ~unwind v = (v ~reduce:true)
303 let from_ens_for_unwind ~unwind v = (v ~reduce:true)
304 let stack_to_env ~reduce ~unwind v = v
305 let compute_to_stack ~reduce ~unwind k e ens t =
309 Cic.Const _ as t -> unwind k e ens t
310 | t -> reduce (k,e,ens,t,[])
313 lazy (unwind k e ens t)
316 if reduce then Lazy.force svalue else Lazy.force lvalue
317 let compute_to_env ~reduce ~unwind k e ens t =
321 Cic.Const _ as t -> unwind k e ens t
322 | t -> reduce (k,e,ens,t,[])
325 lazy (unwind k e ens t)
328 if reduce then Lazy.force svalue else Lazy.force lvalue
332 module ClosuresOnStackByValueFromEnvOrEnsStrategy =
334 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
335 and stack_term = config
336 and env_term = config
337 and ens_term = config
339 let to_env config = config
340 let to_ens config = config
341 let from_stack config = config
342 let from_stack_list_for_unwind ~unwind l = List.map unwind l
345 let from_env_for_unwind ~unwind config = unwind config
346 let from_ens_for_unwind ~unwind config = unwind config
347 let stack_to_env ~reduce ~unwind config = reduce config
348 let compute_to_env ~reduce ~unwind k e ens t = (k,e,ens,t,[])
349 let compute_to_stack ~reduce ~unwind config = config
353 module ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy =
356 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
357 type env_term = Cic.term
358 type ens_term = Cic.term
359 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
362 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
363 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
366 let from_env_for_unwind ~unwind v = v
367 let from_ens_for_unwind ~unwind v = v
368 let stack_to_env ~reduce ~unwind (k,e,ens,t) =
370 Cic.Const _ as t -> unwind k e ens t
371 | t -> reduce (k,e,ens,t,[])
372 let compute_to_env ~reduce ~unwind k e ens t =
374 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
380 module Reduction(RS : Strategy) =
382 type env = RS.env_term list
383 type stack = RS.stack_term list
384 type config = int * env * NCic.term * stack
386 let rec unwind (k,e,t,s) =
390 NCicSubstitution.psubst ~avoid_beta_redexes:true
391 true 0 (RS.from_env_for_unwind ~unwind) e t
394 else NCic.Appl(t::(RS.from_stack_list_for_unwind ~unwind s))
397 let list_nth l n = try List.nth l n with Failure _ -> assert false;;
398 let rec replace i s t =
401 | n,he::tl -> he::(replace (n - 1) tl t)
402 | _,_ -> assert false
405 let rec reduce ~delta ?(subst = []) context : config -> config =
406 let rec aux = function
407 | k, e, NCic.Rel n, s when n <= k ->
408 let k',e',t',s' = RS.from_env (list_nth e (n-1)) in
410 | k, _, NCic.Rel n, s as config (* when n > k *) ->
411 (match List.nth context (n - 1 - k) with
412 | (_,NCic.Decl _) -> config
413 | (_,NCic.Def (x,_)) -> aux (0,[],NCicSubstitution.lift (n - k) x,s))
414 | (k, e, NCic.Meta (n,l), s) as config ->
416 let _,_, term,_ = NCicUtils.lookup_subst n subst in
417 aux (k, e, NCicSubstitution.subst_meta l term,s)
418 with NCicUtils.Subst_not_found _ -> config)
419 | (_, _, NCic.Sort _, _) as config -> config
420 | (_, _, NCic.Implicit _, _) -> assert false
421 | (_, _, NCic.Prod _, _) as config -> config
422 | (_, _, NCic.Lambda _, []) as config -> config
423 | (k, e, NCic.Lambda (_,_,t), p::s) ->
424 aux (k+1, (RS.stack_to_env ~reduce:aux ~unwind p)::e, t,s)
425 | (k, e, NCic.LetIn (_,_,m,t), s) ->
426 let m' = RS.compute_to_env ~reduce:aux ~unwind k e m in
427 aux (k+1, m'::e, t, s)
428 | (_, _, NCic.Appl [], _) -> assert false
429 | (k, e, NCic.Appl (he::tl), s) ->
431 List.map (fun t->RS.compute_to_stack ~reduce:aux ~unwind (k,e,t,[])) tl
433 aux (k, e, he, tl' @ s)
434 | (_, _, NCic.Const(NReference.Ref (_,_,NReference.Def) as refer), s) as config ->
435 let _,_,body,_,_,height = NCicEnvironment.get_checked_def refer in
436 if delta >= height then config else aux (0, [], body, s)
437 | (_, _, NCic.Const (NReference.Ref
438 (_,_,NReference.Fix (_,recindex)) as refer),s) as config ->
439 let _,_,body,_, _, height = NCicEnvironment.get_checked_fix refer in
440 if delta >= height then config else
442 try Some (RS.from_stack (List.nth s recindex))
443 with Failure _ -> None
447 match reduce ~delta:0 ~subst context recparam with
448 | (_,_,NCic.Const (NReference.Ref (_,_,NReference.Con _)), _) as c ->
450 replace recindex s (RS.compute_to_stack ~reduce:aux ~unwind c)
452 aux (0, [], body, new_s)
454 | (_, _, NCic.Const _, _) as config -> config
455 | (k, e, NCic.Match (_,_,term,pl),s) as config ->
456 let decofix = function
457 | (_,_,NCic.Const(NReference.Ref(_,_,NReference.CoFix _)as refer),s)->
458 let _,_,body,_,_,_ = NCicEnvironment.get_checked_cofix refer in
459 reduce ~delta:0 ~subst context (0,[],body,s)
462 (match decofix (reduce ~delta:0 ~subst context (k,e,term,[])) with
463 | (_, _, NCic.Const (NReference.Ref (_,_,NReference.Con (_,j))),[]) ->
464 aux (k, e, List.nth pl (j-1), s)
466 (NReference.Ref (_,_,NReference.Con (_,j)) as refer), s') ->
467 let leftno = NCicEnvironment.get_indty_leftno refer in
468 let _,params = HExtlib.split_nth leftno s' in
469 aux (k, e, List.nth pl (j-1), params@s)
475 let whd ?(delta=0) ?(subst=[]) context t =
476 unwind (reduce ~delta ~subst context (0, [], t, []))
483 (* ROTTO = rompe l'unificazione poiche' riduce gli argomenti di un'applicazione
484 senza ridurre la testa
485 module R = Reduction CallByNameStrategy;; OK 56.368s
486 module R = Reduction CallByValueStrategy;; ROTTO
487 module R = Reduction CallByValueStrategyByNameOnConstants;; ROTTO
488 module R = Reduction LazyCallByValueStrategy;; ROTTO
489 module R = Reduction LazyCallByValueStrategyByNameOnConstants;; ROTTO
490 module R = Reduction LazyCallByNameStrategy;; OK 0m56.398s
492 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns;;
494 module R = Reduction ClosuresOnStackByValueFromEnvOrEnsStrategy;; OK 58.583s
496 ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy;; OK 58.094s
497 module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);; OK 58.127s
499 (*module R = Reduction(CallByValueByNameForUnwind);;*)
500 module RS = CallByValueByNameForUnwind';;
501 (*module R = Reduction(CallByNameStrategy);;*)
502 (*module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);;*)
505 module R = Reduction(RS);;
506 module U = UriManager;;
513 let profiler_whd = HExtlib.profile ~enable:profile "are_convertible.whd" in
514 fun ?(delta=true) ?(subst=[]) context t ->
515 profiler_whd.HExtlib.profile (whd ~delta ~subst context) t
520 (* mimic ocaml (<< 3.08) "=" behaviour. Tests physical equality first then
521 * fallbacks to structural equality *)
523 Pervasives.compare x y = 0
525 (* t1, t2 must be well-typed *)
526 let are_convertible whd ?(subst=[]) ?(metasenv=[]) =
527 let heuristic = ref true in
528 let rec aux test_equality_only context t1 t2 ugraph =
529 let rec aux2 test_equality_only t1 t2 ugraph =
531 (* this trivial euristic cuts down the total time of about five times ;-) *)
532 (* this because most of the time t1 and t2 are "sintactically" the same *)
537 let module C = Cic in
539 (C.Rel n1, C.Rel n2) -> (n1 = n2),ugraph
540 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
541 if U.eq uri1 uri2 then
544 (fun (uri1,x) (uri2,y) (b,ugraph) ->
545 let b',ugraph' = aux test_equality_only context x y ugraph in
546 (U.eq uri1 uri2 && b' && b),ugraph'
547 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
549 Invalid_argument _ -> false,ugraph
553 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
556 let l1 = CicUtil.clean_up_local_context subst metasenv n1 l1 in
557 let l2 = CicUtil.clean_up_local_context subst metasenv n2 l2 in
559 (fun (b,ugraph) t1 t2 ->
563 | _,None -> true,ugraph
564 | Some t1',Some t2' ->
565 aux test_equality_only context t1' t2' ugraph
568 ) (true,ugraph) l1 l2
570 if b2 then true,ugraph1 else false,ugraph
573 | C.Meta (n1,l1), _ ->
575 let _,term,_ = NCicUtils.lookup_subst n1 subst in
576 let term' = CicSubstitution.subst_meta l1 term in
578 prerr_endline ("%?: " ^ CicPp.ppterm t1 ^ " <==> " ^ CicPp.ppterm t2);
579 prerr_endline ("%%%%%%: " ^ CicPp.ppterm term' ^ " <==> " ^ CicPp.ppterm t2);
581 aux test_equality_only context term' t2 ugraph
582 with CicUtil.Subst_not_found _ -> false,ugraph)
583 | _, C.Meta (n2,l2) ->
585 let _,term,_ = CicUtil.lookup_subst n2 subst in
586 let term' = CicSubstitution.subst_meta l2 term in
588 prerr_endline ("%?: " ^ CicPp.ppterm t1 ^ " <==> " ^ CicPp.ppterm t2);
589 prerr_endline ("%%%%%%: " ^ CicPp.ppterm term' ^ " <==> " ^ CicPp.ppterm t1);
591 aux test_equality_only context t1 term' ugraph
592 with CicUtil.Subst_not_found _ -> false,ugraph)
593 (* TASSI: CONSTRAINTS *)
594 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) when test_equality_only ->
596 true,(CicUniv.add_eq t2 t1 ugraph)
597 with CicUniv.UniverseInconsistency _ -> false,ugraph)
598 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
600 true,(CicUniv.add_ge t2 t1 ugraph)
601 with CicUniv.UniverseInconsistency _ -> false,ugraph)
602 | (C.Sort s1, C.Sort (C.Type _)) -> (not test_equality_only),ugraph
603 | (C.Sort s1, C.Sort s2) -> (s1 = s2),ugraph
604 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
605 let b',ugraph' = aux true context s1 s2 ugraph in
607 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
611 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
612 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
614 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
618 | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) ->
619 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
621 aux test_equality_only
622 ((Some (name1, (C.Def (s1,None))))::context) t1 t2 ugraph'
625 | (C.Appl l1, C.Appl l2) ->
628 (fun x y (b,ugraph) ->
630 aux test_equality_only context x y ugraph
632 false,ugraph) l1 l2 (true,ugraph)
634 Invalid_argument _ -> false,ugraph
636 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
637 let b' = U.eq uri1 uri2 in
641 (fun (uri1,x) (uri2,y) (b,ugraph) ->
642 if b && U.eq uri1 uri2 then
643 aux test_equality_only context x y ugraph
646 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
648 Invalid_argument _ -> false,ugraph
652 | (C.MutInd (uri1,i1,exp_named_subst1),
653 C.MutInd (uri2,i2,exp_named_subst2)
655 let b' = U.eq uri1 uri2 && i1 = i2 in
659 (fun (uri1,x) (uri2,y) (b,ugraph) ->
660 if b && U.eq uri1 uri2 then
661 aux test_equality_only context x y ugraph
664 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
666 Invalid_argument _ -> false,ugraph
670 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
671 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
673 let b' = U.eq uri1 uri2 && i1 = i2 && j1 = j2 in
677 (fun (uri1,x) (uri2,y) (b,ugraph) ->
678 if b && U.eq uri1 uri2 then
679 aux test_equality_only context x y ugraph
682 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
684 Invalid_argument _ -> false,ugraph
688 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
689 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
690 let b' = U.eq uri1 uri2 && i1 = i2 in
692 let b'',ugraph''=aux test_equality_only context
693 outtype1 outtype2 ugraph in
695 let b''',ugraph'''= aux test_equality_only context
696 term1 term2 ugraph'' in
698 (fun x y (b,ugraph) ->
700 aux test_equality_only context x y ugraph
703 pl1 pl2 (b''',ugraph''')
708 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
711 (fun (types,len) (n,_,ty,_) ->
712 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
718 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) (b,ugraph) ->
719 if b && recindex1 = recindex2 then
720 let b',ugraph' = aux test_equality_only context ty1 ty2
723 aux test_equality_only (tys@context) bo1 bo2 ugraph'
728 fl1 fl2 (true,ugraph)
731 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
734 (fun (types,len) (n,ty,_) ->
735 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
741 (fun (_,ty1,bo1) (_,ty2,bo2) (b,ugraph) ->
743 let b',ugraph' = aux test_equality_only context ty1 ty2
746 aux test_equality_only (tys@context) bo1 bo2 ugraph'
751 fl1 fl2 (true,ugraph)
754 | C.Cast (bo,_),t -> aux2 test_equality_only bo t ugraph
755 | t,C.Cast (bo,_) -> aux2 test_equality_only t bo ugraph
756 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
757 | (_,_) -> false,ugraph
762 aux2 test_equality_only t1 t2 ugraph
766 if fst res = true then
770 (*if !heuristic then prerr_endline ("NON FACILE: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);*)
771 (* heuristic := false; *)
772 debug t1 [t2] "PREWHD";
773 (*prerr_endline ("PREWHD: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);*)
775 prerr_endline ("PREWHD: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);
776 let t1' = whd ?delta:(Some true) ?subst:(Some subst) context t1 in
777 let t2' = whd ?delta:(Some true) ?subst:(Some subst) context t2 in
778 debug t1' [t2'] "POSTWHD";
780 let rec convert_machines ugraph =
783 | ((k1,env1,ens1,h1,s1),(k2,env2,ens2,h2,s2))::tl ->
784 let (b,ugraph) as res =
785 aux2 test_equality_only
786 (R.unwind (k1,env1,ens1,h1,[])) (R.unwind (k2,env2,ens2,h2,[])) ugraph
794 (fun si-> R.reduce ~delta:false ~subst context(RS.from_stack si))
797 (fun si-> R.reduce ~delta:false ~subst context(RS.from_stack si))
801 Invalid_argument _ -> None
805 | Some problems -> convert_machines ugraph problems
809 convert_machines ugraph
810 [R.reduce ~delta:true ~subst context (0,[],[],t1,[]),
811 R.reduce ~delta:true ~subst context (0,[],[],t2,[])]
812 (*prerr_endline ("POSTWH: " ^ CicPp.ppterm t1' ^ " <===> " ^ CicPp.ppterm t2');*)
814 aux2 test_equality_only t1' t2' ugraph
818 aux false (*c t1 t2 ugraph *)
823 let whd ?(delta=true) ?(subst=[]) context t =
824 let res = whd ~delta ~subst context t in
825 let rescsc = CicReductionNaif.whd ~delta ~subst context t in
826 if not (fst (are_convertible CicReductionNaif.whd ~subst context res rescsc CicUniv.empty_ugraph)) then
828 debug_print (lazy ("PRIMA: " ^ CicPp.ppterm t)) ;
830 debug_print (lazy ("DOPO: " ^ CicPp.ppterm res)) ;
832 debug_print (lazy ("CSC: " ^ CicPp.ppterm rescsc)) ;
835 let _ = are_convertible CicReductionNaif.whd ~subst context res rescsc CicUniv.empty_ugraph in
844 let are_convertible = are_convertible whd
850 let profiler_other_whd = HExtlib.profile ~enable:profile "~are_convertible.whd"
851 let whd ?(delta=true) ?(subst=[]) context t =
853 whd ~delta ~subst context t
855 profiler_other_whd.HExtlib.profile foo ()
859 let rec normalize ?(delta=true) ?(subst=[]) ctx term =
860 let module C = Cic in
861 let t = whd ~delta ~subst ctx term in
862 let aux = normalize ~delta ~subst in
863 let decl name t = Some (name, C.Decl t) in
866 | C.Var (uri,exp_named_subst) ->
867 C.Var (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
869 C.Meta (i,List.map (function Some t -> Some (aux ctx t) | None -> None) l)
872 | C.Cast (te,ty) -> C.Cast (aux ctx te, aux ctx ty)
874 let s' = aux ctx s in
875 C.Prod (n, s', aux ((decl n s')::ctx) t)
876 | C.Lambda (n,s,t) ->
877 let s' = aux ctx s in
878 C.Lambda (n, s', aux ((decl n s')::ctx) t)
880 (* the term is already in weak head normal form *)
882 | C.Appl (h::l) -> C.Appl (h::(List.map (aux ctx) l))
883 | C.Appl [] -> assert false
884 | C.Const (uri,exp_named_subst) ->
885 C.Const (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
886 | C.MutInd (uri,typeno,exp_named_subst) ->
887 C.MutInd (uri,typeno, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
888 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
889 C.MutConstruct (uri, typeno, consno,
890 List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
891 | C.MutCase (sp,i,outt,t,pl) ->
892 C.MutCase (sp,i, aux ctx outt, aux ctx t, List.map (aux ctx) pl)
893 (*CSC: to be completed, I suppose *)
899 let normalize ?delta ?subst ctx term =
900 (* prerr_endline ("NORMALIZE:" ^ CicPp.ppterm term); *)
901 let t = normalize ?delta ?subst ctx term in
902 (* prerr_endline ("NORMALIZED:" ^ CicPp.ppterm t); *)
906 (* performs an head beta/cast reduction
907 let rec head_beta_reduce ?(delta=false) ?(upto=(-1)) t =
912 (Cic.Appl (Cic.Lambda (_,_,t)::he'::tl')) ->
913 let he'' = CicSubstitution.subst he' t in
919 Cic.Appl l -> Cic.Appl (l@tl')
920 | _ -> Cic.Appl (he''::tl')
922 head_beta_reduce ~delta ~upto:(upto - 1) he'''
923 | Cic.Cast (te,_) -> head_beta_reduce ~delta ~upto te
924 | Cic.Appl (Cic.Const (uri,ens)::tl) as t when delta=true ->
926 match fst (CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri) with
927 Cic.Constant (_,bo,_,_,_) -> bo
928 | Cic.Variable _ -> raise ReferenceToVariable
929 | Cic.CurrentProof (_,_,bo,_,_,_) -> Some bo
930 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
935 head_beta_reduce ~upto
936 ~delta (Cic.Appl ((CicSubstitution.subst_vars ens bo)::tl)))
937 | Cic.Const (uri,ens) as t when delta=true ->
939 match fst (CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri) with
940 Cic.Constant (_,bo,_,_,_) -> bo
941 | Cic.Variable _ -> raise ReferenceToVariable
942 | Cic.CurrentProof (_,_,bo,_,_,_) -> Some bo
943 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
948 head_beta_reduce ~delta ~upto (CicSubstitution.subst_vars ens bo))
952 let are_convertible ?subst ?metasenv context t1 t2 ugraph =
953 let before = Unix.gettimeofday () in
954 let res = are_convertible ?subst ?metasenv context t1 t2 ugraph in
955 let after = Unix.gettimeofday () in
956 let diff = after -. before in
959 let nc = List.map (function None -> None | Some (n,_) -> Some n) context in
961 ("\n#(" ^ string_of_float diff ^ "):\n" ^ CicPp.pp t1 nc ^ "\n<=>\n" ^ CicPp.pp t2 nc);