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