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