]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/equalityTactics.ml
The replace tactic is now working again. It can now replace simultaneously
[helm.git] / helm / ocaml / tactics / equalityTactics.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
27 let rewrite_tac ~direction ~pattern equality =
28  let rewrite_tac ~direction ~pattern:(wanted,hyps_pat,concl_pat) equality status
29  =
30   let module C = Cic in
31   let module U = UriManager in
32   let module PET = ProofEngineTypes in
33   let module PER = ProofEngineReduction in
34   let module PEH = ProofEngineHelpers in
35   let module PT = PrimitiveTactics in
36   let module HLO = HelmLibraryObjects in
37   assert (wanted = None);   (* this should be checked syntactically *)
38   assert (hyps_pat = []); (*CSC: not implemented yet! *)
39   let proof,goal = status in
40   let if_right_to_left a b = 
41     match direction with
42     | `RightToLeft -> a
43     | `LeftToRight -> b
44   in
45   let curi, metasenv, pbo, pty = proof in
46   let (metano,context,gty) as conjecture = CicUtil.lookup_meta goal metasenv in
47   let eq_uri = HLO.Logic.eq_URI in
48   let ty_eq,_ = 
49     CicTypeChecker.type_of_aux' metasenv context equality 
50       CicUniv.empty_ugraph
51   in 
52   let eq_ind, ty, t1, t2 =
53     match ty_eq with
54     | C.Appl [C.MutInd (uri, 0, []); ty; t1; t2] when U.eq uri eq_uri ->
55         let eq_ind = 
56           C.Const (if_right_to_left HLO.Logic.eq_ind_URI HLO.Logic.eq_ind_r_URI,[])
57         in
58         if_right_to_left (eq_ind, ty, t2, t1) (eq_ind, ty, t1, t2)
59     | _ -> raise (PET.Fail "Rewrite: argument is not a proof of an equality") in
60   (* now we always do as if direction was `LeftToRight *)
61   let fresh_name = 
62     FreshNamesGenerator.mk_fresh_name 
63     ~subst:[] metasenv context C.Anonymous ~typ:ty in
64   let lifted_t1 = CicSubstitution.lift 1 t1 in
65   let lifted_gty = CicSubstitution.lift 1 gty in
66   let lifted_conjecture =
67     metano,(Some (fresh_name,Cic.Decl ty))::context,lifted_gty in
68   let lifted_pattern = Some lifted_t1,[],CicSubstitution.lift 1 concl_pat in
69   let _,selected_terms_with_context =
70    ProofEngineHelpers.select
71     ~metasenv ~conjecture:lifted_conjecture ~pattern:lifted_pattern in
72   let what,with_what = 
73    (* Note: Rel 1 does not live in the context context_of_t           *)
74    (* The replace_lifting_csc_0 function will take care of lifting it *)
75    (* to context_of_t                                                 *)
76    List.fold_right
77     (fun (context_of_t,t) (l1,l2) -> t::l1, Cic.Rel 1::l2)
78     selected_terms_with_context ([],[]) in
79   let abstr_gty =
80    ProofEngineReduction.replace_lifting_csc 0
81     ~equality:(==) ~what ~with_what:with_what ~where:lifted_gty in
82   let gty' = CicSubstitution.subst t2 abstr_gty in
83   let fresh_meta = ProofEngineHelpers.new_meta_of_proof proof in
84   let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
85   let metasenv' = (fresh_meta,context,gty')::metasenv in
86   let pred = C.Lambda (fresh_name, ty, abstr_gty) in
87   let exact_proof = 
88     C.Appl [eq_ind ; ty ; t2 ; pred ; C.Meta (fresh_meta,irl) ; t1 ;equality]
89   in
90   let (proof',goals) =
91     PET.apply_tactic 
92       (PT.exact_tac ~term:exact_proof) ((curi,metasenv',pbo,pty),goal)
93   in
94   assert (List.length goals = 0) ;
95   (proof',[fresh_meta])
96  in
97   ProofEngineTypes.mk_tactic (rewrite_tac ~direction ~pattern equality)
98   
99  
100 let rewrite_simpl_tac ~direction ~pattern equality =
101  let rewrite_simpl_tac ~direction ~pattern equality status =
102   ProofEngineTypes.apply_tactic
103   (Tacticals.then_ 
104    ~start:(rewrite_tac ~direction ~pattern equality)
105    ~continuation:
106      (ReductionTactics.simpl_tac
107        ~pattern:(ProofEngineTypes.conclusion_pattern None)))
108    status
109  in
110    ProofEngineTypes.mk_tactic (rewrite_simpl_tac ~direction ~pattern equality)
111 ;;
112
113 let replace_tac ~pattern ~with_what =
114  let replace_tac ~pattern:(wanted,hyps_pat,concl_pat) ~with_what status =
115   let (proof, goal) = status in
116   let module C = Cic in
117   let module U = UriManager in
118   let module P = PrimitiveTactics in
119   let module T = Tacticals in
120   let _,metasenv,_,_ = proof in
121   let (_,context,_) as conjecture = CicUtil.lookup_meta goal metasenv in
122   assert (hyps_pat = []); (*CSC: not implemented yet *)
123   let context_len = List.length context in
124   let _,selected_terms_with_context =
125    ProofEngineHelpers.select ~metasenv ~conjecture ~pattern in
126   let ty_of_with_what,u =
127    CicTypeChecker.type_of_aux'
128     metasenv context with_what CicUniv.empty_ugraph in
129   let whats =
130    match selected_terms_with_context with
131       [] -> raise (ProofEngineTypes.Fail "Replace: no term selected")
132     | l ->
133       List.map
134        (fun (context_of_t,t) ->
135          let t_in_context =
136           try
137            let context_of_t_len = List.length context_of_t in
138            if context_of_t_len = context_len then t
139            else
140             (let t_in_context,subst,metasenv' =
141               CicMetaSubst.delift_rels [] metasenv
142                (context_of_t_len - context_len) t
143              in
144               assert (subst = []);
145               assert (metasenv = metasenv');
146               t_in_context)
147           with
148            CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
149             (*CSC: we could implement something stronger by completely changing
150               the semantics of the tactic *)
151             raise (ProofEngineTypes.Fail
152              "Replace: one of the selected terms is not closed") in
153          let ty_of_t_in_context,u = (* TASSI: FIXME *)
154           CicTypeChecker.type_of_aux' metasenv context t_in_context
155            CicUniv.empty_ugraph in
156          let b,u = CicReduction.are_convertible ~metasenv context
157           ty_of_with_what ty_of_t_in_context u in
158          if b then t_in_context
159          else
160           raise
161            (ProofEngineTypes.Fail
162              "Replace: one of the selected terms and the term to be replaced with have not convertible types")
163        ) l
164   in
165    let rec aux whats status =
166     match whats with
167        [] -> ProofEngineTypes.apply_tactic T.id_tac status
168      | what::tl ->
169         ProofEngineTypes.apply_tactic
170           (T.thens
171              ~start:(
172                P.cut_tac 
173                 (C.Appl [
174                   (C.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, [])) ;
175                   ty_of_with_what ; 
176                   what ; 
177                   with_what]))
178              ~continuations:[            
179                T.then_
180                  ~start:(
181                    rewrite_simpl_tac ~direction:`LeftToRight ~pattern (C.Rel 1))
182                   ~continuation:(
183                     let hyp =
184                      try
185                       match List.hd context with
186                          Some (Cic.Name name,_) -> name
187                        | _ -> assert false
188                      with (Failure "hd") -> assert false
189                     in
190                      ProofEngineStructuralRules.clear ~hyp) ;
191                aux_tac tl])
192           status
193    and aux_tac tl = ProofEngineTypes.mk_tactic (aux tl) in
194     aux whats status
195  in
196    ProofEngineTypes.mk_tactic (replace_tac ~pattern ~with_what)
197 ;;
198
199
200 (* All these tacs do is applying the right constructor/theorem *)
201
202 let reflexivity_tac =
203   IntroductionTactics.constructor_tac ~n:1
204 ;;
205
206 let symmetry_tac =
207  let symmetry_tac (proof, goal) =
208   let module C = Cic in
209   let module R = CicReduction in
210   let module U = UriManager in
211    let (_,metasenv,_,_) = proof in
212     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
213      match (R.whd context ty) with
214         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
215          when (U.eq uri HelmLibraryObjects.Logic.eq_URI) ->
216           ProofEngineTypes.apply_tactic 
217            (PrimitiveTactics.apply_tac 
218             ~term: (C.Const (HelmLibraryObjects.Logic.sym_eq_URI, []))) 
219            (proof,goal)
220
221       | _ -> raise (ProofEngineTypes.Fail "Symmetry failed")
222  in
223   ProofEngineTypes.mk_tactic symmetry_tac
224 ;;
225
226 let transitivity_tac ~term =
227  let transitivity_tac ~term status =
228   let (proof, goal) = status in
229   let module C = Cic in
230   let module R = CicReduction in
231   let module U = UriManager in
232   let module T = Tacticals in
233    let (_,metasenv,_,_) = proof in
234     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
235      match (R.whd context ty) with
236         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) 
237         when (uri = HelmLibraryObjects.Logic.eq_URI) ->
238          ProofEngineTypes.apply_tactic 
239          (T.thens
240           ~start:(PrimitiveTactics.apply_tac
241             ~term: (C.Const (HelmLibraryObjects.Logic.trans_eq_URI, [])))
242           ~continuations:
243             [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
244           status
245
246       | _ -> raise (ProofEngineTypes.Fail "Transitivity failed")
247  in
248   ProofEngineTypes.mk_tactic (transitivity_tac ~term)
249 ;;
250
251