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