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