]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/primitiveTactics.ml
This is only a temporary patch. The typecheker raises a
[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 _ 
302      | CicUniv.UniverseInconsistency _ ) when n > 0 ->
303       add_one_argument (n - 1)
304     in
305      add_one_argument goal_arity
306    in
307    let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
308    let apply_subst = CicMetaSubst.apply_subst subst in
309    let old_uninstantiatedmetas,new_uninstantiatedmetas =
310      (* subst_in doesn't need the context. Hence the underscore. *)
311      let subst_in _ = CicMetaSubst.apply_subst subst in
312      classify_metas newmeta in_subst_domain subst_in newmetasenv'
313    in
314    let bo' = apply_subst t in
315    let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
316    let subst_in =
317      (* if we just apply the subtitution, the type is irrelevant:
318               we may use Implicit, since it will be dropped *)
319      CicMetaSubst.apply_subst ((metano,(context,bo',Cic.Implicit None))::subst)
320    in
321    let (newproof, newmetasenv''') = 
322     ProofEngineHelpers.subst_meta_and_metasenv_in_proof proof metano subst_in
323      newmetasenv''
324    in
325    let subst = ((metano,(context,bo',Cic.Implicit None))::subst) in
326    subst,
327    (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas),
328    max maxmeta (CicMkImplicit.new_meta newmetasenv''' subst)
329
330
331 (* ALB *)
332 let apply_with_subst ~term ?(subst=[]) ?(maxmeta=0) status =
333   try
334 (*     apply_tac_verbose ~term status *)
335     apply_with_subst ~term ~subst ~maxmeta status
336       (* TODO cacciare anche altre eccezioni? *)
337   with 
338   | CicUnification.UnificationFailure msg
339   | CicTypeChecker.TypeCheckerFailure msg -> raise (Fail msg)
340   | CicUniv.UniverseInconsistency msg  -> raise (Fail (lazy msg))
341
342 (* ALB *)
343 let apply_tac_verbose ~term status =
344   let subst, status, _ = apply_with_subst ~term status in
345   (CicMetaSubst.apply_subst subst), status
346
347 let apply_tac ~term status = snd (apply_tac_verbose ~term status)
348
349   (* TODO per implementare i tatticali e' necessario che tutte le tattiche
350   sollevino _solamente_ Fail *)
351 let apply_tac ~term =
352  let apply_tac ~term status =
353   try
354     apply_tac ~term status
355       (* TODO cacciare anche altre eccezioni? *)
356   with 
357   | CicUnification.UnificationFailure msg
358   | CicTypeChecker.TypeCheckerFailure msg ->
359       raise (Fail msg)
360  in
361   mk_tactic (apply_tac ~term)
362
363 let intros_tac ?howmany ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) ()=
364  let intros_tac
365   ?(mk_fresh_name_callback = (FreshNamesGenerator.mk_fresh_name ~subst:[])) ()
366   (proof, goal)
367  =
368   let module C = Cic in
369   let module R = CicReduction in
370    let (_,metasenv,_,_) = proof in
371    let metano,context,ty = CicUtil.lookup_meta goal metasenv in
372     let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
373      let (context',ty',bo') =
374       lambda_abstract ?howmany metasenv context newmeta ty mk_fresh_name_callback
375      in
376       let (newproof, _) =
377        ProofEngineHelpers.subst_meta_in_proof proof metano bo'
378         [newmeta,context',ty']
379       in
380        (newproof, [newmeta])
381  in
382   mk_tactic (intros_tac ~mk_fresh_name_callback ())
383   
384 let cut_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
385  let cut_tac
386   ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
387   term (proof, goal)
388  =
389   let module C = Cic in
390    let curi,metasenv,pbo,pty = proof in
391    let metano,context,ty = CicUtil.lookup_meta goal metasenv in
392     let newmeta1 = ProofEngineHelpers.new_meta_of_proof ~proof in
393     let newmeta2 = newmeta1 + 1 in
394     let fresh_name =
395      mk_fresh_name_callback metasenv context (Cic.Name "Hcut") ~typ:term in
396     let context_for_newmeta1 =
397      (Some (fresh_name,C.Decl term))::context in
398     let irl1 =
399      CicMkImplicit.identity_relocation_list_for_metavariable
400       context_for_newmeta1
401     in
402     let irl2 =
403       CicMkImplicit.identity_relocation_list_for_metavariable context
404     in
405      let newmeta1ty = CicSubstitution.lift 1 ty in
406      let bo' =
407       C.Appl
408        [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
409         C.Meta (newmeta2,irl2)]
410      in
411       let (newproof, _) =
412        ProofEngineHelpers.subst_meta_in_proof proof metano bo'
413         [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
414       in
415        (newproof, [newmeta1 ; newmeta2])
416  in
417   mk_tactic (cut_tac ~mk_fresh_name_callback term)
418
419 let letin_tac ?(mk_fresh_name_callback=FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
420  let letin_tac
421   ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
422   term (proof, goal)
423  =
424   let module C = Cic in
425    let curi,metasenv,pbo,pty = proof in
426    (* occur check *)
427    let occur i t =
428      let m = CicUtil.metas_of_term t in 
429      List.exists (fun (j,_) -> i=j) m
430    in
431    let metano,context,ty = CicUtil.lookup_meta goal metasenv in
432    if occur metano term then
433      raise 
434        (ProofEngineTypes.Fail (lazy
435          "You can't letin a term containing the current goal"));
436     let _,_ =
437       CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
438      let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
439      let fresh_name =
440       mk_fresh_name_callback metasenv context (Cic.Name "Hletin") ~typ:term in
441      let context_for_newmeta =
442       (Some (fresh_name,C.Def (term,None)))::context in
443      let irl =
444       CicMkImplicit.identity_relocation_list_for_metavariable
445        context_for_newmeta
446      in
447       let newmetaty = CicSubstitution.lift 1 ty in
448       let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
449        let (newproof, _) =
450          ProofEngineHelpers.subst_meta_in_proof
451            proof metano bo'[newmeta,context_for_newmeta,newmetaty]
452        in
453         (newproof, [newmeta])
454  in
455   mk_tactic (letin_tac ~mk_fresh_name_callback term)
456
457   (** functional part of the "exact" tactic *)
458 let exact_tac ~term =
459  let exact_tac ~term (proof, goal) =
460   (* Assumption: the term bo must be closed in the current context *)
461   let (_,metasenv,_,_) = proof in
462   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
463   let module T = CicTypeChecker in
464   let module R = CicReduction in
465   let ty_term,u = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in
466   let b,_ = R.are_convertible context ty_term ty u in (* TASSI: FIXME *)
467   if b then
468    begin
469     let (newproof, metasenv') =
470       ProofEngineHelpers.subst_meta_in_proof proof metano term [] in
471     (newproof, [])
472    end
473   else
474    raise (Fail (lazy "The type of the provided term is not the one expected."))
475  in
476   mk_tactic (exact_tac ~term)
477
478 (* not really "primitive" tactics .... *)
479 let elim_tac ~term = 
480  let elim_tac ~term (proof, goal) =
481   let module T = CicTypeChecker in
482   let module U = UriManager in
483   let module R = CicReduction in
484   let module C = Cic in
485    let (curi,metasenv,proofbo,proofty) = proof in
486    let metano,context,ty = CicUtil.lookup_meta goal metasenv in
487     let termty,_ = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in
488     let termty = CicReduction.whd context termty in
489     let (termty,metasenv',arguments,fresh_meta) =
490      ProofEngineHelpers.saturate_term
491       (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in
492     let term = if arguments = [] then term else Cic.Appl (term::arguments) in
493     let uri,exp_named_subst,typeno,args =
494      match termty with
495         C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
496       | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
497           (uri,exp_named_subst,typeno,args)
498       | _ -> raise NotAnInductiveTypeToEliminate
499     in
500      let eliminator_uri =
501       let buri = U.buri_of_uri uri in
502       let name = 
503         let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
504        match o with
505           C.InductiveDefinition (tys,_,_,_) ->
506            let (name,_,_,_) = List.nth tys typeno in
507             name
508         | _ -> assert false
509       in
510       let ty_ty,_ = T.type_of_aux' metasenv' context ty CicUniv.empty_ugraph in
511       let ext =
512        match ty_ty with
513           C.Sort C.Prop -> "_ind"
514         | C.Sort C.Set  -> "_rec"
515         | C.Sort C.CProp -> "_rec"
516         | C.Sort (C.Type _)-> "_rect" 
517         | C.Meta (_,_) -> raise TheTypeOfTheCurrentGoalIsAMetaICannotChooseTheRightElimiantionPrinciple
518         | _ -> assert false
519       in
520        U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
521      in
522       let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in
523        let ety,_ = 
524          T.type_of_aux' metasenv' context eliminator_ref CicUniv.empty_ugraph in
525         let rec find_args_no =
526          function
527             C.Prod (_,_,t) -> 1 + find_args_no t
528           | C.Cast (s,_) -> find_args_no s
529           | C.LetIn (_,_,t) -> 0 + find_args_no t
530           | _ -> 0
531         in
532          let args_no = find_args_no ety in
533          let term_to_refine =
534           let rec make_tl base_case =
535            function
536               0 -> [base_case]
537             | n -> (C.Implicit None)::(make_tl base_case (n - 1))
538           in
539            C.Appl (eliminator_ref :: make_tl term (args_no - 1))
540          in
541           let refined_term,_,metasenv'',_ = 
542            CicRefine.type_of_aux' metasenv' context term_to_refine
543              CicUniv.empty_ugraph
544           in
545            let new_goals =
546             ProofEngineHelpers.compare_metasenvs
547              ~oldmetasenv:metasenv ~newmetasenv:metasenv''
548            in
549            let proof' = curi,metasenv'',proofbo,proofty in
550             let proof'', new_goals' =
551              apply_tactic (apply_tac ~term:refined_term) (proof',goal)
552             in
553              (* The apply_tactic can have closed some of the new_goals *)
554              let patched_new_goals =
555               let (_,metasenv''',_,_) = proof'' in
556                List.filter
557                 (function i -> List.exists (function (j,_,_) -> j=i) metasenv'''
558                 ) new_goals @ new_goals'
559              in
560               proof'', patched_new_goals
561  in
562   mk_tactic (elim_tac ~term)
563 ;;
564
565 let elim_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) 
566                     ?depth ?using what =
567  Tacticals.then_ ~start:(elim_tac ~term:what)
568   ~continuation:(intros_tac ~mk_fresh_name_callback ?howmany:depth ())
569 ;;
570
571 (* The simplification is performed only on the conclusion *)
572 let elim_intros_simpl_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
573                           ?depth ?using what =
574  Tacticals.then_ ~start:(elim_tac ~term:what)
575   ~continuation:
576    (Tacticals.thens
577      ~start:(intros_tac ~mk_fresh_name_callback ?howmany:depth ())
578      ~continuations:
579        [ReductionTactics.simpl_tac
580          ~pattern:(ProofEngineTypes.conclusion_pattern None)])
581 ;;
582
583 (* FG: insetrts a "hole" in the context (derived from letin_tac) *)
584
585 module C = Cic
586
587 let letout_tac =
588    let mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[] in
589    let term = C.Sort C.Set in
590    let letout_tac (proof, goal) =
591       let curi, metasenv, pbo, pty = proof in
592       let metano, context, ty = CicUtil.lookup_meta goal metasenv in
593       let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
594       let fresh_name = mk_fresh_name_callback metasenv context (Cic.Name "hole") ~typ:term in
595       let context_for_newmeta = None :: context in
596       let irl = CicMkImplicit.identity_relocation_list_for_metavariable context_for_newmeta in
597       let newmetaty = CicSubstitution.lift 1 ty in
598       let bo' = C.LetIn (fresh_name, term, C.Meta (newmeta,irl)) in
599       let newproof, _ = ProofEngineHelpers.subst_meta_in_proof proof metano bo'[newmeta,context_for_newmeta,newmetaty] in
600       newproof, [newmeta]
601    in
602    mk_tactic letout_tac