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