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