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