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;;
37 let ndebug = ref false;;
42 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
47 let time1 = Unix.gettimeofday () in
48 indent := !indent ^ String.make 1 c;
49 times := time1 :: !times;
50 prerr_endline ("{{{" ^ !indent ^ " ")
56 let time2 = Unix.gettimeofday () in
58 match !times with time1::tl -> times := tl; time1 | [] -> assert false in
59 prerr_endline ("}}} " ^ string_of_float (time2 -. time1));
60 if not ok then prerr_endline "exception raised!";
62 indent := String.sub !indent 0 (String.length !indent -1)
64 Invalid_argument _ -> indent := "??"; ()
70 let debug_print s = if debug then prerr_endline (Lazy.force s)
74 let rec debug_aux t i =
76 let module U = UriManager in
77 CicPp.ppobj (C.Variable ("DEBUG", None, t, [], [])) ^ "\n" ^ i
80 debug_print (lazy (s ^ "\n" ^ List.fold_right debug_aux (t::env) ""))
83 module type Strategy =
88 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
90 reduce: (config -> config) ->
91 unwind: (config -> Cic.term) ->
94 reduce: (config -> config) ->
95 unwind: (config -> Cic.term) ->
97 val from_stack : stack_term -> config
98 val from_stack_list_for_unwind :
99 unwind: (config -> Cic.term) ->
100 stack_term list -> Cic.term list
101 val from_env : env_term -> config
102 val from_env_for_unwind :
103 unwind: (config -> Cic.term) ->
105 val from_ens : ens_term -> config
106 val from_ens_for_unwind :
107 unwind: (config -> Cic.term) ->
110 reduce: (config -> config) ->
111 unwind: (config -> Cic.term) ->
112 stack_term -> env_term
114 reduce: (config -> config) ->
115 unwind: (config -> Cic.term) ->
116 int -> env_term list -> ens_term Cic.explicit_named_substitution ->
118 val compute_to_stack :
119 reduce: (config -> config) ->
120 unwind: (config -> Cic.term) ->
125 module CallByValueByNameForUnwind =
127 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
128 and stack_term = config
129 and env_term = config * config (* cbv, cbn *)
130 and ens_term = config * config (* cbv, cbn *)
134 let from_stack config = config
135 let from_stack_list_for_unwind ~unwind l = List.map unwind l
136 let from_env (c,_) = c
137 let from_ens (c,_) = c
138 let from_env_for_unwind ~unwind (_,c) = unwind c
139 let from_ens_for_unwind ~unwind (_,c) = unwind c
140 let stack_to_env ~reduce ~unwind config = reduce config, (0,[],[],unwind config,[])
141 let compute_to_env ~reduce ~unwind k e ens t = (k,e,ens,t,[]), (k,e,ens,t,[])
142 let compute_to_stack ~reduce ~unwind config = config
146 module CallByValueByNameForUnwind' =
148 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
149 and stack_term = config lazy_t * Cic.term lazy_t (* cbv, cbn *)
150 and env_term = config lazy_t * Cic.term lazy_t (* cbv, cbn *)
151 and ens_term = config lazy_t * Cic.term lazy_t (* cbv, cbn *)
153 let to_env ~reduce ~unwind c = lazy (reduce c),lazy (unwind c)
154 let to_ens ~reduce ~unwind c = lazy (reduce c),lazy (unwind c)
155 let from_stack (c,_) = Lazy.force c
156 let from_stack_list_for_unwind ~unwind l = List.map (function (_,c) -> Lazy.force c) l
157 let from_env (c,_) = Lazy.force c
158 let from_ens (c,_) = Lazy.force c
159 let from_env_for_unwind ~unwind (_,c) = Lazy.force c
160 let from_ens_for_unwind ~unwind (_,c) = Lazy.force c
161 let stack_to_env ~reduce ~unwind config = config
162 let compute_to_env ~reduce ~unwind k e ens t =
163 lazy (reduce (k,e,ens,t,[])), lazy (unwind (k,e,ens,t,[]))
164 let compute_to_stack ~reduce ~unwind config = lazy (reduce config), lazy (unwind config)
170 module CallByNameStrategy =
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 = unwind k e ens t
186 let compute_to_env ~reduce ~unwind k e ens t = unwind k e ens t
191 module CallByNameStrategy =
193 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
194 and stack_term = config
195 and env_term = config
196 and ens_term = config
200 let from_stack config = config
201 let from_stack_list_for_unwind ~unwind l = List.map unwind l
204 let from_env_for_unwind ~unwind c = unwind c
205 let from_ens_for_unwind ~unwind c = unwind c
206 let stack_to_env ~reduce ~unwind config = 0,[],[],unwind config,[]
207 let compute_to_env ~reduce ~unwind k e ens t = k,e,ens,t,[]
208 let compute_to_stack ~reduce ~unwind config = config
212 module CallByValueStrategy =
214 type stack_term = Cic.term
215 type env_term = Cic.term
216 type ens_term = Cic.term
217 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
220 let from_stack ~unwind v = v
221 let from_stack_list ~unwind l = l
224 let from_env_for_unwind ~unwind v = v
225 let from_ens_for_unwind ~unwind v = v
226 let stack_to_env ~reduce ~unwind v = v
227 let compute_to_stack ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
228 let compute_to_env ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
232 module CallByValueStrategyByNameOnConstants =
234 type stack_term = Cic.term
235 type env_term = Cic.term
236 type ens_term = Cic.term
237 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
240 let from_stack ~unwind v = v
241 let from_stack_list ~unwind l = l
244 let from_env_for_unwind ~unwind v = v
245 let from_ens_for_unwind ~unwind v = v
246 let stack_to_env ~reduce ~unwind v = v
247 let compute_to_stack ~reduce ~unwind k e ens =
249 Cic.Const _ as t -> unwind k e ens t
250 | t -> reduce (k,e,ens,t,[])
251 let compute_to_env ~reduce ~unwind k e ens =
253 Cic.Const _ as t -> unwind k e ens t
254 | t -> reduce (k,e,ens,t,[])
258 module LazyCallByValueStrategy =
260 type stack_term = Cic.term lazy_t
261 type env_term = Cic.term lazy_t
262 type ens_term = Cic.term lazy_t
263 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
264 let to_env v = lazy v
265 let to_ens v = lazy v
266 let from_stack ~unwind v = Lazy.force v
267 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
268 let from_env v = Lazy.force v
269 let from_ens v = Lazy.force v
270 let from_env_for_unwind ~unwind v = Lazy.force v
271 let from_ens_for_unwind ~unwind v = Lazy.force v
272 let stack_to_env ~reduce ~unwind v = v
273 let compute_to_stack ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
274 let compute_to_env ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
278 module LazyCallByValueStrategyByNameOnConstants =
280 type stack_term = Cic.term lazy_t
281 type env_term = Cic.term lazy_t
282 type ens_term = Cic.term lazy_t
283 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
284 let to_env v = lazy v
285 let to_ens v = lazy v
286 let from_stack ~unwind v = Lazy.force v
287 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
288 let from_env v = Lazy.force v
289 let from_ens v = Lazy.force v
290 let from_env_for_unwind ~unwind v = Lazy.force v
291 let from_ens_for_unwind ~unwind v = Lazy.force v
292 let stack_to_env ~reduce ~unwind v = v
293 let compute_to_stack ~reduce ~unwind k e ens t =
296 Cic.Const _ as t -> unwind k e ens t
297 | t -> reduce (k,e,ens,t,[]))
298 let compute_to_env ~reduce ~unwind k e ens t =
301 Cic.Const _ as t -> unwind k e ens t
302 | t -> reduce (k,e,ens,t,[]))
306 module LazyCallByNameStrategy =
308 type stack_term = Cic.term lazy_t
309 type env_term = Cic.term lazy_t
310 type ens_term = Cic.term lazy_t
311 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
312 let to_env v = lazy v
313 let to_ens v = lazy v
314 let from_stack ~unwind v = Lazy.force v
315 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
316 let from_env v = Lazy.force v
317 let from_ens v = Lazy.force v
318 let from_env_for_unwind ~unwind v = Lazy.force v
319 let from_ens_for_unwind ~unwind v = Lazy.force v
320 let stack_to_env ~reduce ~unwind v = v
321 let compute_to_stack ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
322 let compute_to_env ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
327 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns
330 type stack_term = reduce:bool -> Cic.term
331 type env_term = reduce:bool -> Cic.term
332 type ens_term = reduce:bool -> Cic.term
333 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
335 let value = lazy v in
336 fun ~reduce -> Lazy.force value
338 let value = lazy v in
339 fun ~reduce -> Lazy.force value
340 let from_stack ~unwind v = (v ~reduce:false)
341 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
342 let from_env v = (v ~reduce:true)
343 let from_ens v = (v ~reduce:true)
344 let from_env_for_unwind ~unwind v = (v ~reduce:true)
345 let from_ens_for_unwind ~unwind v = (v ~reduce:true)
346 let stack_to_env ~reduce ~unwind v = v
347 let compute_to_stack ~reduce ~unwind k e ens t =
351 Cic.Const _ as t -> unwind k e ens t
352 | t -> reduce (k,e,ens,t,[])
355 lazy (unwind k e ens t)
358 if reduce then Lazy.force svalue else Lazy.force lvalue
359 let compute_to_env ~reduce ~unwind k e ens t =
363 Cic.Const _ as t -> unwind k e ens t
364 | t -> reduce (k,e,ens,t,[])
367 lazy (unwind k e ens t)
370 if reduce then Lazy.force svalue else Lazy.force lvalue
374 module ClosuresOnStackByValueFromEnvOrEnsStrategy =
376 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
377 and stack_term = config
378 and env_term = config
379 and ens_term = config
381 let to_env config = config
382 let to_ens config = config
383 let from_stack config = config
384 let from_stack_list_for_unwind ~unwind l = List.map unwind l
387 let from_env_for_unwind ~unwind config = unwind config
388 let from_ens_for_unwind ~unwind config = unwind config
389 let stack_to_env ~reduce ~unwind config = reduce config
390 let compute_to_env ~reduce ~unwind k e ens t = (k,e,ens,t,[])
391 let compute_to_stack ~reduce ~unwind config = config
395 module ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy =
398 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
399 type env_term = Cic.term
400 type ens_term = Cic.term
401 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
404 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
405 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
408 let from_env_for_unwind ~unwind v = v
409 let from_ens_for_unwind ~unwind v = v
410 let stack_to_env ~reduce ~unwind (k,e,ens,t) =
412 Cic.Const _ as t -> unwind k e ens t
413 | t -> reduce (k,e,ens,t,[])
414 let compute_to_env ~reduce ~unwind k e ens t =
416 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
420 module Reduction(RS : Strategy) =
422 type env = RS.env_term list
423 type ens = RS.ens_term Cic.explicit_named_substitution
424 type stack = RS.stack_term list
425 type config = int * env * ens * Cic.term * stack
427 (* k is the length of the environment e *)
428 (* m is the current depth inside the term *)
429 let rec unwind' m k e ens t =
430 let module C = Cic in
431 let module S = CicSubstitution in
432 if k = 0 && ens = [] then
435 let rec unwind_aux m =
438 if n <= m then t else
441 Some (RS.from_env_for_unwind ~unwind (List.nth e (n-m-1)))
442 with Failure _ -> None
446 if m = 0 then t' else S.lift m t'
447 | None -> C.Rel (n-k)
449 | C.Var (uri,exp_named_subst) ->
451 debug_print (lazy ("%%%%%UWVAR " ^ String.concat " ; " (List.map (function (uri,t) -> UriManager.string_of_uri uri ^ " := " ^ CicPp.ppterm t) ens))) ;
453 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
454 CicSubstitution.lift m (RS.from_ens_for_unwind ~unwind (List.assq uri ens))
458 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
461 C.Constant _ -> raise ReferenceToConstant
462 | C.Variable (_,_,_,params,_) -> params
463 | C.CurrentProof _ -> raise ReferenceToCurrentProof
464 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
467 let exp_named_subst' =
468 substaux_in_exp_named_subst params exp_named_subst m
470 C.Var (uri,exp_named_subst')
476 | Some t -> Some (unwind_aux m t)
481 | C.Implicit _ as t -> t
482 | C.Cast (te,ty) -> C.Cast (unwind_aux m te, unwind_aux m ty) (*CSC ???*)
483 | C.Prod (n,s,t) -> C.Prod (n, unwind_aux m s, unwind_aux (m + 1) t)
484 | C.Lambda (n,s,t) -> C.Lambda (n, unwind_aux m s, unwind_aux (m + 1) t)
485 | C.LetIn (n,s,ty,t) ->
486 C.LetIn (n, unwind_aux m s, unwind_aux m ty, unwind_aux (m + 1) t)
487 | C.Appl l -> C.Appl (List.map (unwind_aux m) l)
488 | C.Const (uri,exp_named_subst) ->
491 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
494 C.Constant (_,_,_,params,_) -> params
495 | C.Variable _ -> raise ReferenceToVariable
496 | C.CurrentProof (_,_,_,_,params,_) -> params
497 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
500 let exp_named_subst' =
501 substaux_in_exp_named_subst params exp_named_subst m
503 C.Const (uri,exp_named_subst')
504 | C.MutInd (uri,i,exp_named_subst) ->
507 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
510 C.Constant _ -> raise ReferenceToConstant
511 | C.Variable _ -> raise ReferenceToVariable
512 | C.CurrentProof _ -> raise ReferenceToCurrentProof
513 | C.InductiveDefinition (_,params,_,_) -> params
516 let exp_named_subst' =
517 substaux_in_exp_named_subst params exp_named_subst m
519 C.MutInd (uri,i,exp_named_subst')
520 | C.MutConstruct (uri,i,j,exp_named_subst) ->
523 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
526 C.Constant _ -> raise ReferenceToConstant
527 | C.Variable _ -> raise ReferenceToVariable
528 | C.CurrentProof _ -> raise ReferenceToCurrentProof
529 | C.InductiveDefinition (_,params,_,_) -> params
532 let exp_named_subst' =
533 substaux_in_exp_named_subst params exp_named_subst m
535 C.MutConstruct (uri,i,j,exp_named_subst')
536 | C.MutCase (sp,i,outt,t,pl) ->
537 C.MutCase (sp,i,unwind_aux m outt, unwind_aux m t,
538 List.map (unwind_aux m) pl)
540 let len = List.length fl in
543 (fun (name,i,ty,bo) ->
544 (name, i, unwind_aux m ty, unwind_aux (m+len) bo))
547 C.Fix (i, substitutedfl)
549 let len = List.length fl in
552 (fun (name,ty,bo) -> (name, unwind_aux m ty, unwind_aux (m+len) bo))
555 C.CoFix (i, substitutedfl)
556 and substaux_in_exp_named_subst params exp_named_subst' m =
557 (*CSC: codice copiato e modificato dalla cicSubstitution.subst_vars *)
558 let rec filter_and_lift already_instantiated =
563 (function (uri',_)-> not (UriManager.eq uri uri')) exp_named_subst'
565 not (List.mem uri already_instantiated)
569 (uri,CicSubstitution.lift m (RS.from_ens_for_unwind ~unwind t)) ::
570 (filter_and_lift (uri::already_instantiated) tl)
571 | _::tl -> filter_and_lift already_instantiated tl
574 List.map (function (uri,t) -> uri, unwind_aux m t) exp_named_subst' @
575 (filter_and_lift [] (List.rev ens))
583 [uri,List.assoc uri res]
593 and unwind (k,e,ens,t,s) =
594 let t' = unwind' 0 k e ens t in
595 if s = [] then t' else Cic.Appl (t'::(RS.from_stack_list_for_unwind ~unwind s))
600 let profiler_unwind = HExtlib.profile ~enable:profile "are_convertible.unwind" in
602 profiler_unwind.HExtlib.profile (unwind k e ens) t
606 let reduce ~delta ?(subst = []) context : config -> config =
607 let module C = Cic in
608 let module S = CicSubstitution in
611 (k, e, _, C.Rel n, s) as config ->
613 if not delta then None
616 Some (RS.from_env (List.nth e (n-1)))
621 match List.nth context (n - 1 - k) with
623 | Some (_,C.Decl _) -> None
624 | Some (_,C.Def (x,_)) -> Some (0,[],[],S.lift (n - k) x,[])
630 Some (k',e',ens',t',s') -> reduce (k',e',ens',t',s'@s)
632 | (k, e, ens, C.Var (uri,exp_named_subst), s) as config ->
633 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
634 let (k',e',ens',t',s') = RS.from_ens (List.assq uri ens) in
635 reduce (k',e',ens',t',s'@s)
638 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
641 C.Constant _ -> raise ReferenceToConstant
642 | C.CurrentProof _ -> raise ReferenceToCurrentProof
643 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
644 | C.Variable (_,None,_,_,_) -> config
645 | C.Variable (_,Some body,_,_,_) ->
646 let ens' = push_exp_named_subst k e ens exp_named_subst in
647 reduce (0, [], ens', body, s)
649 | (k, e, ens, C.Meta (n,l), s) as config ->
651 let (_, term,_) = CicUtil.lookup_subst n subst in
652 reduce (k, e, ens,CicSubstitution.subst_meta l term,s)
653 with CicUtil.Subst_not_found _ -> config)
654 | (_, _, _, C.Sort _, _)
655 | (_, _, _, C.Implicit _, _) as config -> config
656 | (k, e, ens, C.Cast (te,ty), s) ->
657 reduce (k, e, ens, te, s)
658 | (_, _, _, C.Prod _, _) as config -> config
659 | (_, _, _, C.Lambda _, []) as config -> config
660 | (k, e, ens, C.Lambda (_,_,t), p::s) ->
661 reduce (k+1, (RS.stack_to_env ~reduce ~unwind p)::e, ens, t,s)
662 | (k, e, ens, C.LetIn (_,m,_,t), s) ->
663 let m' = RS.compute_to_env ~reduce ~unwind k e ens m in
664 reduce (k+1, m'::e, ens, t, s)
665 | (_, _, _, C.Appl [], _) -> assert false
666 | (k, e, ens, C.Appl (he::tl), s) ->
669 (function t -> RS.compute_to_stack ~reduce ~unwind (k,e,ens,t,[])) tl
671 reduce (k, e, ens, he, (List.append tl') s)
672 | (_, _, _, C.Const _, _) as config when delta=false-> config
673 | (k, e, ens, C.Const (uri,exp_named_subst), s) as config ->
675 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
678 C.Constant (_,Some body,_,_,_) ->
679 let ens' = push_exp_named_subst k e ens exp_named_subst in
680 (* constants are closed *)
681 reduce (0, [], ens', body, s)
682 | C.Constant (_,None,_,_,_) -> config
683 | C.Variable _ -> raise ReferenceToVariable
684 | C.CurrentProof (_,_,body,_,_,_) ->
685 let ens' = push_exp_named_subst k e ens exp_named_subst in
686 (* constants are closed *)
687 reduce (0, [], ens', body, s)
688 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
690 | (_, _, _, C.MutInd _, _)
691 | (_, _, _, C.MutConstruct _, _) as config -> config
692 | (k, e, ens, C.MutCase (mutind,i,outty,term,pl),s) as config ->
695 (k, e, ens, C.CoFix (i,fl), s) ->
696 let (_,_,body) = List.nth fl i in
698 let counter = ref (List.length fl) in
700 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
704 reduce (k,e,ens,body',s)
707 (match decofix (reduce (k,e,ens,term,[])) with
708 (k', e', ens', C.MutConstruct (_,_,j,_), []) ->
709 reduce (k, e, ens, (List.nth pl (j-1)), s)
710 | (k', e', ens', C.MutConstruct (_,_,j,_), s') ->
713 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph mutind
716 C.InductiveDefinition (_,_,r,_) -> r
717 | _ -> raise WrongUriToInductiveDefinition
720 let num_to_eat = r in
724 | (n,he::s) when n > 0 -> eat_first (n - 1, s)
725 | _ -> raise (Impossible 5)
727 eat_first (num_to_eat,s')
729 reduce (k, e, ens, (List.nth pl (j-1)), ts@s)
730 | (_, _, _, C.Cast _, _)
731 | (_, _, _, C.Implicit _, _) ->
732 raise (Impossible 2) (* we don't trust our whd ;-) *)
734 (*CSC: here I am unwinding the configuration and for sure I
735 will do it twice; to avoid this unwinding I should push the
736 "match [] with _" continuation on the stack;
737 another possibility is to just return the original configuration,
738 partially undoing the weak-head computation *)
739 (*this code is uncorrect since term' lives in e' <> e
740 let term' = unwind config' in
741 (k, e, ens, C.MutCase (mutind,i,outty,term',pl),s)
744 | (k, e, ens, C.Fix (i,fl), s) as config ->
745 let (_,recindex,_,body) = List.nth fl i in
748 Some (RS.from_stack (List.nth s recindex))
754 (match reduce recparam with
755 (_,_,_,C.MutConstruct _,_) as config ->
756 let leng = List.length fl in
758 let counter = ref 0 in
759 let rec build_env e' =
760 if !counter = leng then e'
764 ((RS.to_env ~reduce ~unwind (k,e,ens,C.Fix (!counter -1, fl),[]))::e'))
768 let rec replace i s t =
771 | n,he::tl -> he::(replace (n - 1) tl t)
772 | _,_ -> assert false in
774 replace recindex s (RS.compute_to_stack ~reduce ~unwind config)
776 reduce (k+leng, new_env, ens, body, new_s)
780 | (_,_,_,C.CoFix _,_) as config -> config
781 and push_exp_named_subst k e ens =
785 push_exp_named_subst k e ((uri,RS.to_ens ~reduce ~unwind (k,e,ens,t,[]))::ens) tl
790 let whd ?(delta=true) ?(subst=[]) context t =
791 unwind (reduce ~delta ~subst context (0, [], [], t, []))
798 (* ROTTO = rompe l'unificazione poiche' riduce gli argomenti di un'applicazione
799 senza ridurre la testa
800 module R = Reduction CallByNameStrategy;; OK 56.368s
801 module R = Reduction CallByValueStrategy;; ROTTO
802 module R = Reduction CallByValueStrategyByNameOnConstants;; ROTTO
803 module R = Reduction LazyCallByValueStrategy;; ROTTO
804 module R = Reduction LazyCallByValueStrategyByNameOnConstants;; ROTTO
805 module R = Reduction LazyCallByNameStrategy;; OK 0m56.398s
807 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns;;
809 module R = Reduction ClosuresOnStackByValueFromEnvOrEnsStrategy;; OK 58.583s
811 ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy;; OK 58.094s
812 module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);; OK 58.127s
814 (*module R = Reduction(CallByValueByNameForUnwind);;*)
815 module RS = CallByValueByNameForUnwind';;
816 (*module R = Reduction(CallByNameStrategy);;*)
817 (*module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);;*)
818 module R = Reduction(RS);;
819 module U = UriManager;;
825 let profiler_whd = HExtlib.profile ~enable:profile "are_convertible.whd" in
826 fun ?(delta=true) ?(subst=[]) context t ->
827 profiler_whd.HExtlib.profile (whd ~delta ~subst context) t
830 (* mimic ocaml (<< 3.08) "=" behaviour. Tests physical equality first then
831 * fallbacks to structural equality *)
833 Pervasives.compare x y = 0
835 (* t1, t2 must be well-typed *)
836 let are_convertible whd ?(subst=[]) ?(metasenv=[]) =
837 let heuristic = ref true in
838 let rec aux test_equality_only context t1 t2 ugraph =
839 (*D*)inside 'B'; try let rc =
840 pp (lazy (CicPp.ppterm t1 ^ " vs " ^ CicPp.ppterm t2));
841 let rec aux2 test_equality_only t1 t2 ugraph =
843 (* this trivial euristic cuts down the total time of about five times ;-) *)
844 (* this because most of the time t1 and t2 are "sintactically" the same *)
849 let module C = Cic in
851 (C.Rel n1, C.Rel n2) -> (n1 = n2),ugraph
852 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
853 if U.eq uri1 uri2 then
856 (fun (uri1,x) (uri2,y) (b,ugraph) ->
857 let b',ugraph' = aux test_equality_only context x y ugraph in
858 (U.eq uri1 uri2 && b' && b),ugraph'
859 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
861 Invalid_argument _ -> false,ugraph
865 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
868 let l1 = CicUtil.clean_up_local_context subst metasenv n1 l1 in
869 let l2 = CicUtil.clean_up_local_context subst metasenv n2 l2 in
871 (fun (b,ugraph) t1 t2 ->
875 | _,None -> true,ugraph
876 | Some t1',Some t2' ->
877 aux test_equality_only context t1' t2' ugraph
880 ) (true,ugraph) l1 l2
882 if b2 then true,ugraph1 else false,ugraph
885 | C.Meta (n1,l1), _ ->
887 let _,term,_ = CicUtil.lookup_subst n1 subst in
888 let term' = CicSubstitution.subst_meta l1 term in
890 prerr_endline ("%?: " ^ CicPp.ppterm t1 ^ " <==> " ^ CicPp.ppterm t2);
891 prerr_endline ("%%%%%%: " ^ CicPp.ppterm term' ^ " <==> " ^ CicPp.ppterm t2);
893 aux test_equality_only context term' t2 ugraph
894 with CicUtil.Subst_not_found _ -> false,ugraph)
895 | _, C.Meta (n2,l2) ->
897 let _,term,_ = CicUtil.lookup_subst n2 subst in
898 let term' = CicSubstitution.subst_meta l2 term in
900 prerr_endline ("%?: " ^ CicPp.ppterm t1 ^ " <==> " ^ CicPp.ppterm t2);
901 prerr_endline ("%%%%%%: " ^ CicPp.ppterm term' ^ " <==> " ^ CicPp.ppterm t1);
903 aux test_equality_only context t1 term' ugraph
904 with CicUtil.Subst_not_found _ -> false,ugraph)
905 | (C.Sort (C.CProp t1|C.Type t1), C.Sort (C.CProp t2|C.Type t2))
906 when test_equality_only ->
907 (try true,(CicUniv.add_eq t2 t1 ugraph)
908 with CicUniv.UniverseInconsistency _ -> false,ugraph)
909 | (C.Sort (C.CProp t1|C.Type t1), C.Sort (C.CProp t2|C.Type t2))
910 when not test_equality_only ->
911 (try true,(CicUniv.add_ge t2 t1 ugraph)
912 with CicUniv.UniverseInconsistency _ -> false,ugraph)
913 | (C.Sort s1, C.Sort (C.Type _))
914 | (C.Sort s1, C.Sort (C.CProp _)) -> (not test_equality_only),ugraph
915 | (C.Sort s1, C.Sort s2) -> (s1 = s2),ugraph
916 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
917 let b',ugraph' = aux true context s1 s2 ugraph in
919 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
923 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
924 let b',ugraph' = aux true context s1 s2 ugraph in
926 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
930 | (C.LetIn (name1,s1,ty1,t1), C.LetIn(_,s2,ty2,t2)) ->
931 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
933 let b',ugraph = aux test_equality_only context ty1 ty2 ugraph in
935 aux test_equality_only
936 ((Some (name1, (C.Def (s1,ty1))))::context) t1 t2 ugraph'
941 | (C.Appl l1, C.Appl l2) ->
943 aux test_equality_only context (List.hd l1) (List.hd l2) ugraph
945 if not b then false, ugraph
949 (fun x y (b,ugraph) ->
951 aux true context x y ugraph
953 false,ugraph) (List.tl l1) (List.tl l2) (true,ugraph)
955 Invalid_argument _ -> false,ugraph
957 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
958 let b' = U.eq uri1 uri2 in
962 (fun (uri1,x) (uri2,y) (b,ugraph) ->
963 if b && U.eq uri1 uri2 then
964 aux test_equality_only context x y ugraph
967 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
969 Invalid_argument _ -> false,ugraph
973 | (C.MutInd (uri1,i1,exp_named_subst1),
974 C.MutInd (uri2,i2,exp_named_subst2)
976 let b' = U.eq uri1 uri2 && i1 = i2 in
980 (fun (uri1,x) (uri2,y) (b,ugraph) ->
981 if b && U.eq uri1 uri2 then
982 aux test_equality_only context x y ugraph
985 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
987 Invalid_argument _ -> false,ugraph
991 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
992 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
994 let b' = U.eq uri1 uri2 && i1 = i2 && j1 = j2 in
998 (fun (uri1,x) (uri2,y) (b,ugraph) ->
999 if b && U.eq uri1 uri2 then
1000 aux test_equality_only context x y ugraph
1003 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
1005 Invalid_argument _ -> false,ugraph
1009 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
1010 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
1011 let b' = U.eq uri1 uri2 && i1 = i2 in
1013 let b'',ugraph''=aux test_equality_only context
1014 outtype1 outtype2 ugraph in
1016 let b''',ugraph'''= aux true context
1017 term1 term2 ugraph'' in
1019 (fun x y (b,ugraph) ->
1021 aux test_equality_only context x y ugraph
1024 pl1 pl2 (b''',ugraph''')
1029 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
1032 (fun (types,len) (n,_,ty,_) ->
1033 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1039 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) (b,ugraph) ->
1040 if b && recindex1 = recindex2 then
1041 let b',ugraph' = aux test_equality_only context ty1 ty2
1044 aux test_equality_only (tys@context) bo1 bo2 ugraph'
1049 fl1 fl2 (true,ugraph)
1052 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
1055 (fun (types,len) (n,ty,_) ->
1056 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1062 (fun (_,ty1,bo1) (_,ty2,bo2) (b,ugraph) ->
1064 let b',ugraph' = aux test_equality_only context ty1 ty2
1067 aux test_equality_only (tys@context) bo1 bo2 ugraph'
1072 fl1 fl2 (true,ugraph)
1075 | C.Cast (bo,_),t -> aux2 test_equality_only bo t ugraph
1076 | t,C.Cast (bo,_) -> aux2 test_equality_only t bo ugraph
1077 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
1078 | (_,_) -> false,ugraph
1083 aux2 test_equality_only t1 t2 ugraph
1087 if fst res = true then
1091 (*if !heuristic then prerr_endline ("NON FACILE: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);*)
1092 (* heuristic := false; *)
1093 debug t1 [t2] "PREWHD";
1094 (*prerr_endline ("PREWHD: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);*)
1096 prerr_endline ("PREWHD: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);
1097 let t1' = whd ?delta:(Some true) ?subst:(Some subst) context t1 in
1098 let t2' = whd ?delta:(Some true) ?subst:(Some subst) context t2 in
1099 debug t1' [t2'] "POSTWHD";
1101 let rec convert_machines test_equality_only ugraph =
1104 | ((k1,env1,ens1,h1,s1),(k2,env2,ens2,h2,s2))::tl ->
1105 let (b,ugraph) as res =
1106 aux2 test_equality_only
1107 (R.unwind (k1,env1,ens1,h1,[])) (R.unwind (k2,env2,ens2,h2,[])) ugraph
1115 (fun si-> R.reduce ~delta:false ~subst context(RS.from_stack si))
1118 (fun si-> R.reduce ~delta:false ~subst context(RS.from_stack si))
1122 Invalid_argument _ -> None
1125 None -> false,ugraph
1126 | Some problems -> convert_machines true ugraph problems
1130 convert_machines test_equality_only ugraph
1131 [R.reduce ~delta:true ~subst context (0,[],[],t1,[]),
1132 R.reduce ~delta:true ~subst context (0,[],[],t2,[])]
1133 (*prerr_endline ("POSTWH: " ^ CicPp.ppterm t1' ^ " <===> " ^ CicPp.ppterm t2');*)
1135 aux2 test_equality_only t1' t2' ugraph
1138 (*D*)in outside true; rc with exc -> outside false; raise exc
1140 aux false (*c t1 t2 ugraph *)
1144 let whd ?(delta=true) ?(subst=[]) context t =
1145 let res = whd ~delta ~subst context t in
1146 let rescsc = CicReductionNaif.whd ~delta ~subst context t in
1147 if not (fst (are_convertible CicReductionNaif.whd ~subst context res rescsc CicUniv.empty_ugraph)) then
1149 debug_print (lazy ("PRIMA: " ^ CicPp.ppterm t)) ;
1151 debug_print (lazy ("DOPO: " ^ CicPp.ppterm res)) ;
1153 debug_print (lazy ("CSC: " ^ CicPp.ppterm rescsc)) ;
1156 let _ = are_convertible CicReductionNaif.whd ~subst context res rescsc CicUniv.empty_ugraph in
1164 let are_convertible = are_convertible whd
1169 let profiler_other_whd = HExtlib.profile ~enable:profile "~are_convertible.whd"
1170 let whd ?(delta=true) ?(subst=[]) context t =
1172 whd ~delta ~subst context t
1174 profiler_other_whd.HExtlib.profile foo ()
1177 let rec normalize ?(delta=true) ?(subst=[]) ctx term =
1178 let module C = Cic in
1179 let t = whd ~delta ~subst ctx term in
1180 let aux = normalize ~delta ~subst in
1181 let decl name t = Some (name, C.Decl t) in
1184 | C.Var (uri,exp_named_subst) ->
1185 C.Var (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1187 C.Meta (i,List.map (function Some t -> Some (aux ctx t) | None -> None) l)
1190 | C.Cast (te,ty) -> C.Cast (aux ctx te, aux ctx ty)
1192 let s' = aux ctx s in
1193 C.Prod (n, s', aux ((decl n s')::ctx) t)
1194 | C.Lambda (n,s,t) ->
1195 let s' = aux ctx s in
1196 C.Lambda (n, s', aux ((decl n s')::ctx) t)
1197 | C.LetIn (n,s,_,t) ->
1198 (* the term is already in weak head normal form *)
1200 | C.Appl (h::l) -> C.Appl (h::(List.map (aux ctx) l))
1201 | C.Appl [] -> assert false
1202 | C.Const (uri,exp_named_subst) ->
1203 C.Const (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1204 | C.MutInd (uri,typeno,exp_named_subst) ->
1205 C.MutInd (uri,typeno, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1206 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
1207 C.MutConstruct (uri, typeno, consno,
1208 List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1209 | C.MutCase (sp,i,outt,t,pl) ->
1210 C.MutCase (sp,i, aux ctx outt, aux ctx t, List.map (aux ctx) pl)
1211 (*CSC: to be completed, I suppose *)
1215 let normalize ?delta ?subst ctx term =
1216 (* prerr_endline ("NORMALIZE:" ^ CicPp.ppterm term); *)
1217 let t = normalize ?delta ?subst ctx term in
1218 (* prerr_endline ("NORMALIZED:" ^ CicPp.ppterm t); *)
1222 (* performs an head beta/cast reduction *)
1223 let rec head_beta_reduce ?(delta=false) ?(upto=(-1)) t =
1228 (Cic.Appl (Cic.Lambda (_,_,t)::he'::tl')) ->
1229 let he'' = CicSubstitution.subst he' t in
1235 Cic.Appl l -> Cic.Appl (l@tl')
1236 | _ -> Cic.Appl (he''::tl')
1238 head_beta_reduce ~delta ~upto:(upto - 1) he'''
1239 | Cic.Cast (te,_) -> head_beta_reduce ~delta ~upto te
1240 | Cic.Appl (Cic.Const (uri,ens)::tl) as t when delta=true ->
1242 match fst (CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri) with
1243 Cic.Constant (_,bo,_,_,_) -> bo
1244 | Cic.Variable _ -> raise ReferenceToVariable
1245 | Cic.CurrentProof (_,_,bo,_,_,_) -> Some bo
1246 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
1251 head_beta_reduce ~upto
1252 ~delta (Cic.Appl ((CicSubstitution.subst_vars ens bo)::tl)))
1253 | Cic.Const (uri,ens) as t when delta=true ->
1255 match fst (CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri) with
1256 Cic.Constant (_,bo,_,_,_) -> bo
1257 | Cic.Variable _ -> raise ReferenceToVariable
1258 | Cic.CurrentProof (_,_,bo,_,_,_) -> Some bo
1259 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
1264 head_beta_reduce ~delta ~upto (CicSubstitution.subst_vars ens bo))
1268 let are_convertible ?subst ?metasenv context t1 t2 ugraph =
1269 let before = Unix.gettimeofday () in
1270 let res = are_convertible ?subst ?metasenv context t1 t2 ugraph in
1271 let after = Unix.gettimeofday () in
1272 let diff = after -. before in
1275 let nc = List.map (function None -> None | Some (n,_) -> Some n) context in
1277 ("\n#(" ^ string_of_float diff ^ "):\n" ^ CicPp.pp t1 nc ^ "\n<=>\n" ^ CicPp.pp t2 nc);