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/.
26 (* TODO unify exceptions *)
28 exception CicReductionInternalError;;
29 exception WrongUriToInductiveDefinition;;
30 exception Impossible of int;;
31 exception ReferenceToConstant;;
32 exception ReferenceToVariable;;
33 exception ReferenceToCurrentProof;;
34 exception ReferenceToInductiveDefinition;;
36 let debug_print = fun _ -> ()
40 let rec debug_aux t i =
42 let module U = UriManager in
43 CicPp.ppobj (C.Variable ("DEBUG", None, t, [], [])) ^ "\n" ^ i
46 debug_print (s ^ "\n" ^ List.fold_right debug_aux (t::env) "")
49 module type Strategy =
54 val to_stack : Cic.term -> stack_term
55 val to_stack_list : Cic.term list -> stack_term list
56 val to_env : Cic.term -> env_term
57 val to_ens : Cic.term -> ens_term
60 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
61 Cic.term -> Cic.term) ->
62 stack_term -> Cic.term
65 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
66 Cic.term -> Cic.term) ->
67 stack_term list -> Cic.term list
68 val from_env : env_term -> Cic.term
69 val from_ens : ens_term -> Cic.term
72 (int * env_term list * ens_term Cic.explicit_named_substitution *
73 Cic.term * stack_term list -> Cic.term) ->
75 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
76 Cic.term -> Cic.term) ->
77 stack_term -> env_term
80 (int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term *
81 stack_term list -> Cic.term) ->
83 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
84 Cic.term -> Cic.term) ->
85 int -> env_term list -> ens_term Cic.explicit_named_substitution ->
87 val compute_to_stack :
89 (int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term *
90 stack_term list -> Cic.term) ->
92 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
93 Cic.term -> Cic.term) ->
94 int -> env_term list -> ens_term Cic.explicit_named_substitution ->
95 Cic.term -> stack_term
99 module CallByNameStrategy =
101 type stack_term = Cic.term
102 type env_term = Cic.term
103 type ens_term = Cic.term
105 let to_stack_list l = l
108 let from_stack ~unwind v = v
109 let from_stack_list ~unwind l = l
112 let stack_to_env ~reduce ~unwind v = v
113 let compute_to_stack ~reduce ~unwind k e ens t = unwind k e ens t
114 let compute_to_env ~reduce ~unwind k e ens t = unwind k e ens t
118 module CallByValueStrategy =
120 type stack_term = Cic.term
121 type env_term = Cic.term
122 type ens_term = Cic.term
124 let to_stack_list l = l
127 let from_stack ~unwind v = v
128 let from_stack_list ~unwind l = l
131 let stack_to_env ~reduce ~unwind v = v
132 let compute_to_stack ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
133 let compute_to_env ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
137 module CallByValueStrategyByNameOnConstants =
139 type stack_term = Cic.term
140 type env_term = Cic.term
141 type ens_term = Cic.term
143 let to_stack_list l = l
146 let from_stack ~unwind v = v
147 let from_stack_list ~unwind l = l
150 let stack_to_env ~reduce ~unwind v = v
151 let compute_to_stack ~reduce ~unwind k e ens =
153 Cic.Const _ as t -> unwind k e ens t
154 | t -> reduce (k,e,ens,t,[])
155 let compute_to_env ~reduce ~unwind k e ens =
157 Cic.Const _ as t -> unwind k e ens t
158 | t -> reduce (k,e,ens,t,[])
162 module LazyCallByValueStrategy =
164 type stack_term = Cic.term lazy_t
165 type env_term = Cic.term lazy_t
166 type ens_term = Cic.term lazy_t
167 let to_stack v = lazy v
168 let to_stack_list l = List.map to_stack l
169 let to_env v = lazy v
170 let to_ens v = lazy v
171 let from_stack ~unwind v = Lazy.force v
172 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
173 let from_env v = Lazy.force v
174 let from_ens v = Lazy.force v
175 let stack_to_env ~reduce ~unwind v = v
176 let compute_to_stack ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
177 let compute_to_env ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
181 module LazyCallByValueStrategyByNameOnConstants =
183 type stack_term = Cic.term lazy_t
184 type env_term = Cic.term lazy_t
185 type ens_term = Cic.term lazy_t
186 let to_stack v = lazy v
187 let to_stack_list l = List.map to_stack l
188 let to_env v = lazy v
189 let to_ens v = lazy v
190 let from_stack ~unwind v = Lazy.force v
191 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
192 let from_env v = Lazy.force v
193 let from_ens v = Lazy.force v
194 let stack_to_env ~reduce ~unwind v = v
195 let compute_to_stack ~reduce ~unwind k e ens t =
198 Cic.Const _ as t -> unwind k e ens t
199 | t -> reduce (k,e,ens,t,[]))
200 let compute_to_env ~reduce ~unwind k e ens t =
203 Cic.Const _ as t -> unwind k e ens t
204 | t -> reduce (k,e,ens,t,[]))
208 module LazyCallByNameStrategy =
210 type stack_term = Cic.term lazy_t
211 type env_term = Cic.term lazy_t
212 type ens_term = Cic.term lazy_t
213 let to_stack v = lazy v
214 let to_stack_list l = List.map to_stack l
215 let to_env v = lazy v
216 let to_ens v = lazy v
217 let from_stack ~unwind v = Lazy.force v
218 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
219 let from_env v = Lazy.force v
220 let from_ens v = Lazy.force v
221 let stack_to_env ~reduce ~unwind v = v
222 let compute_to_stack ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
223 let compute_to_env ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
228 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns
231 type stack_term = reduce:bool -> Cic.term
232 type env_term = reduce:bool -> Cic.term
233 type ens_term = reduce:bool -> Cic.term
235 let value = lazy v in
236 fun ~reduce -> Lazy.force value
237 let to_stack_list l = List.map to_stack l
239 let value = lazy v in
240 fun ~reduce -> Lazy.force value
242 let value = lazy v in
243 fun ~reduce -> Lazy.force value
244 let from_stack ~unwind v = (v ~reduce:false)
245 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
246 let from_env v = (v ~reduce:true)
247 let from_ens v = (v ~reduce:true)
248 let stack_to_env ~reduce ~unwind v = v
249 let compute_to_stack ~reduce ~unwind k e ens t =
253 Cic.Const _ as t -> unwind k e ens t
254 | t -> reduce (k,e,ens,t,[])
257 lazy (unwind k e ens t)
260 if reduce then Lazy.force svalue else Lazy.force lvalue
261 let compute_to_env ~reduce ~unwind k e ens t =
265 Cic.Const _ as t -> unwind k e ens t
266 | t -> reduce (k,e,ens,t,[])
269 lazy (unwind k e ens t)
272 if reduce then Lazy.force svalue else Lazy.force lvalue
276 module ClosuresOnStackByValueFromEnvOrEnsStrategy =
279 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
280 type env_term = Cic.term
281 type ens_term = Cic.term
282 let to_stack v = (0,[],[],v)
283 let to_stack_list l = List.map to_stack l
286 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
287 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
290 let stack_to_env ~reduce ~unwind (k,e,ens,t) = reduce (k,e,ens,t,[])
291 let compute_to_env ~reduce ~unwind k e ens t =
293 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
297 module ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy =
300 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
301 type env_term = Cic.term
302 type ens_term = Cic.term
303 let to_stack v = (0,[],[],v)
304 let to_stack_list l = List.map to_stack l
307 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
308 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
311 let stack_to_env ~reduce ~unwind (k,e,ens,t) =
313 Cic.Const _ as t -> unwind k e ens t
314 | t -> reduce (k,e,ens,t,[])
315 let compute_to_env ~reduce ~unwind k e ens t =
317 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
321 module Reduction(RS : Strategy) =
323 type env = RS.env_term list
324 type ens = RS.ens_term Cic.explicit_named_substitution
325 type stack = RS.stack_term list
326 type config = int * env * ens * Cic.term * stack
328 (* k is the length of the environment e *)
329 (* m is the current depth inside the term *)
330 let unwind' m k e ens t =
331 let module C = Cic in
332 let module S = CicSubstitution in
333 if k = 0 && ens = [] then
336 let rec unwind_aux m =
339 if n <= m then t else
342 Some (RS.from_env (List.nth e (n-m-1)))
347 if m = 0 then t' else S.lift m t'
348 | None -> C.Rel (n-k)
350 | C.Var (uri,exp_named_subst) ->
352 debug_print ("%%%%%UWVAR " ^ String.concat " ; " (List.map (function (uri,t) -> UriManager.string_of_uri uri ^ " := " ^ CicPp.ppterm t) ens)) ;
354 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
355 CicSubstitution.lift m (RS.from_ens (List.assq uri ens))
359 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
362 C.Constant _ -> raise ReferenceToConstant
363 | C.Variable (_,_,_,params,_) -> params
364 | C.CurrentProof _ -> raise ReferenceToCurrentProof
365 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
368 let exp_named_subst' =
369 substaux_in_exp_named_subst params exp_named_subst m
371 C.Var (uri,exp_named_subst')
377 | Some t -> Some (unwind_aux m t)
382 | C.Implicit _ as t -> t
383 | C.Cast (te,ty) -> C.Cast (unwind_aux m te, unwind_aux m ty) (*CSC ???*)
384 | C.Prod (n,s,t) -> C.Prod (n, unwind_aux m s, unwind_aux (m + 1) t)
385 | C.Lambda (n,s,t) -> C.Lambda (n, unwind_aux m s, unwind_aux (m + 1) t)
386 | C.LetIn (n,s,t) -> C.LetIn (n, unwind_aux m s, unwind_aux (m + 1) t)
387 | C.Appl l -> C.Appl (List.map (unwind_aux m) l)
388 | C.Const (uri,exp_named_subst) ->
391 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
394 C.Constant (_,_,_,params,_) -> params
395 | C.Variable _ -> raise ReferenceToVariable
396 | C.CurrentProof (_,_,_,_,params,_) -> params
397 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
400 let exp_named_subst' =
401 substaux_in_exp_named_subst params exp_named_subst m
403 C.Const (uri,exp_named_subst')
404 | C.MutInd (uri,i,exp_named_subst) ->
407 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
410 C.Constant _ -> raise ReferenceToConstant
411 | C.Variable _ -> raise ReferenceToVariable
412 | C.CurrentProof _ -> raise ReferenceToCurrentProof
413 | C.InductiveDefinition (_,params,_,_) -> params
416 let exp_named_subst' =
417 substaux_in_exp_named_subst params exp_named_subst m
419 C.MutInd (uri,i,exp_named_subst')
420 | C.MutConstruct (uri,i,j,exp_named_subst) ->
423 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
426 C.Constant _ -> raise ReferenceToConstant
427 | C.Variable _ -> raise ReferenceToVariable
428 | C.CurrentProof _ -> raise ReferenceToCurrentProof
429 | C.InductiveDefinition (_,params,_,_) -> params
432 let exp_named_subst' =
433 substaux_in_exp_named_subst params exp_named_subst m
435 C.MutConstruct (uri,i,j,exp_named_subst')
436 | C.MutCase (sp,i,outt,t,pl) ->
437 C.MutCase (sp,i,unwind_aux m outt, unwind_aux m t,
438 List.map (unwind_aux m) pl)
440 let len = List.length fl in
443 (fun (name,i,ty,bo) ->
444 (name, i, unwind_aux m ty, unwind_aux (m+len) bo))
447 C.Fix (i, substitutedfl)
449 let len = List.length fl in
452 (fun (name,ty,bo) -> (name, unwind_aux m ty, unwind_aux (m+len) bo))
455 C.CoFix (i, substitutedfl)
456 and substaux_in_exp_named_subst params exp_named_subst' m =
457 (*CSC: Idea di Andrea di ordinare compatibilmente con l'ordine dei params
459 List.map (function (uri,t) -> uri, unwind_aux m t) exp_named_subst' @
460 (*CSC: qui liftiamo tutti gli ens anche se magari me ne servono la meta'!!! *)
461 List.map (function (uri,t) -> uri, CicSubstitution.lift m t) ens
463 let rec filter_and_lift =
467 let r = filter_and_lift tl in
469 (uri,(List.assq uri ens'))::r
474 filter_and_lift params
477 (*CSC: invece di concatenare sarebbe meglio rispettare l'ordine dei params *)
478 (*CSC: e' vero???? una veloce prova non sembra confermare la teoria *)
480 (*CSC: codice copiato e modificato dalla cicSubstitution.subst_vars *)
481 (*CSC: codice altamente inefficiente *)
482 let rec filter_and_lift already_instantiated =
487 (function (uri',_)-> not (UriManager.eq uri uri')) exp_named_subst'
489 not (List.mem uri already_instantiated)
493 (uri,CicSubstitution.lift m (RS.from_ens t)) ::
494 (filter_and_lift (uri::already_instantiated) tl)
495 | _::tl -> filter_and_lift already_instantiated tl
498 debug_print ("---- SKIPPO " ^ UriManager.string_of_uri uri) ;
499 if List.for_all (function (uri',_) -> not (UriManager.eq uri uri'))
500 exp_named_subst' then debug_print "---- OK1" ;
501 debug_print ("++++ uri " ^ UriManager.string_of_uri uri ^ " not in " ^ String.concat " ; " (List.map UriManager.string_of_uri params)) ;
502 if List.mem uri params then debug_print "---- OK2" ;
506 List.map (function (uri,t) -> uri, unwind_aux m t) exp_named_subst' @
507 (filter_and_lift [] (List.rev ens))
516 let reduce ~delta ?(subst = []) context : config -> Cic.term =
517 let module C = Cic in
518 let module S = CicSubstitution in
521 (k, e, _, (C.Rel n as t), s) ->
524 Some (RS.from_env (List.nth e (n-1)))
529 match List.nth context (n - 1 - k) with
531 | Some (_,C.Decl _) -> None
532 | Some (_,C.Def (x,_)) -> Some (S.lift (n - k) x)
538 Some t' -> reduce (0,[],[],t',s)
542 else C.Appl (C.Rel (n-k)::(RS.from_stack_list ~unwind s))
544 | (k, e, ens, (C.Var (uri,exp_named_subst) as t), s) ->
545 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
546 reduce (0, [], [], RS.from_ens (List.assq uri ens), s)
549 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
552 C.Constant _ -> raise ReferenceToConstant
553 | C.CurrentProof _ -> raise ReferenceToCurrentProof
554 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
555 | C.Variable (_,None,_,_,_) ->
556 let t' = unwind k e ens t in
557 if s = [] then t' else
558 C.Appl (t'::(RS.from_stack_list ~unwind s))
559 | C.Variable (_,Some body,_,_,_) ->
560 let ens' = push_exp_named_subst k e ens exp_named_subst in
561 reduce (0, [], ens', body, s)
563 | (k, e, ens, (C.Meta (n,l) as t), s) ->
565 let (_, term,_) = CicUtil.lookup_subst n subst in
566 reduce (k, e, ens,CicSubstitution.subst_meta l term,s)
567 with CicUtil.Subst_not_found _ ->
568 let t' = unwind k e ens t in
569 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s)))
570 | (k, e, _, (C.Sort _ as t), s) -> t (* s should be empty *)
571 | (k, e, _, (C.Implicit _ as t), s) -> t (* s should be empty *)
572 | (k, e, ens, (C.Cast (te,ty) as t), s) ->
573 reduce (k, e, ens, te, s) (* s should be empty *)
574 | (k, e, ens, (C.Prod _ as t), s) ->
575 unwind k e ens t (* s should be empty *)
576 | (k, e, ens, (C.Lambda (_,_,t) as t'), []) -> unwind k e ens t'
577 | (k, e, ens, C.Lambda (_,_,t), p::s) ->
578 reduce (k+1, (RS.stack_to_env ~reduce ~unwind p)::e, ens, t,s)
579 | (k, e, ens, (C.LetIn (_,m,t) as t'), s) ->
580 let m' = RS.compute_to_env ~reduce ~unwind k e ens m in
581 reduce (k+1, m'::e, ens, t, s)
582 | (_, _, _, C.Appl [], _) -> assert false
583 | (k, e, ens, C.Appl (he::tl), s) ->
586 (function t -> RS.compute_to_stack ~reduce ~unwind k e ens t) tl
588 reduce (k, e, ens, he, (List.append tl') s)
589 (* CSC: Old Dead Code
590 | (k, e, ens, C.Appl ((C.Lambda _ as he)::tl), s)
591 | (k, e, ens, C.Appl ((C.Const _ as he)::tl), s)
592 | (k, e, ens, C.Appl ((C.MutCase _ as he)::tl), s)
593 | (k, e, ens, C.Appl ((C.Fix _ as he)::tl), s) ->
594 (* strict evaluation, but constants are NOT unfolded *)
597 C.Const _ as t -> unwind k e ens t
598 | t -> reduce (k,e,ens,t,[])
600 let tl' = List.map red tl in
601 reduce (k, e, ens, he , List.append tl' s)
602 | (k, e, ens, C.Appl l, s) ->
603 C.Appl (List.append (List.map (unwind k e ens) l) s)
605 | (k, e, ens, (C.Const (uri,exp_named_subst) as t), s) when delta=false->
606 let t' = unwind k e ens t in
607 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
608 | (k, e, ens, (C.Const (uri,exp_named_subst) as t), s) ->
610 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph uri
613 C.Constant (_,Some body,_,_,_) ->
614 let ens' = push_exp_named_subst k e ens exp_named_subst in
615 (* constants are closed *)
616 reduce (0, [], ens', body, s)
617 | C.Constant (_,None,_,_,_) ->
618 let t' = unwind k e ens t in
619 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
620 | C.Variable _ -> raise ReferenceToVariable
621 | C.CurrentProof (_,_,body,_,_,_) ->
622 let ens' = push_exp_named_subst k e ens exp_named_subst in
623 (* constants are closed *)
624 reduce (0, [], ens', body, s)
625 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
627 | (k, e, ens, (C.MutInd _ as t),s) ->
628 let t' = unwind k e ens t in
629 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
630 | (k, e, ens, (C.MutConstruct _ as t),s) ->
631 let t' = unwind k e ens t in
632 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
633 | (k, e, ens, (C.MutCase (mutind,i,_,term,pl) as t),s) ->
636 C.CoFix (i,fl) as t ->
637 let (_,_,body) = List.nth fl i in
639 let counter = ref (List.length fl) in
641 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
645 (* the term is the result of a reduction; *)
646 (* so it is already unwinded. *)
647 reduce (0,[],[],body',[])
648 | C.Appl (C.CoFix (i,fl) :: tl) ->
649 let (_,_,body) = List.nth fl i in
651 let counter = ref (List.length fl) in
653 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
657 (* the term is the result of a reduction; *)
658 (* so it is already unwinded. *)
659 reduce (0,[],[],body',RS.to_stack_list tl)
662 (match decofix (reduce (k,e,ens,term,[])) with
663 C.MutConstruct (_,_,j,_) ->
664 reduce (k, e, ens, (List.nth pl (j-1)), s)
665 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
668 CicEnvironment.get_cooked_obj CicUniv.empty_ugraph mutind
671 C.InductiveDefinition (tl,ingredients,r,_) ->
672 let (_,_,arity,_) = List.nth tl i in
674 | _ -> raise WrongUriToInductiveDefinition
677 let num_to_eat = r in
681 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
682 | _ -> raise (Impossible 5)
684 eat_first (num_to_eat,tl)
686 (* ts are already unwinded because they are a sublist of tl *)
687 reduce (k, e, ens, (List.nth pl (j-1)), (RS.to_stack_list ts)@s)
688 | C.Cast _ | C.Implicit _ ->
689 raise (Impossible 2) (* we don't trust our whd ;-) *)
691 let t' = unwind k e ens t in
692 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
694 | (k, e, ens, (C.Fix (i,fl) as t), s) ->
695 let (_,recindex,_,body) = List.nth fl i in
698 Some (RS.from_stack ~unwind (List.nth s recindex))
704 (match reduce (0,[],[],recparam,[]) with
705 (* match recparam with *)
707 | C.Appl ((C.MutConstruct _)::_) ->
710 let counter = ref (List.length fl) in
712 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
716 reduce (k, e, ens, body', s) *)
718 let leng = List.length fl in
720 let unwind_fl (name,recindex,typ,body) =
721 (name,recindex,unwind k e ens typ,
722 unwind' leng k e ens body)
724 List.map unwind_fl fl
727 let counter = ref 0 in
728 let rec build_env e =
729 if !counter = leng then e
732 build_env ((RS.to_env (C.Fix (!counter -1, fl')))::e))
736 reduce (k+leng, new_env, ens, body, s)
738 let t' = unwind k e ens t in
739 if s = [] then t' else
740 C.Appl (t'::(RS.from_stack_list ~unwind s))
743 let t' = unwind k e ens t in
744 if s = [] then t' else
745 C.Appl (t'::(RS.from_stack_list ~unwind s))
747 | (k, e, ens, (C.CoFix (i,fl) as t),s) ->
748 let t' = unwind k e ens t in
749 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
750 and push_exp_named_subst k e ens =
754 push_exp_named_subst k e ((uri,RS.to_ens (unwind k e ens t))::ens) tl
759 let rec whd context t =
761 reduce context (0, [], [], t, [])
763 debug_print (CicPp.ppterm t) ;
768 let rec whd ?(delta=true) ?(subst=[]) context t =
769 reduce ~delta ~subst context (0, [], [], t, [])
775 let res = whd context t in
776 let rescsc = CicReductionNaif.whd context t in
777 if not (CicReductionNaif.are_convertible context res rescsc) then
779 debug_print ("PRIMA: " ^ CicPp.ppterm t) ;
781 debug_print ("DOPO: " ^ CicPp.ppterm res) ;
783 debug_print ("CSC: " ^ CicPp.ppterm rescsc) ;
785 CicReductionNaif.fdebug := 0 ;
786 let _ = CicReductionNaif.are_convertible context res rescsc in
798 module R = Reduction CallByNameStrategy;;
799 module R = Reduction CallByValueStrategy;;
800 module R = Reduction CallByValueStrategyByNameOnConstants;;
801 module R = Reduction LazyCallByValueStrategy;;
802 module R = Reduction LazyCallByValueStrategyByNameOnConstants;;
803 module R = Reduction LazyCallByNameStrategy;;
805 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns;;
806 module R = Reduction ClosuresOnStackByValueFromEnvOrEnsStrategy;;
808 ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy;;
810 module R = Reduction(ClosuresOnStackByValueFromEnvOrEnsStrategy);;
811 module U = UriManager;;
815 (* mimic ocaml (<< 3.08) "=" behaviour. Tests physical equality first then
816 * fallbacks to structural equality *)
817 let (===) x y = (Pervasives.compare x y = 0)
819 (* t1, t2 must be well-typed *)
820 let are_convertible ?(subst=[]) ?(metasenv=[]) =
821 let rec aux test_equality_only context t1 t2 ugraph =
822 let aux2 test_equality_only t1 t2 ugraph =
824 (* this trivial euristic cuts down the total time of about five times ;-) *)
825 (* this because most of the time t1 and t2 are "sintactically" the same *)
830 let module C = Cic in
832 (C.Rel n1, C.Rel n2) -> (n1 = n2),ugraph
833 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
834 if U.eq uri1 uri2 then
837 (fun (uri1,x) (uri2,y) (b,ugraph) ->
838 let b',ugraph' = aux test_equality_only context x y ugraph in
839 (U.eq uri1 uri2 && b' && b),ugraph'
840 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
842 Invalid_argument _ -> false,ugraph
846 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
849 let l1 = CicUtil.clean_up_local_context subst metasenv n1 l1 in
850 let l2 = CicUtil.clean_up_local_context subst metasenv n2 l2 in
852 (fun (b,ugraph) t1 t2 ->
856 | _,None -> true,ugraph
857 | Some t1',Some t2' ->
858 aux test_equality_only context t1' t2' ugraph
861 ) (true,ugraph) l1 l2
863 if b2 then true,ugraph1 else false,ugraph
866 (* TASSI: CONSTRAINTS *)
867 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) when test_equality_only ->
868 true,(CicUniv.add_eq t2 t1 ugraph)
869 (* TASSI: CONSTRAINTS *)
870 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
871 true,(CicUniv.add_ge t2 t1 ugraph)
872 (* TASSI: CONSTRAINTS *)
873 | (C.Sort s1, C.Sort (C.Type _)) -> (not test_equality_only),ugraph
874 (* TASSI: CONSTRAINTS *)
875 | (C.Sort s1, C.Sort s2) -> (s1 = s2),ugraph
876 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
877 let b',ugraph' = aux true context s1 s2 ugraph in
879 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
883 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
884 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
886 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
890 | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) ->
891 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
893 aux test_equality_only
894 ((Some (name1, (C.Def (s1,None))))::context) t1 t2 ugraph'
897 | (C.Appl l1, C.Appl l2) ->
900 (fun x y (b,ugraph) ->
902 aux test_equality_only context x y ugraph
904 false,ugraph) l1 l2 (true,ugraph)
906 Invalid_argument _ -> false,ugraph
908 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
909 let b' = U.eq uri1 uri2 in
913 (fun (uri1,x) (uri2,y) (b,ugraph) ->
914 if b && U.eq uri1 uri2 then
915 aux test_equality_only context x y ugraph
918 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
920 Invalid_argument _ -> false,ugraph
924 | (C.MutInd (uri1,i1,exp_named_subst1),
925 C.MutInd (uri2,i2,exp_named_subst2)
927 let b' = U.eq uri1 uri2 && i1 = i2 in
931 (fun (uri1,x) (uri2,y) (b,ugraph) ->
932 if b && U.eq uri1 uri2 then
933 aux test_equality_only context x y ugraph
936 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
938 Invalid_argument _ -> false,ugraph
942 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
943 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
945 let b' = U.eq uri1 uri2 && i1 = i2 && j1 = j2 in
949 (fun (uri1,x) (uri2,y) (b,ugraph) ->
950 if b && U.eq uri1 uri2 then
951 aux test_equality_only context x y ugraph
954 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
956 Invalid_argument _ -> false,ugraph
960 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
961 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
962 let b' = U.eq uri1 uri2 && i1 = i2 in
964 let b'',ugraph''=aux test_equality_only context
965 outtype1 outtype2 ugraph in
967 let b''',ugraph'''= aux test_equality_only context
968 term1 term2 ugraph'' in
970 (fun x y (b,ugraph) ->
972 aux test_equality_only context x y ugraph
975 pl1 pl2 (b''',ugraph''')
980 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
982 List.map (function (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
986 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) (b,ugraph) ->
987 if b && recindex1 = recindex2 then
988 let b',ugraph' = aux test_equality_only context ty1 ty2
991 aux test_equality_only (tys@context) bo1 bo2 ugraph'
996 fl1 fl2 (true,ugraph)
999 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
1001 List.map (function (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
1005 (fun (_,ty1,bo1) (_,ty2,bo2) (b,ugraph) ->
1007 let b',ugraph' = aux test_equality_only context ty1 ty2
1010 aux test_equality_only (tys@context) bo1 bo2 ugraph'
1015 fl1 fl2 (true,ugraph)
1018 | (C.Cast _, _) | (_, C.Cast _)
1019 | (C.Implicit _, _) | (_, C.Implicit _) ->
1021 | (_,_) -> false,ugraph
1025 debug t1 [t2] "PREWHD";
1029 debug_print (CicPp.ppterm t1);
1030 debug_print (CicPp.ppterm (whd ~subst context t1));
1031 debug_print (CicPp.ppterm t2);
1032 debug_print (CicPp.ppterm (whd ~subst context t2))
1034 let t1' = whd ~subst context t1 in
1035 let t2' = whd ~subst context t2 in
1036 debug t1' [t2'] "POSTWHD";
1037 aux2 test_equality_only t1' t2' ugraph
1040 aux false (*c t1 t2 ugraph *)
1044 let rec normalize ?(delta=true) ?(subst=[]) ctx term =
1045 let module C = Cic in
1046 let t = whd ~delta ~subst ctx term in
1047 let aux = normalize ~delta ~subst in
1048 let decl name t = Some (name, C.Decl t) in
1049 let def name t = Some (name, C.Def (t,None)) in
1052 | C.Var (uri,exp_named_subst) ->
1053 C.Var (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1055 C.Meta (i,List.map (function Some t -> Some (aux ctx t) | None -> None) l)
1058 | C.Cast (te,ty) -> C.Cast (aux ctx te, aux ctx ty)
1060 let s' = aux ctx s in
1061 C.Prod (n, s', aux ((decl n s')::ctx) t)
1062 | C.Lambda (n,s,t) ->
1063 let s' = aux ctx s in
1064 C.Lambda (n, s', aux ((decl n s')::ctx) t)
1065 | C.LetIn (n,s,t) ->
1066 let s' = aux ctx s in
1067 C.LetIn (n, s, aux ((def n s')::ctx) t)
1068 | C.Appl (h::l) -> C.Appl (h::(List.map (aux ctx) l))
1069 | C.Appl [] -> assert false
1070 | C.Const (uri,exp_named_subst) ->
1071 C.Const (uri, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1072 | C.MutInd (uri,typeno,exp_named_subst) ->
1073 C.MutInd (uri,typeno, List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1074 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
1075 C.MutConstruct (uri, typeno, consno,
1076 List.map (fun (n,t) -> n,aux ctx t) exp_named_subst)
1077 | C.MutCase (sp,i,outt,t,pl) ->
1078 C.MutCase (sp,i, aux ctx outt, aux ctx t, List.map (aux ctx) pl)
1082 let normalize ?delta ?subst ctx term =
1083 (* prerr_endline ("NORMALIZE:" ^ CicPp.ppterm term); *)
1084 let t = normalize ?delta ?subst ctx term in
1085 (* prerr_endline ("NORMALIZED:" ^ CicPp.ppterm t); *)
1089 (* performs an head beta/cast reduction *)
1090 let rec head_beta_reduce =
1092 (Cic.Appl (Cic.Lambda (_,_,t)::he'::tl')) ->
1093 let he'' = CicSubstitution.subst he' t in
1099 Cic.Appl l -> Cic.Appl (l@tl')
1100 | _ -> Cic.Appl (he''::tl')
1102 head_beta_reduce he'''
1103 | Cic.Cast (te,_) -> head_beta_reduce te