]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/primitiveTactics.ml
got rid of ~status label so that tactics can now be applied partially,
[helm.git] / helm / ocaml / tactics / primitiveTactics.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 open ProofEngineHelpers
27 open ProofEngineTypes
28
29 exception NotAnInductiveTypeToEliminate
30 exception NotTheRightEliminatorShape
31 exception NoHypothesesFound
32 exception WrongUriToVariable of string
33
34 (* lambda_abstract newmeta ty *)
35 (* returns a triple [bo],[context],[ty'] where              *)
36 (* [ty] = Pi/LetIn [context].[ty'] ([context] is a vector!) *)
37 (* and [bo] = Lambda/LetIn [context].(Meta [newmeta])       *)
38 (* So, lambda_abstract is the core of the implementation of *)
39 (* the Intros tactic.                                       *)
40 let lambda_abstract metasenv context newmeta ty mk_fresh_name =
41  let module C = Cic in
42   let rec collect_context context =
43    function
44       C.Cast (te,_)   -> collect_context context te
45     | C.Prod (n,s,t)  ->
46        let n' = mk_fresh_name metasenv context n ~typ:s in
47         let (context',ty,bo) =
48          collect_context ((Some (n',(C.Decl s)))::context) t
49         in
50          (context',ty,C.Lambda(n',s,bo))
51     | C.LetIn (n,s,t) ->
52        let (context',ty,bo) =
53         collect_context ((Some (n,(C.Def (s,None))))::context) t
54        in
55         (context',ty,C.LetIn(n,s,bo))
56     | _ as t ->
57       let irl =
58         CicMkImplicit.identity_relocation_list_for_metavariable context
59       in
60        context, t, (C.Meta (newmeta,irl))
61   in
62    collect_context context ty
63
64 let eta_expand metasenv context t arg =
65  let module T = CicTypeChecker in
66  let module S = CicSubstitution in
67  let module C = Cic in
68   let rec aux n =
69    function
70       t' when t' = S.lift n arg -> C.Rel (1 + n)
71     | C.Rel m  -> if m <= n then C.Rel m else C.Rel (m+1)
72     | C.Var (uri,exp_named_subst) ->
73        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
74         C.Var (uri,exp_named_subst')
75     | C.Meta _
76     | C.Sort _
77     | C.Implicit _ as t -> t
78     | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
79     | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
80     | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
81     | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
82     | C.Appl l -> C.Appl (List.map (aux n) l)
83     | C.Const (uri,exp_named_subst) ->
84        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
85         C.Const (uri,exp_named_subst')
86     | C.MutInd (uri,i,exp_named_subst) ->
87        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
88         C.MutInd (uri,i,exp_named_subst')
89     | C.MutConstruct (uri,i,j,exp_named_subst) ->
90        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
91         C.MutConstruct (uri,i,j,exp_named_subst')
92     | C.MutCase (sp,i,outt,t,pl) ->
93        C.MutCase (sp,i,aux n outt, aux n t,
94         List.map (aux n) pl)
95     | C.Fix (i,fl) ->
96        let tylen = List.length fl in
97         let substitutedfl =
98          List.map
99           (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
100            fl
101         in
102          C.Fix (i, substitutedfl)
103     | C.CoFix (i,fl) ->
104        let tylen = List.length fl in
105         let substitutedfl =
106          List.map
107           (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
108            fl
109         in
110          C.CoFix (i, substitutedfl)
111   and aux_exp_named_subst n =
112    List.map (function uri,t -> uri,aux n t)
113   in
114    let argty =
115     T.type_of_aux' metasenv context arg
116    in
117     let fresh_name =
118      FreshNamesGenerator.mk_fresh_name
119       metasenv context (Cic.Name "Heta") ~typ:argty
120     in
121      (C.Appl [C.Lambda (fresh_name,argty,aux 0 t) ; arg])
122
123 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
124 let classify_metas newmeta in_subst_domain subst_in metasenv =
125  List.fold_right
126   (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
127     if in_subst_domain i then
128      old_uninst,new_uninst
129     else
130      let ty' = subst_in canonical_context ty in
131       let canonical_context' =
132        List.fold_right
133         (fun entry canonical_context' ->
134           let entry' =
135            match entry with
136               Some (n,Cic.Decl s) ->
137                Some (n,Cic.Decl (subst_in canonical_context' s))
138             | Some (n,Cic.Def (s,None)) ->
139                Some (n,Cic.Def ((subst_in canonical_context' s),None))
140             | None -> None
141             | Some (_,Cic.Def (_,Some _)) -> assert false
142           in
143            entry'::canonical_context'
144         ) canonical_context []
145      in
146       if i < newmeta then
147        ((i,canonical_context',ty')::old_uninst),new_uninst
148       else
149        old_uninst,((i,canonical_context',ty')::new_uninst)
150   ) metasenv ([],[])
151
152 (* Auxiliary function for apply: given a type (a backbone), it returns its   *)
153 (* head, a META environment in which there is new a META for each hypothesis,*)
154 (* a list of arguments for the new applications and the indexes of the first *)
155 (* and last new METAs introduced. The nth argument in the list of arguments  *)
156 (* is just the nth new META.                                                 *)
157 let new_metasenv_for_apply newmeta proof context ty =
158  let module C = Cic in
159  let module S = CicSubstitution in
160   let rec aux newmeta =
161    function
162       C.Cast (he,_) -> aux newmeta he
163       (* If the expected type is a Type, then also Set is OK ==>
164       *  we accept any term of type Type *)
165       (*CSC: BUG HERE: in this way it is possible for the term of
166       * type Type to be different from a Sort!!! *)
167     | C.Prod (name,(C.Sort C.Type as s),t) ->
168        let irl =
169          CicMkImplicit.identity_relocation_list_for_metavariable context
170        in
171         let newargument = C.Meta (newmeta+1,irl) in
172          let (res,newmetasenv,arguments,lastmeta) =
173           aux (newmeta + 2) (S.subst newargument t)
174          in
175           res,
176            (newmeta,[],s)::(newmeta+1,context,C.Meta (newmeta,[]))::newmetasenv,
177            newargument::arguments,lastmeta
178     | C.Prod (name,s,t) ->
179        let irl =
180          CicMkImplicit.identity_relocation_list_for_metavariable context
181        in
182         let newargument = C.Meta (newmeta,irl) in
183          let (res,newmetasenv,arguments,lastmeta) =
184           aux (newmeta + 1) (S.subst newargument t)
185          in
186           res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
187     | t -> t,[],[],newmeta
188   in
189    (* WARNING: here we are using the invariant that above the most *)
190    (* recente new_meta() there are no used metas.                  *)
191    let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
192     res,newmetasenv,arguments,lastmeta
193
194 (* Useful only inside apply_tac *)
195 let
196  generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
197 =
198  let module C = Cic in
199   let params =
200    match CicEnvironment.get_obj uri with
201       C.Constant (_,_,_,params)
202     | C.CurrentProof (_,_,_,_,params)
203     | C.Variable (_,_,_,params)
204     | C.InductiveDefinition (_,params,_) -> params
205   in
206    let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
207     let next_fresh_meta = ref newmeta in
208     let newmetasenvfragment = ref [] in
209     let exp_named_subst_diff = ref [] in
210      let rec aux =
211       function
212          [],[] -> []
213        | uri::tl,[] ->
214           let ty =
215            match CicEnvironment.get_obj uri with
216               C.Variable (_,_,ty,_) ->
217                CicSubstitution.subst_vars !exp_named_subst_diff ty
218             | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
219           in
220            (match ty with
221                C.Sort C.Type as s ->
222                  let fresh_meta = !next_fresh_meta in
223                  let fresh_meta' = fresh_meta + 1 in
224                   next_fresh_meta := !next_fresh_meta + 2 ;
225                   let subst_item = uri,C.Meta (fresh_meta',[]) in
226                    newmetasenvfragment :=
227                     (fresh_meta,[],C.Sort C.Type) ::
228                      (fresh_meta',[],C.Meta (fresh_meta,[])) :: !newmetasenvfragment ;
229                    exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
230                    subst_item::(aux (tl,[]))
231              | _ ->
232               let irl =
233                 CicMkImplicit.identity_relocation_list_for_metavariable context
234               in
235               let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
236                newmetasenvfragment :=
237                 (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
238                exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
239                incr next_fresh_meta ;
240                subst_item::(aux (tl,[])))
241        | uri::tl1,((uri',_) as s)::tl2 ->
242           assert (UriManager.eq uri uri') ;
243           s::(aux (tl1,tl2))
244        | [],_ -> assert false
245      in
246       let exp_named_subst' = aux (params,exp_named_subst) in
247        !exp_named_subst_diff,!next_fresh_meta,
248         List.rev !newmetasenvfragment, exp_named_subst'
249    in
250     new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
251 ;;
252
253 let apply_tac ~term (proof, goal) =
254   (* Assumption: The term "term" must be closed in the current context *)
255  let module T = CicTypeChecker in
256  let module R = CicReduction in
257  let module C = Cic in
258   let (_,metasenv,_,_) = proof in
259   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
260   let newmeta = new_meta_of_proof ~proof in
261    let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
262     match term with
263        C.Var (uri,exp_named_subst) ->
264         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
265          generalize_exp_named_subst_with_fresh_metas context newmeta uri
266           exp_named_subst
267         in
268          exp_named_subst_diff,newmeta',newmetasenvfragment,
269           C.Var (uri,exp_named_subst')
270      | C.Const (uri,exp_named_subst) ->
271         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
272          generalize_exp_named_subst_with_fresh_metas context newmeta uri
273           exp_named_subst
274         in
275          exp_named_subst_diff,newmeta',newmetasenvfragment,
276           C.Const (uri,exp_named_subst')
277      | C.MutInd (uri,tyno,exp_named_subst) ->
278         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
279          generalize_exp_named_subst_with_fresh_metas context newmeta uri
280           exp_named_subst
281         in
282          exp_named_subst_diff,newmeta',newmetasenvfragment,
283           C.MutInd (uri,tyno,exp_named_subst')
284      | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
285         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
286          generalize_exp_named_subst_with_fresh_metas context newmeta uri
287           exp_named_subst
288         in
289          exp_named_subst_diff,newmeta',newmetasenvfragment,
290           C.MutConstruct (uri,tyno,consno,exp_named_subst')
291      | _ -> [],newmeta,[],term
292    in
293    let metasenv' = metasenv@newmetasenvfragment in
294    let termty =
295     CicSubstitution.subst_vars exp_named_subst_diff
296      (CicTypeChecker.type_of_aux' metasenv' context term)
297    in
298     (* newmeta is the lowest index of the new metas introduced *)
299     let (consthead,newmetas,arguments,_) =
300      new_metasenv_for_apply newmeta' proof context termty
301     in
302      let newmetasenv = metasenv'@newmetas in
303       let subst,newmetasenv' =
304         CicUnification.fo_unif newmetasenv context consthead ty
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' =
314           apply_subst
315            (if List.length newmetas = 0 then
316              term'
317             else
318              Cic.Appl (term'::arguments)
319            )
320          in
321           let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
322           let (newproof, newmetasenv''') =
323            let subst_in = CicMetaSubst.apply_subst ((metano,bo')::subst) in
324             subst_meta_and_metasenv_in_proof
325               proof metano subst_in newmetasenv''
326           in
327            (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
328
329   (* TODO per implementare i tatticali e' necessario che tutte le tattiche
330   sollevino _solamente_ Fail *)
331 let apply_tac ~term status =
332   try
333     apply_tac ~term status
334       (* TODO cacciare anche altre eccezioni? *)
335   with CicUnification.UnificationFailure _ as e ->
336     raise (Fail (Printexc.to_string e))
337
338 let intros_tac
339  ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name) ()
340  (proof, goal)
341 =
342  let module C = Cic in
343  let module R = CicReduction in
344   let (_,metasenv,_,_) = proof in
345   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
346    let newmeta = new_meta_of_proof ~proof in
347     let (context',ty',bo') =
348      lambda_abstract metasenv context newmeta ty mk_fresh_name_callback
349     in
350      let (newproof, _) =
351        subst_meta_in_proof proof metano bo' [newmeta,context',ty']
352      in
353       (newproof, [newmeta])
354
355 let cut_tac
356  ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
357  term (proof, goal)
358 =
359  let module C = Cic in
360   let curi,metasenv,pbo,pty = proof in
361   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
362    let newmeta1 = new_meta_of_proof ~proof in
363    let newmeta2 = newmeta1 + 1 in
364    let fresh_name =
365     mk_fresh_name_callback metasenv context (Cic.Name "Hcut") ~typ:term in
366    let context_for_newmeta1 =
367     (Some (fresh_name,C.Decl term))::context in
368    let irl1 =
369     CicMkImplicit.identity_relocation_list_for_metavariable
370      context_for_newmeta1
371    in
372    let irl2 =
373      CicMkImplicit.identity_relocation_list_for_metavariable context
374    in
375     let newmeta1ty = CicSubstitution.lift 1 ty in
376     let bo' =
377      C.Appl
378       [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
379        C.Meta (newmeta2,irl2)]
380     in
381      let (newproof, _) =
382       subst_meta_in_proof proof metano bo'
383        [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
384      in
385       (newproof, [newmeta1 ; newmeta2])
386
387 let letin_tac
388  ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
389  term (proof, goal)
390 =
391  let module C = Cic in
392   let curi,metasenv,pbo,pty = proof in
393   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
394    let _ = CicTypeChecker.type_of_aux' metasenv context term in
395     let newmeta = new_meta_of_proof ~proof in
396     let fresh_name =
397      mk_fresh_name_callback metasenv context (Cic.Name "Hletin") ~typ:term in
398     let context_for_newmeta =
399      (Some (fresh_name,C.Def (term,None)))::context in
400     let irl =
401      CicMkImplicit.identity_relocation_list_for_metavariable
402       context_for_newmeta
403     in
404      let newmetaty = CicSubstitution.lift 1 ty in
405      let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
406       let (newproof, _) =
407         subst_meta_in_proof
408           proof metano bo'[newmeta,context_for_newmeta,newmetaty]
409       in
410        (newproof, [newmeta])
411
412   (** functional part of the "exact" tactic *)
413 let exact_tac ~term (proof, goal) =
414  (* Assumption: the term bo must be closed in the current context *)
415  let (_,metasenv,_,_) = proof in
416  let metano,context,ty = CicUtil.lookup_meta goal metasenv in
417  let module T = CicTypeChecker in
418  let module R = CicReduction in
419  if R.are_convertible context (T.type_of_aux' metasenv context term) ty then
420   begin
421    let (newproof, metasenv') =
422      subst_meta_in_proof proof metano term [] in
423    (newproof, [])
424   end
425  else
426   raise (Fail "The type of the provided term is not the one expected.")
427
428
429 (* not really "primitive" tactics .... *)
430
431 let elim_tac ~term (proof, goal) =
432  let module T = CicTypeChecker in
433  let module U = UriManager in
434  let module R = CicReduction in
435  let module C = Cic in
436   let (curi,metasenv,_,_) = proof in
437   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
438    let termty = T.type_of_aux' metasenv context term in
439    let uri,exp_named_subst,typeno,args =
440     match termty with
441        C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
442      | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
443          (uri,exp_named_subst,typeno,args)
444      | _ -> raise NotAnInductiveTypeToEliminate
445    in
446     let eliminator_uri =
447      let buri = U.buri_of_uri uri in
448      let name = 
449       match CicEnvironment.get_obj uri with
450          C.InductiveDefinition (tys,_,_) ->
451           let (name,_,_,_) = List.nth tys typeno in
452            name
453        | _ -> assert false
454      in
455      let ext =
456       match T.type_of_aux' metasenv context ty with
457          C.Sort C.Prop -> "_ind"
458        | C.Sort C.Set  -> "_rec"
459        | C.Sort C.CProp -> "_rec"
460        | C.Sort C.Type -> "_rect"
461        | _ -> assert false
462      in
463       U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
464     in
465      let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in
466       let ety = T.type_of_aux' metasenv context eliminator_ref in
467       let newmeta = new_meta_of_proof ~proof in
468        let (econclusion,newmetas,arguments,lastmeta) =
469          new_metasenv_for_apply newmeta proof context ety
470        in
471         (* Here we assume that we have only one inductive hypothesis to *)
472         (* eliminate and that it is the last hypothesis of the theorem. *)
473         (* A better approach would be fingering the hypotheses in some  *)
474         (* way.                                                         *)
475         let meta_of_corpse =
476          let (_,canonical_context,_) =
477            CicUtil.lookup_meta (lastmeta - 1) newmetas
478          in
479           let irl =
480            CicMkImplicit.identity_relocation_list_for_metavariable
481             canonical_context
482           in
483            Cic.Meta (lastmeta - 1, irl)
484         in
485         let newmetasenv = newmetas @ metasenv in
486         let subst1,newmetasenv' =
487          CicUnification.fo_unif newmetasenv context term meta_of_corpse
488         in
489          let ueconclusion = CicMetaSubst.apply_subst subst1 econclusion in
490           (* The conclusion of our elimination principle is *)
491           (*  (?i farg1 ... fargn)                         *)
492           (* The conclusion of our goal is ty. So, we can   *)
493           (* eta-expand ty w.r.t. farg1 .... fargn to get   *)
494           (* a new ty equal to (P farg1 ... fargn). Now     *)
495           (* ?i can be instantiated with P and we are ready *)
496           (* to refine the term.                            *)
497           let emeta, fargs =
498            match ueconclusion with
499               C.Appl ((C.Meta (emeta,_))::fargs) -> emeta,fargs
500             | C.Meta (emeta,_) -> emeta,[]
501             | _ -> raise NotTheRightEliminatorShape
502           in
503            let ty' = CicMetaSubst.apply_subst subst1 ty in
504            let eta_expanded_ty =
505 (*CSC: newmetasenv' era metasenv ??????????? *)
506             List.fold_left (eta_expand newmetasenv' context) ty' fargs
507            in
508             let subst2,newmetasenv'' =
509 (*CSC: passo newmetasenv', ma alcune variabili sono gia' state sostituite
510 da subst1!!!! Dovrei rimuoverle o sono innocue?*)
511              CicUnification.fo_unif
512               newmetasenv' context ueconclusion eta_expanded_ty
513             in
514              let in_subst_domain i =
515               let eq_to_i = function (j,_) -> i=j in
516                List.exists eq_to_i subst1 ||
517                List.exists eq_to_i subst2
518              in
519               (* When unwinding the META that corresponds to the elimination *)
520               (* predicate (which is emeta), we must also perform one-step   *)
521               (* beta-reduction. apply_subst doesn't need the context. Hence *)
522               (* the underscore.                                             *)
523               let apply_subst _ t =
524                let t' = CicMetaSubst.apply_subst subst1 t in
525                 CicMetaSubst.apply_subst_reducing
526                  (Some (emeta,List.length fargs)) subst2 t'
527               in
528                 let old_uninstantiatedmetas,new_uninstantiatedmetas =
529                  classify_metas newmeta in_subst_domain apply_subst
530                   newmetasenv''
531                 in
532                  let arguments' = List.map (apply_subst context) arguments in
533                   let bo' = Cic.Appl (eliminator_ref::arguments') in
534                    let newmetasenv''' =
535                     new_uninstantiatedmetas@old_uninstantiatedmetas
536                    in
537                     let (newproof, newmetasenv'''') =
538                      (* When unwinding the META that corresponds to the *)
539                      (* elimination predicate (which is emeta), we must *)
540                      (* also perform one-step beta-reduction.           *)
541                      (* The only difference w.r.t. apply_subst is that  *)
542                      (* we also substitute metano with bo'.             *)
543                      (*CSC: Nota: sostituire nuovamente subst1 e' superfluo, *)
544                      (*CSC: no?                                              *)
545                      let apply_subst' t =
546                       let t' = CicMetaSubst.apply_subst subst1 t in
547                        CicMetaSubst.apply_subst_reducing
548                         (Some (emeta,List.length fargs))
549                         ((metano,bo')::subst2) t'
550                      in
551                       subst_meta_and_metasenv_in_proof
552                         proof metano apply_subst' newmetasenv'''
553                     in
554                      (newproof,
555                       List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
556 ;;
557
558 (* The simplification is performed only on the conclusion *)
559 let elim_intros_simpl_tac ~term =
560  Tacticals.then_ ~start:(elim_tac ~term)
561   ~continuation:
562    (Tacticals.thens
563      ~start:(intros_tac ())
564      ~continuations:
565        [ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None])
566 ;;
567
568 exception NotConvertible
569
570 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal,  *)
571 (*CSC: while [what] can have a richer context (because of binders)           *)
572 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
573 (*CSC: Is that evident? Is that right? Or should it be changed?              *)
574 let change_tac ~what ~with_what (proof, goal) =
575  let curi,metasenv,pbo,pty = proof in
576  let metano,context,ty = CicUtil.lookup_meta goal metasenv in
577   (* are_convertible works only on well-typed terms *)
578   ignore (CicTypeChecker.type_of_aux' metasenv context with_what) ;
579   if CicReduction.are_convertible context what with_what then
580    begin
581     let replace =
582      ProofEngineReduction.replace
583       ~equality:(==) ~what:[what] ~with_what:[with_what]
584     in
585     let ty' = replace ty in
586     let context' =
587      List.map
588       (function
589           Some (name,Cic.Def (t,None)) -> Some (name,Cic.Def ((replace t),None))
590         | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
591         | None -> None
592         | Some (_,Cic.Def (_,Some _)) -> assert false
593       ) context
594     in
595      let metasenv' = 
596       List.map
597        (function
598            (n,_,_) when n = metano -> (metano,context',ty')
599          | _ as t -> t
600        ) metasenv
601      in
602       (curi,metasenv',pbo,pty), [metano]
603    end
604   else
605    raise (ProofEngineTypes.Fail "Not convertible")