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