]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/proofEngineReduction.ml
New tactic rewrite implemented.
[helm.git] / helm / gTopLevel / proofEngineReduction.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 12/04/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36
37 (* The code of this module is derived from the code of CicReduction *)
38
39 exception Impossible of int;;
40 exception ReferenceToDefinition;;
41 exception ReferenceToAxiom;;
42 exception ReferenceToVariable;;
43 exception ReferenceToCurrentProof;;
44 exception ReferenceToInductiveDefinition;;
45 exception WrongUriToInductiveDefinition;;
46 exception RelToHiddenHypothesis;;
47
48 (* syntactic_equality up to cookingsno for uris *)
49 (* (which is often syntactically irrilevant)    *)
50 let rec syntactic_equality t t' =
51  let module C = Cic in
52   if t = t' then true
53   else
54    match t,t' with
55       C.Rel _, C.Rel _
56     | C.Var _, C.Var _
57     | C.Meta _, C.Meta _
58     | C.Sort _, C.Sort _
59     | C.Implicit, C.Implicit -> false (* we already know that t != t' *)
60     | C.Cast (te,ty), C.Cast (te',ty') ->
61        syntactic_equality te te' &&
62         syntactic_equality ty ty'
63     | C.Prod (n,s,t), C.Prod (n',s',t') ->
64        n = n' &&
65         syntactic_equality s s' &&
66          syntactic_equality t t'
67     | C.Lambda (n,s,t), C.Lambda (n',s',t') ->
68        n = n' &&
69         syntactic_equality s s' &&
70          syntactic_equality t t'
71     | C.LetIn (n,s,t), C.LetIn(n',s',t') ->
72        n = n' &&
73         syntactic_equality s s' &&
74          syntactic_equality t t'
75     | C.Appl l, C.Appl l' ->
76        List.fold_left2 (fun b t1 t2 -> b && syntactic_equality t1 t2) true l l'
77     | C.Const (uri,_), C.Const (uri',_) -> UriManager.eq uri uri'
78     | C.MutInd (uri,_,i), C.MutInd (uri',_,i') ->
79        UriManager.eq uri uri' && i = i'
80     | C.MutConstruct (uri,_,i,j), C.MutConstruct (uri',_,i',j') ->
81        UriManager.eq uri uri' && i = i' && j = j'
82     | C.MutCase (sp,_,i,outt,t,pl), C.MutCase (sp',_,i',outt',t',pl') ->
83        UriManager.eq sp sp' && i = i' &&
84         syntactic_equality outt outt' &&
85          syntactic_equality t t' &&
86           List.fold_left2
87            (fun b t1 t2 -> b && syntactic_equality t1 t2) true pl pl'
88     | C.Fix (i,fl), C.Fix (i',fl') ->
89        i = i' &&
90         List.fold_left2
91          (fun b (name,i,ty,bo) (name',i',ty',bo') ->
92            b && name = name' && i = i' &&
93             syntactic_equality ty ty' &&
94              syntactic_equality bo bo') true fl fl'
95     | C.CoFix (i,fl), C.CoFix (i',fl') ->
96        i = i' &&
97         List.fold_left2
98          (fun b (name,ty,bo) (name',ty',bo') ->
99            b && name = name' &&
100             syntactic_equality ty ty' &&
101              syntactic_equality bo bo') true fl fl'
102     | _,_ -> false
103 ;;
104
105 (* "textual" replacement of a subterm with another one *)
106 let replace ~equality ~what ~with_what ~where =
107  let module C = Cic in
108   let rec aux =
109    function
110       t when (equality t what) -> with_what
111     | C.Rel _ as t -> t
112     | C.Var _ as t  -> t
113     | C.Meta _ as t -> t
114     | C.Sort _ as t -> t
115     | C.Implicit as t -> t
116     | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
117     | C.Prod (n,s,t) -> C.Prod (n, aux s, aux t)
118     | C.Lambda (n,s,t) -> C.Lambda (n, aux s, aux t)
119     | C.LetIn (n,s,t) -> C.LetIn (n, aux s, aux t)
120     | C.Appl l ->
121        (* Invariant enforced: no application of an application *)
122        (match List.map aux l with
123            (C.Appl l')::tl -> C.Appl (l'@tl)
124          | l' -> C.Appl l')
125     | C.Const _ as t -> t
126     | C.MutInd _ as t -> t
127     | C.MutConstruct _ as t -> t
128     | C.MutCase (sp,cookingsno,i,outt,t,pl) ->
129        C.MutCase (sp,cookingsno,i,aux outt, aux t,
130         List.map aux pl)
131     | C.Fix (i,fl) ->
132        let substitutedfl =
133         List.map
134          (fun (name,i,ty,bo) -> (name, i, aux ty, aux bo))
135           fl
136        in
137         C.Fix (i, substitutedfl)
138     | C.CoFix (i,fl) ->
139        let substitutedfl =
140         List.map
141          (fun (name,ty,bo) -> (name, aux ty, aux bo))
142           fl
143        in
144         C.CoFix (i, substitutedfl)
145   in
146    aux where
147 ;;
148
149 (* replaces in a term a term with another one. *)
150 (* Lifting are performed as usual.             *)
151 let replace_lifting ~equality ~what ~with_what ~where =
152  let rec substaux k =
153   let module C = Cic in
154    function
155       t when (equality t what) -> CicSubstitution.lift (k-1) with_what
156     | C.Rel n as t -> t (*CSC: ??? BUG ? *)
157     | C.Var _ as t  -> t
158     | C.Meta (i, l) as t -> 
159        let l' =
160         List.map
161          (function
162              None -> None
163            | Some t -> Some (substaux k t)
164          ) l
165        in
166         C.Meta(i,l')
167     | C.Sort _ as t -> t
168     | C.Implicit as t -> t
169     | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
170     | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
171     | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
172     | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
173     | C.Appl (he::tl) ->
174        (* Invariant: no Appl applied to another Appl *)
175        let tl' = List.map (substaux k) tl in
176         begin
177          match substaux k he with
178             C.Appl l -> C.Appl (l@tl')
179           | _ as he' -> C.Appl (he'::tl')
180         end
181     | C.Appl _ -> assert false
182     | C.Const _ as t -> t
183     | C.MutInd _ as t -> t
184     | C.MutConstruct _ as t -> t
185     | C.MutCase (sp,cookingsno,i,outt,t,pl) ->
186        C.MutCase (sp,cookingsno,i,substaux k outt, substaux k t,
187         List.map (substaux k) pl)
188     | C.Fix (i,fl) ->
189        let len = List.length fl in
190        let substitutedfl =
191         List.map
192          (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
193           fl
194        in
195         C.Fix (i, substitutedfl)
196     | C.CoFix (i,fl) ->
197        let len = List.length fl in
198        let substitutedfl =
199         List.map
200          (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
201           fl
202        in
203         C.CoFix (i, substitutedfl)
204  in
205   substaux 1 where
206 ;;
207
208 (* Takes a well-typed term and fully reduces it. *)
209 (*CSC: It does not perform reduction in a Case *)
210 let reduce context =
211  let rec reduceaux context l =
212   let module C = Cic in
213   let module S = CicSubstitution in
214    function
215       C.Rel n as t ->
216        (match List.nth context (n-1) with
217            Some (_,C.Decl _) -> if l = [] then t else C.Appl (t::l)
218          | Some (_,C.Def bo) -> reduceaux context l (S.lift n bo)
219          | None -> raise RelToHiddenHypothesis
220        )
221     | C.Var uri as t ->
222        (match CicEnvironment.get_cooked_obj uri 0 with
223            C.Definition _ -> raise ReferenceToDefinition
224          | C.Axiom _ -> raise ReferenceToAxiom
225          | C.CurrentProof _ -> raise ReferenceToCurrentProof
226          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
227          | C.Variable (_,None,_) -> if l = [] then t else C.Appl (t::l)
228          | C.Variable (_,Some body,_) -> reduceaux context l body
229        )
230     | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
231     | C.Sort _ as t -> t (* l should be empty *)
232     | C.Implicit as t -> t
233     | C.Cast (te,ty) ->
234        C.Cast (reduceaux context l te, reduceaux context l ty)
235     | C.Prod (name,s,t) ->
236        assert (l = []) ;
237        C.Prod (name,
238         reduceaux context [] s,
239         reduceaux ((Some (name,C.Decl s))::context) [] t)
240     | C.Lambda (name,s,t) ->
241        (match l with
242            [] ->
243             C.Lambda (name,
244              reduceaux context [] s,
245              reduceaux ((Some (name,C.Decl s))::context) [] t)
246          | he::tl -> reduceaux context tl (S.subst he t)
247            (* when name is Anonimous the substitution should be superfluous *)
248        )
249     | C.LetIn (n,s,t) ->
250        reduceaux context l (S.subst (reduceaux context [] s) t)
251     | C.Appl (he::tl) ->
252        let tl' = List.map (reduceaux context []) tl in
253         reduceaux context (tl'@l) he
254     | C.Appl [] -> raise (Impossible 1)
255     | C.Const (uri,cookingsno) as t ->
256        (match CicEnvironment.get_cooked_obj uri cookingsno with
257            C.Definition (_,body,_,_) -> reduceaux context l body
258          | C.Axiom _ -> if l = [] then t else C.Appl (t::l)
259          | C.Variable _ -> raise ReferenceToVariable
260          | C.CurrentProof (_,_,body,_) -> reduceaux context l body
261          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
262        )
263     | C.MutInd (uri,_,_) as t -> if l = [] then t else C.Appl (t::l)
264     | C.MutConstruct (uri,_,_,_) as t -> if l = [] then t else C.Appl (t::l)
265     | C.MutCase (mutind,cookingsno,i,outtype,term,pl) ->
266        let decofix =
267         function
268            C.CoFix (i,fl) as t ->
269             let tys =
270              List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
271             in
272              let (_,_,body) = List.nth fl i in
273               let body' =
274                let counter = ref (List.length fl) in
275                 List.fold_right
276                  (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
277                  fl
278                  body
279               in
280                reduceaux (tys@context) [] body'
281          | C.Appl (C.CoFix (i,fl) :: tl) ->
282             let tys =
283              List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
284             in
285              let (_,_,body) = List.nth fl i in
286               let body' =
287                let counter = ref (List.length fl) in
288                 List.fold_right
289                  (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
290                  fl
291                  body
292               in
293                let tl' = List.map (reduceaux context []) tl in
294                 reduceaux (tys@context) tl' body'
295          | t -> t
296        in
297         (match decofix (reduceaux context [] term) with
298             C.MutConstruct (_,_,_,j) -> reduceaux context l (List.nth pl (j-1))
299           | C.Appl (C.MutConstruct (_,_,_,j) :: tl) ->
300              let (arity, r, num_ingredients) =
301               match CicEnvironment.get_obj mutind with
302                  C.InductiveDefinition (tl,ingredients,r) ->
303                    let (_,_,arity,_) = List.nth tl i
304                    and num_ingredients =
305                     List.fold_right
306                      (fun (k,l) i ->
307                        if k < cookingsno then i + List.length l else i
308                      ) ingredients 0
309                    in
310                     (arity,r,num_ingredients)
311                | _ -> raise WrongUriToInductiveDefinition
312              in
313               let ts =
314                let num_to_eat = r + num_ingredients in
315                 let rec eat_first =
316                  function
317                     (0,l) -> l
318                   | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
319                   | _ -> raise (Impossible 5)
320                 in
321                  eat_first (num_to_eat,tl)
322               in
323                reduceaux context (ts@l) (List.nth pl (j-1))
324          | C.Cast _ | C.Implicit ->
325             raise (Impossible 2) (* we don't trust our whd ;-) *)
326          | _ ->
327            let outtype' = reduceaux context [] outtype in
328            let term' = reduceaux context [] term in
329            let pl' = List.map (reduceaux context []) pl in
330             let res =
331              C.MutCase (mutind,cookingsno,i,outtype',term',pl')
332             in
333              if l = [] then res else C.Appl (res::l)
334        )
335     | C.Fix (i,fl) ->
336        let tys =
337         List.map (function (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) fl
338        in
339         let t' () =
340          let fl' =
341           List.map
342            (function (n,recindex,ty,bo) ->
343              (n,recindex,reduceaux context [] ty, reduceaux (tys@context) [] bo)
344            ) fl
345          in
346           C.Fix (i, fl')
347         in
348          let (_,recindex,_,body) = List.nth fl i in
349           let recparam =
350            try
351             Some (List.nth l recindex)
352            with
353             _ -> None
354           in
355            (match recparam with
356                Some recparam ->
357                 (match reduceaux context [] recparam with
358                     C.MutConstruct _
359                   | C.Appl ((C.MutConstruct _)::_) ->
360                      let body' =
361                       let counter = ref (List.length fl) in
362                        List.fold_right
363                         (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
364                         fl
365                         body
366                      in
367                       (* Possible optimization: substituting whd recparam in l*)
368                       reduceaux context l body'
369                   | _ -> if l = [] then t' () else C.Appl ((t' ())::l)
370                 )
371              | None -> if l = [] then t' () else C.Appl ((t' ())::l)
372            )
373     | C.CoFix (i,fl) ->
374        let tys =
375         List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
376        in
377         let t' =
378          let fl' =
379           List.map
380            (function (n,ty,bo) ->
381              (n,reduceaux context [] ty, reduceaux (tys@context) [] bo)
382            ) fl
383          in
384           C.CoFix (i, fl')
385         in
386          if l = [] then t' else C.Appl (t'::l)
387  in
388   reduceaux context []
389 ;;
390
391 exception WrongShape;;
392 exception AlreadySimplified;;
393
394 (*CSC: I fear it is still weaker than Coq's one. For example, Coq is *)
395 (*CSCS: able to simpl (foo (S n) (S n)) to (foo (S O) n) where       *)
396 (*CSC:  Fix foo                                                      *)
397 (*CSC:   {foo [n,m:nat]:nat :=                                       *)
398 (*CSC:     Cases m of O => n | (S p) => (foo (S O) p) end            *)
399 (*CSC:   }                                                           *)
400 (* Takes a well-typed term and                                               *)
401 (*  1) Performs beta-iota-zeta reduction until delta reduction is needed     *)
402 (*  2) Attempts delta-reduction. If the residual is a Fix lambda-abstracted  *)
403 (*     w.r.t. zero or more variables and if the Fix can be reduced, than it  *)
404 (*     is reduced, the delta-reduction is succesfull and the whole algorithm *)
405 (*     is applied again to the new redex; Step 3) is applied to the result   *)
406 (*     of the recursive simplification. Otherwise, if the Fix can not be     *)
407 (*     reduced, than the delta-reductions fails and the delta-redex is       *)
408 (*     not reduced. Otherwise, if the delta-residual is not the              *)
409 (*     lambda-abstraction of a Fix, then it is reduced and the result is     *)
410 (*     directly returned, without performing step 3).                        *) 
411 (*  3) Folds the application of the constant to the arguments that did not   *)
412 (*     change in every iteration, i.e. to the actual arguments for the       *)
413 (*     lambda-abstractions that precede the Fix.                             *)
414 (*CSC: It does not perform simplification in a Case *)
415 let simpl context =
416  (* reduceaux is equal to the reduceaux locally defined inside *)
417  (*reduce, but for the const case.                             *) 
418  (**** Step 1 ****)
419  let rec reduceaux context l =
420   let module C = Cic in
421   let module S = CicSubstitution in
422    function
423       C.Rel n as t ->
424        (match List.nth context (n-1) with
425            Some (_,C.Decl _) -> if l = [] then t else C.Appl (t::l)
426          | Some (_,C.Def bo) -> reduceaux context l (S.lift n bo)
427          | None -> raise RelToHiddenHypothesis
428        )
429     | C.Var uri as t ->
430        (match CicEnvironment.get_cooked_obj uri 0 with
431            C.Definition _ -> raise ReferenceToDefinition
432          | C.Axiom _ -> raise ReferenceToAxiom
433          | C.CurrentProof _ -> raise ReferenceToCurrentProof
434          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
435          | C.Variable (_,None,_) -> if l = [] then t else C.Appl (t::l)
436          | C.Variable (_,Some body,_) -> reduceaux context l body
437        )
438     | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
439     | C.Sort _ as t -> t (* l should be empty *)
440     | C.Implicit as t -> t
441     | C.Cast (te,ty) ->
442        C.Cast (reduceaux context l te, reduceaux context l ty)
443     | C.Prod (name,s,t) ->
444        assert (l = []) ;
445        C.Prod (name,
446         reduceaux context [] s,
447         reduceaux ((Some (name,C.Decl s))::context) [] t)
448     | C.Lambda (name,s,t) ->
449        (match l with
450            [] ->
451             C.Lambda (name,
452              reduceaux context [] s,
453              reduceaux ((Some (name,C.Decl s))::context) [] t)
454          | he::tl -> reduceaux context tl (S.subst he t)
455            (* when name is Anonimous the substitution should be superfluous *)
456        )
457     | C.LetIn (n,s,t) ->
458        reduceaux context l (S.subst (reduceaux context [] s) t)
459     | C.Appl (he::tl) ->
460        let tl' = List.map (reduceaux context []) tl in
461         reduceaux context (tl'@l) he
462     | C.Appl [] -> raise (Impossible 1)
463     | C.Const (uri,cookingsno) as t ->
464        (match CicEnvironment.get_cooked_obj uri cookingsno with
465            C.Definition (_,body,_,_) ->
466             begin
467              try
468               (**** Step 2 ****)
469               let res,constant_args =
470                let rec aux rev_constant_args l =
471                 function
472                    C.Lambda (name,s,t) as t' ->
473                     begin
474                      match l with
475                         [] -> raise WrongShape
476                       | he::tl ->
477                          (* when name is Anonimous the substitution should be *)
478                          (* superfluous                                       *)
479                          aux (he::rev_constant_args) tl (S.subst he t)
480                     end
481                  | C.LetIn (_,s,t) ->
482                     aux rev_constant_args l (S.subst s t)
483                  | C.Fix (i,fl) as t ->
484                     let tys =
485                      List.map (function (name,_,ty,_) ->
486                       Some (C.Name name, C.Decl ty)) fl
487                     in
488                      let (_,recindex,_,body) = List.nth fl i in
489                       let recparam =
490                        try
491                         List.nth l recindex
492                        with
493                         _ -> raise AlreadySimplified
494                       in
495                        (match CicReduction.whd context recparam with
496                            C.MutConstruct _
497                          | C.Appl ((C.MutConstruct _)::_) ->
498                             let body' =
499                              let counter = ref (List.length fl) in
500                               List.fold_right
501                                (function _ ->
502                                  decr counter ; S.subst (C.Fix (!counter,fl))
503                                ) fl body
504                             in
505                              (* Possible optimization: substituting whd *)
506                              (* recparam in l                           *)
507                              reduceaux (tys@context) l body',
508                               List.rev rev_constant_args
509                          | _ -> raise AlreadySimplified
510                        )
511                  | _ -> raise WrongShape
512                in
513                 aux [] l body
514               in
515                (**** Step 3 ****)
516                let term_to_fold =
517                 match constant_args with
518                    [] -> C.Const (uri,cookingsno)
519                  | _ -> C.Appl ((C.Const (uri,cookingsno))::constant_args)
520                in
521                 let reduced_term_to_fold = reduce context term_to_fold in
522                  replace (=) reduced_term_to_fold term_to_fold res
523              with
524                 WrongShape ->
525                  (* The constant does not unfold to a Fix lambda-abstracted   *)
526                  (* w.r.t. zero or more variables. We just perform reduction. *)
527                  reduceaux context l body
528               | AlreadySimplified ->
529                  (* If we performed delta-reduction, we would find a Fix   *)
530                  (* not applied to a constructor. So, we refuse to perform *)
531                  (* delta-reduction.                                       *)
532                  if l = [] then
533                     t
534                  else
535                   C.Appl (t::l)
536             end
537          | C.Axiom _ -> if l = [] then t else C.Appl (t::l)
538          | C.Variable _ -> raise ReferenceToVariable
539          | C.CurrentProof (_,_,body,_) -> reduceaux context l body
540          | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
541        )
542     | C.MutInd (uri,_,_) as t -> if l = [] then t else C.Appl (t::l)
543     | C.MutConstruct (uri,_,_,_) as t -> if l = [] then t else C.Appl (t::l)
544     | C.MutCase (mutind,cookingsno,i,outtype,term,pl) ->
545        let decofix =
546         function
547            C.CoFix (i,fl) as t ->
548             let tys =
549              List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl            in
550              let (_,_,body) = List.nth fl i in
551               let body' =
552                let counter = ref (List.length fl) in
553                 List.fold_right
554                  (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
555                  fl
556                  body
557               in
558                reduceaux (tys@context) [] body'
559          | C.Appl (C.CoFix (i,fl) :: tl) ->
560             let tys =
561              List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl            in
562              let (_,_,body) = List.nth fl i in
563               let body' =
564                let counter = ref (List.length fl) in
565                 List.fold_right
566                  (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
567                  fl
568                  body
569               in
570                let tl' = List.map (reduceaux context []) tl in
571                 reduceaux (tys@context) tl body'
572          | t -> t
573        in
574         (match decofix (reduceaux context [] term) with
575             C.MutConstruct (_,_,_,j) -> reduceaux context l (List.nth pl (j-1))
576           | C.Appl (C.MutConstruct (_,_,_,j) :: tl) ->
577              let (arity, r, num_ingredients) =
578               match CicEnvironment.get_obj mutind with
579                  C.InductiveDefinition (tl,ingredients,r) ->
580                    let (_,_,arity,_) = List.nth tl i
581                    and num_ingredients =
582                     List.fold_right
583                      (fun (k,l) i ->
584                        if k < cookingsno then i + List.length l else i
585                      ) ingredients 0
586                    in
587                     (arity,r,num_ingredients)
588                | _ -> raise WrongUriToInductiveDefinition
589              in
590               let ts =
591                let num_to_eat = r + num_ingredients in
592                 let rec eat_first =
593                  function
594                     (0,l) -> l
595                   | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
596                   | _ -> raise (Impossible 5)
597                 in
598                  eat_first (num_to_eat,tl)
599               in
600                reduceaux context (ts@l) (List.nth pl (j-1))
601          | C.Cast _ | C.Implicit ->
602             raise (Impossible 2) (* we don't trust our whd ;-) *)
603          | _ ->
604            let outtype' = reduceaux context [] outtype in
605            let term' = reduceaux context [] term in
606            let pl' = List.map (reduceaux context []) pl in
607             let res =
608              C.MutCase (mutind,cookingsno,i,outtype',term',pl')
609             in
610              if l = [] then res else C.Appl (res::l)
611        )
612     | C.Fix (i,fl) ->
613        let tys =
614         List.map (function (name,_,ty,_) -> Some (C.Name name, C.Decl ty)) fl
615        in
616         let t' () =
617          let fl' =
618           List.map
619            (function (n,recindex,ty,bo) ->
620              (n,recindex,reduceaux context [] ty, reduceaux (tys@context) [] bo)
621            ) fl
622          in
623           C.Fix (i, fl')
624         in
625          let (_,recindex,_,body) = List.nth fl i in
626           let recparam =
627            try
628             Some (List.nth l recindex)
629            with
630             _ -> None
631           in
632            (match recparam with
633                Some recparam ->
634                 (match reduceaux context [] recparam with
635                     C.MutConstruct _
636                   | C.Appl ((C.MutConstruct _)::_) ->
637                      let body' =
638                       let counter = ref (List.length fl) in
639                        List.fold_right
640                         (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
641                         fl
642                         body
643                      in
644                       (* Possible optimization: substituting whd recparam in l*)
645                       reduceaux context l body'
646                   | _ -> if l = [] then t' () else C.Appl ((t' ())::l)
647                 )
648              | None -> if l = [] then t' () else C.Appl ((t' ())::l)
649            )
650     | C.CoFix (i,fl) ->
651        let tys =
652         List.map (function (name,ty,_) -> Some (C.Name name, C.Decl ty)) fl
653        in
654         let t' =
655          let fl' =
656           List.map
657            (function (n,ty,bo) ->
658              (n,reduceaux context [] ty, reduceaux (tys@context) [] bo)
659            ) fl
660          in
661          C.CoFix (i, fl')
662        in
663          if l = [] then t' else C.Appl (t'::l)
664  in
665   reduceaux context []
666 ;;