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