]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/proofEngineHelpers.ml
1. PrimitiveTactics.new_metasenv_for_apply changed a little bit, moved to
[helm.git] / helm / ocaml / tactics / proofEngineHelpers.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 exception Bad_pattern of string
27
28 let new_meta_of_proof ~proof:(_, metasenv, _, _) =
29   CicMkImplicit.new_meta metasenv []
30
31 let subst_meta_in_proof proof meta term newmetasenv =
32  let uri,metasenv,bo,ty = proof in
33    (* empty context is ok for term since it wont be used by apply_subst *)
34    (* hack: since we do not know the context and the type of term, we
35       create a substitution with cc =[] and type = Implicit; they will be
36       in  any case dropped by apply_subst, but it would be better to rewrite
37       the code. Cannot we just use apply_subst_metasenv, etc. ?? *)
38   let subst_in = CicMetaSubst.apply_subst [meta,([], term,Cic.Implicit None)] in
39    let metasenv' =
40     newmetasenv @ (List.filter (function (m,_,_) -> m <> meta) metasenv)
41    in
42     let metasenv'' =
43      List.map
44       (function i,canonical_context,ty ->
45         let canonical_context' =
46          List.map
47           (function
48               Some (n,Cic.Decl s) -> Some (n,Cic.Decl (subst_in s))
49             | Some (n,Cic.Def (s,None)) -> Some (n,Cic.Def ((subst_in s),None))
50             | None -> None
51             | Some (_,Cic.Def (_,Some _)) -> assert false
52           ) canonical_context
53         in
54          i,canonical_context',(subst_in ty)
55       ) metasenv'
56     in
57      let bo' = subst_in bo in
58      (* Metavariables can appear also in the *statement* of the theorem
59       * since the parser does not reject as statements terms with
60       * metavariable therein *)
61      let ty' = subst_in ty in
62       let newproof = uri,metasenv'',bo',ty' in
63        (newproof, metasenv'')
64
65 (*CSC: commento vecchio *)
66 (* refine_meta_with_brand_new_metasenv meta term subst_in newmetasenv     *)
67 (* This (heavy) function must be called when a tactic can instantiate old *)
68 (* metavariables (i.e. existential variables). It substitues the metasenv *)
69 (* of the proof with the result of removing [meta] from the domain of     *)
70 (* [newmetasenv]. Then it replaces Cic.Meta [meta] with [term] everywhere *)
71 (* in the current proof. Finally it applies [apply_subst_replacing] to    *)
72 (*  current proof.                                                        *)
73 (*CSC: A questo punto perche' passare un bo' gia' istantiato, se tanto poi *)
74 (*CSC: ci ripasso sopra apply_subst!!!                                     *)
75 (*CSC: Attenzione! Ora questa funzione applica anche [subst_in] a *)
76 (*CSC: [newmetasenv].                                             *)
77 let subst_meta_and_metasenv_in_proof proof meta subst_in newmetasenv =
78  let (uri,_,bo,ty) = proof in
79   let bo' = subst_in bo in
80   (* Metavariables can appear also in the *statement* of the theorem
81    * since the parser does not reject as statements terms with
82    * metavariable therein *)
83   let ty' = subst_in ty in
84   let metasenv' =
85    List.fold_right
86     (fun metasenv_entry i ->
87       match metasenv_entry with
88          (m,canonical_context,ty) when m <> meta ->
89            let canonical_context' =
90             List.map
91              (function
92                  None -> None
93                | Some (i,Cic.Decl t) -> Some (i,Cic.Decl (subst_in t))
94                | Some (i,Cic.Def (t,None))  ->
95                   Some (i,Cic.Def ((subst_in t),None))
96                | Some (_,Cic.Def (_,Some _))  -> assert false
97              ) canonical_context
98            in
99             (m,canonical_context',subst_in ty)::i
100        | _ -> i
101     ) newmetasenv []
102   in
103    let newproof = uri,metasenv',bo',ty' in
104     (newproof, metasenv')
105
106 let compare_metasenvs ~oldmetasenv ~newmetasenv =
107  List.map (function (i,_,_) -> i)
108   (List.filter
109    (function (i,_,_) ->
110      not (List.exists (fun (j,_,_) -> i=j) oldmetasenv)) newmetasenv)
111 ;;
112
113 (** finds the _pointers_ to subterms that are alpha-equivalent to wanted in t *)
114 let find_subterms ~subst ~metasenv ~ugraph ~wanted ~context t =
115   let rec find subst metasenv ugraph context w t =
116    try
117     let subst,metasenv,ugraph =
118      CicUnification.fo_unif_subst subst context metasenv w t ugraph
119     in
120       subst,metasenv,ugraph,[context,t]
121    with
122      CicUnification.UnificationFailure _
123    | CicUnification.Uncertain _ ->
124       match t with
125       | Cic.Sort _ 
126       | Cic.Rel _ -> subst,metasenv,ugraph,[]
127       | Cic.Meta (_, ctx) -> 
128           List.fold_left (
129             fun (subst,metasenv,ugraph,acc) e -> 
130               match e with 
131               | None -> subst,metasenv,ugraph,acc 
132               | Some t ->
133                  let subst,metasenv,ugraph,res =
134                   find subst metasenv ugraph context w t
135                  in
136                   subst,metasenv,ugraph, res @ acc
137           ) (subst,metasenv,ugraph,[]) ctx
138       | Cic.Lambda (name, t1, t2) 
139       | Cic.Prod (name, t1, t2) ->
140          let subst,metasenv,ugraph,rest1 =
141           find subst metasenv ugraph context w t1 in
142          let subst,metasenv,ugraph,rest2 =
143           find subst metasenv ugraph (Some (name, Cic.Decl t1)::context)
144            (CicSubstitution.lift 1 w) t2
145          in
146           subst,metasenv,ugraph,rest1 @ rest2
147       | Cic.LetIn (name, t1, t2) -> 
148          let subst,metasenv,ugraph,rest1 =
149           find subst metasenv ugraph context w t1 in
150          let subst,metasenv,ugraph,rest2 =
151           find subst metasenv ugraph (Some (name, Cic.Def (t1,None))::context)
152            (CicSubstitution.lift 1 w) t2
153          in
154           subst,metasenv,ugraph,rest1 @ rest2
155       | Cic.Appl l -> 
156           List.fold_left
157            (fun (subst,metasenv,ugraph,acc) t ->
158              let subst,metasenv,ugraph,res =
159               find subst metasenv ugraph context w t
160              in
161               subst,metasenv,ugraph,res @ acc)
162            (subst,metasenv,ugraph,[]) l
163       | Cic.Cast (t, ty) ->
164          let subst,metasenv,ugraph,rest =
165           find subst metasenv ugraph context w t in
166          let subst,metasenv,ugraph,resty =
167           find subst metasenv ugraph context w ty
168          in
169           subst,metasenv,ugraph,rest @ resty
170       | Cic.Implicit _ -> assert false
171       | Cic.Const (_, esubst)
172       | Cic.Var (_, esubst) 
173       | Cic.MutInd (_, _, esubst) 
174       | Cic.MutConstruct (_, _, _, esubst) -> 
175           List.fold_left
176            (fun (subst,metasenv,ugraph,acc) (_, t) ->
177              let subst,metasenv,ugraph,res =
178               find subst metasenv ugraph context w t
179              in
180               subst,metasenv,ugraph,res @ acc)
181            (subst,metasenv,ugraph,[]) esubst
182       | Cic.MutCase (_, _, outty, indterm, patterns) -> 
183          let subst,metasenv,ugraph,resoutty =
184           find subst metasenv ugraph context w outty in
185          let subst,metasenv,ugraph,resindterm =
186           find subst metasenv ugraph context w indterm in
187          let subst,metasenv,ugraph,respatterns =
188           List.fold_left
189            (fun (subst,metasenv,ugraph,acc) p ->
190              let subst,metaseng,ugraph,res =
191               find subst metasenv ugraph context w p
192              in
193               subst,metasenv,ugraph,res @ acc
194            ) (subst,metasenv,ugraph,[]) patterns
195          in
196           subst,metasenv,ugraph,resoutty @ resindterm @ respatterns
197 (*CSC: c'e' ancora un problema: il caso vs Meta puo' alterare il goal ==>
198        bisogna ricominciare da capo sul nuovo goal per preservare i puntatori
199        fisici
200       | Cic.Fix (_, funl) -> 
201          let tys =
202           List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funl
203          in
204           List.fold_left (
205             fun acc (_, _, ty, bo) ->
206              find context w ty @ find (tys @ context) w bo @ acc
207           ) [] funl
208       | Cic.CoFix (_, funl) ->
209          let tys =
210           List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funl
211          in
212           List.fold_left (
213             fun acc (_, ty, bo) ->
214              find context w ty @ find (tys @ context) w bo @ acc
215           ) [] funl
216 *)
217   in
218   find subst metasenv ugraph context wanted t
219   
220 let select_in_term ~metasenv ~context ~ugraph ~term ~pattern:(wanted,where) =
221   let add_ctx context name entry =
222       (Some (name, entry)) :: context
223   in
224   let rec aux context where term =
225     match (where, term) with
226     | Cic.Implicit (Some `Hole), t -> [context,t]
227     | Cic.Implicit (Some `Type), t -> []
228     | Cic.Implicit None,_ -> []
229     | Cic.Meta (_, ctxt1), Cic.Meta (_, ctxt2) ->
230         List.concat
231           (List.map2
232             (fun t1 t2 ->
233               (match (t1, t2) with
234                   Some t1, Some t2 -> aux context t1 t2
235                 | _ -> []))
236             ctxt1 ctxt2)
237     | Cic.Cast (te1, ty1), Cic.Cast (te2, ty2) ->
238        aux context te1 te2 @ aux context ty1 ty2
239     | Cic.Prod (Cic.Anonymous, s1, t1), Cic.Prod (name, s2, t2)
240     | Cic.Lambda (Cic.Anonymous, s1, t1), Cic.Lambda (name, s2, t2) ->
241         aux context s1 s2 @ aux (add_ctx context name (Cic.Decl s2)) t1 t2
242     | Cic.Prod (Cic.Name n1, s1, t1), 
243       Cic.Prod ((Cic.Name n2) as name , s2, t2)
244     | Cic.Lambda (Cic.Name n1, s1, t1), 
245       Cic.Lambda ((Cic.Name n2) as name, s2, t2) when n1 = n2->
246         aux context s1 s2 @ aux (add_ctx context name (Cic.Decl s2)) t1 t2
247     | Cic.Prod (name1, s1, t1), Cic.Prod (name2, s2, t2)
248     | Cic.Lambda (name1, s1, t1), Cic.Lambda (name2, s2, t2) -> []
249     | Cic.LetIn (Cic.Anonymous, s1, t1), Cic.LetIn (name, s2, t2) -> 
250         aux context s1 s2 @ aux (add_ctx context name (Cic.Def (s2,None))) t1 t2
251     | Cic.LetIn (Cic.Name n1, s1, t1), 
252       Cic.LetIn ((Cic.Name n2) as name, s2, t2) when n1 = n2-> 
253         aux context s1 s2 @ aux (add_ctx context name (Cic.Def (s2,None))) t1 t2
254     | Cic.LetIn (name1, s1, t1), Cic.LetIn (name2, s2, t2) -> []
255     | Cic.Appl terms1, Cic.Appl terms2 -> auxs context terms1 terms2
256     | Cic.Var (_, subst1), Cic.Var (_, subst2)
257     | Cic.Const (_, subst1), Cic.Const (_, subst2)
258     | Cic.MutInd (_, _, subst1), Cic.MutInd (_, _, subst2)
259     | Cic.MutConstruct (_, _, _, subst1), Cic.MutConstruct (_, _, _, subst2) ->
260         auxs context (List.map snd subst1) (List.map snd subst2)
261     | Cic.MutCase (_, _, out1, t1, pat1), Cic.MutCase (_ , _, out2, t2, pat2) ->
262         aux context out1 out2 @ aux context t1 t2 @ auxs context pat1 pat2
263     | Cic.Fix (_, funs1), Cic.Fix (_, funs2) ->
264        let tys =
265         List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs2
266        in
267         List.concat
268           (List.map2
269             (fun (_, _, ty1, bo1) (_, _, ty2, bo2) -> 
270               aux context ty1 ty2 @ aux (tys @ context) bo1 bo2)
271             funs1 funs2)
272     | Cic.CoFix (_, funs1), Cic.CoFix (_, funs2) ->
273        let tys =
274         List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs2
275        in
276         List.concat
277           (List.map2
278             (fun (_, ty1, bo1) (_, ty2, bo2) ->
279               aux context ty1 ty2 @ aux (tys @ context) bo1 bo2)
280             funs1 funs2)
281     | x,y -> 
282         raise (Bad_pattern 
283                 (Printf.sprintf "Pattern %s versus term %s" 
284                   (CicPp.ppterm x)
285                   (CicPp.ppterm y)))
286   and auxs context terms1 terms2 =  (* as aux for list of terms *)
287     List.concat (List.map2 (fun t1 t2 -> aux context t1 t2) terms1 terms2)
288   in
289    let context_len = List.length context in
290    let roots = aux context where term in
291     match wanted with
292        None -> [],metasenv,ugraph,roots
293      | Some wanted ->
294         let rec find_in_roots =
295          function
296             [] -> [],metasenv,ugraph,[]
297           | (context',where)::tl ->
298              let subst,metasenv,ugraph,tl' = find_in_roots tl in
299              let context'_len = List.length context' in
300              let subst,metasenv,ugraph,found =
301               let wanted =
302                CicSubstitution.lift (context'_len - context_len) wanted
303               in
304                find_subterms ~subst ~metasenv ~ugraph ~wanted ~context where
305              in
306               subst,metasenv,ugraph,found @ tl'
307         in
308          find_in_roots roots
309
310 (** create a pattern from a term and a list of subterms.
311 * the pattern is granted to have a ? for every subterm that has no selected
312 * subterms
313 * @param equality equality function used while walking the term. Defaults to
314 * physical equality (==) *)
315 let pattern_of ?(equality=(==)) ~term terms =
316   let (===) x y = equality x y in
317   let not_found = false, Cic.Implicit None in
318   let rec aux t =
319     match t with
320     | t when List.exists (fun t' -> t === t') terms ->
321        true,Cic.Implicit (Some `Hole)
322     | Cic.Var (uri, subst) ->
323        let b,subst = aux_subst subst in
324         if b then
325          true,Cic.Var (uri, subst)
326         else
327          not_found
328     | Cic.Meta (i, ctxt) ->
329         let b,ctxt =
330           List.fold_right
331            (fun e (b,ctxt) ->
332              match e with
333                 None -> b,None::ctxt
334               | Some t -> let bt,t = aux t in b||bt ,Some t::ctxt
335            ) ctxt (false,[])
336         in
337          if b then
338           true,Cic.Meta (i, ctxt)
339          else
340           not_found
341     | Cic.Cast (te, ty) ->
342        let b1,te = aux te in
343        let b2,ty = aux ty in
344         if b1||b2 then true,Cic.Cast (te, ty)
345         else
346          not_found
347     | Cic.Prod (name, s, t) ->
348        let b1,s = aux s in
349        let b2,t = aux t in
350         if b1||b2 then
351          true, Cic.Prod (name, s, t)
352         else
353          not_found
354     | Cic.Lambda (name, s, t) ->
355        let b1,s = aux s in
356        let b2,t = aux t in
357         if b1||b2 then
358          true, Cic.Lambda (name, s, t)
359         else
360          not_found
361     | Cic.LetIn (name, s, t) ->
362        let b1,s = aux s in
363        let b2,t = aux t in
364         if b1||b2 then
365          true, Cic.LetIn (name, s, t)
366         else
367          not_found
368     | Cic.Appl terms ->
369        let b,terms =
370         List.fold_right
371          (fun t (b,terms) ->
372            let bt,t = aux t in
373             b||bt,t::terms
374          ) terms (false,[])
375        in
376         if b then
377          true,Cic.Appl terms
378         else
379          not_found
380     | Cic.Const (uri, subst) ->
381        let b,subst = aux_subst subst in
382         if b then
383          true, Cic.Const (uri, subst)
384         else
385          not_found
386     | Cic.MutInd (uri, tyno, subst) ->
387        let b,subst = aux_subst subst in
388         if b then
389          true, Cic.MutInd (uri, tyno, subst)
390         else
391          not_found
392     | Cic.MutConstruct (uri, tyno, consno, subst) ->
393        let b,subst = aux_subst subst in
394         if b then
395          true, Cic.MutConstruct (uri, tyno, consno, subst)
396         else
397          not_found
398     | Cic.MutCase (uri, tyno, outty, t, pat) ->
399        let b1,outty = aux outty in
400        let b2,t = aux t in
401        let b3,pat =
402         List.fold_right
403          (fun t (b,pat) ->
404            let bt,t = aux t in
405             bt||b,t::pat
406          ) pat (false,[])
407        in
408         if b1 || b2 || b3 then
409          true, Cic.MutCase (uri, tyno, outty, t, pat)
410         else
411          not_found
412     | Cic.Fix (funno, funs) ->
413         let b,funs =
414           List.fold_right
415            (fun (name, i, ty, bo) (b,funs) ->
416              let b1,ty = aux ty in
417              let b2,bo = aux bo in
418               b||b1||b2, (name, i, ty, bo)::funs) funs (false,[])
419         in
420          if b then
421           true, Cic.Fix (funno, funs)
422          else
423           not_found
424     | Cic.CoFix (funno, funs) ->
425         let b,funs =
426           List.fold_right
427            (fun (name, ty, bo) (b,funs) ->
428              let b1,ty = aux ty in
429              let b2,bo = aux bo in
430               b||b1||b2, (name, ty, bo)::funs) funs (false,[])
431         in
432          if b then
433           true, Cic.CoFix (funno, funs)
434          else
435           not_found
436     | Cic.Rel _
437     | Cic.Sort _
438     | Cic.Implicit _ -> not_found
439   and aux_subst subst =
440     List.fold_right
441      (fun (uri, t) (b,subst) ->
442        let b1,t = aux t in
443         b||b1,(uri, t)::subst) subst (false,[])
444   in
445    snd (aux term)
446
447 exception Fail of string
448
449   (** select metasenv conjecture pattern
450   * select all subterms of [conjecture] matching [pattern].
451   * It returns the set of matched terms (that can be compared using physical
452   * equality to the subterms of [conjecture]) together with their contexts.
453   * The representation of the set mimics the ProofEngineTypes.pattern type:
454   * a list of hypothesis (names of) together with the list of its matched
455   * subterms (and their contexts) + the list of matched subterms of the
456   * with their context conclusion. Note: in the result the list of hypothesis
457   * has an entry for each entry in the context and in the same order.
458   * Of course the list of terms (with their context) associated to the
459   * hypothesis name may be empty. *)
460   let select ~metasenv ~ugraph ~conjecture:(_,context,ty)
461        ~pattern:(what,hyp_patterns,goal_pattern)
462   =
463    let find_pattern_for name =
464      try Some (snd (List.find (fun (n, pat) -> Cic.Name n = name) hyp_patterns))
465      with Not_found -> None in
466    let subst,metasenv,ugraph,ty_terms =
467     select_in_term ~metasenv ~context ~ugraph ~term:ty
468      ~pattern:(what,goal_pattern) in
469    let context_len = List.length context in
470    let subst,metasenv,ugraph,context_terms =
471     let subst,metasenv,ugraph,res,_ =
472      (List.fold_right
473       (fun entry (subst,metasenv,ugraph,res,context) ->
474         match entry with
475           None -> subst,metasenv,ugraph,(None::res),(None::context)
476         | Some (name,Cic.Decl term) ->
477             (match find_pattern_for name with
478             | None ->
479                subst,metasenv,ugraph,((Some (`Decl []))::res),(entry::context)
480             | Some pat ->
481                try
482                 let what =
483                  match what with
484                     None -> None
485                   | Some what ->
486                      let what,subst',metasenv' =
487                       CicMetaSubst.delift_rels [] metasenv
488                        (context_len - List.length context) what
489                      in
490                       assert (subst' = []);
491                       assert (metasenv' = metasenv);
492                       Some what in
493                 let subst,metasenv,ugraph,terms =
494                  select_in_term ~metasenv ~context ~ugraph ~term
495                   ~pattern:(what,pat)
496                 in
497                  subst,metasenv,ugraph,((Some (`Decl terms))::res),
498                   (entry::context)
499                with
500                 CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
501                  raise
502                   (Fail
503                     ("The term the user wants to convert is not closed " ^
504                      "in the context of the position of the substitution.")))
505         | Some (name,Cic.Def (bo, ty)) ->
506             (match find_pattern_for name with
507             | None ->
508                let selected_ty=match ty with None -> None | Some _ -> Some [] in
509                 subst,metasenv,ugraph,((Some (`Def ([],selected_ty)))::res),
510                  (entry::context)
511             | Some pat -> 
512                try
513                 let what =
514                  match what with
515                     None -> None
516                   | Some what ->
517                      let what,subst',metasenv' =
518                       CicMetaSubst.delift_rels [] metasenv
519                        (context_len - List.length context) what
520                      in
521                       assert (subst' = []);
522                       assert (metasenv' = metasenv);
523                       Some what in
524                 let subst,metasenv,ugraph,terms_bo =
525                  select_in_term ~metasenv ~context ~ugraph ~term:bo
526                   ~pattern:(what,pat) in
527                 let subst,metasenv,ugraph,terms_ty =
528                  match ty with
529                     None -> subst,metasenv,ugraph,None
530                   | Some ty ->
531                      let subst,metasenv,ugraph,res =
532                       select_in_term ~metasenv ~context ~ugraph ~term:ty
533                        ~pattern:(what,pat)
534                      in
535                       subst,metasenv,ugraph,Some res
536                 in
537                  subst,metasenv,ugraph,((Some (`Def (terms_bo,terms_ty)))::res),
538                   (entry::context)
539                with
540                 CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
541                  raise
542                   (Fail
543                     ("The term the user wants to convert is not closed " ^
544                      "in the context of the position of the substitution.")))
545       ) context (subst,metasenv,ugraph,[],[]))
546     in
547      subst,metasenv,ugraph,res
548    in
549     subst,metasenv,ugraph,context_terms, ty_terms
550
551 (* saturate_term newmeta metasenv context ty                                  *)
552 (* Given a type [ty] (a backbone), it returns its head and a new metasenv in  *)
553 (* which there is new a META for each hypothesis, a list of arguments for the *)
554 (* new applications and the index of the last new META introduced. The nth    *)
555 (* argument in the list of arguments is just the nth new META.                *)
556 let saturate_term newmeta metasenv context ty =
557  let module C = Cic in
558  let module S = CicSubstitution in
559   let rec aux newmeta ty =
560    let ty' = ty in
561    match ty' with
562       C.Cast (he,_) -> aux newmeta he
563 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
564       (* If the expected type is a Type, then also Set is OK ==>
565       *  we accept any term of type Type *)
566       (*CSC: BUG HERE: in this way it is possible for the term of
567       * type Type to be different from a Sort!!! *)
568     | C.Prod (name,(C.Sort (C.Type _) as s),t) ->
569        (* TASSI: ask CSC if BUG HERE refers to the C.Cast or C.Propd case *)
570        let irl =
571          CicMkImplicit.identity_relocation_list_for_metavariable context
572        in
573         let newargument = C.Meta (newmeta+1,irl) in
574          let (res,newmetasenv,arguments,lastmeta) =
575           aux (newmeta + 2) (S.subst newargument t)
576          in
577           res,
578            (newmeta,[],s)::(newmeta+1,context,C.Meta (newmeta,[]))::newmetasenv,
579            newargument::arguments,lastmeta
580 *)
581     | C.Prod (name,s,t) ->
582        let irl =
583          CicMkImplicit.identity_relocation_list_for_metavariable context
584        in
585         let newargument = C.Meta (newmeta,irl) in
586          let (res,newmetasenv,arguments,lastmeta) =
587           aux (newmeta + 1) (S.subst newargument t)
588          in
589          let s' = CicReduction.normalize ~delta:false context s in
590           res,(newmeta,context,s')::newmetasenv,newargument::arguments,lastmeta
591           (** NORMALIZE RATIONALE 
592            * we normalize the target only NOW since we may be in this case:
593            * A1 -> A2 -> T where T = (\lambda x.A3 -> P) k  
594            * and we want a mesasenv with ?1:A1 and ?2:A2 and not
595            * ?1, ?2, ?3 (that is the one we whould get if we start from the
596            * beta-normalized A1 -> A2 -> A3 -> P **)
597     | t -> (CicReduction.normalize ~delta:false context t),[],[],newmeta
598   in
599    (* WARNING: here we are using the invariant that above the most *)
600    (* recente new_meta() there are no used metas.                  *)
601    let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
602     res,metasenv @ newmetasenv,arguments,lastmeta
603