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