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