]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/primitiveTactics.ml
ported to cicMetaSubst
[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 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 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      ProofEngineHelpers.mk_fresh_name context (Cic.Name "Heta") ~typ:argty
119     in
120      (C.Appl [C.Lambda (fresh_name,argty,aux 0 t) ; arg])
121
122 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
123 let classify_metas newmeta in_subst_domain subst_in metasenv =
124  List.fold_right
125   (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
126     if in_subst_domain i then
127      old_uninst,new_uninst
128     else
129      let ty' = subst_in canonical_context ty in
130       let canonical_context' =
131        List.fold_right
132         (fun entry canonical_context' ->
133           let entry' =
134            match entry with
135               Some (n,Cic.Decl s) ->
136                Some (n,Cic.Decl (subst_in canonical_context' s))
137             | Some (n,Cic.Def (s,None)) ->
138                Some (n,Cic.Def ((subst_in canonical_context' s),None))
139             | None -> None
140             | Some (_,Cic.Def (_,Some _)) -> assert false
141           in
142            entry'::canonical_context'
143         ) canonical_context []
144      in
145       if i < newmeta then
146        ((i,canonical_context',ty')::old_uninst),new_uninst
147       else
148        old_uninst,((i,canonical_context',ty')::new_uninst)
149   ) metasenv ([],[])
150
151 (* Auxiliary function for apply: given a type (a backbone), it returns its   *)
152 (* head, a META environment in which there is new a META for each hypothesis,*)
153 (* a list of arguments for the new applications and the indexes of the first *)
154 (* and last new METAs introduced. The nth argument in the list of arguments  *)
155 (* is just the nth new META.                                                 *)
156 let new_metasenv_for_apply newmeta proof context ty =
157  let module C = Cic in
158  let module S = CicSubstitution in
159   let rec aux newmeta =
160    function
161       C.Cast (he,_) -> aux newmeta he
162     | C.Prod (name,s,t) ->
163        let irl =
164          CicMkImplicit.identity_relocation_list_for_metavariable context
165        in
166         let newargument = C.Meta (newmeta,irl) in
167          let (res,newmetasenv,arguments,lastmeta) =
168           aux (newmeta + 1) (S.subst newargument t)
169          in
170           res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
171     | t -> t,[],[],newmeta
172   in
173    (* WARNING: here we are using the invariant that above the most *)
174    (* recente new_meta() there are no used metas.                  *)
175    let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
176     res,newmetasenv,arguments,lastmeta
177
178 (* Useful only inside apply_tac *)
179 let
180  generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
181 =
182  let module C = Cic in
183   let params =
184    match CicEnvironment.get_obj uri with
185       C.Constant (_,_,_,params)
186     | C.CurrentProof (_,_,_,_,params)
187     | C.Variable (_,_,_,params)
188     | C.InductiveDefinition (_,params,_) -> params
189   in
190    let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
191     let next_fresh_meta = ref newmeta in
192     let newmetasenvfragment = ref [] in
193     let exp_named_subst_diff = ref [] in
194      let rec aux =
195       function
196          [],[] -> []
197        | uri::tl,[] ->
198           let ty =
199            match CicEnvironment.get_obj uri with
200               C.Variable (_,_,ty,_) ->
201                CicSubstitution.subst_vars !exp_named_subst_diff ty
202             | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
203           in
204            let irl =
205              CicMkImplicit.identity_relocation_list_for_metavariable context
206            in
207            let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
208             newmetasenvfragment :=
209              (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
210             exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
211             incr next_fresh_meta ;
212             subst_item::(aux (tl,[]))
213        | uri::tl1,((uri',_) as s)::tl2 ->
214           assert (UriManager.eq uri uri') ;
215           s::(aux (tl1,tl2))
216        | [],_ -> assert false
217      in
218       let exp_named_subst' = aux (params,exp_named_subst) in
219        !exp_named_subst_diff,!next_fresh_meta,
220         List.rev !newmetasenvfragment, exp_named_subst'
221    in
222 prerr_endline ("@@@ " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst)) ^ " |--> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst'))) ;
223     new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
224 ;;
225
226 let apply_tac ~term ~status:(proof, goal) =
227   (* Assumption: The term "term" must be closed in the current context *)
228  let module T = CicTypeChecker in
229  let module R = CicReduction in
230  let module C = Cic in
231   let (_,metasenv,_,_) = proof in
232   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
233   let newmeta = new_meta_of_proof ~proof in
234    let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
235     match term with
236        C.Var (uri,exp_named_subst) ->
237         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
238          generalize_exp_named_subst_with_fresh_metas context newmeta uri
239           exp_named_subst
240         in
241          exp_named_subst_diff,newmeta',newmetasenvfragment,
242           C.Var (uri,exp_named_subst')
243      | C.Const (uri,exp_named_subst) ->
244         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
245          generalize_exp_named_subst_with_fresh_metas context newmeta uri
246           exp_named_subst
247         in
248          exp_named_subst_diff,newmeta',newmetasenvfragment,
249           C.Const (uri,exp_named_subst')
250      | C.MutInd (uri,tyno,exp_named_subst) ->
251         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
252          generalize_exp_named_subst_with_fresh_metas context newmeta uri
253           exp_named_subst
254         in
255          exp_named_subst_diff,newmeta',newmetasenvfragment,
256           C.MutInd (uri,tyno,exp_named_subst')
257      | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
258         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
259          generalize_exp_named_subst_with_fresh_metas context newmeta uri
260           exp_named_subst
261         in
262          exp_named_subst_diff,newmeta',newmetasenvfragment,
263           C.MutConstruct (uri,tyno,consno,exp_named_subst')
264      | _ -> [],newmeta,[],term
265    in
266    let metasenv' = metasenv@newmetasenvfragment in
267 prerr_endline ("^^^^^TERM': " ^ CicPp.ppterm term') ; 
268    let termty =
269     CicSubstitution.subst_vars exp_named_subst_diff
270      (CicTypeChecker.type_of_aux' metasenv' context term)
271    in
272 prerr_endline ("^^^^^TERMTY: " ^ CicPp.ppterm termty) ; 
273     (* newmeta is the lowest index of the new metas introduced *)
274     let (consthead,newmetas,arguments,_) =
275      new_metasenv_for_apply newmeta' proof context termty
276     in
277      let newmetasenv = metasenv'@newmetas in
278       let subst,newmetasenv' =
279        CicUnification.fo_unif newmetasenv context consthead ty
280       in
281        let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
282        let apply_subst = CicMetaSubst.apply_subst subst in
283         let old_uninstantiatedmetas,new_uninstantiatedmetas =
284          (* subst_in doesn't need the context. Hence the underscore. *)
285          let subst_in _ = CicMetaSubst.apply_subst subst in
286           classify_metas newmeta in_subst_domain subst_in newmetasenv'
287         in
288          let bo' =
289           apply_subst
290            (if List.length newmetas = 0 then
291              term'
292             else
293              Cic.Appl (term'::arguments)
294            )
295          in
296 prerr_endline ("XXXX " ^ CicPp.ppterm (if List.length newmetas = 0 then term' else Cic.Appl (term'::arguments)) ^ " |>>> " ^ CicPp.ppterm bo') ;
297           let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
298           let (newproof, newmetasenv''') =
299            let subst_in = CicMetaSubst.apply_subst ((metano,bo')::subst) in
300             subst_meta_and_metasenv_in_proof
301               proof metano subst_in newmetasenv''
302           in
303            (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
304
305   (* TODO per implementare i tatticali e' necessario che tutte le tattiche
306   sollevino _solamente_ Fail *)
307 let apply_tac ~term ~status =
308   try
309     apply_tac ~term ~status
310       (* TODO cacciare anche altre eccezioni? *)
311   with CicUnification.UnificationFailure _ as e ->
312     raise (Fail (Printexc.to_string e))
313
314 let intros_tac
315  ?(mk_fresh_name_callback = ProofEngineHelpers.mk_fresh_name) ()
316  ~status:(proof, goal)
317 =
318  let module C = Cic in
319  let module R = CicReduction in
320   let (_,metasenv,_,_) = proof in
321   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
322    let newmeta = new_meta_of_proof ~proof in
323     let (context',ty',bo') =
324      lambda_abstract context newmeta ty mk_fresh_name_callback
325     in
326      let (newproof, _) =
327        subst_meta_in_proof proof metano bo' [newmeta,context',ty']
328      in
329       (newproof, [newmeta])
330
331 let cut_tac
332  ?(mk_fresh_name_callback = ProofEngineHelpers.mk_fresh_name)
333  term ~status:(proof, goal)
334 =
335  let module C = Cic in
336   let curi,metasenv,pbo,pty = proof in
337   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
338    let newmeta1 = new_meta_of_proof ~proof in
339    let newmeta2 = newmeta1 + 1 in
340    let fresh_name =
341     mk_fresh_name_callback context (Cic.Name "Hcut") ~typ:term in
342    let context_for_newmeta1 =
343     (Some (fresh_name,C.Decl term))::context in
344    let irl1 =
345     CicMkImplicit.identity_relocation_list_for_metavariable
346      context_for_newmeta1
347    in
348    let irl2 =
349      CicMkImplicit.identity_relocation_list_for_metavariable context
350    in
351     let newmeta1ty = CicSubstitution.lift 1 ty in
352     let bo' =
353      C.Appl
354       [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
355        C.Meta (newmeta2,irl2)]
356     in
357      let (newproof, _) =
358       subst_meta_in_proof proof metano bo'
359        [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
360      in
361       (newproof, [newmeta1 ; newmeta2])
362
363 let letin_tac
364  ?(mk_fresh_name_callback = ProofEngineHelpers.mk_fresh_name)
365  term ~status:(proof, goal)
366 =
367  let module C = Cic in
368   let curi,metasenv,pbo,pty = proof in
369   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
370    let _ = CicTypeChecker.type_of_aux' metasenv context term in
371     let newmeta = new_meta_of_proof ~proof in
372     let fresh_name =
373      mk_fresh_name_callback context (Cic.Name "Hletin") ~typ:term in
374     let context_for_newmeta =
375      (Some (fresh_name,C.Def (term,None)))::context in
376     let irl =
377      CicMkImplicit.identity_relocation_list_for_metavariable
378       context_for_newmeta
379     in
380      let newmetaty = CicSubstitution.lift 1 ty in
381      let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
382       let (newproof, _) =
383         subst_meta_in_proof
384           proof metano bo'[newmeta,context_for_newmeta,newmetaty]
385       in
386        (newproof, [newmeta])
387
388   (** functional part of the "exact" tactic *)
389 let exact_tac ~term ~status:(proof, goal) =
390  (* Assumption: the term bo must be closed in the current context *)
391  let (_,metasenv,_,_) = proof in
392  let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
393  let module T = CicTypeChecker in
394  let module R = CicReduction in
395  if R.are_convertible context (T.type_of_aux' metasenv context term) ty then
396   begin
397    let (newproof, metasenv') =
398      subst_meta_in_proof proof metano term [] in
399    (newproof, [])
400   end
401  else
402   raise (Fail "The type of the provided term is not the one expected.")
403
404
405 (* not really "primitive" tactics .... *)
406
407 let elim_tac ~term ~status:(proof, goal) =
408  let module T = CicTypeChecker in
409  let module U = UriManager in
410  let module R = CicReduction in
411  let module C = Cic in
412   let (curi,metasenv,_,_) = proof in
413   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
414    let termty = T.type_of_aux' metasenv context term in
415    let uri,exp_named_subst,typeno,args =
416     match termty with
417        C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
418      | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
419          (uri,exp_named_subst,typeno,args)
420      | _ -> raise NotAnInductiveTypeToEliminate
421    in
422     let eliminator_uri =
423      let buri = U.buri_of_uri uri in
424      let name = 
425       match CicEnvironment.get_obj uri with
426          C.InductiveDefinition (tys,_,_) ->
427           let (name,_,_,_) = List.nth tys typeno in
428            name
429        | _ -> assert false
430      in
431      let ext =
432       match T.type_of_aux' metasenv context ty with
433          C.Sort C.Prop -> "_ind"
434        | C.Sort C.Set  -> "_rec"
435        | C.Sort C.CProp -> "_rec"
436        | C.Sort C.Type -> "_rect"
437        | _ -> assert false
438      in
439       U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
440     in
441      let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in
442       let ety = T.type_of_aux' metasenv context eliminator_ref in
443       let newmeta = new_meta_of_proof ~proof in
444        let (econclusion,newmetas,arguments,lastmeta) =
445          new_metasenv_for_apply newmeta proof context ety
446        in
447         (* Here we assume that we have only one inductive hypothesis to *)
448         (* eliminate and that it is the last hypothesis of the theorem. *)
449         (* A better approach would be fingering the hypotheses in some  *)
450         (* way.                                                         *)
451         let meta_of_corpse =
452          let (_,canonical_context,_) =
453           List.find (function (m,_,_) -> m=(lastmeta - 1)) newmetas
454          in
455           let irl =
456            CicMkImplicit.identity_relocation_list_for_metavariable
457             canonical_context
458           in
459            Cic.Meta (lastmeta - 1, irl)
460         in
461         let newmetasenv = newmetas @ metasenv in
462         let subst1,newmetasenv' =
463          CicUnification.fo_unif newmetasenv context term meta_of_corpse
464         in
465          let ueconclusion = CicMetaSubst.apply_subst subst1 econclusion in
466           (* The conclusion of our elimination principle is *)
467           (*  (?i farg1 ... fargn)                         *)
468           (* The conclusion of our goal is ty. So, we can   *)
469           (* eta-expand ty w.r.t. farg1 .... fargn to get   *)
470           (* a new ty equal to (P farg1 ... fargn). Now     *)
471           (* ?i can be instantiated with P and we are ready *)
472           (* to refine the term.                            *)
473           let emeta, fargs =
474            match ueconclusion with
475               C.Appl ((C.Meta (emeta,_))::fargs) -> emeta,fargs
476             | C.Meta (emeta,_) -> emeta,[]
477             | _ -> raise NotTheRightEliminatorShape
478           in
479            let ty' = CicMetaSubst.apply_subst subst1 ty in
480            let eta_expanded_ty =
481 (*CSC: newmetasenv' era metasenv ??????????? *)
482             List.fold_left (eta_expand newmetasenv' context) ty' fargs
483            in
484             let subst2,newmetasenv'' =
485 (*CSC: passo newmetasenv', ma alcune variabili sono gia' state sostituite
486 da subst1!!!! Dovrei rimuoverle o sono innocue?*)
487              CicUnification.fo_unif
488               newmetasenv' context ueconclusion eta_expanded_ty
489             in
490              let in_subst_domain i =
491               let eq_to_i = function (j,_) -> i=j in
492                List.exists eq_to_i subst1 ||
493                List.exists eq_to_i subst2
494              in
495               (* When unwinding the META that corresponds to the elimination *)
496               (* predicate (which is emeta), we must also perform one-step   *)
497               (* beta-reduction. apply_subst doesn't need the context. Hence *)
498               (* the underscore.                                             *)
499               let apply_subst _ t =
500                let t' = CicMetaSubst.apply_subst subst1 t in
501                 CicMetaSubst.apply_subst_reducing
502                  subst2 (Some (emeta,List.length fargs)) t'
503               in
504                 let old_uninstantiatedmetas,new_uninstantiatedmetas =
505                  classify_metas newmeta in_subst_domain apply_subst
506                   newmetasenv''
507                 in
508                  let arguments' = List.map (apply_subst context) arguments in
509                   let bo' = Cic.Appl (eliminator_ref::arguments') in
510                    let newmetasenv''' =
511                     new_uninstantiatedmetas@old_uninstantiatedmetas
512                    in
513                     let (newproof, newmetasenv'''') =
514                      (* When unwinding the META that corresponds to the *)
515                      (* elimination predicate (which is emeta), we must *)
516                      (* also perform one-step beta-reduction.           *)
517                      (* The only difference w.r.t. apply_subst is that  *)
518                      (* we also substitute metano with bo'.             *)
519                      (*CSC: Nota: sostituire nuovamente subst1 e' superfluo, *)
520                      (*CSC: no?                                              *)
521                      let apply_subst' t =
522                       let t' = CicMetaSubst.apply_subst subst1 t in
523                        CicMetaSubst.apply_subst_reducing
524                         ((metano,bo')::subst2)
525                         (Some (emeta,List.length fargs)) t'
526                      in
527                       subst_meta_and_metasenv_in_proof
528                         proof metano apply_subst' newmetasenv'''
529                     in
530                      (newproof,
531                       List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
532 ;;
533
534 (* The simplification is performed only on the conclusion *)
535 let elim_intros_simpl_tac ~term =
536  Tacticals.then_ ~start:(elim_tac ~term)
537   ~continuation:
538    (Tacticals.thens
539      ~start:(intros_tac ())
540      ~continuations:
541        [ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None])
542 ;;
543
544 exception NotConvertible
545
546 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal,  *)
547 (*CSC: while [what] can have a richer context (because of binders)           *)
548 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
549 (*CSC: Is that evident? Is that right? Or should it be changed?              *)
550 let change_tac ~what ~with_what ~status:(proof, goal) =
551  let curi,metasenv,pbo,pty = proof in
552  let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
553   (* are_convertible works only on well-typed terms *)
554   ignore (CicTypeChecker.type_of_aux' metasenv context with_what) ;
555   if CicReduction.are_convertible context what with_what then
556    begin
557     let replace =
558      ProofEngineReduction.replace
559       ~equality:(==) ~what:[what] ~with_what:[with_what]
560     in
561     let ty' = replace ty in
562     let context' =
563      List.map
564       (function
565           Some (name,Cic.Def (t,None)) -> Some (name,Cic.Def ((replace t),None))
566         | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
567         | None -> None
568         | Some (_,Cic.Def (_,Some _)) -> assert false
569       ) context
570     in
571      let metasenv' = 
572       List.map
573        (function
574            (n,_,_) when n = metano -> (metano,context',ty')
575          | _ as t -> t
576        ) metasenv
577      in
578       (curi,metasenv',pbo,pty), [metano]
579    end
580   else
581    raise (ProofEngineTypes.Fail "Not convertible")