]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/equalityTactics.ml
1. rewrite_* and rewrite_back_* merged into one function
[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 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   let proof,goal = status in
38   let if_left a b = 
39     match direction with
40     | `Right -> b
41     | `Left -> a
42   in
43   let curi, metasenv, pbo, pty = proof in
44   let metano, context, gty = CicUtil.lookup_meta goal metasenv in
45   let eq_uri = HLO.Logic.eq_URI in
46   let ty_eq,_ = 
47     CicTypeChecker.type_of_aux' metasenv context equality 
48       CicUniv.empty_ugraph
49   in 
50   let eq_ind, ty, t1, t2 =
51     match ty_eq with
52     | C.Appl [C.MutInd (uri, 0, []); ty; t1; t2] when U.eq uri eq_uri ->
53         let eq_ind = 
54           C.Const (if_left HLO.Logic.eq_ind_URI HLO.Logic.eq_ind_r_URI,[])
55         in
56         if_left (eq_ind, ty, t2, t1) (eq_ind, ty, t1, t2)
57     | _ -> raise (PET.Fail "Rewrite: argument is not a proof of an equality")
58   in
59   (* now we always do as if direction was `Left *)
60   let gty' = CicSubstitution.lift 1 gty in
61   let t1' = CicSubstitution.lift 1 t1 in
62   let eq_kind, what = 
63     match where with
64     | None  
65     | Some ([], None) ->   
66         PER.alpha_equivalence, [t1']
67     | Some (hp_paths, goal_path) -> 
68         assert (hp_paths = []); 
69         match goal_path with
70         | None -> assert false (* (==), [t1'] *)
71         | Some path -> 
72             let roots = ProofEngineHelpers.select ~term:gty' ~pattern:path in
73             let subterms = 
74               List.fold_left (
75                 fun acc (i, r) -> 
76                   let wanted = CicSubstitution.lift (List.length i) t1' in
77                   PEH.find_subterms ~eq:PER.alpha_equivalence ~wanted r @ acc
78             ) [] roots
79             in
80             (==), subterms
81   in
82   let with_what = 
83     let rec aux = function 
84       | 0 -> []
85       | n -> C.Rel 1 :: aux (n-1)
86     in
87     aux (List.length what)
88   in
89   let gty'' =
90    ProofEngineReduction.replace_lifting_csc 0
91     ~equality:eq_kind ~what ~with_what ~where:gty'
92   in
93   let gty_red = CicSubstitution.subst t2 gty'' in
94   let fresh_meta = ProofEngineHelpers.new_meta_of_proof proof in
95   let irl =CicMkImplicit.identity_relocation_list_for_metavariable context in
96   let metasenv' = (fresh_meta,context,gty_red)::metasenv in
97   let fresh_name = 
98     FreshNamesGenerator.mk_fresh_name 
99     ~subst:[] metasenv context C.Anonymous ~typ:ty
100   in
101   let pred = C.Lambda (fresh_name, ty, gty'') in
102   let exact_proof = 
103     C.Appl [eq_ind ; ty ; t2 ; pred ; C.Meta (fresh_meta,irl) ; t1 ;equality]
104   in
105   let (proof',goals) =
106     PET.apply_tactic 
107       (PT.exact_tac ~term:exact_proof) ((curi,metasenv',pbo,pty),goal)
108   in
109   assert (List.length goals = 0) ;
110   (proof',[fresh_meta])
111   ProofEngineTypes.mk_tactic (rewrite_tac ?where ~term)
112 *) assert false in
113  ProofEngineTypes.mk_tactic (rewrite_tac ~direction ~pattern equality)
114   
115  
116 let rewrite_simpl_tac ~direction ~pattern equality =
117  let rewrite_simpl_tac ~direction ~pattern equality status =
118   ProofEngineTypes.apply_tactic
119   (Tacticals.then_ 
120    ~start:(rewrite_tac ~direction:`LeftToRight ~pattern equality)
121    ~continuation:
122      (ReductionTactics.simpl_tac
123        ~pattern:(ProofEngineTypes.conclusion_pattern None)))
124    status
125  in
126    ProofEngineTypes.mk_tactic (rewrite_simpl_tac ~direction ~pattern equality)
127 ;;
128
129 let replace_tac ~pattern ~with_what =
130 (*
131  let replace_tac ~pattern ~with_what status =
132   let (proof, goal) = status in
133   let module C = Cic in
134   let module U = UriManager in
135   let module P = PrimitiveTactics in
136   let module T = Tacticals in
137   let _,metasenv,_,_ = proof in
138   let _,context,_ = CicUtil.lookup_meta goal metasenv in
139   let wty,u = (* TASSI: FIXME *)
140     CicTypeChecker.type_of_aux' metasenv context what CicUniv.empty_ugraph in
141   let wwty,_ = CicTypeChecker.type_of_aux' metasenv context with_what u in
142     try
143       if (wty = wwty) then
144         ProofEngineTypes.apply_tactic
145           (T.thens
146              ~start:(
147                P.cut_tac 
148                        (C.Appl [
149                           (C.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, [])) ;
150                           wty ; 
151                           what ; 
152                           with_what]))
153              ~continuations:[            
154                
155                T.then_     ~start:(rewrite_simpl_tac ~term:(C.Rel 1) ())
156                            ~continuation:(
157                                  ProofEngineStructuralRules.clear
158                                                 ~hyp:(List.hd context)) ;
159                T.id_tac])
160           status
161       else raise (ProofEngineTypes.Fail "Replace: terms not replaceable")
162     with (Failure "hd") -> 
163       raise (ProofEngineTypes.Fail "Replace: empty context")
164  in
165    ProofEngineTypes.mk_tactic (replace_tac ~what ~with_what)
166 *) assert false
167 ;;
168
169
170 (* All these tacs do is applying the right constructor/theorem *)
171
172 let reflexivity_tac =
173   IntroductionTactics.constructor_tac ~n:1
174 ;;
175
176 let symmetry_tac =
177  let symmetry_tac (proof, goal) =
178   let module C = Cic in
179   let module R = CicReduction in
180   let module U = UriManager in
181    let (_,metasenv,_,_) = proof in
182     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
183      match (R.whd context ty) with
184         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
185          when (U.eq uri HelmLibraryObjects.Logic.eq_URI) ->
186           ProofEngineTypes.apply_tactic 
187            (PrimitiveTactics.apply_tac 
188             ~term: (C.Const (HelmLibraryObjects.Logic.sym_eq_URI, []))) 
189            (proof,goal)
190
191       | _ -> raise (ProofEngineTypes.Fail "Symmetry failed")
192  in
193   ProofEngineTypes.mk_tactic symmetry_tac
194 ;;
195
196 let transitivity_tac ~term =
197  let transitivity_tac ~term status =
198   let (proof, goal) = status in
199   let module C = Cic in
200   let module R = CicReduction in
201   let module U = UriManager in
202   let module T = Tacticals in
203    let (_,metasenv,_,_) = proof in
204     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
205      match (R.whd context ty) with
206         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) 
207         when (uri = HelmLibraryObjects.Logic.eq_URI) ->
208          ProofEngineTypes.apply_tactic 
209          (T.thens
210           ~start:(PrimitiveTactics.apply_tac
211             ~term: (C.Const (HelmLibraryObjects.Logic.trans_eq_URI, [])))
212           ~continuations:
213             [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
214           status
215
216       | _ -> raise (ProofEngineTypes.Fail "Transitivity failed")
217  in
218   ProofEngineTypes.mk_tactic (transitivity_tac ~term)
219 ;;
220
221