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