]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/equalityTactics.ml
A few other tactics made available to matita.
[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 = ProofEngineHelpers.select ~term:gty' ~pattern:path in
70             let subterms = 
71               List.fold_left (
72                 fun acc (i, r) -> 
73                   let wanted = CicSubstitution.lift (List.length 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_csc 0
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 ~pattern ~with_what =
147 (*
148  let replace_tac ~pattern ~with_what status =
149   let (proof, goal) = status in
150   let module C = Cic in
151   let module U = UriManager in
152   let module P = PrimitiveTactics in
153   let module T = Tacticals in
154   let _,metasenv,_,_ = proof in
155   let _,context,_ = CicUtil.lookup_meta goal metasenv in
156   let wty,u = (* TASSI: FIXME *)
157     CicTypeChecker.type_of_aux' metasenv context what CicUniv.empty_ugraph in
158   let wwty,_ = CicTypeChecker.type_of_aux' metasenv context with_what u in
159     try
160       if (wty = wwty) then
161         ProofEngineTypes.apply_tactic
162           (T.thens
163              ~start:(
164                P.cut_tac 
165                        (C.Appl [
166                           (C.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, [])) ;
167                           wty ; 
168                           what ; 
169                           with_what]))
170              ~continuations:[            
171                
172                T.then_     ~start:(rewrite_simpl_tac ~term:(C.Rel 1) ())
173                            ~continuation:(
174                                  ProofEngineStructuralRules.clear
175                                                 ~hyp:(List.hd context)) ;
176                T.id_tac])
177           status
178       else raise (ProofEngineTypes.Fail "Replace: terms not replaceable")
179     with (Failure "hd") -> 
180       raise (ProofEngineTypes.Fail "Replace: empty context")
181  in
182    ProofEngineTypes.mk_tactic (replace_tac ~what ~with_what)
183 *) assert false
184 ;;
185
186
187 (* All these tacs do is applying the right constructor/theorem *)
188
189 let reflexivity_tac =
190   IntroductionTactics.constructor_tac ~n:1
191 ;;
192
193 let symmetry_tac =
194  let symmetry_tac (proof, goal) =
195   let module C = Cic in
196   let module R = CicReduction in
197   let module U = UriManager in
198    let (_,metasenv,_,_) = proof in
199     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
200      match (R.whd context ty) with
201         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
202          when (U.eq uri HelmLibraryObjects.Logic.eq_URI) ->
203           ProofEngineTypes.apply_tactic 
204            (PrimitiveTactics.apply_tac 
205             ~term: (C.Const (HelmLibraryObjects.Logic.sym_eq_URI, []))) 
206            (proof,goal)
207
208       | _ -> raise (ProofEngineTypes.Fail "Symmetry failed")
209  in
210   ProofEngineTypes.mk_tactic symmetry_tac
211 ;;
212
213 let transitivity_tac ~term =
214  let transitivity_tac ~term status =
215   let (proof, goal) = status in
216   let module C = Cic in
217   let module R = CicReduction in
218   let module U = UriManager in
219   let module T = Tacticals in
220    let (_,metasenv,_,_) = proof in
221     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
222      match (R.whd context ty) with
223         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) 
224         when (uri = HelmLibraryObjects.Logic.eq_URI) ->
225          ProofEngineTypes.apply_tactic 
226          (T.thens
227           ~start:(PrimitiveTactics.apply_tac
228             ~term: (C.Const (HelmLibraryObjects.Logic.trans_eq_URI, [])))
229           ~continuations:
230             [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
231           status
232
233       | _ -> raise (ProofEngineTypes.Fail "Transitivity failed")
234  in
235   ProofEngineTypes.mk_tactic (transitivity_tac ~term)
236 ;;
237
238