]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/primitiveTactics.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / ocaml / tactics / primitiveTactics.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 open ProofEngineHelpers
27 open ProofEngineTypes
28
29 exception NotAnInductiveTypeToEliminate
30 exception NotTheRightEliminatorShape
31 exception NoHypothesesFound
32 exception WrongUriToVariable of string
33
34 (* lambda_abstract newmeta ty *)
35 (* returns a triple [bo],[context],[ty'] where              *)
36 (* [ty] = Pi/LetIn [context].[ty'] ([context] is a vector!) *)
37 (* and [bo] = Lambda/LetIn [context].(Meta [newmeta])       *)
38 (* So, lambda_abstract is the core of the implementation of *)
39 (* the Intros tactic.                                       *)
40 let lambda_abstract context newmeta ty mk_fresh_name =
41  let module C = Cic in
42   let rec collect_context context =
43    function
44       C.Cast (te,_)   -> collect_context context te
45     | C.Prod (n,s,t)  ->
46        let n' = mk_fresh_name context n ~typ:s in
47         let (context',ty,bo) =
48          collect_context ((Some (n',(C.Decl s)))::context) t
49         in
50          (context',ty,C.Lambda(n',s,bo))
51     | C.LetIn (n,s,t) ->
52        let (context',ty,bo) =
53         collect_context ((Some (n,(C.Def s)))::context) t
54        in
55         (context',ty,C.LetIn(n,s,bo))
56     | _ as t ->
57       let irl = identity_relocation_list_for_metavariable context in
58        context, t, (C.Meta (newmeta,irl))
59   in
60    collect_context context ty
61
62 let eta_expand metasenv context t arg =
63  let module T = CicTypeChecker in
64  let module S = CicSubstitution in
65  let module C = Cic in
66   let rec aux n =
67    function
68       t' when t' = S.lift n arg -> C.Rel (1 + n)
69     | C.Rel m  -> if m <= n then C.Rel m else C.Rel (m+1)
70     | C.Var (uri,exp_named_subst) ->
71        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
72         C.Var (uri,exp_named_subst')
73     | C.Meta _
74     | C.Sort _
75     | C.Implicit as t -> t
76     | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
77     | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
78     | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
79     | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
80     | C.Appl l -> C.Appl (List.map (aux n) l)
81     | C.Const (uri,exp_named_subst) ->
82        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
83         C.Const (uri,exp_named_subst')
84     | C.MutInd (uri,i,exp_named_subst) ->
85        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
86         C.MutInd (uri,i,exp_named_subst')
87     | C.MutConstruct (uri,i,j,exp_named_subst) ->
88        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
89         C.MutConstruct (uri,i,j,exp_named_subst')
90     | C.MutCase (sp,i,outt,t,pl) ->
91        C.MutCase (sp,i,aux n outt, aux n t,
92         List.map (aux n) pl)
93     | C.Fix (i,fl) ->
94        let tylen = List.length fl in
95         let substitutedfl =
96          List.map
97           (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
98            fl
99         in
100          C.Fix (i, substitutedfl)
101     | C.CoFix (i,fl) ->
102        let tylen = List.length fl in
103         let substitutedfl =
104          List.map
105           (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
106            fl
107         in
108          C.CoFix (i, substitutedfl)
109   and aux_exp_named_subst n =
110    List.map (function uri,t -> uri,aux n t)
111   in
112    let argty =
113     T.type_of_aux' metasenv context arg
114    in
115     let fresh_name =
116      ProofEngineHelpers.mk_fresh_name context (Cic.Name "Heta") ~typ:argty
117     in
118      (C.Appl [C.Lambda (fresh_name,argty,aux 0 t) ; arg])
119
120 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
121 let classify_metas newmeta in_subst_domain subst_in metasenv =
122  List.fold_right
123   (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
124     if in_subst_domain i then
125      old_uninst,new_uninst
126     else
127      let ty' = subst_in canonical_context ty in
128       let canonical_context' =
129        List.fold_right
130         (fun entry canonical_context' ->
131           let entry' =
132            match entry with
133               Some (n,Cic.Decl s) ->
134                Some (n,Cic.Decl (subst_in canonical_context' s))
135             | Some (n,Cic.Def s) ->
136                Some (n,Cic.Def (subst_in canonical_context' s))
137             | None -> None
138           in
139            entry'::canonical_context'
140         ) canonical_context []
141      in
142       if i < newmeta then
143        ((i,canonical_context',ty')::old_uninst),new_uninst
144       else
145        old_uninst,((i,canonical_context',ty')::new_uninst)
146   ) metasenv ([],[])
147
148 (* Auxiliary function for apply: given a type (a backbone), it returns its   *)
149 (* head, a META environment in which there is new a META for each hypothesis,*)
150 (* a list of arguments for the new applications and the indexes of the first *)
151 (* and last new METAs introduced. The nth argument in the list of arguments  *)
152 (* is just the nth new META.                                                 *)
153 let new_metasenv_for_apply newmeta proof context ty =
154  let module C = Cic in
155  let module S = CicSubstitution in
156   let rec aux newmeta =
157    function
158       C.Cast (he,_) -> aux newmeta he
159     | C.Prod (name,s,t) ->
160        let irl = identity_relocation_list_for_metavariable context in
161         let newargument = C.Meta (newmeta,irl) in
162          let (res,newmetasenv,arguments,lastmeta) =
163           aux (newmeta + 1) (S.subst newargument t)
164          in
165           res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
166     | t -> t,[],[],newmeta
167   in
168    (* WARNING: here we are using the invariant that above the most *)
169    (* recente new_meta() there are no used metas.                  *)
170    let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
171     res,newmetasenv,arguments,lastmeta
172
173 (* Useful only inside apply_tac *)
174 let
175  generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
176 =
177  let module C = Cic in
178   let params =
179    match CicEnvironment.get_obj uri with
180       C.Constant (_,_,_,params)
181     | C.CurrentProof (_,_,_,_,params)
182     | C.Variable (_,_,_,params)
183     | C.InductiveDefinition (_,params,_) -> params
184   in
185    let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
186     let next_fresh_meta = ref newmeta in
187     let newmetasenvfragment = ref [] in
188     let exp_named_subst_diff = ref [] in
189      let rec aux =
190       function
191          [],[] -> []
192        | uri::tl,[] ->
193           let ty =
194            match CicEnvironment.get_obj uri with
195               C.Variable (_,_,ty,_) ->
196                CicSubstitution.subst_vars !exp_named_subst_diff ty
197             | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
198           in
199            let irl = identity_relocation_list_for_metavariable context in
200            let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
201             newmetasenvfragment :=
202              (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
203             exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
204             incr next_fresh_meta ;
205             subst_item::(aux (tl,[]))
206        | uri::tl1,((uri',_) as s)::tl2 ->
207           assert (UriManager.eq uri uri') ;
208           s::(aux (tl1,tl2))
209        | [],_ -> assert false
210      in
211       let exp_named_subst' = aux (params,exp_named_subst) in
212        !exp_named_subst_diff,!next_fresh_meta,
213         List.rev !newmetasenvfragment, exp_named_subst'
214    in
215 prerr_endline ("@@@ " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst)) ^ " |--> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst'))) ;
216     new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
217 ;;
218
219 let apply_tac ~term ~status:(proof, goal) =
220   (* Assumption: The term "term" must be closed in the current context *)
221  let module T = CicTypeChecker in
222  let module R = CicReduction in
223  let module C = Cic in
224   let (_,metasenv,_,_) = proof in
225   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
226   let newmeta = new_meta ~proof in
227    let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
228     match term with
229        C.Var (uri,exp_named_subst) ->
230         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
231          generalize_exp_named_subst_with_fresh_metas context newmeta uri
232           exp_named_subst
233         in
234          exp_named_subst_diff,newmeta',newmetasenvfragment,
235           C.Var (uri,exp_named_subst')
236      | C.Const (uri,exp_named_subst) ->
237         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
238          generalize_exp_named_subst_with_fresh_metas context newmeta uri
239           exp_named_subst
240         in
241          exp_named_subst_diff,newmeta',newmetasenvfragment,
242           C.Const (uri,exp_named_subst')
243      | C.MutInd (uri,tyno,exp_named_subst) ->
244         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
245          generalize_exp_named_subst_with_fresh_metas context newmeta uri
246           exp_named_subst
247         in
248          exp_named_subst_diff,newmeta',newmetasenvfragment,
249           C.MutInd (uri,tyno,exp_named_subst')
250      | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
251         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
252          generalize_exp_named_subst_with_fresh_metas context newmeta uri
253           exp_named_subst
254         in
255          exp_named_subst_diff,newmeta',newmetasenvfragment,
256           C.MutConstruct (uri,tyno,consno,exp_named_subst')
257      | _ -> [],newmeta,[],term
258    in
259    let metasenv' = metasenv@newmetasenvfragment in
260 prerr_endline ("^^^^^TERM': " ^ CicPp.ppterm term') ; 
261    let termty =
262     CicSubstitution.subst_vars exp_named_subst_diff
263      (CicTypeChecker.type_of_aux' metasenv' context term)
264    in
265 prerr_endline ("^^^^^TERMTY: " ^ CicPp.ppterm termty) ; 
266     (* newmeta is the lowest index of the new metas introduced *)
267     let (consthead,newmetas,arguments,_) =
268      new_metasenv_for_apply newmeta' proof context termty
269     in
270      let newmetasenv = metasenv'@newmetas in
271       let subst,newmetasenv' =
272        CicUnification.fo_unif newmetasenv context consthead ty
273       in
274        let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
275        let apply_subst = CicUnification.apply_subst subst in
276         let old_uninstantiatedmetas,new_uninstantiatedmetas =
277          (* subst_in doesn't need the context. Hence the underscore. *)
278          let subst_in _ = CicUnification.apply_subst subst in
279           classify_metas newmeta in_subst_domain subst_in newmetasenv'
280         in
281          let bo' =
282           apply_subst
283            (if List.length newmetas = 0 then
284              term'
285             else
286              Cic.Appl (term'::arguments)
287            )
288          in
289 prerr_endline ("XXXX " ^ CicPp.ppterm (if List.length newmetas = 0 then term' else Cic.Appl (term'::arguments)) ^ " |>>> " ^ CicPp.ppterm bo') ;
290           let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
291           let (newproof, newmetasenv''') =
292            let subst_in = CicUnification.apply_subst ((metano,bo')::subst) in
293             subst_meta_and_metasenv_in_proof
294               proof metano subst_in newmetasenv''
295           in
296            (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
297
298   (* TODO per implementare i tatticali e' necessario che tutte le tattiche
299   sollevino _solamente_ Fail *)
300 let apply_tac ~term ~status =
301   try
302     apply_tac ~term ~status
303       (* TODO cacciare anche altre eccezioni? *)
304   with CicUnification.UnificationFailed as e ->
305     raise (Fail (Printexc.to_string e))
306
307 let intros_tac
308  ?(mk_fresh_name_callback = ProofEngineHelpers.mk_fresh_name) ()
309  ~status:(proof, goal)
310 =
311  let module C = Cic in
312  let module R = CicReduction in
313   let (_,metasenv,_,_) = proof in
314   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
315    let newmeta = new_meta ~proof in
316     let (context',ty',bo') =
317      lambda_abstract context newmeta ty mk_fresh_name_callback
318     in
319      let (newproof, _) =
320        subst_meta_in_proof proof metano bo' [newmeta,context',ty']
321      in
322       (newproof, [newmeta])
323
324 let cut_tac
325  ?(mk_fresh_name_callback = ProofEngineHelpers.mk_fresh_name)
326  term ~status:(proof, goal)
327 =
328  let module C = Cic in
329   let curi,metasenv,pbo,pty = proof in
330   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
331    let newmeta1 = new_meta ~proof in
332    let newmeta2 = newmeta1 + 1 in
333    let fresh_name =
334     mk_fresh_name_callback context (Cic.Name "Hcut") ~typ:term in
335    let context_for_newmeta1 =
336     (Some (fresh_name,C.Decl term))::context in
337    let irl1 =
338     identity_relocation_list_for_metavariable context_for_newmeta1 in
339    let irl2 = identity_relocation_list_for_metavariable context in
340     let newmeta1ty = CicSubstitution.lift 1 ty in
341     let bo' =
342      C.Appl
343       [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
344        C.Meta (newmeta2,irl2)]
345     in
346      let (newproof, _) =
347       subst_meta_in_proof proof metano bo'
348        [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
349      in
350       (newproof, [newmeta1 ; newmeta2])
351
352 let letin_tac
353  ?(mk_fresh_name_callback = ProofEngineHelpers.mk_fresh_name)
354  term ~status:(proof, goal)
355 =
356  let module C = Cic in
357   let curi,metasenv,pbo,pty = proof in
358   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
359    let _ = CicTypeChecker.type_of_aux' metasenv context term in
360     let newmeta = new_meta ~proof in
361     let fresh_name =
362      mk_fresh_name_callback context (Cic.Name "Hletin") ~typ:term in
363     let context_for_newmeta =
364      (Some (fresh_name,C.Def term))::context in
365     let irl =
366      identity_relocation_list_for_metavariable context_for_newmeta in
367      let newmetaty = CicSubstitution.lift 1 ty in
368      let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
369       let (newproof, _) =
370         subst_meta_in_proof
371           proof metano bo'[newmeta,context_for_newmeta,newmetaty]
372       in
373        (newproof, [newmeta])
374
375   (** functional part of the "exact" tactic *)
376 let exact_tac ~term ~status:(proof, goal) =
377  (* Assumption: the term bo must be closed in the current context *)
378  let (_,metasenv,_,_) = proof in
379  let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
380  let module T = CicTypeChecker in
381  let module R = CicReduction in
382  if R.are_convertible context (T.type_of_aux' metasenv context term) ty then
383   begin
384    let (newproof, metasenv') =
385      subst_meta_in_proof proof metano term [] in
386    (newproof, [])
387   end
388  else
389   raise (Fail "The type of the provided term is not the one expected.")
390
391
392 (* not really "primitive" tactics .... *)
393
394 let elim_tac ~term ~status:(proof, goal) =
395  let module T = CicTypeChecker in
396  let module U = UriManager in
397  let module R = CicReduction in
398  let module C = Cic in
399   let (curi,metasenv,_,_) = proof in
400   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
401    let termty = T.type_of_aux' metasenv context term in
402    let uri,exp_named_subst,typeno,args =
403     match termty with
404        C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
405      | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
406          (uri,exp_named_subst,typeno,args)
407      | _ -> raise NotAnInductiveTypeToEliminate
408    in
409     let eliminator_uri =
410      let buri = U.buri_of_uri uri in
411      let name = 
412       match CicEnvironment.get_obj uri with
413          C.InductiveDefinition (tys,_,_) ->
414           let (name,_,_,_) = List.nth tys typeno in
415            name
416        | _ -> assert false
417      in
418      let ext =
419       match T.type_of_aux' metasenv context ty with
420          C.Sort C.Prop -> "_ind"
421        | C.Sort C.Set  -> "_rec"
422        | C.Sort C.Type -> "_rect"
423        | _ -> assert false
424      in
425       U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
426     in
427      let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in
428       let ety = T.type_of_aux' metasenv context eliminator_ref in
429       let newmeta = new_meta ~proof in
430        let (econclusion,newmetas,arguments,lastmeta) =
431          new_metasenv_for_apply newmeta proof context ety
432        in
433         (* Here we assume that we have only one inductive hypothesis to *)
434         (* eliminate and that it is the last hypothesis of the theorem. *)
435         (* A better approach would be fingering the hypotheses in some  *)
436         (* way.                                                         *)
437         let meta_of_corpse =
438          let (_,canonical_context,_) =
439           List.find (function (m,_,_) -> m=(lastmeta - 1)) newmetas
440          in
441           let irl =
442            identity_relocation_list_for_metavariable canonical_context
443           in
444            Cic.Meta (lastmeta - 1, irl)
445         in
446         let newmetasenv = newmetas @ metasenv in
447         let subst1,newmetasenv' =
448          CicUnification.fo_unif newmetasenv context term meta_of_corpse
449         in
450          let ueconclusion = CicUnification.apply_subst subst1 econclusion in
451           (* The conclusion of our elimination principle is *)
452           (*  (?i farg1 ... fargn)                         *)
453           (* The conclusion of our goal is ty. So, we can   *)
454           (* eta-expand ty w.r.t. farg1 .... fargn to get   *)
455           (* a new ty equal to (P farg1 ... fargn). Now     *)
456           (* ?i can be instantiated with P and we are ready *)
457           (* to refine the term.                            *)
458           let emeta, fargs =
459            match ueconclusion with
460               C.Appl ((C.Meta (emeta,_))::fargs) -> emeta,fargs
461             | C.Meta (emeta,_) -> emeta,[]
462             | _ -> raise NotTheRightEliminatorShape
463           in
464            let ty' = CicUnification.apply_subst subst1 ty in
465            let eta_expanded_ty =
466 (*CSC: newmetasenv' era metasenv ??????????? *)
467             List.fold_left (eta_expand newmetasenv' context) ty' fargs
468            in
469             let subst2,newmetasenv'' =
470 (*CSC: passo newmetasenv', ma alcune variabili sono gia' state sostituite
471 da subst1!!!! Dovrei rimuoverle o sono innocue?*)
472              CicUnification.fo_unif
473               newmetasenv' context ueconclusion eta_expanded_ty
474             in
475              let in_subst_domain i =
476               let eq_to_i = function (j,_) -> i=j in
477                List.exists eq_to_i subst1 ||
478                List.exists eq_to_i subst2
479              in
480               (* When unwinding the META that corresponds to the elimination *)
481               (* predicate (which is emeta), we must also perform one-step   *)
482               (* beta-reduction. apply_subst doesn't need the context. Hence *)
483               (* the underscore.                                             *)
484               let apply_subst _ t =
485                let t' = CicUnification.apply_subst subst1 t in
486                 CicUnification.apply_subst_reducing
487                  subst2 (Some (emeta,List.length fargs)) t'
488               in
489                 let old_uninstantiatedmetas,new_uninstantiatedmetas =
490                  classify_metas newmeta in_subst_domain apply_subst
491                   newmetasenv''
492                 in
493                  let arguments' = List.map (apply_subst context) arguments in
494                   let bo' = Cic.Appl (eliminator_ref::arguments') in
495                    let newmetasenv''' =
496                     new_uninstantiatedmetas@old_uninstantiatedmetas
497                    in
498                     let (newproof, newmetasenv'''') =
499                      (* When unwinding the META that corresponds to the *)
500                      (* elimination predicate (which is emeta), we must *)
501                      (* also perform one-step beta-reduction.           *)
502                      (* The only difference w.r.t. apply_subst is that  *)
503                      (* we also substitute metano with bo'.             *)
504                      (*CSC: Nota: sostituire nuovamente subst1 e' superfluo, *)
505                      (*CSC: no?                                              *)
506                      let apply_subst' t =
507                       let t' = CicUnification.apply_subst subst1 t in
508                        CicUnification.apply_subst_reducing
509                         ((metano,bo')::subst2)
510                         (Some (emeta,List.length fargs)) t'
511                      in
512                       subst_meta_and_metasenv_in_proof
513                         proof metano apply_subst' newmetasenv'''
514                     in
515                      (newproof,
516                       List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
517 ;;
518
519 (* The simplification is performed only on the conclusion *)
520 let elim_intros_simpl_tac ~term =
521  Tacticals.then_ ~start:(elim_tac ~term)
522   ~continuation:
523    (Tacticals.thens
524      ~start:(intros_tac ())
525      ~continuations:
526        [ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None])
527 ;;
528
529 exception NotConvertible
530
531 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal,  *)
532 (*CSC: while [what] can have a richer context (because of binders)           *)
533 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
534 (*CSC: Is that evident? Is that right? Or should it be changed?              *)
535 let change_tac ~what ~with_what ~status:(proof, goal) =
536  let curi,metasenv,pbo,pty = proof in
537  let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
538   (* are_convertible works only on well-typed terms *)
539   ignore (CicTypeChecker.type_of_aux' metasenv context with_what) ;
540   if CicReduction.are_convertible context what with_what then
541    begin
542     let replace =
543      ProofEngineReduction.replace
544       ~equality:(==) ~what:[what] ~with_what:[with_what]
545     in
546     let ty' = replace ty in
547     let context' =
548      List.map
549       (function
550           Some (name,Cic.Def  t) -> Some (name,Cic.Def  (replace t))
551         | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
552         | None -> None
553       ) context
554     in
555      let metasenv' = 
556       List.map
557        (function
558            (n,_,_) when n = metano -> (metano,context',ty')
559          | _ as t -> t
560        ) metasenv
561      in
562       (curi,metasenv',pbo,pty), [metano]
563    end
564   else
565    raise (ProofEngineTypes.Fail "Not convertible")