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
52 let alpha_equivalence =
57 C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2) ->
58 UriManager.eq uri1 uri2 &&
59 aux_exp_named_subst exp_named_subst1 exp_named_subst2
60 | C.Cast (te,ty), C.Cast (te',ty') ->
61 aux te te' && aux ty ty'
62 | C.Prod (_,s,t), C.Prod (_,s',t') ->
64 | C.Lambda (_,s,t), C.Lambda (_,s',t') ->
66 | C.LetIn (_,s,t), C.LetIn(_,s',t') ->
68 | C.Appl l, C.Appl l' ->
71 (fun b t1 t2 -> b && aux t1 t2) true l l'
73 Invalid_argument _ -> false)
74 | C.Const (uri,exp_named_subst1), C.Const (uri',exp_named_subst2) ->
75 UriManager.eq uri uri' &&
76 aux_exp_named_subst exp_named_subst1 exp_named_subst2
77 | C.MutInd (uri,i,exp_named_subst1), C.MutInd (uri',i',exp_named_subst2) ->
78 UriManager.eq uri uri' && i = i' &&
79 aux_exp_named_subst exp_named_subst1 exp_named_subst2
80 | C.MutConstruct (uri,i,j,exp_named_subst1),
81 C.MutConstruct (uri',i',j',exp_named_subst2) ->
82 UriManager.eq uri uri' && i = i' && j = j' &&
83 aux_exp_named_subst exp_named_subst1 exp_named_subst2
84 | C.MutCase (sp,i,outt,t,pl), C.MutCase (sp',i',outt',t',pl') ->
85 UriManager.eq sp sp' && i = i' &&
86 aux outt outt' && aux t t' &&
89 (fun b t1 t2 -> b && aux t1 t2) true pl pl'
91 Invalid_argument _ -> false)
92 | C.Fix (i,fl), C.Fix (i',fl') ->
96 (fun b (_,i,ty,bo) (_,i',ty',bo') ->
97 b && i = i' && aux ty ty' && aux bo bo'
100 Invalid_argument _ -> false)
101 | C.CoFix (i,fl), C.CoFix (i',fl') ->
105 (fun b (_,ty,bo) (_,ty',bo') ->
106 b && aux ty ty' && aux bo bo'
109 Invalid_argument _ -> false)
110 | _,_ -> false (* we already know that t != t' *)
111 and aux_exp_named_subst exp_named_subst1 exp_named_subst2 =
114 (fun b (uri1,t1) (uri2,t2) ->
115 b && UriManager.eq uri1 uri2 && aux t1 t2
116 ) true exp_named_subst1 exp_named_subst2
118 Invalid_argument _ -> false
123 exception WhatAndWithWhatDoNotHaveTheSameLength;;
125 (* "textual" replacement of several subterms with other ones *)
126 let replace ~equality ~what ~with_what ~where =
128 let rec find_image_aux =
130 [],[] -> raise Not_found
131 | what::tl1,with_what::tl2 ->
132 if equality what t then with_what else find_image_aux (tl1,tl2)
133 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
135 find_image_aux (what,with_what)
143 | C.Var (uri,exp_named_subst) ->
144 C.Var (uri,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
147 | C.Implicit _ as t -> t
148 | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
149 | C.Prod (n,s,t) -> C.Prod (n, aux s, aux t)
150 | C.Lambda (n,s,t) -> C.Lambda (n, aux s, aux t)
151 | C.LetIn (n,s,t) -> C.LetIn (n, aux s, aux t)
153 (* Invariant enforced: no application of an application *)
154 (match List.map aux l with
155 (C.Appl l')::tl -> C.Appl (l'@tl)
157 | C.Const (uri,exp_named_subst) ->
158 C.Const (uri,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
159 | C.MutInd (uri,i,exp_named_subst) ->
161 (uri,i,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
162 | C.MutConstruct (uri,i,j,exp_named_subst) ->
164 (uri,i,j,List.map (function (uri,t) -> uri, aux t) exp_named_subst)
165 | C.MutCase (sp,i,outt,t,pl) ->
166 C.MutCase (sp,i,aux outt, aux t,List.map aux pl)
170 (fun (name,i,ty,bo) -> (name, i, aux ty, aux bo))
173 C.Fix (i, substitutedfl)
177 (fun (name,ty,bo) -> (name, aux ty, aux bo))
180 C.CoFix (i, substitutedfl)
185 (* replaces in a term a term with another one. *)
186 (* Lifting are performed as usual. *)
187 let replace_lifting ~equality ~what ~with_what ~where =
188 let find_image what t =
189 let rec find_image_aux =
191 [],[] -> raise Not_found
192 | what::tl1,with_what::tl2 ->
193 if equality what t then with_what else find_image_aux (tl1,tl2)
194 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
196 find_image_aux (what,with_what)
198 let rec substaux k what t =
200 S.lift (k-1) (find_image what t)
204 | C.Var (uri,exp_named_subst) ->
205 let exp_named_subst' =
206 List.map (function (uri,t) -> uri,substaux k what t) exp_named_subst
208 C.Var (uri,exp_named_subst')
214 | Some t -> Some (substaux k what t)
219 | C.Implicit _ as t -> t
220 | C.Cast (te,ty) -> C.Cast (substaux k what te, substaux k what ty)
223 (n, substaux k what s, substaux (k + 1) (List.map (S.lift 1) what) t)
224 | C.Lambda (n,s,t) ->
226 (n, substaux k what s, substaux (k + 1) (List.map (S.lift 1) what) t)
229 (n, substaux k what s, substaux (k + 1) (List.map (S.lift 1) what) t)
231 (* Invariant: no Appl applied to another Appl *)
232 let tl' = List.map (substaux k what) tl in
234 match substaux k what he with
235 C.Appl l -> C.Appl (l@tl')
236 | _ as he' -> C.Appl (he'::tl')
238 | C.Appl _ -> assert false
239 | C.Const (uri,exp_named_subst) ->
240 let exp_named_subst' =
241 List.map (function (uri,t) -> uri,substaux k what t) exp_named_subst
243 C.Const (uri,exp_named_subst')
244 | C.MutInd (uri,i,exp_named_subst) ->
245 let exp_named_subst' =
246 List.map (function (uri,t) -> uri,substaux k what t) exp_named_subst
248 C.MutInd (uri,i,exp_named_subst')
249 | C.MutConstruct (uri,i,j,exp_named_subst) ->
250 let exp_named_subst' =
251 List.map (function (uri,t) -> uri,substaux k what t) exp_named_subst
253 C.MutConstruct (uri,i,j,exp_named_subst')
254 | C.MutCase (sp,i,outt,t,pl) ->
255 C.MutCase (sp,i,substaux k what outt, substaux k what t,
256 List.map (substaux k what) pl)
258 let len = List.length fl in
261 (fun (name,i,ty,bo) ->
262 (name, i, substaux k what ty,
263 substaux (k+len) (List.map (S.lift len) what) bo)
266 C.Fix (i, substitutedfl)
268 let len = List.length fl in
272 (name, substaux k what ty,
273 substaux (k+len) (List.map (S.lift len) what) bo)
276 C.CoFix (i, substitutedfl)
278 substaux 1 what where
281 (* replaces in a term a list of terms with other ones. *)
282 (* Lifting are performed as usual. *)
283 let replace_lifting_csc nnn ~equality ~what ~with_what ~where =
285 let rec find_image_aux =
287 [],[] -> raise Not_found
288 | what::tl1,with_what::tl2 ->
289 if equality what t then with_what else find_image_aux (tl1,tl2)
290 | _,_ -> raise WhatAndWithWhatDoNotHaveTheSameLength
292 find_image_aux (what,with_what)
294 let rec substaux k t =
296 S.lift (k-1) (find_image t)
300 if n < k then C.Rel n else C.Rel (n + nnn)
301 | C.Var (uri,exp_named_subst) ->
302 let exp_named_subst' =
303 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
305 C.Var (uri,exp_named_subst')
311 | Some t -> Some (substaux k t)
316 | C.Implicit _ as t -> t
317 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
319 C.Prod (n, substaux k s, substaux (k + 1) t)
320 | C.Lambda (n,s,t) ->
321 C.Lambda (n, substaux k s, substaux (k + 1) t)
323 C.LetIn (n, substaux k s, substaux (k + 1) t)
325 (* Invariant: no Appl applied to another Appl *)
326 let tl' = List.map (substaux k) tl in
328 match substaux k he with
329 C.Appl l -> C.Appl (l@tl')
330 | _ as he' -> C.Appl (he'::tl')
332 | C.Appl _ -> assert false
333 | C.Const (uri,exp_named_subst) ->
334 let exp_named_subst' =
335 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
337 C.Const (uri,exp_named_subst')
338 | C.MutInd (uri,i,exp_named_subst) ->
339 let exp_named_subst' =
340 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
342 C.MutInd (uri,i,exp_named_subst')
343 | C.MutConstruct (uri,i,j,exp_named_subst) ->
344 let exp_named_subst' =
345 List.map (function (uri,t) -> uri,substaux k t) exp_named_subst
347 C.MutConstruct (uri,i,j,exp_named_subst')
348 | C.MutCase (sp,i,outt,t,pl) ->
349 C.MutCase (sp,i,substaux k outt, substaux k t,
350 List.map (substaux k) pl)
352 let len = List.length fl in
355 (fun (name,i,ty,bo) ->
356 (name, i, substaux k ty, substaux (k+len) bo))
359 C.Fix (i, substitutedfl)
361 let len = List.length fl in
365 (name, substaux k ty, substaux (k+len) bo))
368 C.CoFix (i, substitutedfl)
373 (* This is the inverse of the subst function. *)
374 let subst_inv ~equality ~what =
375 let rec find_image t = function
377 | hd :: tl -> equality t hd || find_image t tl
379 let rec subst_term k t =
380 if find_image t what then C.Rel k else inspect_term k t
381 and inspect_term k = function
382 | C.Rel n -> if n < k then C.Rel n else C.Rel (succ n)
384 | C.Implicit _ as t -> t
385 | C.Var (uri, enss) ->
386 let enss = List.map (subst_ens k) enss in
388 | C.Const (uri ,enss) ->
389 let enss = List.map (subst_ens k) enss in
391 | C.MutInd (uri, tyno, enss) ->
392 let enss = List.map (subst_ens k) enss in
393 C.MutInd (uri, tyno, enss)
394 | C.MutConstruct (uri, tyno, consno, enss) ->
395 let enss = List.map (subst_ens k) enss in
396 C.MutConstruct (uri, tyno, consno, enss)
398 let mss = List.map (subst_ms k) mss in
400 | C.Cast (t, v) -> C.Cast (subst_term k t, subst_term k v)
402 let ts = List.map (subst_term k) ts in
404 | C.MutCase (uri, tyno, outty, t, cases) ->
405 let cases = List.map (subst_term k) cases in
406 C.MutCase (uri, tyno, subst_term k outty, subst_term k t, cases)
407 | C.Prod (n, v, t) ->
408 C.Prod (n, subst_term k v, subst_term (succ k) t)
409 | C.Lambda (n, v, t) ->
410 C.Lambda (n, subst_term k v, subst_term (succ k) t)
411 | C.LetIn (n, v, t) ->
412 C.LetIn (n, subst_term k v, subst_term (succ k) t)
413 | C.Fix (i, fixes) ->
414 let fixesno = List.length fixes in
415 let fixes = List.map (subst_fix fixesno k) fixes in
417 | C.CoFix (i, cofixes) ->
418 let cofixesno = List.length cofixes in
419 let cofixes = List.map (subst_cofix cofixesno k) cofixes in
421 and subst_ens k (uri, t) = uri, subst_term k t
422 and subst_ms k = function
424 | Some t -> Some (subst_term k t)
425 and subst_fix fixesno k (n, ind, ty, bo) =
426 n, ind, subst_term k ty, subst_term (k + fixesno) bo
427 and subst_cofix cofixesno k (n, ty, bo) =
428 n, subst_term k ty, subst_term (k + cofixesno) bo
435 (* Takes a well-typed term and fully reduces it. *)
436 (*CSC: It does not perform reduction in a Case *)
438 let rec reduceaux context l =
441 (match List.nth context (n-1) with
442 Some (_,C.Decl _) -> if l = [] then t else C.Appl (t::l)
443 | Some (_,C.Def (bo,_)) -> reduceaux context l (S.lift n bo)
444 | None -> raise RelToHiddenHypothesis
446 | C.Var (uri,exp_named_subst) ->
447 let exp_named_subst' =
448 reduceaux_exp_named_subst context l exp_named_subst
450 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
452 C.Constant _ -> raise ReferenceToConstant
453 | C.CurrentProof _ -> raise ReferenceToCurrentProof
454 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
455 | C.Variable (_,None,_,_,_) ->
456 let t' = C.Var (uri,exp_named_subst') in
457 if l = [] then t' else C.Appl (t'::l)
458 | C.Variable (_,Some body,_,_,_) ->
460 (CicSubstitution.subst_vars exp_named_subst' body))
462 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
463 | C.Sort _ as t -> t (* l should be empty *)
464 | C.Implicit _ as t -> t
466 C.Cast (reduceaux context l te, reduceaux context l ty)
467 | C.Prod (name,s,t) ->
470 reduceaux context [] s,
471 reduceaux ((Some (name,C.Decl s))::context) [] t)
472 | C.Lambda (name,s,t) ->
476 reduceaux context [] s,
477 reduceaux ((Some (name,C.Decl s))::context) [] t)
478 | he::tl -> reduceaux context tl (S.subst he t)
479 (* when name is Anonimous the substitution should be superfluous *)
482 reduceaux context l (S.subst (reduceaux context [] s) t)
484 let tl' = List.map (reduceaux context []) tl in
485 reduceaux context (tl'@l) he
486 | C.Appl [] -> raise (Impossible 1)
487 | C.Const (uri,exp_named_subst) ->
488 let exp_named_subst' =
489 reduceaux_exp_named_subst context l exp_named_subst
491 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
493 C.Constant (_,Some body,_,_,_) ->
495 (CicSubstitution.subst_vars exp_named_subst' body))
496 | C.Constant (_,None,_,_,_) ->
497 let t' = C.Const (uri,exp_named_subst') in
498 if l = [] then t' else C.Appl (t'::l)
499 | C.Variable _ -> raise ReferenceToVariable
500 | C.CurrentProof (_,_,body,_,_,_) ->
502 (CicSubstitution.subst_vars exp_named_subst' body))
503 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
505 | C.MutInd (uri,i,exp_named_subst) ->
506 let exp_named_subst' =
507 reduceaux_exp_named_subst context l exp_named_subst
509 let t' = C.MutInd (uri,i,exp_named_subst') in
510 if l = [] then t' else C.Appl (t'::l)
511 | C.MutConstruct (uri,i,j,exp_named_subst) ->
512 let exp_named_subst' =
513 reduceaux_exp_named_subst context l exp_named_subst
515 let t' = C.MutConstruct (uri,i,j,exp_named_subst') in
516 if l = [] then t' else C.Appl (t'::l)
517 | C.MutCase (mutind,i,outtype,term,pl) ->
521 let (_,_,body) = List.nth fl i in
523 let counter = ref (List.length fl) in
525 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
529 reduceaux context [] body'
530 | C.Appl (C.CoFix (i,fl) :: tl) ->
531 let (_,_,body) = List.nth fl i in
533 let counter = ref (List.length fl) in
535 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
539 let tl' = List.map (reduceaux context []) tl in
540 reduceaux context tl' body'
543 (match decofix (reduceaux context [] term) with
544 C.MutConstruct (_,_,j,_) -> reduceaux context l (List.nth pl (j-1))
545 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
547 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph mutind in
549 C.InductiveDefinition (tl,_,r,_) ->
550 let (_,_,arity,_) = List.nth tl i in
552 | _ -> raise WrongUriToInductiveDefinition
558 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
559 | _ -> raise (Impossible 5)
563 reduceaux context (ts@l) (List.nth pl (j-1))
564 | C.Cast _ | C.Implicit _ ->
565 raise (Impossible 2) (* we don't trust our whd ;-) *)
567 let outtype' = reduceaux context [] outtype in
568 let term' = reduceaux context [] term in
569 let pl' = List.map (reduceaux context []) pl in
571 C.MutCase (mutind,i,outtype',term',pl')
573 if l = [] then res else C.Appl (res::l)
577 List.map (function (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) fl
582 (function (n,recindex,ty,bo) ->
583 (n,recindex,reduceaux context [] ty, reduceaux (tys@context) [] bo)
588 let (_,recindex,_,body) = List.nth fl i in
591 Some (List.nth l recindex)
597 (match reduceaux context [] recparam with
599 | C.Appl ((C.MutConstruct _)::_) ->
601 let counter = ref (List.length fl) in
603 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
607 (* Possible optimization: substituting whd recparam in l*)
608 reduceaux context l body'
609 | _ -> if l = [] then t' () else C.Appl ((t' ())::l)
611 | None -> if l = [] then t' () else C.Appl ((t' ())::l)
615 List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
620 (function (n,ty,bo) ->
621 (n,reduceaux context [] ty, reduceaux (tys@context) [] bo)
626 if l = [] then t' else C.Appl (t'::l)
627 and reduceaux_exp_named_subst context l =
628 List.map (function uri,t -> uri,reduceaux context [] t)
633 exception WrongShape;;
634 exception AlreadySimplified;;
636 (* Takes a well-typed term and *)
637 (* 1) Performs beta-iota-zeta reduction until delta reduction is needed *)
638 (* 2) Attempts delta-reduction. If the residual is a Fix lambda-abstracted *)
639 (* w.r.t. zero or more variables and if the Fix can be reductaed, than it*)
640 (* is reduced, the delta-reduction is succesfull and the whole algorithm *)
641 (* is applied again to the new redex; Step 3.1) is applied to the result *)
642 (* of the recursive simplification. Otherwise, if the Fix can not be *)
643 (* reduced, than the delta-reductions fails and the delta-redex is *)
644 (* not reduced. Otherwise, if the delta-residual is not the *)
645 (* lambda-abstraction of a Fix, then it performs step 3.2). *)
646 (* 3.1) Folds the application of the constant to the arguments that did not *)
647 (* change in every iteration, i.e. to the actual arguments for the *)
648 (* lambda-abstractions that precede the Fix. *)
649 (* 3.2) Computes the head beta-zeta normal form of the term. Then it tries *)
650 (* reductions. If the reduction cannot be performed, it returns the *)
651 (* original term (not the head beta-zeta normal form of the definiendum) *)
652 (*CSC: It does not perform simplification in a Case *)
655 (* a simplified term is active if it can create a redex when used as an *)
656 (* actual parameter *)
661 | C.Appl (C.MutConstruct _::_)
663 | C.Cast (bo,_) -> is_active bo
664 | C.LetIn _ -> assert false
667 (* reduceaux is equal to the reduceaux locally defined inside *)
668 (* reduce, but for the const case. *)
670 let rec reduceaux context l =
673 (* we never perform delta expansion automatically *)
674 if l = [] then t else C.Appl (t::l)
675 | C.Var (uri,exp_named_subst) ->
676 let exp_named_subst' =
677 reduceaux_exp_named_subst context l exp_named_subst
679 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
681 C.Constant _ -> raise ReferenceToConstant
682 | C.CurrentProof _ -> raise ReferenceToCurrentProof
683 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
684 | C.Variable (_,None,_,_,_) ->
685 let t' = C.Var (uri,exp_named_subst') in
686 if l = [] then t' else C.Appl (t'::l)
687 | C.Variable (_,Some body,_,_,_) ->
689 (CicSubstitution.subst_vars exp_named_subst' body)
691 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
692 | C.Sort _ as t -> t (* l should be empty *)
693 | C.Implicit _ as t -> t
695 C.Cast (reduceaux context l te, reduceaux context [] ty)
696 | C.Prod (name,s,t) ->
699 reduceaux context [] s,
700 reduceaux ((Some (name,C.Decl s))::context) [] t)
701 | C.Lambda (name,s,t) ->
705 reduceaux context [] s,
706 reduceaux ((Some (name,C.Decl s))::context) [] t)
707 | he::tl -> reduceaux context tl (S.subst he t)
708 (* when name is Anonimous the substitution should be superfluous *)
711 reduceaux context l (S.subst (reduceaux context [] s) t)
713 let tl' = List.map (reduceaux context []) tl in
714 reduceaux context (tl'@l) he
715 | C.Appl [] -> raise (Impossible 1)
716 | C.Const (uri,exp_named_subst) ->
717 let exp_named_subst' =
718 reduceaux_exp_named_subst context l exp_named_subst
720 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
722 C.Constant (_,Some body,_,_,_) ->
723 if List.exists is_active l then
724 try_delta_expansion context l
725 (C.Const (uri,exp_named_subst'))
726 (CicSubstitution.subst_vars exp_named_subst' body)
728 let t' = C.Const (uri,exp_named_subst') in
729 if l = [] then t' else C.Appl (t'::l)
730 | C.Constant (_,None,_,_,_) ->
731 let t' = C.Const (uri,exp_named_subst') in
732 if l = [] then t' else C.Appl (t'::l)
733 | C.Variable _ -> raise ReferenceToVariable
734 | C.CurrentProof (_,_,body,_,_,_) -> reduceaux context l body
735 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
737 | C.MutInd (uri,i,exp_named_subst) ->
738 let exp_named_subst' =
739 reduceaux_exp_named_subst context l exp_named_subst
741 let t' = C.MutInd (uri,i,exp_named_subst') in
742 if l = [] then t' else C.Appl (t'::l)
743 | C.MutConstruct (uri,i,j,exp_named_subst) ->
744 let exp_named_subst' =
745 reduceaux_exp_named_subst context l exp_named_subst
747 let t' = C.MutConstruct(uri,i,j,exp_named_subst') in
748 if l = [] then t' else C.Appl (t'::l)
749 | C.MutCase (mutind,i,outtype,term,pl) ->
753 let (_,_,body) = List.nth fl i in
755 let counter = ref (List.length fl) in
757 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
761 reduceaux context [] body'
762 | C.Appl (C.CoFix (i,fl) :: tl) ->
763 let (_,_,body) = List.nth fl i in
765 let counter = ref (List.length fl) in
767 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
771 let tl' = List.map (reduceaux context []) tl in
772 reduceaux context tl' body'
775 (match decofix (reduceaux context [] term) (*(CicReduction.whd context term)*) with
776 C.MutConstruct (_,_,j,_) -> reduceaux context l (List.nth pl (j-1))
777 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
779 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph mutind in
781 C.InductiveDefinition (tl,ingredients,r,_) ->
782 let (_,_,arity,_) = List.nth tl i in
784 | _ -> raise WrongUriToInductiveDefinition
790 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
791 | _ -> raise (Impossible 5)
795 reduceaux context (ts@l) (List.nth pl (j-1))
796 | C.Cast _ | C.Implicit _ ->
797 raise (Impossible 2) (* we don't trust our whd ;-) *)
799 let outtype' = reduceaux context [] outtype in
800 let term' = reduceaux context [] term in
801 let pl' = List.map (reduceaux context []) pl in
803 C.MutCase (mutind,i,outtype',term',pl')
805 if l = [] then res else C.Appl (res::l)
809 List.map (function (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) fl
814 (function (n,recindex,ty,bo) ->
815 (n,recindex,reduceaux context [] ty, reduceaux (tys@context) [] bo)
820 let (_,recindex,_,body) = List.nth fl i in
823 Some (List.nth l recindex)
829 (match reduceaux context [] recparam with
831 | C.Appl ((C.MutConstruct _)::_) ->
833 let counter = ref (List.length fl) in
835 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
839 (* Possible optimization: substituting whd recparam in l*)
840 reduceaux context l body'
841 | _ -> if l = [] then t' () else C.Appl ((t' ())::l)
843 | None -> if l = [] then t' () else C.Appl ((t' ())::l)
847 List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
852 (function (n,ty,bo) ->
853 (n,reduceaux context [] ty, reduceaux (tys@context) [] bo)
858 if l = [] then t' else C.Appl (t'::l)
859 and reduceaux_exp_named_subst context l =
860 List.map (function uri,t -> uri,reduceaux context [] t)
862 and try_delta_expansion context l term body =
864 let res,constant_args =
865 let rec aux rev_constant_args l =
867 C.Lambda (name,s,t) ->
870 [] -> raise WrongShape
872 (* when name is Anonimous the substitution should *)
874 aux (he::rev_constant_args) tl (S.subst he t)
877 aux rev_constant_args l (S.subst s t)
879 let (_,recindex,_,body) = List.nth fl i in
884 _ -> raise AlreadySimplified
886 (match reduceaux context [] recparam (*CicReduction.whd context recparam*) with
888 | C.Appl ((C.MutConstruct _)::_) ->
890 let counter = ref (List.length fl) in
893 decr counter ; S.subst (C.Fix (!counter,fl))
896 (* Possible optimization: substituting whd *)
898 reduceaux context l body',
899 List.rev rev_constant_args
900 | _ -> raise AlreadySimplified
902 | _ -> raise WrongShape
907 let term_to_fold, delta_expanded_term_to_fold =
908 match constant_args with
910 | _ -> C.Appl (term::constant_args), C.Appl (body::constant_args)
912 let simplified_term_to_fold =
913 reduceaux context [] delta_expanded_term_to_fold
915 replace_lifting (=) [simplified_term_to_fold] [term_to_fold] res
921 C.Lambda (name,s,t) ->
923 [] -> raise AlreadySimplified
925 (* when name is Anonimous the substitution should *)
927 aux tl (S.subst he t))
928 | C.LetIn (_,s,t) -> aux l (S.subst s t)
930 let simplified = reduceaux context l t in
931 let t' = if l = [] then t else C.Appl (t::l) in
932 if t' = simplified then
933 raise AlreadySimplified
940 if l = [] then term else C.Appl (term::l))
941 | AlreadySimplified ->
942 (* If we performed delta-reduction, we would find a Fix *)
943 (* not applied to a constructor. So, we refuse to perform *)
944 (* delta-reduction. *)
945 if l = [] then term else C.Appl (term::l)
950 let unfold ?what context where =
951 let contextlen = List.length context in
952 let first_is_the_expandable_head_of_second context' t1 t2 =
954 Cic.Const (uri,_), Cic.Const (uri',_)
955 | Cic.Var (uri,_), Cic.Var (uri',_)
956 | Cic.Const (uri,_), Cic.Appl (Cic.Const (uri',_)::_)
957 | Cic.Var (uri,_), Cic.Appl (Cic.Var (uri',_)::_) -> UriManager.eq uri uri'
959 | Cic.Var _, _ -> false
960 | Cic.Rel n, Cic.Rel m
961 | Cic.Rel n, Cic.Appl (Cic.Rel m::_) ->
962 n + (List.length context' - contextlen) = m
963 | Cic.Rel _, _ -> false
966 (ProofEngineTypes.Fail
967 (lazy "The term to unfold is not a constant, a variable or a bound variable "))
970 if tl = [] then he else Cic.Appl (he::tl) in
971 let cannot_delta_expand t =
973 (ProofEngineTypes.Fail
974 (lazy ("The term " ^ CicPp.ppterm t ^ " cannot be delta-expanded"))) in
975 let rec hd_delta_beta context tl =
979 match List.nth context (n-1) with
980 Some (_,Cic.Decl _) -> cannot_delta_expand t
981 | Some (_,Cic.Def (bo,_)) ->
982 CicReduction.head_beta_reduce
983 (appl (CicSubstitution.lift n bo) tl)
984 | None -> raise RelToHiddenHypothesis
986 Failure _ -> assert false)
987 | Cic.Const (uri,exp_named_subst) as t ->
988 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
990 Cic.Constant (_,Some body,_,_,_) ->
991 CicReduction.head_beta_reduce
992 (appl (CicSubstitution.subst_vars exp_named_subst body) tl)
993 | Cic.Constant (_,None,_,_,_) -> cannot_delta_expand t
994 | Cic.Variable _ -> raise ReferenceToVariable
995 | Cic.CurrentProof _ -> raise ReferenceToCurrentProof
996 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
998 | Cic.Var (uri,exp_named_subst) as t ->
999 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1001 Cic.Constant _ -> raise ReferenceToConstant
1002 | Cic.CurrentProof _ -> raise ReferenceToCurrentProof
1003 | Cic.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
1004 | Cic.Variable (_,Some body,_,_,_) ->
1005 CicReduction.head_beta_reduce
1006 (appl (CicSubstitution.subst_vars exp_named_subst body) tl)
1007 | Cic.Variable (_,None,_,_,_) -> cannot_delta_expand t
1009 | Cic.Appl [] -> assert false
1010 | Cic.Appl (he::tl) -> hd_delta_beta context tl he
1011 | t -> cannot_delta_expand t
1013 let context_and_matched_term_list =
1015 None -> [context, where]
1018 ProofEngineHelpers.locate_in_term
1019 ~equality:first_is_the_expandable_head_of_second
1024 (ProofEngineTypes.Fail
1025 (lazy ("Term "^ CicPp.ppterm what ^ " not found in " ^ CicPp.ppterm where)))
1031 (function (context,where) -> hd_delta_beta context [] where)
1032 context_and_matched_term_list in
1033 let whats = List.map snd context_and_matched_term_list in
1034 replace ~equality:(==) ~what:whats ~with_what:reduced_terms ~where