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 exception CicReductionInternalError;;
27 exception WrongUriToInductiveDefinition;;
28 exception Impossible of int;;
29 exception ReferenceToConstant;;
30 exception ReferenceToVariable;;
31 exception ReferenceToCurrentProof;;
32 exception ReferenceToInductiveDefinition;;
36 let rec debug_aux t i =
38 let module U = UriManager in
39 CicPp.ppobj (C.Variable ("DEBUG", None, t, [])) ^ "\n" ^ i
43 print_endline (s ^ "\n" ^ List.fold_right debug_aux (t::env) "") ;
48 module type Strategy =
53 val to_stack : Cic.term -> stack_term
54 val to_stack_list : Cic.term list -> stack_term list
55 val to_env : Cic.term -> env_term
56 val to_ens : Cic.term -> ens_term
59 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
60 Cic.term -> Cic.term) ->
61 stack_term -> Cic.term
64 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
65 Cic.term -> Cic.term) ->
66 stack_term list -> Cic.term list
67 val from_env : env_term -> Cic.term
68 val from_ens : ens_term -> Cic.term
71 (int * env_term list * ens_term Cic.explicit_named_substitution *
72 Cic.term * stack_term list -> Cic.term) ->
74 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
75 Cic.term -> Cic.term) ->
76 stack_term -> env_term
79 (int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term *
80 stack_term list -> Cic.term) ->
82 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
83 Cic.term -> Cic.term) ->
84 int -> env_term list -> ens_term Cic.explicit_named_substitution ->
86 val compute_to_stack :
88 (int * env_term list * ens_term Cic.explicit_named_substitution * Cic.term *
89 stack_term list -> Cic.term) ->
91 (int -> env_term list -> ens_term Cic.explicit_named_substitution ->
92 Cic.term -> Cic.term) ->
93 int -> env_term list -> ens_term Cic.explicit_named_substitution ->
94 Cic.term -> stack_term
98 module CallByNameStrategy =
100 type stack_term = Cic.term
101 type env_term = Cic.term
102 type ens_term = Cic.term
104 let to_stack_list l = l
107 let from_stack ~unwind v = v
108 let from_stack_list ~unwind l = l
111 let stack_to_env ~reduce ~unwind v = v
112 let compute_to_stack ~reduce ~unwind k e ens t = unwind k e ens t
113 let compute_to_env ~reduce ~unwind k e ens t = unwind k e ens t
117 module CallByValueStrategy =
119 type stack_term = Cic.term
120 type env_term = Cic.term
121 type ens_term = Cic.term
123 let to_stack_list l = l
126 let from_stack ~unwind v = v
127 let from_stack_list ~unwind l = l
130 let stack_to_env ~reduce ~unwind v = v
131 let compute_to_stack ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
132 let compute_to_env ~reduce ~unwind k e ens t = reduce (k,e,ens,t,[])
136 module CallByValueStrategyByNameOnConstants =
138 type stack_term = Cic.term
139 type env_term = Cic.term
140 type ens_term = Cic.term
142 let to_stack_list l = l
145 let from_stack ~unwind v = v
146 let from_stack_list ~unwind l = l
149 let stack_to_env ~reduce ~unwind v = v
150 let compute_to_stack ~reduce ~unwind k e ens =
152 Cic.Const _ as t -> unwind k e ens t
153 | t -> reduce (k,e,ens,t,[])
154 let compute_to_env ~reduce ~unwind k e ens =
156 Cic.Const _ as t -> unwind k e ens t
157 | t -> reduce (k,e,ens,t,[])
161 module LazyCallByValueStrategy =
163 type stack_term = Cic.term lazy_t
164 type env_term = Cic.term lazy_t
165 type ens_term = Cic.term lazy_t
166 let to_stack v = lazy v
167 let to_stack_list l = List.map to_stack l
168 let to_env v = lazy v
169 let to_ens v = lazy v
170 let from_stack ~unwind v = Lazy.force v
171 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
172 let from_env v = Lazy.force v
173 let from_ens v = Lazy.force v
174 let stack_to_env ~reduce ~unwind v = v
175 let compute_to_stack ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
176 let compute_to_env ~reduce ~unwind k e ens t = lazy (reduce (k,e,ens,t,[]))
180 module LazyCallByValueStrategyByNameOnConstants =
182 type stack_term = Cic.term lazy_t
183 type env_term = Cic.term lazy_t
184 type ens_term = Cic.term lazy_t
185 let to_stack v = lazy v
186 let to_stack_list l = List.map to_stack l
187 let to_env v = lazy v
188 let to_ens v = lazy v
189 let from_stack ~unwind v = Lazy.force v
190 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
191 let from_env v = Lazy.force v
192 let from_ens v = Lazy.force v
193 let stack_to_env ~reduce ~unwind v = v
194 let compute_to_stack ~reduce ~unwind k e ens t =
197 Cic.Const _ as t -> unwind k e ens t
198 | t -> reduce (k,e,ens,t,[]))
199 let compute_to_env ~reduce ~unwind k e ens t =
202 Cic.Const _ as t -> unwind k e ens t
203 | t -> reduce (k,e,ens,t,[]))
207 module LazyCallByNameStrategy =
209 type stack_term = Cic.term lazy_t
210 type env_term = Cic.term lazy_t
211 type ens_term = Cic.term lazy_t
212 let to_stack v = lazy v
213 let to_stack_list l = List.map to_stack l
214 let to_env v = lazy v
215 let to_ens v = lazy v
216 let from_stack ~unwind v = Lazy.force v
217 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
218 let from_env v = Lazy.force v
219 let from_ens v = Lazy.force v
220 let stack_to_env ~reduce ~unwind v = v
221 let compute_to_stack ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
222 let compute_to_env ~reduce ~unwind k e ens t = lazy (unwind k e ens t)
227 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns
230 type stack_term = reduce:bool -> Cic.term
231 type env_term = reduce:bool -> Cic.term
232 type ens_term = reduce:bool -> Cic.term
234 let value = lazy v in
235 fun ~reduce -> Lazy.force value
236 let to_stack_list l = List.map to_stack l
238 let value = lazy v in
239 fun ~reduce -> Lazy.force value
241 let value = lazy v in
242 fun ~reduce -> Lazy.force value
243 let from_stack ~unwind v = (v ~reduce:false)
244 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
245 let from_env v = (v ~reduce:true)
246 let from_ens v = (v ~reduce:true)
247 let stack_to_env ~reduce ~unwind v = v
248 let compute_to_stack ~reduce ~unwind k e ens t =
252 Cic.Const _ as t -> unwind k e ens t
253 | t -> reduce (k,e,ens,t,[])
256 lazy (unwind k e ens t)
259 if reduce then Lazy.force svalue else Lazy.force lvalue
260 let compute_to_env ~reduce ~unwind k e ens t =
264 Cic.Const _ as t -> unwind k e ens t
265 | t -> reduce (k,e,ens,t,[])
268 lazy (unwind k e ens t)
271 if reduce then Lazy.force svalue else Lazy.force lvalue
275 module ClosuresOnStackByValueFromEnvOrEnsStrategy =
278 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
279 type env_term = Cic.term
280 type ens_term = Cic.term
281 let to_stack v = (0,[],[],v)
282 let to_stack_list l = List.map to_stack l
285 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
286 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
289 let stack_to_env ~reduce ~unwind (k,e,ens,t) = reduce (k,e,ens,t,[])
290 let compute_to_env ~reduce ~unwind k e ens t =
292 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
296 module ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy =
299 int * Cic.term list * Cic.term Cic.explicit_named_substitution * Cic.term
300 type env_term = Cic.term
301 type ens_term = Cic.term
302 let to_stack v = (0,[],[],v)
303 let to_stack_list l = List.map to_stack l
306 let from_stack ~unwind (k,e,ens,t) = unwind k e ens t
307 let from_stack_list ~unwind l = List.map (from_stack ~unwind) l
310 let stack_to_env ~reduce ~unwind (k,e,ens,t) =
312 Cic.Const _ as t -> unwind k e ens t
313 | t -> reduce (k,e,ens,t,[])
314 let compute_to_env ~reduce ~unwind k e ens t =
316 let compute_to_stack ~reduce ~unwind k e ens t = (k,e,ens,t)
320 module Reduction(RS : Strategy) =
322 type env = RS.env_term list
323 type ens = RS.ens_term Cic.explicit_named_substitution
324 type stack = RS.stack_term list
325 type config = int * env * ens * Cic.term * stack
327 (* k is the length of the environment e *)
328 (* m is the current depth inside the term *)
329 let unwind' m k e ens t =
330 let module C = Cic in
331 let module S = CicSubstitution in
332 if k = 0 && ens = [] then
335 let rec unwind_aux m =
338 if n <= m then t else
341 Some (RS.from_env (List.nth e (n-m-1)))
346 if m = 0 then t' else S.lift m t'
347 | None -> C.Rel (n-k)
349 | C.Var (uri,exp_named_subst) ->
351 prerr_endline ("%%%%%UWVAR " ^ String.concat " ; " (List.map (function (uri,t) -> UriManager.string_of_uri uri ^ " := " ^ CicPp.ppterm t) ens)) ;
353 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
354 CicSubstitution.lift m (RS.from_ens (List.assq uri ens))
357 (match CicEnvironment.get_obj uri with
358 C.Constant _ -> raise ReferenceToConstant
359 | C.Variable (_,_,_,params) -> params
360 | C.CurrentProof _ -> raise ReferenceToCurrentProof
361 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
364 let exp_named_subst' =
365 substaux_in_exp_named_subst params exp_named_subst m
367 C.Var (uri,exp_named_subst')
373 | Some t -> Some (unwind_aux m t)
378 | C.Implicit as t -> t
379 | C.Cast (te,ty) -> C.Cast (unwind_aux m te, unwind_aux m ty) (*CSC ???*)
380 | C.Prod (n,s,t) -> C.Prod (n, unwind_aux m s, unwind_aux (m + 1) t)
381 | C.Lambda (n,s,t) -> C.Lambda (n, unwind_aux m s, unwind_aux (m + 1) t)
382 | C.LetIn (n,s,t) -> C.LetIn (n, unwind_aux m s, unwind_aux (m + 1) t)
383 | C.Appl l -> C.Appl (List.map (unwind_aux m) l)
384 | C.Const (uri,exp_named_subst) ->
386 (match CicEnvironment.get_obj uri with
387 C.Constant (_,_,_,params) -> params
388 | C.Variable _ -> raise ReferenceToVariable
389 | C.CurrentProof (_,_,_,_,params) -> params
390 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
393 let exp_named_subst' =
394 substaux_in_exp_named_subst params exp_named_subst m
396 C.Const (uri,exp_named_subst')
397 | C.MutInd (uri,i,exp_named_subst) ->
399 (match CicEnvironment.get_obj uri with
400 C.Constant _ -> raise ReferenceToConstant
401 | C.Variable _ -> raise ReferenceToVariable
402 | C.CurrentProof _ -> raise ReferenceToCurrentProof
403 | C.InductiveDefinition (_,params,_) -> params
406 let exp_named_subst' =
407 substaux_in_exp_named_subst params exp_named_subst m
409 C.MutInd (uri,i,exp_named_subst')
410 | C.MutConstruct (uri,i,j,exp_named_subst) ->
412 (match CicEnvironment.get_obj uri with
413 C.Constant _ -> raise ReferenceToConstant
414 | C.Variable _ -> raise ReferenceToVariable
415 | C.CurrentProof _ -> raise ReferenceToCurrentProof
416 | C.InductiveDefinition (_,params,_) -> params
419 let exp_named_subst' =
420 substaux_in_exp_named_subst params exp_named_subst m
422 C.MutConstruct (uri,i,j,exp_named_subst')
423 | C.MutCase (sp,i,outt,t,pl) ->
424 C.MutCase (sp,i,unwind_aux m outt, unwind_aux m t,
425 List.map (unwind_aux m) pl)
427 let len = List.length fl in
430 (fun (name,i,ty,bo) ->
431 (name, i, unwind_aux m ty, unwind_aux (m+len) bo))
434 C.Fix (i, substitutedfl)
436 let len = List.length fl in
439 (fun (name,ty,bo) -> (name, unwind_aux m ty, unwind_aux (m+len) bo))
442 C.CoFix (i, substitutedfl)
443 and substaux_in_exp_named_subst params exp_named_subst' m =
444 (*CSC: Idea di Andrea di ordinare compatibilmente con l'ordine dei params
446 List.map (function (uri,t) -> uri, unwind_aux m t) exp_named_subst' @
447 (*CSC: qui liftiamo tutti gli ens anche se magari me ne servono la meta'!!! *)
448 List.map (function (uri,t) -> uri, CicSubstitution.lift m t) ens
450 let rec filter_and_lift =
454 let r = filter_and_lift tl in
456 (uri,(List.assq uri ens'))::r
461 filter_and_lift params
464 (*CSC: invece di concatenare sarebbe meglio rispettare l'ordine dei params *)
465 (*CSC: e' vero???? una veloce prova non sembra confermare la teoria *)
467 (*CSC: codice copiato e modificato dalla cicSubstitution.subst_vars *)
468 (*CSC: codice altamente inefficiente *)
469 let rec filter_and_lift already_instantiated =
474 (function (uri',_)-> not (UriManager.eq uri uri')) exp_named_subst'
476 not (List.mem uri already_instantiated)
480 (uri,CicSubstitution.lift m (RS.from_ens t)) ::
481 (filter_and_lift (uri::already_instantiated) tl)
482 | _::tl -> filter_and_lift already_instantiated tl
485 prerr_endline ("---- SKIPPO " ^ UriManager.string_of_uri uri) ;
486 if List.for_all (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst' then prerr_endline "---- OK1" ;
487 prerr_endline ("++++ uri " ^ UriManager.string_of_uri uri ^ " not in " ^ String.concat " ; " (List.map UriManager.string_of_uri params)) ;
488 if List.mem uri params then prerr_endline "---- OK2" ;
492 List.map (function (uri,t) -> uri, unwind_aux m t) exp_named_subst' @
493 (filter_and_lift [] (List.rev ens))
502 let reduce context : config -> Cic.term =
503 let module C = Cic in
504 let module S = CicSubstitution in
507 (k, e, _, (C.Rel n as t), s) ->
510 Some (RS.from_env (List.nth e (n-1)))
515 match List.nth context (n - 1 - k) with
517 | Some (_,C.Decl _) -> None
518 | Some (_,C.Def (x,_)) -> Some (S.lift (n - k) x)
524 Some t' -> reduce (0,[],[],t',s)
528 else C.Appl (C.Rel (n-k)::(RS.from_stack_list ~unwind s))
530 | (k, e, ens, (C.Var (uri,exp_named_subst) as t), s) ->
531 if List.exists (function (uri',_) -> UriManager.eq uri' uri) ens then
532 reduce (0, [], [], RS.from_ens (List.assq uri ens), s)
534 (match CicEnvironment.get_obj uri with
535 C.Constant _ -> raise ReferenceToConstant
536 | C.CurrentProof _ -> raise ReferenceToCurrentProof
537 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
538 | C.Variable (_,None,_,_) ->
539 let t' = unwind k e ens t in
540 if s = [] then t' else
541 C.Appl (t'::(RS.from_stack_list ~unwind s))
542 | C.Variable (_,Some body,_,_) ->
543 let ens' = push_exp_named_subst k e ens exp_named_subst in
544 reduce (0, [], ens', body, s)
546 | (k, e, ens, (C.Meta _ as t), s) ->
547 let t' = unwind k e ens t in
548 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
549 | (k, e, _, (C.Sort _ as t), s) -> t (* s should be empty *)
550 | (k, e, _, (C.Implicit as t), s) -> t (* s should be empty *)
551 | (k, e, ens, (C.Cast (te,ty) as t), s) ->
552 reduce (k, e, ens, te, s) (* s should be empty *)
553 | (k, e, ens, (C.Prod _ as t), s) ->
554 unwind k e ens t (* s should be empty *)
555 | (k, e, ens, (C.Lambda (_,_,t) as t'), []) -> unwind k e ens t'
556 | (k, e, ens, C.Lambda (_,_,t), p::s) ->
557 reduce (k+1, (RS.stack_to_env ~reduce ~unwind p)::e, ens, t,s)
558 | (k, e, ens, (C.LetIn (_,m,t) as t'), s) ->
559 let m' = RS.compute_to_env ~reduce ~unwind k e ens m in
560 reduce (k+1, m'::e, ens, t, s)
561 | (_, _, _, C.Appl [], _) -> raise (Impossible 1)
562 | (k, e, ens, C.Appl (he::tl), s) ->
565 (function t -> RS.compute_to_stack ~reduce ~unwind k e ens t) tl
567 reduce (k, e, ens, he, (List.append tl') s)
568 (* CSC: Old Dead Code
569 | (k, e, ens, C.Appl ((C.Lambda _ as he)::tl), s)
570 | (k, e, ens, C.Appl ((C.Const _ as he)::tl), s)
571 | (k, e, ens, C.Appl ((C.MutCase _ as he)::tl), s)
572 | (k, e, ens, C.Appl ((C.Fix _ as he)::tl), s) ->
573 (* strict evaluation, but constants are NOT unfolded *)
576 C.Const _ as t -> unwind k e ens t
577 | t -> reduce (k,e,ens,t,[])
579 let tl' = List.map red tl in
580 reduce (k, e, ens, he , List.append tl' s)
581 | (k, e, ens, C.Appl l, s) ->
582 C.Appl (List.append (List.map (unwind k e ens) l) s)
584 | (k, e, ens, (C.Const (uri,exp_named_subst) as t), s) ->
585 (match CicEnvironment.get_obj uri with
586 C.Constant (_,Some body,_,_) ->
587 let ens' = push_exp_named_subst k e ens exp_named_subst in
588 (* constants are closed *)
589 reduce (0, [], ens', body, s)
590 | C.Constant (_,None,_,_) ->
591 let t' = unwind k e ens t in
592 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
593 | C.Variable _ -> raise ReferenceToVariable
594 | C.CurrentProof (_,_,body,_,_) ->
595 let ens' = push_exp_named_subst k e ens exp_named_subst in
596 (* constants are closed *)
597 reduce (0, [], ens', body, s)
598 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
600 | (k, e, ens, (C.MutInd _ as t),s) ->
601 let t' = unwind k e ens t in
602 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
603 | (k, e, ens, (C.MutConstruct _ as t),s) ->
604 let t' = unwind k e ens t in
605 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
606 | (k, e, ens, (C.MutCase (mutind,i,_,term,pl) as t),s) ->
609 C.CoFix (i,fl) as t ->
610 let (_,_,body) = List.nth fl i in
612 let counter = ref (List.length fl) in
614 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
618 (* the term is the result of a reduction; *)
619 (* so it is already unwinded. *)
620 reduce (0,[],[],body',[])
621 | C.Appl (C.CoFix (i,fl) :: tl) ->
622 let (_,_,body) = List.nth fl i in
624 let counter = ref (List.length fl) in
626 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
630 (* the term is the result of a reduction; *)
631 (* so it is already unwinded. *)
632 reduce (0,[],[],body',RS.to_stack_list tl)
635 (match decofix (reduce (k,e,ens,term,[])) with
636 C.MutConstruct (_,_,j,_) ->
637 reduce (k, e, ens, (List.nth pl (j-1)), s)
638 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
640 match CicEnvironment.get_obj mutind with
641 C.InductiveDefinition (tl,ingredients,r) ->
642 let (_,_,arity,_) = List.nth tl i in
644 | _ -> raise WrongUriToInductiveDefinition
647 let num_to_eat = r in
651 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
652 | _ -> raise (Impossible 5)
654 eat_first (num_to_eat,tl)
656 (* ts are already unwinded because they are a sublist of tl *)
657 reduce (k, e, ens, (List.nth pl (j-1)), (RS.to_stack_list ts)@s)
658 | C.Cast _ | C.Implicit ->
659 raise (Impossible 2) (* we don't trust our whd ;-) *)
661 let t' = unwind k e ens t in
662 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
664 | (k, e, ens, (C.Fix (i,fl) as t), s) ->
665 let (_,recindex,_,body) = List.nth fl i in
668 Some (RS.from_stack ~unwind (List.nth s recindex))
674 (match reduce (0,[],[],recparam,[]) with
675 (* match recparam with *)
677 | C.Appl ((C.MutConstruct _)::_) ->
680 let counter = ref (List.length fl) in
682 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
686 reduce (k, e, ens, body', s) *)
688 let leng = List.length fl in
690 let unwind_fl (name,recindex,typ,body) =
691 (name,recindex,unwind k e ens typ,
692 unwind' leng k e ens body)
694 List.map unwind_fl fl
697 let counter = ref 0 in
698 let rec build_env e =
699 if !counter = leng then e
702 build_env ((RS.to_env (C.Fix (!counter -1, fl')))::e))
706 reduce (k+leng, new_env, ens, body, s)
708 let t' = unwind k e ens t in
709 if s = [] then t' else
710 C.Appl (t'::(RS.from_stack_list ~unwind s))
713 let t' = unwind k e ens t in
714 if s = [] then t' else
715 C.Appl (t'::(RS.from_stack_list ~unwind s))
717 | (k, e, ens, (C.CoFix (i,fl) as t),s) ->
718 let t' = unwind k e ens t in
719 if s = [] then t' else C.Appl (t'::(RS.from_stack_list ~unwind s))
720 and push_exp_named_subst k e ens =
724 push_exp_named_subst k e ((uri,RS.to_ens (unwind k e ens t))::ens) tl
729 let rec whd context t = reduce context (0, [], [], t, []);;
733 let res = whd context t in
734 let rescsc = CicReductionNaif.whd context t in
735 if not (CicReductionNaif.are_convertible context res rescsc) then
737 prerr_endline ("PRIMA: " ^ CicPp.ppterm t) ;
739 prerr_endline ("DOPO: " ^ CicPp.ppterm res) ;
741 prerr_endline ("CSC: " ^ CicPp.ppterm rescsc) ;
743 CicReductionNaif.fdebug := 0 ;
744 let _ = CicReductionNaif.are_convertible context res rescsc in
756 module R = Reduction CallByNameStrategy;;
757 module R = Reduction CallByValueStrategy;;
758 module R = Reduction CallByValueStrategyByNameOnConstants;;
759 module R = Reduction LazyCallByValueStrategy;;
760 module R = Reduction LazyCallByValueStrategyByNameOnConstants;;
761 module R = Reduction LazyCallByNameStrategy;;
763 LazyCallByValueByNameOnConstantsWhenFromStack_ByNameStrategyWhenFromEnvOrEns;;
764 module R = Reduction ClosuresOnStackByValueFromEnvOrEnsStrategy;;
766 ClosuresOnStackByValueFromEnvOrEnsByNameOnConstantsStrategy;;
768 module R = Reduction ClosuresOnStackByValueFromEnvOrEnsStrategy;;
772 (* t1, t2 must be well-typed *)
773 let are_convertible =
774 let module U = UriManager in
775 let rec aux context t1 t2 =
777 (* this trivial euristic cuts down the total time of about five times ;-) *)
778 (* this because most of the time t1 and t2 are "sintactically" the same *)
783 let module C = Cic in
785 (C.Rel n1, C.Rel n2) -> n1 = n2
786 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
790 (fun (uri1,x) (uri2,y) b ->
791 U.eq uri1 uri2 && aux context x y && b
792 ) exp_named_subst1 exp_named_subst2 true
794 Invalid_argument _ -> false
796 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
804 | Some t1',Some t2' -> aux context t1' t2'
806 | (C.Sort s1, C.Sort s2) -> true (*CSC da finire con gli universi *)
807 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
809 aux ((Some (name1, (C.Decl s1)))::context) t1 t2
810 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
812 aux ((Some (name1, (C.Decl s1)))::context) t1 t2
813 | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) ->
815 aux ((Some (name1, (C.Def (s1,None))))::context) t1 t2
816 | (C.Appl l1, C.Appl l2) ->
818 List.fold_right2 (fun x y b -> aux context x y && b) l1 l2 true
820 Invalid_argument _ -> false
822 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
826 (fun (uri1,x) (uri2,y) b ->
827 U.eq uri1 uri2 && aux context x y && b
828 ) exp_named_subst1 exp_named_subst2 true
830 Invalid_argument _ -> false
832 | (C.MutInd (uri1,i1,exp_named_subst1),
833 C.MutInd (uri2,i2,exp_named_subst2)
835 U.eq uri1 uri2 && i1 = i2 &&
838 (fun (uri1,x) (uri2,y) b ->
839 U.eq uri1 uri2 && aux context x y && b
840 ) exp_named_subst1 exp_named_subst2 true
842 Invalid_argument _ -> false
844 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
845 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
847 U.eq uri1 uri2 && i1 = i2 && j1 = j2 &&
850 (fun (uri1,x) (uri2,y) b ->
851 U.eq uri1 uri2 && aux context x y && b
852 ) exp_named_subst1 exp_named_subst2 true
854 Invalid_argument _ -> false
856 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
857 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
858 U.eq uri1 uri2 && i1 = i2 && aux context outtype1 outtype2 &&
859 aux context term1 term2 &&
860 List.fold_right2 (fun x y b -> b && aux context x y) pl1 pl2 true
861 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
863 List.map (function (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
867 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) b ->
868 b && recindex1 = recindex2 && aux context ty1 ty2 &&
869 aux (tys@context) bo1 bo2)
871 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
873 List.map (function (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
877 (fun (_,ty1,bo1) (_,ty2,bo2) b ->
878 b && aux context ty1 ty2 && aux (tys@context) bo1 bo2)
880 | (C.Cast _, _) | (_, C.Cast _)
881 | (C.Implicit, _) | (_, C.Implicit) ->
882 raise (Impossible 3) (* we don't trust our whd ;-) *)
886 if aux2 t1 t2 then true
889 debug t1 [t2] "PREWHD";
890 let t1' = whd context t1 in
891 let t2' = whd context t2 in
892 debug t1' [t2'] "POSTWHD";