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