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