]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/equalityTactics.ml
Added Decompose tactic
[helm.git] / helm / gTopLevel / 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 ~term:equality ~status:(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 = List.find (function (m,_,_) -> m=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 (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind") ->
36          let eq_ind_r =
37           C.Const
38            (U.uri_of_string "cic:/Coq/Init/Logic/eq_ind_r.con",[])
39          in
40           eq_ind_r,ty,t1,t2
41      | C.Appl [C.MutInd (uri,0,[]) ; ty ; t1 ; t2]
42         when U.eq uri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind") ->
43          let eqT_ind_r =
44           C.Const
45            (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT_ind_r.con",[])
46          in
47           eqT_ind_r,ty,t1,t2
48      | _ ->
49        raise
50         (ProofEngineTypes.Fail
51           "Rewrite: the argument is not a proof of an equality")
52    in
53     let pred =
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       C.Lambda (C.Name "dummy_for_rewrite", ty, gty'')
62     in
63 prerr_endline ("#### Sintetizzato: " ^ CicPp.ppterm pred);
64     let fresh_meta = ProofEngineHelpers.new_meta proof in
65     let irl =
66      ProofEngineHelpers.identity_relocation_list_for_metavariable context in
67     let metasenv' = (fresh_meta,context,C.Appl [pred ; t2])::metasenv in
68
69      let (proof',goals) =
70       PrimitiveTactics.exact_tac
71        ~term:(C.Appl
72          [eq_ind_r ; ty ; t2 ; pred ; C.Meta (fresh_meta,irl) ; t1 ;equality])
73         ~status:((curi,metasenv',pbo,pty),goal)
74      in
75       assert (List.length goals = 0) ;
76       (proof',[fresh_meta])
77 ;;
78
79
80 let rewrite_simpl_tac ~term ~status =
81  Tacticals.then_ 
82   ~start:(rewrite_tac ~term)
83   ~continuation:
84    (ReductionTactics.simpl_tac ~also_in_hypotheses:false ~term:None)
85   ~status
86 ;;
87
88
89 let rewrite_back_tac ~term:equality ~status:(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 = List.find (function (m,_,_) -> m=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 (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind") ->
98          let eq_ind_r =
99           C.Const
100            (U.uri_of_string "cic:/Coq/Init/Logic/eq_ind.con",[])
101          in
102           eq_ind_r,ty,t2,t1
103      | C.Appl [C.MutInd (uri,0,[]) ; ty ; t1 ; t2]
104         when U.eq uri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind") ->
105          let eqT_ind_r =
106           C.Const
107            (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT_ind.con",[])
108          in
109           eqT_ind_r,ty,t2,t1
110      | _ ->
111        raise
112         (ProofEngineTypes.Fail
113           "Rewrite: the argument is not a proof of an equality")
114    in
115     let pred =
116      let gty' = CicSubstitution.lift 1 gty in
117      let t1' = CicSubstitution.lift 1 t1 in
118      let gty'' =
119       ProofEngineReduction.replace_lifting
120        ~equality:ProofEngineReduction.alpha_equivalence
121        ~what:t1' ~with_what:(C.Rel 1) ~where:gty'
122      in
123       C.Lambda (C.Name "dummy_for_rewrite", ty, gty'')
124     in
125 prerr_endline ("#### Sintetizzato: " ^ CicPp.ppterm pred);
126     let fresh_meta = ProofEngineHelpers.new_meta proof in
127     let irl =
128      ProofEngineHelpers.identity_relocation_list_for_metavariable context in
129     let metasenv' = (fresh_meta,context,C.Appl [pred ; t2])::metasenv in
130
131      let (proof',goals) =
132       PrimitiveTactics.exact_tac
133        ~term:(C.Appl
134          [eq_ind_r ; ty ; t2 ; pred ; C.Meta (fresh_meta,irl) ; t1 ;equality])
135         ~status:((curi,metasenv',pbo,pty),goal)
136      in
137       assert (List.length goals = 0) ;
138       (proof',[fresh_meta])
139
140 ;;
141
142
143 let rewrite_back_simpl_tac ~term ~status =
144  Tacticals.then_ 
145   ~start:(rewrite_back_tac ~term)
146   ~continuation:
147    (ReductionTactics.simpl_tac ~also_in_hypotheses:false ~term:None)
148   ~status
149 ;;
150
151
152 let replace_tac ~what ~with_what ~status:((proof, goal) as status) =
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,_ = List.find (function (m,_,_) -> m=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 T.thens
163               ~start:(
164                 P.cut_tac 
165                    ~term:(
166                      C.Appl [
167                       (C.MutInd ((U.uri_of_string "cic:/Coq/Init/Logic/eq.ind"), 0, [])) ; (* quale uguaglianza usare, eq o eqT ? *)
168                       wty ; 
169                       what ; 
170                       with_what]))
171               ~continuations:[
172                 T.then_
173                    ~start:(rewrite_back_tac ~term:(C.Rel 1)) (* C.Name "dummy_for_replace" *)
174                    ~continuation:(
175                      ProofEngineStructuralRules.clear
176                       ~hyp:(List.hd context)) ;
177 (*                                                      (Some(C.Name "dummy_for_replace" , C.Def (CicTypeChecker.type_of_aux' metasenv context (C.Rel 1)) (* NOOOO!!!!! tipo di dummy *) )))) ; *)
178                 T.id_tac]
179               ~status
180        else raise (ProofEngineTypes.Fail "Replace: terms not replaceable")
181        with (Failure "hd") -> raise (ProofEngineTypes.Fail "Replace: empty context")
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
192 let symmetry_tac ~status:(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 = List.find (function (m,_,_) -> m=goal) metasenv in
198      match (R.whd context ty) with
199         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) when (U.eq uri (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind")) ->
200          PrimitiveTactics.apply_tac ~status:(proof,goal)
201           ~term: (C.Const (U.uri_of_string "cic:/Coq/Init/Logic/sym_eq.con", []))
202
203       | (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) when (U.eq uri (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) ->
204          PrimitiveTactics.apply_tac ~status:(proof,goal)
205           ~term: (C.Const (U.uri_of_string "cic:/Coq/Init/Logic_Type/sym_eqT.con", []))
206
207       | _ -> raise (ProofEngineTypes.Fail "Symmetry failed")
208 ;;
209
210
211 let transitivity_tac ~term ~status:((proof, goal) as status) =
212   let module C = Cic in
213   let module R = CicReduction in
214   let module U = UriManager in
215   let module T = Tacticals in
216    let (_,metasenv,_,_) = proof in
217     let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
218      match (R.whd context ty) with
219         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) when (uri = (U.uri_of_string "cic:/Coq/Init/Logic/eq.ind")) ->
220          T.thens
221           ~start:(PrimitiveTactics.apply_tac
222             ~term: (C.Const (U.uri_of_string "cic:/Coq/Init/Logic/trans_eq.con", [])))
223           ~continuations:
224             [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac]
225           ~status
226
227       | (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) when (uri = (U.uri_of_string "cic:/Coq/Init/Logic_Type/eqT.ind")) ->
228          T.thens
229           ~start:(PrimitiveTactics.apply_tac
230             ~term: (C.Const (U.uri_of_string "cic:/Coq/Init/Logic_Type/trans_eqT.con", [])))
231           ~continuations:
232             [T.id_tac ; T.id_tac ; PrimitiveTactics.exact_tac ~term]
233           ~status
234
235       | _ -> raise (ProofEngineTypes.Fail "Transitivity failed")
236 ;;
237
238