]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/equalityTactics.ml
Two bugs fixed in the apply tactic:
[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 ~direction ~pattern equality =
27  let rewrite_tac ~direction ~pattern:(wanted,hyps_pat,concl_pat) equality status
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   assert (wanted = None);   (* this should be checked syntactically *)
36   assert (hyps_pat = []); (*CSC: not implemented yet! *)
37   let proof,goal = status in
38   let if_right_to_left a b = 
39     match direction with
40     | `RightToLeft -> a
41     | `LeftToRight -> b
42   in
43   let curi, metasenv, pbo, pty = proof in
44   let (metano,context,gty) as conjecture = CicUtil.lookup_meta goal metasenv in
45   let ty_eq,ugraph = 
46     CicTypeChecker.type_of_aux' metasenv context equality 
47       CicUniv.empty_ugraph in 
48   let (ty_eq,metasenv',arguments,fresh_meta) =
49    ProofEngineHelpers.saturate_term
50     (ProofEngineHelpers.new_meta_of_proof proof) metasenv context ty_eq 0 in
51   let equality =
52    if List.length arguments = 0 then
53     equality
54    else
55     C.Appl (equality :: arguments) in
56   let eq_ind, ty, t1, t2 =
57     match ty_eq with
58     | C.Appl [C.MutInd (uri, 0, []); ty; t1; t2]
59       when LibraryObjects.is_eq_URI uri ->
60         let ind_uri =
61          if_right_to_left LibraryObjects.eq_ind_URI LibraryObjects.eq_ind_r_URI
62         in
63         let eq_ind = C.Const (ind_uri uri,[]) in
64          if_right_to_left (eq_ind, ty, t2, t1) (eq_ind, ty, t1, t2)
65     | _ -> raise (PET.Fail "Rewrite: argument is not a proof of an equality") in
66   (* now we always do as if direction was `LeftToRight *)
67   let fresh_name = 
68     FreshNamesGenerator.mk_fresh_name 
69     ~subst:[] metasenv' context C.Anonymous ~typ:ty in
70   let lifted_t1 = CicSubstitution.lift 1 t1 in
71   let lifted_gty = CicSubstitution.lift 1 gty in
72   let lifted_conjecture =
73     metano,(Some (fresh_name,Cic.Decl ty))::context,lifted_gty in
74   let lifted_pattern =
75     Some (fun _ m u -> lifted_t1, m, u),[],CicSubstitution.lift 1 concl_pat
76   in
77   let subst,metasenv',ugraph,_,selected_terms_with_context =
78    ProofEngineHelpers.select
79     ~metasenv:metasenv' ~ugraph ~conjecture:lifted_conjecture
80      ~pattern:lifted_pattern in
81   let metasenv' = CicMetaSubst.apply_subst_metasenv subst metasenv' in
82   let what,with_what = 
83    (* Note: Rel 1 does not live in the context context_of_t           *)
84    (* The replace_lifting_csc_0 function will take care of lifting it *)
85    (* to context_of_t                                                 *)
86    List.fold_right
87     (fun (context_of_t,t) (l1,l2) -> t::l1, Cic.Rel 1::l2)
88     selected_terms_with_context ([],[]) in
89   let abstr_gty =
90    ProofEngineReduction.replace_lifting_csc 0
91     ~equality:(==) ~what ~with_what:with_what ~where:lifted_gty in
92   let abstr_gty = CicMetaSubst.apply_subst subst abstr_gty in
93   let t1 = CicMetaSubst.apply_subst subst t1 in
94   let t2 = CicMetaSubst.apply_subst subst t2 in
95   let equality = CicMetaSubst.apply_subst subst equality in
96   let gty' = CicSubstitution.subst t2 abstr_gty in
97   let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
98   let metasenv' = (fresh_meta,context,gty')::metasenv' in
99   let pred = C.Lambda (fresh_name, ty, abstr_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   let goals =
109    ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv
110     ~newmetasenv:metasenv'
111   in
112    (proof',goals)
113  in
114   ProofEngineTypes.mk_tactic (rewrite_tac ~direction ~pattern equality)
115   
116  
117 let rewrite_simpl_tac ~direction ~pattern equality =
118  let rewrite_simpl_tac ~direction ~pattern equality status =
119   ProofEngineTypes.apply_tactic
120   (Tacticals.then_ 
121    ~start:(rewrite_tac ~direction ~pattern equality)
122    ~continuation:
123      (ReductionTactics.simpl_tac
124        ~pattern:(ProofEngineTypes.conclusion_pattern None)))
125    status
126  in
127    ProofEngineTypes.mk_tactic (rewrite_simpl_tac ~direction ~pattern equality)
128 ;;
129
130 let replace_tac ~pattern ~with_what =
131  let replace_tac ~pattern:(wanted,hyps_pat,concl_pat) ~with_what status =
132   let (proof, goal) = status in
133   let module C = Cic in
134   let module U = UriManager in
135   let module P = PrimitiveTactics in
136   let module T = Tacticals in
137   let uri,metasenv,pbo,pty = proof in
138   let (_,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
139   assert (hyps_pat = []); (*CSC: not implemented yet *)
140   let context_len = List.length context in
141   let subst,metasenv,u,_,selected_terms_with_context =
142    ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph
143     ~conjecture ~pattern in
144   let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
145   let with_what, metasenv, u = with_what context metasenv u in
146   let with_what = CicMetaSubst.apply_subst subst with_what in
147   let pbo = CicMetaSubst.apply_subst subst pbo in
148   let pty = CicMetaSubst.apply_subst subst pty in
149   let status = (uri,metasenv,pbo,pty),goal in
150   let ty_of_with_what,u =
151    CicTypeChecker.type_of_aux'
152     metasenv context with_what CicUniv.empty_ugraph in
153   let whats =
154    match selected_terms_with_context with
155       [] -> raise (ProofEngineTypes.Fail "Replace: no term selected")
156     | l ->
157       List.map
158        (fun (context_of_t,t) ->
159          let t_in_context =
160           try
161            let context_of_t_len = List.length context_of_t in
162            if context_of_t_len = context_len then t
163            else
164             (let t_in_context,subst,metasenv' =
165               CicMetaSubst.delift_rels [] metasenv
166                (context_of_t_len - context_len) t
167              in
168               assert (subst = []);
169               assert (metasenv = metasenv');
170               t_in_context)
171           with
172            CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
173             (*CSC: we could implement something stronger by completely changing
174               the semantics of the tactic *)
175             raise (ProofEngineTypes.Fail
176              "Replace: one of the selected terms is not closed") in
177          let ty_of_t_in_context,u = (* TASSI: FIXME *)
178           CicTypeChecker.type_of_aux' metasenv context t_in_context
179            CicUniv.empty_ugraph in
180          let b,u = CicReduction.are_convertible ~metasenv context
181           ty_of_with_what ty_of_t_in_context u in
182          if b then
183           let concl_pat_for_t = ProofEngineHelpers.pattern_of ~term:ty [t] in
184           let pattern_for_t = None,[],concl_pat_for_t in
185            t_in_context,pattern_for_t
186          else
187           raise
188            (ProofEngineTypes.Fail
189              "Replace: one of the selected terms and the term to be replaced with have not convertible types")
190        ) l in
191   let rec aux n whats status =
192    match whats with
193       [] -> ProofEngineTypes.apply_tactic T.id_tac status
194     | (what,pattern)::tl ->
195        let what = CicSubstitution.lift n what in
196        let with_what = CicSubstitution.lift n with_what in
197        let ty_of_with_what = CicSubstitution.lift n ty_of_with_what in
198        ProofEngineTypes.apply_tactic
199          (T.thens
200             ~start:(
201               P.cut_tac 
202                (C.Appl [
203                  (C.MutInd (LibraryObjects.eq_URI (), 0, [])) ;
204                  ty_of_with_what ; 
205                  what ; 
206                  with_what]))
207             ~continuations:[            
208               T.then_
209                 ~start:(
210                   rewrite_tac ~direction:`LeftToRight ~pattern (C.Rel 1))
211                  ~continuation:(
212                    T.then_
213                     ~start:(
214                       ProofEngineTypes.mk_tactic
215                        (function ((proof,goal) as status) ->
216                          let _,metasenv,_,_ = proof in
217                          let _,context,_ = CicUtil.lookup_meta goal metasenv in
218                          let hyp =
219                           try
220                            match List.hd context with
221                               Some (Cic.Name name,_) -> name
222                             | _ -> assert false
223                           with (Failure "hd") -> assert false
224                          in
225                           ProofEngineTypes.apply_tactic
226                            (ProofEngineStructuralRules.clear ~hyp) status))
227                     ~continuation:(aux_tac (n + 1) tl));
228               T.id_tac])
229          status
230   and aux_tac n tl = ProofEngineTypes.mk_tactic (aux n tl) in
231    aux 0 whats status
232  in
233    ProofEngineTypes.mk_tactic (replace_tac ~pattern ~with_what)
234 ;;
235
236
237 (* All these tacs do is applying the right constructor/theorem *)
238
239 let reflexivity_tac =
240   IntroductionTactics.constructor_tac ~n:1
241 ;;
242
243 let symmetry_tac =
244  let symmetry_tac (proof, goal) =
245   let module C = Cic in
246   let module R = CicReduction in
247   let module U = UriManager in
248    let (_,metasenv,_,_) = proof in
249     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
250      match (R.whd context ty) with
251         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
252          when LibraryObjects.is_eq_URI uri ->
253           ProofEngineTypes.apply_tactic 
254            (PrimitiveTactics.apply_tac 
255             ~term: (C.Const (LibraryObjects.sym_eq_URI uri, []))) 
256            (proof,goal)
257
258       | _ -> raise (ProofEngineTypes.Fail "Symmetry failed")
259  in
260   ProofEngineTypes.mk_tactic symmetry_tac
261 ;;
262
263 let transitivity_tac ~term =
264  let transitivity_tac ~term status =
265   let (proof, goal) = status in
266   let module C = Cic in
267   let module R = CicReduction in
268   let module U = UriManager in
269   let module T = Tacticals in
270    let (_,metasenv,_,_) = proof in
271     let metano,context,ty = CicUtil.lookup_meta goal metasenv in
272      match (R.whd context ty) with
273         (C.Appl [(C.MutInd (uri, 0, [])); _; _; _]) 
274         when LibraryObjects.is_eq_URI uri ->
275          ProofEngineTypes.apply_tactic 
276          (T.thens
277           ~start:(PrimitiveTactics.apply_tac
278             ~term: (C.Const (LibraryObjects.trans_eq_URI uri, [])))
279           ~continuations:
280             [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
281           status
282
283       | _ -> raise (ProofEngineTypes.Fail "Transitivity failed")
284  in
285   ProofEngineTypes.mk_tactic (transitivity_tac ~term)
286 ;;
287
288