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 =
57 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
59 reduce: (config -> config) ->
60 unwind: (config -> Cic.term) ->
63 reduce: (config -> config) ->
64 unwind: (config -> Cic.term) ->
66 val from_stack : stack_term -> config
67 val from_stack_list_for_unwind :
68 unwind: (config -> Cic.term) ->
69 stack_term list -> Cic.term list
70 val from_env : env_term -> config
71 val from_env_for_unwind :
72 unwind: (config -> Cic.term) ->
74 val from_ens : ens_term -> config
75 val from_ens_for_unwind :
76 unwind: (config -> Cic.term) ->
79 reduce: (config -> config) ->
80 unwind: (config -> Cic.term) ->
81 stack_term -> env_term
83 reduce: (config -> config) ->
84 unwind: (config -> Cic.term) ->
85 int -> env_term list -> ens_term Cic.explicit_named_substitution ->
87 val compute_to_stack :
88 reduce: (config -> config) ->
89 unwind: (config -> Cic.term) ->
94 module CallByValueByNameForUnwind =
96 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
97 and stack_term = config
98 and env_term = config * config (* cbv, cbn *)
99 and ens_term = config * config (* cbv, cbn *)
103 let from_stack config = config
104 let from_stack_list_for_unwind ~unwind l = List.map unwind l
105 let from_env (c,_) = c
106 let from_ens (c,_) = c
107 let from_env_for_unwind ~unwind (_,c) = unwind c
108 let from_ens_for_unwind ~unwind (_,c) = unwind c
109 let stack_to_env ~reduce ~unwind config = reduce config, (0,[],[],unwind config,[])
110 let compute_to_env ~reduce ~unwind k e ens t = (k,e,ens,t,[]), (k,e,ens,t,[])
111 let compute_to_stack ~reduce ~unwind config = config
115 module CallByValueByNameForUnwind' =
117 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
118 and stack_term = config lazy_t * Cic.term lazy_t (* cbv, cbn *)
119 and env_term = config lazy_t * Cic.term lazy_t (* cbv, cbn *)
120 and ens_term = config lazy_t * Cic.term lazy_t (* cbv, cbn *)
122 let to_env ~reduce ~unwind c = lazy (reduce c),lazy (unwind c)
123 let to_ens ~reduce ~unwind c = lazy (reduce c),lazy (unwind c)
124 let from_stack (c,_) = Lazy.force c
125 let from_stack_list_for_unwind ~unwind l = List.map (function (_,c) -> Lazy.force c) l
126 let from_env (c,_) = Lazy.force c
127 let from_ens (c,_) = Lazy.force c
128 let from_env_for_unwind ~unwind (_,c) = Lazy.force c
129 let from_ens_for_unwind ~unwind (_,c) = Lazy.force c
130 let stack_to_env ~reduce ~unwind config = config
131 let compute_to_env ~reduce ~unwind k e ens t =
132 lazy (reduce (k,e,ens,t,[])), lazy (unwind (k,e,ens,t,[]))
133 let compute_to_stack ~reduce ~unwind config = lazy (reduce config), lazy (unwind config)
139 module CallByNameStrategy =
141 type stack_term = Cic.term
142 type env_term = Cic.term
143 type ens_term = Cic.term
144 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
147 let from_stack ~unwind v = v
148 let from_stack_list ~unwind l = l
151 let from_env_for_unwind ~unwind v = v
152 let from_ens_for_unwind ~unwind v = v
153 let stack_to_env ~reduce ~unwind v = v
154 let compute_to_stack ~reduce ~unwind k e ens t = unwind k e ens t
155 let compute_to_env ~reduce ~unwind k e ens t = unwind k e ens t
160 module CallByNameStrategy =
162 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
163 and stack_term = config
164 and env_term = config
165 and ens_term = config
169 let from_stack config = config
170 let from_stack_list_for_unwind ~unwind l = List.map unwind l
173 let from_env_for_unwind ~unwind c = unwind c
174 let from_ens_for_unwind ~unwind c = unwind c
175 let stack_to_env ~reduce ~unwind config = 0,[],[],unwind config,[]
176 let compute_to_env ~reduce ~unwind k e ens t = k,e,ens,t,[]
177 let compute_to_stack ~reduce ~unwind config = config
181 module CallByValueStrategy =
183 type stack_term = Cic.term
184 type env_term = Cic.term
185 type ens_term = Cic.term
186 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
189 let from_stack ~unwind v = v
190 let from_stack_list ~unwind l = l
193 let from_env_for_unwind ~unwind v = v
194 let from_ens_for_unwind ~unwind v = v
195 let stack_to_env ~reduce ~unwind v = v
196 let compute_to_stack ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
197 let compute_to_env ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
201 module CallByValueStrategyByNameOnConstants =
203 type stack_term = Cic.term
204 type env_term = Cic.term
205 type ens_term = Cic.term
206 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
209 let from_stack ~unwind v = v
210 let from_stack_list ~unwind l = l
213 let from_env_for_unwind ~unwind v = v
214 let from_ens_for_unwind ~unwind v = v
215 let stack_to_env ~reduce ~unwind v = v
216 let compute_to_stack ~reduce ~unwind k e ens =
218 Cic.Const _ as t -> unwind k e ens t
219 | t -> reduce (k,e,ens,t,[])
220 let compute_to_env ~reduce ~unwind k e ens =
222 Cic.Const _ as t -> unwind k e ens t
223 | t -> reduce (k,e,ens,t,[])
227 module LazyCallByValueStrategy =
229 type stack_term = Cic.term lazy_t
230 type env_term = Cic.term lazy_t
231 type ens_term = Cic.term lazy_t
232 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
233 let to_env v = lazy v
234 let to_ens v = lazy v
235 let from_stack ~unwind v = Lazy.force v
236 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
237 let from_env v = Lazy.force v
238 let from_ens v = Lazy.force v
239 let from_env_for_unwind ~unwind v = Lazy.force v
240 let from_ens_for_unwind ~unwind v = Lazy.force v
241 let stack_to_env ~reduce ~unwind v = v
242 let compute_to_stack ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
243 let compute_to_env ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
247 module LazyCallByValueStrategyByNameOnConstants =
249 type stack_term = Cic.term lazy_t
250 type env_term = Cic.term lazy_t
251 type ens_term = Cic.term lazy_t
252 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
253 let to_env v = lazy v
254 let to_ens v = lazy v
255 let from_stack ~unwind v = Lazy.force v
256 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
257 let from_env v = Lazy.force v
258 let from_ens v = Lazy.force v
259 let from_env_for_unwind ~unwind v = Lazy.force v
260 let from_ens_for_unwind ~unwind v = Lazy.force v
261 let stack_to_env ~reduce ~unwind v = v
262 let compute_to_stack ~reduce ~unwind k e ens t =
265 Cic.Const _ as t -> unwind k e ens t
266 | t -> reduce (k,e,ens,t,[]))
267 let compute_to_env ~reduce ~unwind k e ens t =
270 Cic.Const _ as t -> unwind k e ens t
271 | t -> reduce (k,e,ens,t,[]))
275 module LazyCallByNameStrategy =
277 type stack_term = Cic.term lazy_t
278 type env_term = Cic.term lazy_t
279 type ens_term = Cic.term lazy_t
280 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
281 let to_env v = lazy v
282 let to_ens v = lazy v
283 let from_stack ~unwind v = Lazy.force v
284 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
285 let from_env v = Lazy.force v
286 let from_ens v = Lazy.force v
287 let from_env_for_unwind ~unwind v = Lazy.force v
288 let from_ens_for_unwind ~unwind v = Lazy.force v
289 let stack_to_env ~reduce ~unwind v = v
290 let compute_to_stack ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
291 let compute_to_env ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
296 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns
299 type stack_term = reduce:bool -> Cic.term
300 type env_term = reduce:bool -> Cic.term
301 type ens_term = reduce:bool -> Cic.term
302 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
304 let value = lazy v in
305 fun ~reduce -> Lazy.force value
307 let value = lazy v in
308 fun ~reduce -> Lazy.force value
309 let from_stack ~unwind v = (v ~reduce:false)
310 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
311 let from_env v = (v ~reduce:true)
312 let from_ens v = (v ~reduce:true)
313 let from_env_for_unwind ~unwind v = (v ~reduce:true)
314 let from_ens_for_unwind ~unwind v = (v ~reduce:true)
315 let stack_to_env ~reduce ~unwind v = v
316 let compute_to_stack ~reduce ~unwind k e ens t =
320 Cic.Const _ as t -> unwind k e ens t
321 | t -> reduce (k,e,ens,t,[])
324 lazy (unwind k e ens t)
327 if reduce then Lazy.force svalue else Lazy.force lvalue
328 let compute_to_env ~reduce ~unwind k e ens t =
332 Cic.Const _ as t -> unwind k e ens t
333 | t -> reduce (k,e,ens,t,[])
336 lazy (unwind k e ens t)
339 if reduce then Lazy.force svalue else Lazy.force lvalue
343 module ClosuresOnStackByValueFromEnvOrEnsStrategy =
345 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
346 and stack_term = config
347 and env_term = config
348 and ens_term = config
350 let to_env config = config
351 let to_ens config = config
352 let from_stack config = config
353 let from_stack_list_for_unwind ~unwind l = List.map unwind l
356 let from_env_for_unwind ~unwind config = unwind config
357 let from_ens_for_unwind ~unwind config = unwind config
358 let stack_to_env ~reduce ~unwind config = reduce config
359 let compute_to_env ~reduce ~unwind k e ens t = (k,e,ens,t,[])
360 let compute_to_stack ~reduce ~unwind config = config
364 module ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy =
367 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
368 type env_term = Cic.term
369 type ens_term = Cic.term
370 type config = int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term * stack_term list
373 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
374 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
377 let from_env_for_unwind ~unwind v = v
378 let from_ens_for_unwind ~unwind v = v
379 let stack_to_env ~reduce ~unwind (k,e,ens,t) =
381 Cic.Const _ as t -> unwind k e ens t
382 | t -> reduce (k,e,ens,t,[])
383 let compute_to_env ~reduce ~unwind k e ens t =
385 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
389 module Reduction(RS : Strategy) =
391 type env = RS.env_term list
392 type ens = RS.ens_term Cic.explicit_named_substitution
393 type stack = RS.stack_term list
394 type config = int * env * ens * Cic.term * stack
396 (* k is the length of the environment e *)
397 (* m is the current depth inside the term *)
398 let rec unwind' m k e ens t =
399 let module C = Cic in
400 let module S = CicSubstitution in
401 if k = 0 && ens = [] then
404 let rec unwind_aux m =
407 if n <= m then t else
410 Some (RS.from_env_for_unwind ~unwind (List.nth e (n-m-1)))
411 with Failure _ -> None
415 if m = 0 then t' else S.lift m t'
416 | None -> C.Rel (n-k)
418 | C.Var (uri,exp_named_subst) ->
420 debug_print (lazy ("%%%%%UWVAR " ^ String.concat " ; " (List.map (function (uri,t) -> UriManager.string_of_uri uri ^ " := " ^ CicPp.ppterm t) ens))) ;
422 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
423 CicSubstitution.lift m (RS.from_ens_for_unwind ~unwind (List.assq uri ens))
427 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
430 C.Constant _ -> raise ReferenceToConstant
431 | C.Variable (_,_,_,params,_) -> params
432 | C.CurrentProof _ -> raise ReferenceToCurrentProof
433 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
436 let exp_named_subst' =
437 substaux_in_exp_named_subst params exp_named_subst m
439 C.Var (uri,exp_named_subst')
445 | Some t -> Some (unwind_aux m t)
450 | C.Implicit _ as t -> t
451 | C.Cast (te,ty) -> C.Cast (unwind_aux m te, unwind_aux m ty) (*CSC ???*)
452 | C.Prod (n,s,t) -> C.Prod (n, unwind_aux m s, unwind_aux (m + 1) t)
453 | C.Lambda (n,s,t) -> C.Lambda (n, unwind_aux m s, unwind_aux (m + 1) t)
454 | C.LetIn (n,s,ty,t) ->
455 C.LetIn (n, unwind_aux m s, unwind_aux m ty, unwind_aux (m + 1) t)
456 | C.Appl l -> C.Appl (List.map (unwind_aux m) l)
457 | C.Const (uri,exp_named_subst) ->
460 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
463 C.Constant (_,_,_,params,_) -> params
464 | C.Variable _ -> raise ReferenceToVariable
465 | C.CurrentProof (_,_,_,_,params,_) -> params
466 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
469 let exp_named_subst' =
470 substaux_in_exp_named_subst params exp_named_subst m
472 C.Const (uri,exp_named_subst')
473 | C.MutInd (uri,i,exp_named_subst) ->
476 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
479 C.Constant _ -> raise ReferenceToConstant
480 | C.Variable _ -> raise ReferenceToVariable
481 | C.CurrentProof _ -> raise ReferenceToCurrentProof
482 | C.InductiveDefinition (_,params,_,_) -> params
485 let exp_named_subst' =
486 substaux_in_exp_named_subst params exp_named_subst m
488 C.MutInd (uri,i,exp_named_subst')
489 | C.MutConstruct (uri,i,j,exp_named_subst) ->
492 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
495 C.Constant _ -> raise ReferenceToConstant
496 | C.Variable _ -> raise ReferenceToVariable
497 | C.CurrentProof _ -> raise ReferenceToCurrentProof
498 | C.InductiveDefinition (_,params,_,_) -> params
501 let exp_named_subst' =
502 substaux_in_exp_named_subst params exp_named_subst m
504 C.MutConstruct (uri,i,j,exp_named_subst')
505 | C.MutCase (sp,i,outt,t,pl) ->
506 C.MutCase (sp,i,unwind_aux m outt, unwind_aux m t,
507 List.map (unwind_aux m) pl)
509 let len = List.length fl in
512 (fun (name,i,ty,bo) ->
513 (name, i, unwind_aux m ty, unwind_aux (m+len) bo))
516 C.Fix (i, substitutedfl)
518 let len = List.length fl in
521 (fun (name,ty,bo) -> (name, unwind_aux m ty, unwind_aux (m+len) bo))
524 C.CoFix (i, substitutedfl)
525 and substaux_in_exp_named_subst params exp_named_subst' m =
526 (*CSC: codice copiato e modificato dalla cicSubstitution.subst_vars *)
527 let rec filter_and_lift already_instantiated =
532 (function (uri',_)-> not (UriManager.eq uri uri')) exp_named_subst'
534 not (List.mem uri already_instantiated)
538 (uri,CicSubstitution.lift m (RS.from_ens_for_unwind ~unwind t)) ::
539 (filter_and_lift (uri::already_instantiated) tl)
540 | _::tl -> filter_and_lift already_instantiated tl
543 List.map (function (uri,t) -> uri, unwind_aux m t) exp_named_subst' @
544 (filter_and_lift [] (List.rev ens))
552 [uri,List.assoc uri res]
562 and unwind (k,e,ens,t,s) =
563 let t' = unwind' 0 k e ens t in
564 if s = [] then t' else Cic.Appl (t'::(RS.from_stack_list_for_unwind ~unwind s))
569 let profiler_unwind = HExtlib.profile ~enable:profile "are_convertible.unwind" in
571 profiler_unwind.HExtlib.profile (unwind k e ens) t
575 let reduce ~delta ?(subst = []) context : config -> config =
576 let module C = Cic in
577 let module S = CicSubstitution in
580 (k, e, _, C.Rel n, s) as config ->
582 if not delta then None
585 Some (RS.from_env (List.nth e (n-1)))
590 match List.nth context (n - 1 - k) with
592 | Some (_,C.Decl _) -> None
593 | Some (_,C.Def (x,_)) -> Some (0,[],[],S.lift (n - k) x,[])
599 Some (k',e',ens',t',s') -> reduce (k',e',ens',t',s'@s)
601 | (k, e, ens, C.Var (uri,exp_named_subst), s) as config ->
602 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
603 let (k',e',ens',t',s') = RS.from_ens (List.assq uri ens) in
604 reduce (k',e',ens',t',s'@s)
607 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
610 C.Constant _ -> raise ReferenceToConstant
611 | C.CurrentProof _ -> raise ReferenceToCurrentProof
612 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
613 | C.Variable (_,None,_,_,_) -> config
614 | C.Variable (_,Some body,_,_,_) ->
615 let ens' = push_exp_named_subst k e ens exp_named_subst in
616 reduce (0, [], ens', body, s)
618 | (k, e, ens, C.Meta (n,l), s) as config ->
620 let (_, term,_) = CicUtil.lookup_subst n subst in
621 reduce (k, e, ens,CicSubstitution.subst_meta l term,s)
622 with CicUtil.Subst_not_found _ -> config)
623 | (_, _, _, C.Sort _, _)
624 | (_, _, _, C.Implicit _, _) as config -> config
625 | (k, e, ens, C.Cast (te,ty), s) ->
626 reduce (k, e, ens, te, s)
627 | (_, _, _, C.Prod _, _) as config -> config
628 | (_, _, _, C.Lambda _, []) as config -> config
629 | (k, e, ens, C.Lambda (_,_,t), p::s) ->
630 reduce (k+1, (RS.stack_to_env ~reduce ~unwind p)::e, ens, t,s)
631 | (k, e, ens, C.LetIn (_,m,_,t), s) ->
632 let m' = RS.compute_to_env ~reduce ~unwind k e ens m in
633 reduce (k+1, m'::e, ens, t, s)
634 | (_, _, _, C.Appl [], _) -> assert false
635 | (k, e, ens, C.Appl (he::tl), s) ->
638 (function t -> RS.compute_to_stack ~reduce ~unwind (k,e,ens,t,[])) tl
640 reduce (k, e, ens, he, (List.append tl') s)
641 | (_, _, _, C.Const _, _) as config when delta=false-> config
642 | (k, e, ens, C.Const (uri,exp_named_subst), s) as config ->
644 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
647 C.Constant (_,Some body,_,_,_) ->
648 let ens' = push_exp_named_subst k e ens exp_named_subst in
649 (* constants are closed *)
650 reduce (0, [], ens', body, s)
651 | C.Constant (_,None,_,_,_) -> config
652 | C.Variable _ -> raise ReferenceToVariable
653 | C.CurrentProof (_,_,body,_,_,_) ->
654 let ens' = push_exp_named_subst k e ens exp_named_subst in
655 (* constants are closed *)
656 reduce (0, [], ens', body, s)
657 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
659 | (_, _, _, C.MutInd _, _)
660 | (_, _, _, C.MutConstruct _, _) as config -> config
661 | (k, e, ens, C.MutCase (mutind,i,outty,term,pl),s) as config ->
664 (k, e, ens, C.CoFix (i,fl), s) ->
665 let (_,_,body) = List.nth fl i in
667 let counter = ref (List.length fl) in
669 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
673 reduce (k,e,ens,body',s)
676 (match decofix (reduce (k,e,ens,term,[])) with
677 (k', e', ens', C.MutConstruct (_,_,j,_), []) ->
678 reduce (k, e, ens, (List.nth pl (j-1)), s)
679 | (k', e', ens', C.MutConstruct (_,_,j,_), s') ->
682 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph mutind
685 C.InductiveDefinition (_,_,r,_) -> r
686 | _ -> raise WrongUriToInductiveDefinition
689 let num_to_eat = r in
693 | (n,he::s) when n > 0 -> eat_first (n - 1, s)
694 | _ -> raise (Impossible 5)
696 eat_first (num_to_eat,s')
698 reduce (k, e, ens, (List.nth pl (j-1)), ts@s)
699 | (_, _, _, C.Cast _, _)
700 | (_, _, _, C.Implicit _, _) ->
701 raise (Impossible 2) (* we don't trust our whd ;-) *)
703 (*CSC: here I am unwinding the configuration and for sure I
704 will do it twice; to avoid this unwinding I should push the
705 "match [] with _" continuation on the stack;
706 another possibility is to just return the original configuration,
707 partially undoing the weak-head computation *)
708 (*this code is uncorrect since term' lives in e' <> e
709 let term' = unwind config' in
710 (k, e, ens, C.MutCase (mutind,i,outty,term',pl),s)
713 | (k, e, ens, C.Fix (i,fl), s) as config ->
714 let (_,recindex,_,body) = List.nth fl i in
717 Some (RS.from_stack (List.nth s recindex))
723 (match reduce recparam with
724 (_,_,_,C.MutConstruct _,_) as config ->
725 let leng = List.length fl in
727 let counter = ref 0 in
728 let rec build_env e' =
729 if !counter = leng then e'
733 ((RS.to_env ~reduce ~unwind (k,e,ens,C.Fix (!counter -1, fl),[]))::e'))
737 let rec replace i s t =
740 | n,he::tl -> he::(replace (n - 1) tl t)
741 | _,_ -> assert false in
743 replace recindex s (RS.compute_to_stack ~reduce ~unwind config)
745 reduce (k+leng, new_env, ens, body, new_s)
749 | (_,_,_,C.CoFix _,_) as config -> config
750 and push_exp_named_subst k e ens =
754 push_exp_named_subst k e ((uri,RS.to_ens ~reduce ~unwind (k,e,ens,t,[]))::ens) tl
759 let whd ?(delta=true) ?(subst=[]) context t =
760 unwind (reduce ~delta ~subst context (0, [], [], t, []))
767 (* ROTTO = rompe l'unificazione poiche' riduce gli argomenti di un'applicazione
768 senza ridurre la testa
769 module R = Reduction CallByNameStrategy;; OK 56.368s
770 module R = Reduction CallByValueStrategy;; ROTTO
771 module R = Reduction CallByValueStrategyByNameOnConstants;; ROTTO
772 module R = Reduction LazyCallByValueStrategy;; ROTTO
773 module R = Reduction LazyCallByValueStrategyByNameOnConstants;; ROTTO
774 module R = Reduction LazyCallByNameStrategy;; OK 0m56.398s
776 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns;;
778 module R = Reduction ClosuresOnStackByValueFromEnvOrEnsStrategy;; OK 58.583s
780 ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy;; OK 58.094s
781 module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);; OK 58.127s
783 (*module R = Reduction(CallByValueByNameForUnwind);;*)
784 module RS = CallByValueByNameForUnwind';;
785 (*module R = Reduction(CallByNameStrategy);;*)
786 (*module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);;*)
787 module R = Reduction(RS);;
788 module U = UriManager;;
794 let profiler_whd = HExtlib.profile ~enable:profile "are_convertible.whd" in
795 fun ?(delta=true) ?(subst=[]) context t ->
796 profiler_whd.HExtlib.profile (whd ~delta ~subst context) t
799 (* mimic ocaml (<< 3.08) "=" behaviour. Tests physical equality first then
800 * fallbacks to structural equality *)
802 Pervasives.compare x y = 0
804 (* t1, t2 must be well-typed *)
805 let are_convertible whd ?(subst=[]) ?(metasenv=[]) =
806 let heuristic = ref true in
807 let rec aux test_equality_only context t1 t2 ugraph =
808 let rec aux2 test_equality_only t1 t2 ugraph =
810 (* this trivial euristic cuts down the total time of about five times ;-) *)
811 (* this because most of the time t1 and t2 are "sintactically" the same *)
816 let module C = Cic in
818 (C.Rel n1, C.Rel n2) -> (n1 = n2),ugraph
819 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
820 if U.eq uri1 uri2 then
823 (fun (uri1,x) (uri2,y) (b,ugraph) ->
824 let b',ugraph' = aux test_equality_only context x y ugraph in
825 (U.eq uri1 uri2 && b' && b),ugraph'
826 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
828 Invalid_argument _ -> false,ugraph
832 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
835 let l1 = CicUtil.clean_up_local_context subst metasenv n1 l1 in
836 let l2 = CicUtil.clean_up_local_context subst metasenv n2 l2 in
838 (fun (b,ugraph) t1 t2 ->
842 | _,None -> true,ugraph
843 | Some t1',Some t2' ->
844 aux test_equality_only context t1' t2' ugraph
847 ) (true,ugraph) l1 l2
849 if b2 then true,ugraph1 else false,ugraph
852 | C.Meta (n1,l1), _ ->
854 let _,term,_ = CicUtil.lookup_subst n1 subst in
855 let term' = CicSubstitution.subst_meta l1 term in
857 prerr_endline ("%?: " ^ CicPp.ppterm t1 ^ " <==> " ^ CicPp.ppterm t2);
858 prerr_endline ("%%%%%%: " ^ CicPp.ppterm term' ^ " <==> " ^ CicPp.ppterm t2);
860 aux test_equality_only context term' t2 ugraph
861 with CicUtil.Subst_not_found _ -> false,ugraph)
862 | _, C.Meta (n2,l2) ->
864 let _,term,_ = CicUtil.lookup_subst n2 subst in
865 let term' = CicSubstitution.subst_meta l2 term in
867 prerr_endline ("%?: " ^ CicPp.ppterm t1 ^ " <==> " ^ CicPp.ppterm t2);
868 prerr_endline ("%%%%%%: " ^ CicPp.ppterm term' ^ " <==> " ^ CicPp.ppterm t1);
870 aux test_equality_only context t1 term' ugraph
871 with CicUtil.Subst_not_found _ -> false,ugraph)
872 | (C.Sort (C.CProp t1|C.Type t1), C.Sort (C.CProp t2|C.Type t2))
873 when test_equality_only ->
874 (try true,(CicUniv.add_eq t2 t1 ugraph)
875 with CicUniv.UniverseInconsistency _ -> false,ugraph)
876 | (C.Sort (C.CProp t1|C.Type t1), C.Sort (C.CProp t2|C.Type t2))
877 when not test_equality_only ->
878 (try true,(CicUniv.add_ge t2 t1 ugraph)
879 with CicUniv.UniverseInconsistency _ -> false,ugraph)
880 | (C.Sort s1, C.Sort (C.Type _))
881 | (C.Sort s1, C.Sort (C.CProp _)) -> (not test_equality_only),ugraph
882 | (C.Sort s1, C.Sort s2) -> (s1 = s2),ugraph
883 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
884 let b',ugraph' = aux true context s1 s2 ugraph in
886 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
890 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
891 let b',ugraph' = aux true context s1 s2 ugraph in
893 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
897 | (C.LetIn (name1,s1,ty1,t1), C.LetIn(_,s2,ty2,t2)) ->
898 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
900 let b',ugraph = aux test_equality_only context ty1 ty2 ugraph in
902 aux test_equality_only
903 ((Some (name1, (C.Def (s1,ty1))))::context) t1 t2 ugraph'
908 | (C.Appl l1, C.Appl l2) ->
911 (fun x y (b,ugraph) ->
913 aux test_equality_only context x y ugraph
915 false,ugraph) l1 l2 (true,ugraph)
917 Invalid_argument _ -> false,ugraph
919 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
920 let b' = U.eq uri1 uri2 in
924 (fun (uri1,x) (uri2,y) (b,ugraph) ->
925 if b && U.eq uri1 uri2 then
926 aux test_equality_only context x y ugraph
929 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
931 Invalid_argument _ -> false,ugraph
935 | (C.MutInd (uri1,i1,exp_named_subst1),
936 C.MutInd (uri2,i2,exp_named_subst2)
938 let b' = U.eq uri1 uri2 && i1 = i2 in
942 (fun (uri1,x) (uri2,y) (b,ugraph) ->
943 if b && U.eq uri1 uri2 then
944 aux test_equality_only context x y ugraph
947 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
949 Invalid_argument _ -> false,ugraph
953 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
954 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
956 let b' = U.eq uri1 uri2 && i1 = i2 && j1 = j2 in
960 (fun (uri1,x) (uri2,y) (b,ugraph) ->
961 if b && U.eq uri1 uri2 then
962 aux test_equality_only context x y ugraph
965 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
967 Invalid_argument _ -> false,ugraph
971 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
972 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
973 let b' = U.eq uri1 uri2 && i1 = i2 in
975 let b'',ugraph''=aux test_equality_only context
976 outtype1 outtype2 ugraph in
978 let b''',ugraph'''= aux test_equality_only context
979 term1 term2 ugraph'' in
981 (fun x y (b,ugraph) ->
983 aux test_equality_only context x y ugraph
986 pl1 pl2 (b''',ugraph''')
991 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
994 (fun (types,len) (n,_,ty,_) ->
995 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1001 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) (b,ugraph) ->
1002 if b && recindex1 = recindex2 then
1003 let b',ugraph' = aux test_equality_only context ty1 ty2
1006 aux test_equality_only (tys@context) bo1 bo2 ugraph'
1011 fl1 fl2 (true,ugraph)
1014 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
1017 (fun (types,len) (n,ty,_) ->
1018 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1024 (fun (_,ty1,bo1) (_,ty2,bo2) (b,ugraph) ->
1026 let b',ugraph' = aux test_equality_only context ty1 ty2
1029 aux test_equality_only (tys@context) bo1 bo2 ugraph'
1034 fl1 fl2 (true,ugraph)
1037 | C.Cast (bo,_),t -> aux2 test_equality_only bo t ugraph
1038 | t,C.Cast (bo,_) -> aux2 test_equality_only t bo ugraph
1039 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
1040 | (_,_) -> false,ugraph
1045 aux2 test_equality_only t1 t2 ugraph
1049 if fst res = true then
1053 (*if !heuristic then prerr_endline ("NON FACILE: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);*)
1054 (* heuristic := false; *)
1055 debug t1 [t2] "PREWHD";
1056 (*prerr_endline ("PREWHD: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);*)
1058 prerr_endline ("PREWHD: " ^ CicPp.ppterm t1 ^ " <===> " ^ CicPp.ppterm t2);
1059 let t1' = whd ?delta:(Some true) ?subst:(Some subst) context t1 in
1060 let t2' = whd ?delta:(Some true) ?subst:(Some subst) context t2 in
1061 debug t1' [t2'] "POSTWHD";
1063 let rec convert_machines ugraph =
1066 | ((k1,env1,ens1,h1,s1),(k2,env2,ens2,h2,s2))::tl ->
1067 let (b,ugraph) as res =
1068 aux2 test_equality_only
1069 (R.unwind (k1,env1,ens1,h1,[])) (R.unwind (k2,env2,ens2,h2,[])) ugraph
1077 (fun si-> R.reduce ~delta:false ~subst context(RS.from_stack si))
1080 (fun si-> R.reduce ~delta:false ~subst context(RS.from_stack si))
1084 Invalid_argument _ -> None
1087 None -> false,ugraph
1088 | Some problems -> convert_machines ugraph problems
1092 convert_machines ugraph
1093 [R.reduce ~delta:true ~subst context (0,[],[],t1,[]),
1094 R.reduce ~delta:true ~subst context (0,[],[],t2,[])]
1095 (*prerr_endline ("POSTWH: " ^ CicPp.ppterm t1' ^ " <===> " ^ CicPp.ppterm t2');*)
1097 aux2 test_equality_only t1' t2' ugraph
1101 aux false (*c t1 t2 ugraph *)
1105 let whd ?(delta=true) ?(subst=[]) context t =
1106 let res = whd ~delta ~subst context t in
1107 let rescsc = CicReductionNaif.whd ~delta ~subst context t in
1108 if not (fst (are_convertible CicReductionNaif.whd ~subst context res rescsc CicUniv.empty_ugraph)) then
1110 debug_print (lazy ("PRIMA: " ^ CicPp.ppterm t)) ;
1112 debug_print (lazy ("DOPO: " ^ CicPp.ppterm res)) ;
1114 debug_print (lazy ("CSC: " ^ CicPp.ppterm rescsc)) ;
1117 let _ = are_convertible CicReductionNaif.whd ~subst context res rescsc CicUniv.empty_ugraph in
1125 let are_convertible = are_convertible whd
1130 let profiler_other_whd = HExtlib.profile ~enable:profile "~are_convertible.whd"
1131 let whd ?(delta=true) ?(subst=[]) context t =
1133 whd ~delta ~subst context t
1135 profiler_other_whd.HExtlib.profile foo ()
1138 let rec normalize ?(delta=true) ?(subst=[]) ctx term =
1139 let module C = Cic in
1140 let t = whd ~delta ~subst ctx term in
1141 let aux = normalize ~delta ~subst in
1142 let decl name t = Some (name, C.Decl t) in
1145 | C.Var (uri,exp_named_subst) ->
1146 C.Var (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1148 C.Meta (i,List.map (function Some t -> Some (aux ctx t) | None -> None) l)
1151 | C.Cast (te,ty) -> C.Cast (aux ctx te, aux ctx ty)
1153 let s' = aux ctx s in
1154 C.Prod (n, s', aux ((decl n s')::ctx) t)
1155 | C.Lambda (n,s,t) ->
1156 let s' = aux ctx s in
1157 C.Lambda (n, s', aux ((decl n s')::ctx) t)
1158 | C.LetIn (n,s,_,t) ->
1159 (* the term is already in weak head normal form *)
1161 | C.Appl (h::l) -> C.Appl (h::(List.map (aux ctx) l))
1162 | C.Appl [] -> assert false
1163 | C.Const (uri,exp_named_subst) ->
1164 C.Const (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1165 | C.MutInd (uri,typeno,exp_named_subst) ->
1166 C.MutInd (uri,typeno, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1167 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
1168 C.MutConstruct (uri, typeno, consno,
1169 List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1170 | C.MutCase (sp,i,outt,t,pl) ->
1171 C.MutCase (sp,i, aux ctx outt, aux ctx t, List.map (aux ctx) pl)
1172 (*CSC: to be completed, I suppose *)
1176 let normalize ?delta ?subst ctx term =
1177 (* prerr_endline ("NORMALIZE:" ^ CicPp.ppterm term); *)
1178 let t = normalize ?delta ?subst ctx term in
1179 (* prerr_endline ("NORMALIZED:" ^ CicPp.ppterm t); *)
1183 (* performs an head beta/cast reduction *)
1184 let rec head_beta_reduce ?(delta=false) ?(upto=(-1)) t =
1189 (Cic.Appl (Cic.Lambda (_,_,t)::he'::tl')) ->
1190 let he'' = CicSubstitution.subst he' t in
1196 Cic.Appl l -> Cic.Appl (l@tl')
1197 | _ -> Cic.Appl (he''::tl')
1199 head_beta_reduce ~delta ~upto:(upto - 1) he'''
1200 | Cic.Cast (te,_) -> head_beta_reduce ~delta ~upto te
1201 | Cic.Appl (Cic.Const (uri,ens)::tl) as t when delta=true ->
1203 match fst (CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri) with
1204 Cic.Constant (_,bo,_,_,_) -> bo
1205 | Cic.Variable _ -> raise ReferenceToVariable
1206 | Cic.CurrentProof (_,_,bo,_,_,_) -> Some bo
1207 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
1212 head_beta_reduce ~upto
1213 ~delta (Cic.Appl ((CicSubstitution.subst_vars ens bo)::tl)))
1214 | Cic.Const (uri,ens) as t when delta=true ->
1216 match fst (CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri) with
1217 Cic.Constant (_,bo,_,_,_) -> bo
1218 | Cic.Variable _ -> raise ReferenceToVariable
1219 | Cic.CurrentProof (_,_,bo,_,_,_) -> Some bo
1220 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
1225 head_beta_reduce ~delta ~upto (CicSubstitution.subst_vars ens bo))
1229 let are_convertible ?subst ?metasenv context t1 t2 ugraph =
1230 let before = Unix.gettimeofday () in
1231 let res = are_convertible ?subst ?metasenv context t1 t2 ugraph in
1232 let after = Unix.gettimeofday () in
1233 let diff = after -. before in
1236 let nc = List.map (function None -> None | Some (n,_) -> Some n) context in
1238 ("\n#(" ^ string_of_float diff ^ "):\n" ^ CicPp.pp t1 nc ^ "\n<=>\n" ^ CicPp.pp t2 nc);