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