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