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