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