]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/primitiveTactics.ml
Undo of the previous commit (that was a mistake).
[helm.git] / helm / gTopLevel / 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 (* TODO problemone del fresh_name, aggiungerlo allo status? *)
35 let fresh_name () = "FOO"
36
37 (* lambda_abstract newmeta ty *)
38 (* returns a triple [bo],[context],[ty'] where              *)
39 (* [ty] = Pi/LetIn [context].[ty'] ([context] is a vector!) *)
40 (* and [bo] = Lambda/LetIn [context].(Meta [newmeta])       *)
41 (* So, lambda_abstract is the core of the implementation of *)
42 (* the Intros tactic.                                       *)
43 let lambda_abstract context newmeta ty name =
44  let module C = Cic in
45   let rec collect_context context =
46    function
47       C.Cast (te,_)   -> collect_context context te
48     | C.Prod (n,s,t)  ->
49        let n' =
50         match n with
51            C.Name _ -> n
52 (*CSC: generatore di nomi? Chiedere il nome? *)
53          | C.Anonymous -> C.Name name
54        in
55         let (context',ty,bo) =
56          collect_context ((Some (n',(C.Decl s)))::context) 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)))::context) t
62        in
63         (context',ty,C.LetIn(n,s,bo))
64     | _ as t ->
65       let irl = identity_relocation_list_for_metavariable context in
66        context, t, (C.Meta (newmeta,irl))
67   in
68    collect_context context ty
69
70 let eta_expand metasenv context t arg =
71  let module T = CicTypeChecker in
72  let module S = CicSubstitution in
73  let module C = Cic in
74   let rec aux n =
75    function
76       t' when t' = S.lift n arg -> C.Rel (1 + n)
77     | C.Rel m  -> if m <= n then C.Rel m else C.Rel (m+1)
78     | C.Var (uri,exp_named_subst) ->
79        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
80         C.Var (uri,exp_named_subst')
81     | C.Meta _
82     | C.Sort _
83     | C.Implicit as t -> t
84     | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
85     | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
86     | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
87     | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
88     | C.Appl l -> C.Appl (List.map (aux n) l)
89     | C.Const (uri,exp_named_subst) ->
90        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
91         C.Const (uri,exp_named_subst')
92     | C.MutInd (uri,i,exp_named_subst) ->
93        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
94         C.MutInd (uri,i,exp_named_subst')
95     | C.MutConstruct (uri,i,j,exp_named_subst) ->
96        let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
97         C.MutConstruct (uri,i,j,exp_named_subst')
98     | C.MutCase (sp,i,outt,t,pl) ->
99        C.MutCase (sp,i,aux n outt, aux n t,
100         List.map (aux n) pl)
101     | C.Fix (i,fl) ->
102        let tylen = List.length fl in
103         let substitutedfl =
104          List.map
105           (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
106            fl
107         in
108          C.Fix (i, substitutedfl)
109     | C.CoFix (i,fl) ->
110        let tylen = List.length fl in
111         let substitutedfl =
112          List.map
113           (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
114            fl
115         in
116          C.CoFix (i, substitutedfl)
117   and aux_exp_named_subst n =
118    List.map (function uri,t -> uri,aux n t)
119   in
120    let argty =
121     T.type_of_aux' metasenv context arg
122    in
123     (C.Appl [C.Lambda ((C.Name "dummy"),argty,aux 0 t) ; arg])
124
125 (*CSC: The call to the Intros tactic is embedded inside the code of the *)
126 (*CSC: Elim tactic. Do we already need tacticals?                       *)
127 (* Auxiliary function for apply: given a type (a backbone), it returns its   *)
128 (* head, a META environment in which there is new a META for each hypothesis,*)
129 (* a list of arguments for the new applications and the indexes of the first *)
130 (* and last new METAs introduced. The nth argument in the list of arguments  *)
131 (* is the nth new META lambda-abstracted as much as possible. Hence, this    *)
132 (* functions already provides the behaviour of Intros on the new goals.      *)
133 let new_metasenv_for_apply_intros proof context ty =
134  let module C = Cic in
135  let module S = CicSubstitution in
136   let rec aux newmeta =
137    function
138       C.Cast (he,_) -> aux newmeta he
139     | C.Prod (name,s,t) ->
140        let newcontext,ty',newargument =
141          lambda_abstract context newmeta s (fresh_name ())
142        in
143         let (res,newmetasenv,arguments,lastmeta) =
144          aux (newmeta + 1) (S.subst newargument t)
145         in
146          res,(newmeta,newcontext,ty')::newmetasenv,newargument::arguments,lastmeta
147     | t -> t,[],[],newmeta
148   in
149    let newmeta = new_meta ~proof in
150     (* WARNING: here we are using the invariant that above the most *)
151     (* recente new_meta() there are no used metas.                  *)
152     let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
153      res,newmetasenv,arguments,newmeta,lastmeta
154
155 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
156 let classify_metas newmeta in_subst_domain subst_in metasenv =
157  List.fold_right
158   (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
159     if in_subst_domain i then
160      old_uninst,new_uninst
161     else
162      let ty' = subst_in canonical_context ty in
163       let canonical_context' =
164        List.fold_right
165         (fun entry canonical_context' ->
166           let entry' =
167            match entry with
168               Some (n,Cic.Decl s) ->
169                Some (n,Cic.Decl (subst_in canonical_context' s))
170             | Some (n,Cic.Def s) ->
171                Some (n,Cic.Def (subst_in canonical_context' s))
172             | None -> None
173           in
174            entry'::canonical_context'
175         ) canonical_context []
176      in
177       if i < newmeta then
178        ((i,canonical_context',ty')::old_uninst),new_uninst
179       else
180        old_uninst,((i,canonical_context',ty')::new_uninst)
181   ) metasenv ([],[])
182
183 (* Auxiliary function for apply: given a type (a backbone), it returns its   *)
184 (* head, a META environment in which there is new a META for each hypothesis,*)
185 (* a list of arguments for the new applications and the indexes of the first *)
186 (* and last new METAs introduced. The nth argument in the list of arguments  *)
187 (* is just the nth new META.                                                 *)
188 let new_metasenv_for_apply newmeta proof context ty =
189  let module C = Cic in
190  let module S = CicSubstitution in
191   let rec aux newmeta =
192    function
193       C.Cast (he,_) -> aux newmeta he
194     | C.Prod (name,s,t) ->
195        let irl = identity_relocation_list_for_metavariable context in
196         let newargument = C.Meta (newmeta,irl) in
197          let (res,newmetasenv,arguments,lastmeta) =
198           aux (newmeta + 1) (S.subst newargument t)
199          in
200           res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
201     | t -> t,[],[],newmeta
202   in
203    (* WARNING: here we are using the invariant that above the most *)
204    (* recente new_meta() there are no used metas.                  *)
205    let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
206     res,newmetasenv,arguments,lastmeta
207
208 (* Useful only inside apply_tac *)
209 let
210  generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
211 =
212  let module C = Cic in
213   let params =
214    match CicEnvironment.get_obj uri with
215       C.Constant (_,_,_,params)
216     | C.CurrentProof (_,_,_,_,params)
217     | C.Variable (_,_,_,params)
218     | C.InductiveDefinition (_,params,_) -> params
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            match CicEnvironment.get_obj uri with
230               C.Variable (_,_,ty,_) ->
231                CicSubstitution.subst_vars !exp_named_subst_diff ty
232             | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
233           in
234            let irl = identity_relocation_list_for_metavariable context 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 prerr_endline ("@@@ " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst)) ^ " |--> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst'))) ;
251     new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
252 ;;
253
254 let apply_tac ~term ~status:(proof, goal) =
255   (* Assumption: The term "term" must be closed in the current context *)
256  let module T = CicTypeChecker in
257  let module R = CicReduction in
258  let module C = Cic in
259   let (_,metasenv,_,_) = proof in
260   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
261   let newmeta = new_meta ~proof in
262    let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
263     match term with
264        C.Var (uri,exp_named_subst) ->
265         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
266          generalize_exp_named_subst_with_fresh_metas context newmeta uri
267           exp_named_subst
268         in
269          exp_named_subst_diff,newmeta',newmetasenvfragment,
270           C.Var (uri,exp_named_subst')
271      | C.Const (uri,exp_named_subst) ->
272         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
273          generalize_exp_named_subst_with_fresh_metas context newmeta uri
274           exp_named_subst
275         in
276          exp_named_subst_diff,newmeta',newmetasenvfragment,
277           C.Const (uri,exp_named_subst')
278      | C.MutInd (uri,tyno,exp_named_subst) ->
279         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
280          generalize_exp_named_subst_with_fresh_metas context newmeta uri
281           exp_named_subst
282         in
283          exp_named_subst_diff,newmeta',newmetasenvfragment,
284           C.MutInd (uri,tyno,exp_named_subst')
285      | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
286         let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
287          generalize_exp_named_subst_with_fresh_metas context newmeta uri
288           exp_named_subst
289         in
290          exp_named_subst_diff,newmeta',newmetasenvfragment,
291           C.MutConstruct (uri,tyno,consno,exp_named_subst')
292      | _ -> [],newmeta,[],term
293    in
294    let metasenv' = newmetasenvfragment@metasenv in
295 prerr_endline ("^^^^^TERM': " ^ CicPp.ppterm term') ; 
296    let termty =
297     CicSubstitution.subst_vars exp_named_subst_diff
298      (CicTypeChecker.type_of_aux' metasenv' context term)
299    in
300 prerr_endline ("^^^^^TERMTY: " ^ CicPp.ppterm termty) ; 
301     (* newmeta is the lowest index of the new metas introduced *)
302     let (consthead,newmetas,arguments,_) =
303      new_metasenv_for_apply newmeta' proof context termty
304     in
305      let newmetasenv = newmetas@metasenv' in
306       let subst,newmetasenv' =
307        CicUnification.fo_unif newmetasenv context consthead ty
308       in
309        let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
310        let apply_subst = CicUnification.apply_subst subst in
311         let old_uninstantiatedmetas,new_uninstantiatedmetas =
312          (* subst_in doesn't need the context. Hence the underscore. *)
313          let subst_in _ = CicUnification.apply_subst subst in
314           classify_metas newmeta' in_subst_domain subst_in newmetasenv'
315         in
316          let bo' =
317           apply_subst
318            (if List.length newmetas = 0 then
319              term'
320             else
321              Cic.Appl (term'::arguments)
322            )
323          in
324 prerr_endline ("XXXX " ^ CicPp.ppterm (if List.length newmetas = 0 then term' else Cic.Appl (term'::arguments)) ^ " |>>> " ^ CicPp.ppterm bo') ;
325           let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
326           let (newproof, newmetasenv''') =
327            let subst_in = CicUnification.apply_subst ((metano,bo')::subst) in
328             subst_meta_and_metasenv_in_proof
329               proof metano subst_in newmetasenv''
330           in
331            (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
332
333   (* TODO per implementare i tatticali e' necessario che tutte le tattiche
334   sollevino _solamente_ Fail *)
335 let apply_tac ~term ~status =
336   try
337     apply_tac ~term ~status
338       (* TODO cacciare anche altre eccezioni? *)
339   with CicUnification.UnificationFailed as e ->
340     raise (Fail (Printexc.to_string e))
341
342 let intros_tac ~name ~status:(proof, goal) =
343  let module C = Cic in
344  let module R = CicReduction in
345   let (_,metasenv,_,_) = proof in
346   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
347    let newmeta = new_meta ~proof in
348     let (context',ty',bo') = lambda_abstract context newmeta ty name in
349      let (newproof, _) =
350        subst_meta_in_proof proof metano bo' [newmeta,context',ty']
351      in
352       (newproof, [newmeta])
353
354 let cut_tac ~term ~status:(proof, goal) =
355  let module C = Cic in
356   let curi,metasenv,pbo,pty = proof in
357   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
358    let newmeta1 = new_meta ~proof in
359    let newmeta2 = newmeta1 + 1 in
360    let context_for_newmeta1 =
361     (Some (C.Name "dummy_for_cut",C.Decl term))::context in
362    let irl1 =
363     identity_relocation_list_for_metavariable context_for_newmeta1 in
364    let irl2 = identity_relocation_list_for_metavariable context in
365     let newmeta1ty = CicSubstitution.lift 1 ty in
366     let bo' =
367      C.Appl
368       [C.Lambda (C.Name "dummy_for_cut",term,C.Meta (newmeta1,irl1)) ;
369        C.Meta (newmeta2,irl2)]
370     in
371      let (newproof, _) =
372       subst_meta_in_proof proof metano bo'
373        [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
374      in
375       (newproof, [newmeta1 ; newmeta2])
376
377 let letin_tac ~term ~status:(proof, goal) =
378  let module C = Cic in
379   let curi,metasenv,pbo,pty = proof in
380   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
381    let _ = CicTypeChecker.type_of_aux' metasenv context term in
382     let newmeta = new_meta ~proof in
383     let context_for_newmeta =
384      (Some (C.Name "dummy_for_letin",C.Def term))::context in
385     let irl =
386      identity_relocation_list_for_metavariable context_for_newmeta in
387      let newmetaty = CicSubstitution.lift 1 ty in
388      let bo' = C.LetIn (C.Name "dummy_for_letin",term,C.Meta (newmeta,irl)) in
389       let (newproof, _) =
390         subst_meta_in_proof
391           proof metano bo'[newmeta,context_for_newmeta,newmetaty]
392       in
393        (newproof, [newmeta])
394
395   (** functional part of the "exact" tactic *)
396 let exact_tac ~term ~status:(proof, goal) =
397  (* Assumption: the term bo must be closed in the current context *)
398  let (_,metasenv,_,_) = proof in
399  let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
400  let module T = CicTypeChecker in
401  let module R = CicReduction in
402  if R.are_convertible context (T.type_of_aux' metasenv context term) ty then
403   begin
404    let (newproof, metasenv') =
405      subst_meta_in_proof proof metano term [] in
406    (newproof, [])
407   end
408  else
409   raise (Fail "The type of the provided term is not the one expected.")
410
411
412 (* not really "primite" tactics .... *)
413
414 let elim_intros_simpl_tac ~term ~status:(proof, goal) =
415  let module T = CicTypeChecker in
416  let module U = UriManager in
417  let module R = CicReduction in
418  let module C = Cic in
419   let (curi,metasenv,_,_) = proof in
420   let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
421    let termty = T.type_of_aux' metasenv context term in
422    let uri,exp_named_subst,typeno,args =
423     match termty with
424        C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
425      | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
426          (uri,exp_named_subst,typeno,args)
427      | _ ->
428          prerr_endline ("MALFATTORE" ^ (CicPp.ppterm termty));
429          flush stderr;
430          raise NotAnInductiveTypeToEliminate
431    in
432     let eliminator_uri =
433      let buri = U.buri_of_uri uri in
434      let name = 
435       match CicEnvironment.get_obj uri with
436          C.InductiveDefinition (tys,_,_) ->
437           let (name,_,_,_) = List.nth tys typeno in
438            name
439        | _ -> assert false
440      in
441      let ext =
442       match T.type_of_aux' metasenv context ty with
443          C.Sort C.Prop -> "_ind"
444        | C.Sort C.Set  -> "_rec"
445        | C.Sort C.Type -> "_rect"
446        | _ -> assert false
447      in
448       U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
449     in
450 (*CSC: BUG HERE!!! [] MUST BE COMPUTED SOMEHOW. USING UNIFICATION? *)
451      let eliminator_ref = C.Const (eliminator_uri,[]) in
452       let ety =
453        T.type_of_aux' [] [] eliminator_ref
454       in
455        let (econclusion,newmetas,arguments,newmeta,lastmeta) =
456 (*
457         new_metasenv_for_apply context ety
458 *)
459         new_metasenv_for_apply_intros proof context ety
460        in
461         (* Here we assume that we have only one inductive hypothesis to *)
462         (* eliminate and that it is the last hypothesis of the theorem. *)
463         (* A better approach would be fingering the hypotheses in some  *)
464         (* way.                                                         *)
465         let meta_of_corpse =
466          let (_,canonical_context,_) =
467           List.find (function (m,_,_) -> m=(lastmeta - 1)) newmetas
468          in
469           let irl =
470            identity_relocation_list_for_metavariable canonical_context
471           in
472            Cic.Meta (lastmeta - 1, irl)
473         in
474         let newmetasenv = newmetas @ metasenv in
475         let subst1,newmetasenv' =
476          CicUnification.fo_unif newmetasenv context term meta_of_corpse
477         in
478          let ueconclusion = CicUnification.apply_subst subst1 econclusion in
479           (* The conclusion of our elimination principle is *)
480           (*  (?i farg1 ... fargn)                         *)
481           (* The conclusion of our goal is ty. So, we can   *)
482           (* eta-expand ty w.r.t. farg1 .... fargn to get   *)
483           (* a new ty equal to (P farg1 ... fargn). Now     *)
484           (* ?i can be instantiated with P and we are ready *)
485           (* to refine the term.                            *)
486           let emeta, fargs =
487            match ueconclusion with
488 (*CSC: Code to be used for Apply
489               C.Appl ((C.Meta (emeta,_))::fargs) -> emeta,fargs
490             | C.Meta (emeta,_) -> emeta,[]
491 *)
492 (*CSC: Code to be used for ApplyIntros *)
493               C.Appl (he::fargs) ->
494                let rec find_head =
495                 function
496                    C.Meta (emeta,_) -> emeta
497                  | C.Lambda (_,_,t) -> find_head t
498                  | C.LetIn (_,_,t) -> find_head t
499                  | _ ->raise NotTheRightEliminatorShape
500                in
501                 find_head he,fargs
502             | C.Meta (emeta,_) -> emeta,[]
503 (* *)
504             | _ -> raise NotTheRightEliminatorShape
505           in
506            let ty' = CicUnification.apply_subst subst1 ty in
507            let eta_expanded_ty =
508 (*CSC: newmetasenv' era metasenv ??????????? *)
509             List.fold_left (eta_expand newmetasenv' context) ty' fargs
510            in
511             let subst2,newmetasenv'' =
512 (*CSC: passo newmetasenv', ma alcune variabili sono gia' state sostituite
513 da subst1!!!! Dovrei rimuoverle o sono innocue?*)
514              CicUnification.fo_unif
515               newmetasenv' context ueconclusion eta_expanded_ty
516             in
517              let in_subst_domain i =
518               let eq_to_i = function (j,_) -> i=j in
519                List.exists eq_to_i subst1 ||
520                List.exists eq_to_i subst2
521              in
522 (*CSC: codice per l'elim
523               (* When unwinding the META that corresponds to the elimination *)
524               (* predicate (which is emeta), we must also perform one-step   *)
525               (* beta-reduction. apply_subst doesn't need the context. Hence *)
526               (* the underscore.                                             *)
527               let apply_subst _ t =
528                let t' = CicUnification.apply_subst subst1 t in
529                 CicUnification.apply_subst_reducing
530                  subst2 (Some (emeta,List.length fargs)) t'
531               in
532 *)
533 (*CSC: codice per l'elim_intros_simpl. Non effettua semplificazione. *)
534               let apply_subst context t =
535                let t' = CicUnification.apply_subst (subst1@subst2) t in
536                 ProofEngineReduction.simpl context t'
537               in
538 (* *)
539                 let old_uninstantiatedmetas,new_uninstantiatedmetas =
540                  classify_metas newmeta in_subst_domain apply_subst
541                   newmetasenv''
542                 in
543                  let arguments' = List.map (apply_subst context) arguments in
544                   let bo' = Cic.Appl (eliminator_ref::arguments') in
545                    let newmetasenv''' =
546                     new_uninstantiatedmetas@old_uninstantiatedmetas
547                    in
548                     let (newproof, newmetasenv'''') =
549                      (* When unwinding the META that corresponds to the *)
550                      (* elimination predicate (which is emeta), we must *)
551                      (* also perform one-step beta-reduction.           *)
552                      (* The only difference w.r.t. apply_subst is that  *)
553                      (* we also substitute metano with bo'.             *)
554                      (*CSC: Nota: sostituire nuovamente subst1 e' superfluo, *)
555                      (*CSC: no?                                              *)
556 (*CSC: codice per l'elim
557                      let apply_subst' t =
558                       let t' = CicUnification.apply_subst subst1 t in
559                        CicUnification.apply_subst_reducing
560                         ((metano,bo')::subst2)
561                         (Some (emeta,List.length fargs)) t'
562                      in
563 *)
564 (*CSC: codice per l'elim_intros_simpl *)
565                      let apply_subst' t =
566                       CicUnification.apply_subst
567                        ((metano,bo')::(subst1@subst2)) t
568                      in
569 (* *)
570                       subst_meta_and_metasenv_in_proof
571                         proof metano apply_subst' newmetasenv'''
572                     in
573                      (newproof,
574                       List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
575
576
577 exception NotConvertible
578
579 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal,  *)
580 (*CSC: while [what] can have a richer context (because of binders)           *)
581 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
582 (*CSC: Is that evident? Is that right? Or should it be changed?              *)
583 let change_tac ~what ~with_what ~status:(proof, goal) =
584  let curi,metasenv,pbo,pty = proof in
585  let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
586   (* are_convertible works only on well-typed terms *)
587   ignore (CicTypeChecker.type_of_aux' metasenv context with_what) ;
588   if CicReduction.are_convertible context what with_what then
589    begin
590     let replace =
591      ProofEngineReduction.replace ~equality:(==) ~what ~with_what
592     in
593     let ty' = replace ty in
594     let context' =
595      List.map
596       (function
597           Some (name,Cic.Def  t) -> Some (name,Cic.Def  (replace t))
598         | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
599         | None -> None
600       ) context
601     in
602      let metasenv' = 
603       List.map
604        (function
605            (n,_,_) when n = metano -> (metano,context',ty')
606          | _ as t -> t
607        ) metasenv
608      in
609       (curi,metasenv',pbo,pty), [metano]
610    end
611   else
612    raise (ProofEngineTypes.Fail "Not convertible")