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