1 (* Copyright (C) 2002, 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 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
34 (******************************************************************************)
38 (* The code of this module is derived from the code of CicReduction *)
40 exception Impossible of int;;
41 exception ReferenceToConstant;;
42 exception ReferenceToVariable;;
43 exception ReferenceToCurrentProof;;
44 exception ReferenceToInductiveDefinition;;
45 exception WrongUriToInductiveDefinition;;
46 exception WrongUriToConstant;;
47 exception RelToHiddenHypothesis;;
50 module S = CicSubstitution
54 if debug then prerr_endline else (fun x -> ())
57 exception WhatAndWithWhatDoNotHaveTheSameLength;;
59 (* Replaces "textually" in "where" every term in "what" with the corresponding
60 term in "with_what". The terms in "what" ARE NOT lifted when binders are
61 crossed. The terms in "with_what" ARE NOT lifted when binders are crossed.
62 Every free variable in "where" IS NOT lifted by nnn.
64 let replace ~equality ~what ~with_what ~where =
66 let rec find_image_aux =
68 [],[] -> raise Not_found
69 | what::tl1,with_what::tl2 ->
70 if equality what t then with_what else find_image_aux (tl1,tl2)
71 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
73 find_image_aux (what,with_what)
81 | C.Var (uri,exp_named_subst) ->
82 C.Var (uri,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
85 | C.Implicit _ as t -> t
86 | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
87 | C.Prod (n,s,t) -> C.Prod (n, aux s, aux t)
88 | C.Lambda (n,s,t) -> C.Lambda (n, aux s, aux t)
89 | C.LetIn (n,s,t) -> C.LetIn (n, aux s, aux t)
91 (* Invariant enforced: no application of an application *)
92 (match List.map aux l with
93 (C.Appl l')::tl -> C.Appl (l'@tl)
95 | C.Const (uri,exp_named_subst) ->
96 C.Const (uri,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
97 | C.MutInd (uri,i,exp_named_subst) ->
99 (uri,i,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
100 | C.MutConstruct (uri,i,j,exp_named_subst) ->
102 (uri,i,j,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
103 | C.MutCase (sp,i,outt,t,pl) ->
104 C.MutCase (sp,i,aux outt, aux t,List.map aux pl)
108 (fun (name,i,ty,bo) -> (name, i, aux ty, aux bo))
111 C.Fix (i, substitutedfl)
115 (fun (name,ty,bo) -> (name, aux ty, aux bo))
118 C.CoFix (i, substitutedfl)
123 (* Replaces in "where" every term in "what" with the corresponding
124 term in "with_what". The terms in "what" ARE lifted when binders are
125 crossed. The terms in "with_what" ARE lifted when binders are crossed.
126 Every free variable in "where" IS NOT lifted by nnn.
127 Thus "replace_lifting_csc 1 ~with_what:[Rel 1; ... ; Rel 1]" is the
128 inverse of subst up to the fact that free variables in "where" are NOT
130 let replace_lifting ~equality ~context ~what ~with_what ~where =
131 let find_image ctx what t =
132 let rec find_image_aux =
134 [],[] -> raise Not_found
135 | what::tl1,with_what::tl2 ->
136 if equality ctx what t then with_what else find_image_aux (tl1,tl2)
137 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
139 find_image_aux (what,with_what)
141 let add_ctx ctx n s = (Some (n, Cic.Decl s))::ctx in
142 let add_ctx1 ctx n s = (Some (n, Cic.Def (s,None)))::ctx in
143 let rec substaux k ctx what t =
145 S.lift (k-1) (find_image ctx what t)
149 | C.Var (uri,exp_named_subst) ->
150 let exp_named_subst' =
151 List.map (function (uri,t) -> uri,substaux k ctx what t) exp_named_subst
153 C.Var (uri,exp_named_subst')
159 | Some t -> Some (substaux k ctx what t)
164 | C.Implicit _ as t -> t
165 | C.Cast (te,ty) -> C.Cast (substaux k ctx what te, substaux k ctx what ty)
168 (n, substaux k ctx what s, substaux (k + 1) (add_ctx ctx n s) (List.map (S.lift 1) what) t)
169 | C.Lambda (n,s,t) ->
171 (n, substaux k ctx what s, substaux (k + 1) (add_ctx ctx n s) (List.map (S.lift 1) what) t)
174 (n, substaux k ctx what s, substaux (k + 1) (add_ctx1 ctx n s) (List.map (S.lift 1) what) t)
176 (* Invariant: no Appl applied to another Appl *)
177 let tl' = List.map (substaux k ctx what) tl in
179 match substaux k ctx what he with
180 C.Appl l -> C.Appl (l@tl')
181 | _ as he' -> C.Appl (he'::tl')
183 | C.Appl _ -> assert false
184 | C.Const (uri,exp_named_subst) ->
185 let exp_named_subst' =
186 List.map (function (uri,t) -> uri,substaux k ctx what t) exp_named_subst
188 C.Const (uri,exp_named_subst')
189 | C.MutInd (uri,i,exp_named_subst) ->
190 let exp_named_subst' =
191 List.map (function (uri,t) -> uri,substaux k ctx what t) exp_named_subst
193 C.MutInd (uri,i,exp_named_subst')
194 | C.MutConstruct (uri,i,j,exp_named_subst) ->
195 let exp_named_subst' =
196 List.map (function (uri,t) -> uri,substaux k ctx what t) exp_named_subst
198 C.MutConstruct (uri,i,j,exp_named_subst')
199 | C.MutCase (sp,i,outt,t,pl) ->
200 C.MutCase (sp,i,substaux k ctx what outt, substaux k ctx what t,
201 List.map (substaux k ctx what) pl)
203 let len = List.length fl in
206 (fun (name,i,ty,bo) -> (* WRONG CTX *)
207 (name, i, substaux k ctx what ty,
208 substaux (k+len) ctx (List.map (S.lift len) what) bo)
211 C.Fix (i, substitutedfl)
213 let len = List.length fl in
216 (fun (name,ty,bo) -> (* WRONG CTX *)
217 (name, substaux k ctx what ty,
218 substaux (k+len) ctx (List.map (S.lift len) what) bo)
221 C.CoFix (i, substitutedfl)
223 substaux 1 context what where
226 (* Replaces in "where" every term in "what" with the corresponding
227 term in "with_what". The terms in "what" ARE NOT lifted when binders are
228 crossed. The terms in "with_what" ARE lifted when binders are crossed.
229 Every free variable in "where" IS lifted by nnn.
230 Thus "replace_lifting_csc 1 ~with_what:[Rel 1; ... ; Rel 1]" is the
231 inverse of subst up to the fact that "what" terms are NOT lifted. *)
232 let replace_lifting_csc nnn ~equality ~what ~with_what ~where =
234 let rec find_image_aux =
236 [],[] -> raise Not_found
237 | what::tl1,with_what::tl2 ->
238 if equality what t then with_what else find_image_aux (tl1,tl2)
239 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
241 find_image_aux (what,with_what)
243 let rec substaux k t =
245 S.lift (k-1) (find_image t)
249 if n < k then C.Rel n else C.Rel (n + nnn)
250 | C.Var (uri,exp_named_subst) ->
251 let exp_named_subst' =
252 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
254 C.Var (uri,exp_named_subst')
260 | Some t -> Some (substaux k t)
265 | C.Implicit _ as t -> t
266 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
268 C.Prod (n, substaux k s, substaux (k + 1) t)
269 | C.Lambda (n,s,t) ->
270 C.Lambda (n, substaux k s, substaux (k + 1) t)
272 C.LetIn (n, substaux k s, substaux (k + 1) t)
274 (* Invariant: no Appl applied to another Appl *)
275 let tl' = List.map (substaux k) tl in
277 match substaux k he with
278 C.Appl l -> C.Appl (l@tl')
279 | _ as he' -> C.Appl (he'::tl')
281 | C.Appl _ -> assert false
282 | C.Const (uri,exp_named_subst) ->
283 let exp_named_subst' =
284 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
286 C.Const (uri,exp_named_subst')
287 | C.MutInd (uri,i,exp_named_subst) ->
288 let exp_named_subst' =
289 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
291 C.MutInd (uri,i,exp_named_subst')
292 | C.MutConstruct (uri,i,j,exp_named_subst) ->
293 let exp_named_subst' =
294 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
296 C.MutConstruct (uri,i,j,exp_named_subst')
297 | C.MutCase (sp,i,outt,t,pl) ->
298 C.MutCase (sp,i,substaux k outt, substaux k t,
299 List.map (substaux k) pl)
301 let len = List.length fl in
304 (fun (name,i,ty,bo) ->
305 (name, i, substaux k ty, substaux (k+len) bo))
308 C.Fix (i, substitutedfl)
310 let len = List.length fl in
314 (name, substaux k ty, substaux (k+len) bo))
317 C.CoFix (i, substitutedfl)
322 (* This is like "replace_lifting_csc 1 ~with_what:[Rel 1; ... ; Rel 1]"
323 up to the fact that the index to start from can be specified *)
324 let replace_with_rel_1_from ~equality ~what =
325 let rec find_image t = function
327 | hd :: tl -> equality t hd || find_image t tl
329 let rec subst_term k t =
330 if find_image t what then C.Rel k else inspect_term k t
331 and inspect_term k = function
332 | C.Rel i -> if i < k then C.Rel i else C.Rel (succ i)
334 | C.Implicit _ as t -> t
335 | C.Var (uri, enss) ->
336 let enss = List.map (subst_ens k) enss in
338 | C.Const (uri ,enss) ->
339 let enss = List.map (subst_ens k) enss in
341 | C.MutInd (uri, tyno, enss) ->
342 let enss = List.map (subst_ens k) enss in
343 C.MutInd (uri, tyno, enss)
344 | C.MutConstruct (uri, tyno, consno, enss) ->
345 let enss = List.map (subst_ens k) enss in
346 C.MutConstruct (uri, tyno, consno, enss)
348 let mss = List.map (subst_ms k) mss in
350 | C.Cast (t, v) -> C.Cast (subst_term k t, subst_term k v)
352 let ts = List.map (subst_term k) ts in
354 | C.MutCase (uri, tyno, outty, t, cases) ->
355 let cases = List.map (subst_term k) cases in
356 C.MutCase (uri, tyno, subst_term k outty, subst_term k t, cases)
357 | C.Prod (n, v, t) ->
358 C.Prod (n, subst_term k v, subst_term (succ k) t)
359 | C.Lambda (n, v, t) ->
360 C.Lambda (n, subst_term k v, subst_term (succ k) t)
361 | C.LetIn (n, v, t) ->
362 C.LetIn (n, subst_term k v, subst_term (succ k) t)
363 | C.Fix (i, fixes) ->
364 let fixesno = List.length fixes in
365 let fixes = List.map (subst_fix fixesno k) fixes in
367 | C.CoFix (i, cofixes) ->
368 let cofixesno = List.length cofixes in
369 let cofixes = List.map (subst_cofix cofixesno k) cofixes in
371 and subst_ens k (uri, t) = uri, subst_term k t
372 and subst_ms k = function
374 | Some t -> Some (subst_term k t)
375 and subst_fix fixesno k (n, ind, ty, bo) =
376 n, ind, subst_term k ty, subst_term (k + fixesno) bo
377 and subst_cofix cofixesno k (n, ty, bo) =
378 n, subst_term k ty, subst_term (k + cofixesno) bo
385 (* Takes a well-typed term and fully reduces it. *)
386 (*CSC: It does not perform reduction in a Case *)
388 let rec reduceaux context l =
391 (match List.nth context (n-1) with
392 Some (_,C.Decl _) -> if l = [] then t else C.Appl (t::l)
393 | Some (_,C.Def (bo,_)) -> reduceaux context l (S.lift n bo)
394 | None -> raise RelToHiddenHypothesis
396 | C.Var (uri,exp_named_subst) ->
397 let exp_named_subst' =
398 reduceaux_exp_named_subst context l exp_named_subst
400 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
402 C.Constant _ -> raise ReferenceToConstant
403 | C.CurrentProof _ -> raise ReferenceToCurrentProof
404 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
405 | C.Variable (_,None,_,_,_) ->
406 let t' = C.Var (uri,exp_named_subst') in
407 if l = [] then t' else C.Appl (t'::l)
408 | C.Variable (_,Some body,_,_,_) ->
410 (CicSubstitution.subst_vars exp_named_subst' body))
412 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
413 | C.Sort _ as t -> t (* l should be empty *)
414 | C.Implicit _ as t -> t
416 C.Cast (reduceaux context l te, reduceaux context l ty)
417 | C.Prod (name,s,t) ->
420 reduceaux context [] s,
421 reduceaux ((Some (name,C.Decl s))::context) [] t)
422 | C.Lambda (name,s,t) ->
426 reduceaux context [] s,
427 reduceaux ((Some (name,C.Decl s))::context) [] t)
428 | he::tl -> reduceaux context tl (S.subst he t)
429 (* when name is Anonimous the substitution should be superfluous *)
432 reduceaux context l (S.subst (reduceaux context [] s) t)
434 let tl' = List.map (reduceaux context []) tl in
435 reduceaux context (tl'@l) he
436 | C.Appl [] -> raise (Impossible 1)
437 | C.Const (uri,exp_named_subst) ->
438 let exp_named_subst' =
439 reduceaux_exp_named_subst context l exp_named_subst
441 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
443 C.Constant (_,Some body,_,_,_) ->
445 (CicSubstitution.subst_vars exp_named_subst' body))
446 | C.Constant (_,None,_,_,_) ->
447 let t' = C.Const (uri,exp_named_subst') in
448 if l = [] then t' else C.Appl (t'::l)
449 | C.Variable _ -> raise ReferenceToVariable
450 | C.CurrentProof (_,_,body,_,_,_) ->
452 (CicSubstitution.subst_vars exp_named_subst' body))
453 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
455 | C.MutInd (uri,i,exp_named_subst) ->
456 let exp_named_subst' =
457 reduceaux_exp_named_subst context l exp_named_subst
459 let t' = C.MutInd (uri,i,exp_named_subst') in
460 if l = [] then t' else C.Appl (t'::l)
461 | C.MutConstruct (uri,i,j,exp_named_subst) ->
462 let exp_named_subst' =
463 reduceaux_exp_named_subst context l exp_named_subst
465 let t' = C.MutConstruct (uri,i,j,exp_named_subst') in
466 if l = [] then t' else C.Appl (t'::l)
467 | C.MutCase (mutind,i,outtype,term,pl) ->
471 let (_,_,body) = List.nth fl i in
473 let counter = ref (List.length fl) in
475 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
479 reduceaux context [] body'
480 | C.Appl (C.CoFix (i,fl) :: tl) ->
481 let (_,_,body) = List.nth fl i in
483 let counter = ref (List.length fl) in
485 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
489 let tl' = List.map (reduceaux context []) tl in
490 reduceaux context tl' body'
493 (match decofix (reduceaux context [] term) with
494 C.MutConstruct (_,_,j,_) -> reduceaux context l (List.nth pl (j-1))
495 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
497 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph mutind in
499 C.InductiveDefinition (tl,_,r,_) ->
500 let (_,_,arity,_) = List.nth tl i in
502 | _ -> raise WrongUriToInductiveDefinition
508 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
509 | _ -> raise (Impossible 5)
513 reduceaux context (ts@l) (List.nth pl (j-1))
514 | C.Cast _ | C.Implicit _ ->
515 raise (Impossible 2) (* we don't trust our whd ;-) *)
517 let outtype' = reduceaux context [] outtype in
518 let term' = reduceaux context [] term in
519 let pl' = List.map (reduceaux context []) pl in
521 C.MutCase (mutind,i,outtype',term',pl')
523 if l = [] then res else C.Appl (res::l)
528 (fun (types,len) (n,_,ty,_) ->
529 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
536 (function (n,recindex,ty,bo) ->
537 (n,recindex,reduceaux context [] ty, reduceaux (tys@context) [] bo)
542 let (_,recindex,_,body) = List.nth fl i in
545 Some (List.nth l recindex)
551 (match reduceaux context [] recparam with
553 | C.Appl ((C.MutConstruct _)::_) ->
555 let counter = ref (List.length fl) in
557 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
561 (* Possible optimization: substituting whd recparam in l*)
562 reduceaux context l body'
563 | _ -> if l = [] then t' () else C.Appl ((t' ())::l)
565 | None -> if l = [] then t' () else C.Appl ((t' ())::l)
570 (fun (types,len) (n,ty,_) ->
571 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
578 (function (n,ty,bo) ->
579 (n,reduceaux context [] ty, reduceaux (tys@context) [] bo)
584 if l = [] then t' else C.Appl (t'::l)
585 and reduceaux_exp_named_subst context l =
586 List.map (function uri,t -> uri,reduceaux context [] t)
592 let unfold ?what context where =
593 let contextlen = List.length context in
594 let first_is_the_expandable_head_of_second context' t1 t2 =
596 Cic.Const (uri,_), Cic.Const (uri',_)
597 | Cic.Var (uri,_), Cic.Var (uri',_)
598 | Cic.Const (uri,_), Cic.Appl (Cic.Const (uri',_)::_)
599 | Cic.Var (uri,_), Cic.Appl (Cic.Var (uri',_)::_) -> UriManager.eq uri uri'
601 | Cic.Var _, _ -> false
602 | Cic.Rel n, Cic.Rel m
603 | Cic.Rel n, Cic.Appl (Cic.Rel m::_) ->
604 n + (List.length context' - contextlen) = m
605 | Cic.Rel _, _ -> false
608 (ProofEngineTypes.Fail
609 (lazy "The term to unfold is not a constant, a variable or a bound variable "))
612 if tl = [] then he else Cic.Appl (he::tl) in
613 let cannot_delta_expand t =
615 (ProofEngineTypes.Fail
616 (lazy ("The term " ^ CicPp.ppterm t ^ " cannot be delta-expanded"))) in
617 let rec hd_delta_beta context tl =
621 match List.nth context (n-1) with
622 Some (_,Cic.Decl _) -> cannot_delta_expand t
623 | Some (_,Cic.Def (bo,_)) ->
624 CicReduction.head_beta_reduce
625 (appl (CicSubstitution.lift n bo) tl)
626 | None -> raise RelToHiddenHypothesis
628 Failure _ -> assert false)
629 | Cic.Const (uri,exp_named_subst) as t ->
630 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
632 Cic.Constant (_,Some body,_,_,_) ->
633 CicReduction.head_beta_reduce
634 (appl (CicSubstitution.subst_vars exp_named_subst body) tl)
635 | Cic.Constant (_,None,_,_,_) -> cannot_delta_expand t
636 | Cic.Variable _ -> raise ReferenceToVariable
637 | Cic.CurrentProof _ -> raise ReferenceToCurrentProof
638 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
640 | Cic.Var (uri,exp_named_subst) as t ->
641 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
643 Cic.Constant _ -> raise ReferenceToConstant
644 | Cic.CurrentProof _ -> raise ReferenceToCurrentProof
645 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
646 | Cic.Variable (_,Some body,_,_,_) ->
647 CicReduction.head_beta_reduce
648 (appl (CicSubstitution.subst_vars exp_named_subst body) tl)
649 | Cic.Variable (_,None,_,_,_) -> cannot_delta_expand t
651 | Cic.Appl [] -> assert false
652 | Cic.Appl (he::tl) -> hd_delta_beta context tl he
653 | t -> cannot_delta_expand t
655 let context_and_matched_term_list =
657 None -> [context, where]
660 ProofEngineHelpers.locate_in_term
661 ~equality:first_is_the_expandable_head_of_second
666 (ProofEngineTypes.Fail
667 (lazy ("Term "^ CicPp.ppterm what ^ " not found in " ^ CicPp.ppterm where)))
673 (function (context,where) -> hd_delta_beta context [] where)
674 context_and_matched_term_list in
675 let whats = List.map snd context_and_matched_term_list in
676 replace ~equality:(==) ~what:whats ~with_what:reduced_terms ~where
679 exception WrongShape;;
680 exception AlreadySimplified;;
682 (* Takes a well-typed term and *)
683 (* 1) Performs beta-iota-zeta reduction until delta reduction is needed *)
684 (* 2) Attempts delta-reduction. If the residual is a Fix lambda-abstracted *)
685 (* w.r.t. zero or more variables and if the Fix can be reductaed, than it*)
686 (* is reduced, the delta-reduction is succesfull and the whole algorithm *)
687 (* is applied again to the new redex; Step 3.1) is applied to the result *)
688 (* of the recursive simplification. Otherwise, if the Fix can not be *)
689 (* reduced, than the delta-reductions fails and the delta-redex is *)
690 (* not reduced. Otherwise, if the delta-residual is not the *)
691 (* lambda-abstraction of a Fix, then it performs step 3.2). *)
692 (* 3.1) Folds the application of the constant to the arguments that did not *)
693 (* change in every iteration, i.e. to the actual arguments for the *)
694 (* lambda-abstractions that precede the Fix. *)
695 (* 3.2) Computes the head beta-zeta normal form of the term. Then it tries *)
696 (* reductions. If the reduction cannot be performed, it returns the *)
697 (* original term (not the head beta-zeta normal form of the definiendum) *)
698 (*CSC: It does not perform simplification in a Case *)
701 (* a simplified term is active if it can create a redex when used as an *)
702 (* actual parameter *)
707 | C.Appl (C.MutConstruct _::_)
709 | C.Cast (bo,_) -> is_active bo
710 | C.LetIn _ -> assert false
713 (* reduceaux is equal to the reduceaux locally defined inside *)
714 (* reduce, but for the const case. *)
716 let rec reduceaux context l =
719 (* we never perform delta expansion automatically *)
720 if l = [] then t else C.Appl (t::l)
721 | C.Var (uri,exp_named_subst) ->
722 let exp_named_subst' =
723 reduceaux_exp_named_subst context l exp_named_subst
725 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
727 C.Constant _ -> raise ReferenceToConstant
728 | C.CurrentProof _ -> raise ReferenceToCurrentProof
729 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
730 | C.Variable (_,None,_,_,_) ->
731 let t' = C.Var (uri,exp_named_subst') in
732 if l = [] then t' else C.Appl (t'::l)
733 | C.Variable (_,Some body,_,_,_) ->
735 (CicSubstitution.subst_vars exp_named_subst' body)
737 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
738 | C.Sort _ as t -> t (* l should be empty *)
739 | C.Implicit _ as t -> t
741 C.Cast (reduceaux context l te, reduceaux context [] ty)
742 | C.Prod (name,s,t) ->
745 reduceaux context [] s,
746 reduceaux ((Some (name,C.Decl s))::context) [] t)
747 | C.Lambda (name,s,t) ->
751 reduceaux context [] s,
752 reduceaux ((Some (name,C.Decl s))::context) [] t)
753 | he::tl -> reduceaux context tl (S.subst he t)
754 (* when name is Anonimous the substitution should be superfluous *)
757 reduceaux context l (S.subst (reduceaux context [] s) t)
759 let tl' = List.map (reduceaux context []) tl in
760 reduceaux context (tl'@l) he
761 | C.Appl [] -> raise (Impossible 1)
762 | C.Const (uri,exp_named_subst) ->
763 let exp_named_subst' =
764 reduceaux_exp_named_subst context l exp_named_subst
766 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
768 C.Constant (_,Some body,_,_,_) ->
769 if List.exists is_active l then
770 try_delta_expansion context l
771 (C.Const (uri,exp_named_subst'))
772 (CicSubstitution.subst_vars exp_named_subst' body)
774 let t' = C.Const (uri,exp_named_subst') in
775 if l = [] then t' else C.Appl (t'::l)
776 | C.Constant (_,None,_,_,_) ->
777 let t' = C.Const (uri,exp_named_subst') in
778 if l = [] then t' else C.Appl (t'::l)
779 | C.Variable _ -> raise ReferenceToVariable
780 | C.CurrentProof (_,_,body,_,_,_) -> reduceaux context l body
781 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
783 | C.MutInd (uri,i,exp_named_subst) ->
784 let exp_named_subst' =
785 reduceaux_exp_named_subst context l exp_named_subst
787 let t' = C.MutInd (uri,i,exp_named_subst') in
788 if l = [] then t' else C.Appl (t'::l)
789 | C.MutConstruct (uri,i,j,exp_named_subst) ->
790 let exp_named_subst' =
791 reduceaux_exp_named_subst context l exp_named_subst
793 let t' = C.MutConstruct(uri,i,j,exp_named_subst') in
794 if l = [] then t' else C.Appl (t'::l)
795 | C.MutCase (mutind,i,outtype,term,pl) ->
799 let (_,_,body) = List.nth fl i in
801 let counter = ref (List.length fl) in
803 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
807 reduceaux context [] body'
808 | C.Appl (C.CoFix (i,fl) :: tl) ->
809 let (_,_,body) = List.nth fl i in
811 let counter = ref (List.length fl) in
813 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
817 let tl' = List.map (reduceaux context []) tl in
818 reduceaux context tl' body'
821 (match decofix (reduceaux context [] term) (*(CicReduction.whd context term)*) with
822 C.MutConstruct (_,_,j,_) -> reduceaux context l (List.nth pl (j-1))
823 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
825 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph mutind in
827 C.InductiveDefinition (tl,ingredients,r,_) ->
828 let (_,_,arity,_) = List.nth tl i in
830 | _ -> raise WrongUriToInductiveDefinition
836 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
837 | _ -> raise (Impossible 5)
841 reduceaux context (ts@l) (List.nth pl (j-1))
842 | C.Cast _ | C.Implicit _ ->
843 raise (Impossible 2) (* we don't trust our whd ;-) *)
845 let outtype' = reduceaux context [] outtype in
846 let term' = reduceaux context [] term in
847 let pl' = List.map (reduceaux context []) pl in
849 C.MutCase (mutind,i,outtype',term',pl')
851 if l = [] then res else C.Appl (res::l)
856 (fun (types,len) (n,_,ty,_) ->
857 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
864 (function (n,recindex,ty,bo) ->
865 (n,recindex,reduceaux context [] ty, reduceaux (tys@context) [] bo)
870 let (_,recindex,_,body) = List.nth fl i in
873 Some (List.nth l recindex)
879 (match reduceaux context [] recparam with
881 | C.Appl ((C.MutConstruct _)::_) ->
883 let counter = ref (List.length fl) in
885 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
889 (* Possible optimization: substituting whd recparam in l*)
890 reduceaux context l body'
891 | _ -> if l = [] then t' () else C.Appl ((t' ())::l)
893 | None -> if l = [] then t' () else C.Appl ((t' ())::l)
898 (fun (types,len) (n,ty,_) ->
899 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
906 (function (n,ty,bo) ->
907 (n,reduceaux context [] ty, reduceaux (tys@context) [] bo)
912 if l = [] then t' else C.Appl (t'::l)
913 and reduceaux_exp_named_subst context l =
914 List.map (function uri,t -> uri,reduceaux context [] t)
916 and reduce_with_no_hope_to_fold_back t l =
917 prerr_endline "reduce_with_no_hope_to_fold_back";
918 let simplified = reduceaux context l t in
919 let t' = if l = [] then t else C.Appl (t::l) in
920 if t' = simplified then
921 raise AlreadySimplified
925 and try_delta_expansion context l term body =
927 let res,constant_args =
928 let rec aux rev_constant_args l =
930 C.Lambda (name,s,t) ->
933 [] -> raise WrongShape
935 (* when name is Anonimous the substitution should *)
937 aux (he::rev_constant_args) tl (S.subst he t)
940 aux rev_constant_args l (S.subst s t)
942 let (_,recindex,_,body) = List.nth fl i in
947 _ -> raise AlreadySimplified
949 (match reduceaux context [] recparam (*CicReduction.whd context recparam*) with
951 | C.Appl ((C.MutConstruct _)::_) ->
953 let counter = ref (List.length fl) in
956 decr counter ; S.subst (C.Fix (!counter,fl))
959 (* Possible optimization: substituting whd *)
961 reduceaux context l body',
962 List.rev rev_constant_args
963 | _ -> raise AlreadySimplified
965 | _ -> raise WrongShape
970 let term_to_fold, delta_expanded_term_to_fold =
971 match constant_args with
973 | _ -> C.Appl (term::constant_args), C.Appl (body::constant_args)
975 let simplified_term_to_fold =
976 reduceaux context [] delta_expanded_term_to_fold
978 replace_lifting ~equality:(fun _ x y -> x = y) ~context
979 ~what:[simplified_term_to_fold] ~with_what:[term_to_fold] ~where:res
982 let rec skip_lambda n = function
983 | Cic.Lambda (_,_,t) -> skip_lambda (n+1) t | t -> t, n
986 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
987 | Cic.Constant (_,Some bo, _, _,_) ->
988 (let t, _ = skip_lambda 0 bo in
989 match t with | Cic.Fix _ -> true | _ -> false)
992 let guess_recno uri =
993 prerr_endline ("GUESS: " ^ UriManager.string_of_uri uri);
994 match fst(CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) with
995 | Cic.Constant (_,Some bo, _, _,_ ) ->
996 let t, n = skip_lambda 0 bo in
999 let _,recno,_,_ = List.nth fl i in
1000 prerr_endline ("GUESSED: " ^ string_of_int recno ^ " after " ^
1001 string_of_int n ^ " lambdas");
1003 | _ -> assert false)
1006 let original_args = l in
1007 (**** Step 3.2 ****)
1010 | C.Lambda (name,s,t) ->
1012 | [] -> raise AlreadySimplified
1014 (* when name is Anonimous the substitution should *)
1015 (* be superfluous *)
1016 aux tl (S.subst he t))
1017 | C.LetIn (_,s,t) -> aux l (S.subst s t)
1018 | Cic.Appl (Cic.Const (uri,_) :: args) as t when is_fix uri ->
1020 prerr_endline ("cerco : " ^ string_of_int (guess_recno uri)
1021 ^ " in: " ^ String.concat " "
1022 (List.map (fun x -> CicPp.ppterm x) args));
1023 prerr_endline ("e piglio il rispettivo in :"^String.concat " "
1024 (List.map (fun x -> CicPp.ppterm x) original_args));
1025 (* look for args[regno] in saved_args *)
1026 let wanted = List.nth (args@l) (guess_recno uri) in
1027 let rec aux n = function
1028 | [] -> n (* DA CAPIRE *)
1029 | t::_ when t = wanted -> n
1030 | _::tl -> aux (n+1) tl
1034 if recno = List.length original_args then
1035 reduce_with_no_hope_to_fold_back t l
1037 let simplified = reduceaux context l t in
1038 let rec mk_implicits = function
1039 | n,_::tl when n = recno ->
1040 Cic.Implicit None :: (mk_implicits (n+1,tl))
1041 | n,arg::tl -> arg :: (mk_implicits (n+1,tl))
1044 (* we try to fold back constant that do not expand to Fix *)
1045 let _ = prerr_endline
1046 ("INIZIO (" ^ string_of_int recno ^ ") : " ^ CicPp.ppterm
1049 Cic.Appl (term:: mk_implicits (0,original_args))
1052 let term_to_fold, _, metasenv, _ =
1053 CicRefine.type_of_aux' [] context term_to_fold
1054 CicUniv.oblivion_ugraph
1057 prerr_endline ("RAFFINA: "^CicPp.ppterm term_to_fold) in
1060 ("RAFFINA: "^CicMetaSubst.ppmetasenv [] metasenv) in
1061 let simplified_term_to_fold = unfold context term_to_fold in
1063 prerr_endline ("SEMPLIFICA: " ^
1064 CicPp.ppterm simplified_term_to_fold)
1068 if t1 = t then t else do_n f t1
1072 let subst = ref [] in
1073 let myunif ctx t1 t2 =
1074 if !subst <> [] then false
1077 prerr_endline "MUNIF";
1078 prerr_endline (CicPp.ppterm t1);
1080 prerr_endline (CicPp.ppterm t2 ^ "\n");
1082 CicUnification.fo_unif metasenv ctx t1 t2
1083 CicUniv.empty_ugraph
1085 prerr_endline "UNIFICANO\n\n\n";
1089 | CicUnification.UnificationFailure s
1090 | CicUnification.Uncertain s
1091 | CicUnification.AssertFailure s ->
1092 prerr_endline (Lazy.force s); false
1093 | CicUtil.Meta_not_found _ -> false
1096 prerr_endline (Printexc.to_string exn);
1100 replace_lifting myunif context
1101 [simplified_term_to_fold] [term_to_fold] simplified
1103 let _ = prerr_endline "UNIFICA" in
1104 if List.length metasenv <> List.length !subst then
1105 let _ = prerr_endline ("SUBST CORTA " ^
1106 CicMetaSubst.ppsubst !subst ~metasenv)
1110 if t = simplified then
1111 let _ = prerr_endline "NULLA DI FATTO" in
1114 let t = CicMetaSubst.apply_subst !subst t in
1115 prerr_endline ("ECCO: " ^ CicPp.ppterm t); t)
1118 | CicRefine.RefineFailure s
1119 | CicRefine.Uncertain s
1120 | CicRefine.AssertFailure s ->
1121 prerr_endline (Lazy.force s); simplified
1122 (*| exn -> prerr_endline (Printexc.to_string exn); simplified*))
1123 | t -> reduce_with_no_hope_to_fold_back t l
1127 AlreadySimplified ->
1128 if l = [] then term else C.Appl (term::l))
1129 | AlreadySimplified ->
1130 (* If we performed delta-reduction, we would find a Fix *)
1131 (* not applied to a constructor. So, we refuse to perform *)
1132 (* delta-reduction. *)
1133 if l = [] then term else C.Appl (term::l)
1135 reduceaux context []