]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/substTactic.ml
AMBDA-TYPES: some improvements. subst now fully exploited
[helm.git] / components / tactics / substTactic.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 module C    = Cic
27 module DT   = DiscriminationTactics
28 module DTI  = DoubleTypeInference
29 module ET   = EqualityTactics
30 module HEL  = HExtlib
31 module LO   = LibraryObjects
32 module PEH  = ProofEngineHelpers
33 module PESR = ProofEngineStructuralRules
34 module PET  = ProofEngineTypes
35 module RT   = ReductionTactics
36 module S    = CicSubstitution
37 module T    = Tacticals
38 module TC   = CicTypeChecker
39
40 let lift_rewrite_tac ~context ~direction ~pattern equality =
41    let lift_rewrite_tac status =
42       let (proof, goal) = status in
43       let (_, metasenv, _, _, _) = proof in
44       let _, new_context, _ = CicUtil.lookup_meta goal metasenv in
45       let n = List.length new_context - List.length context in
46       let equality = S.lift n equality in
47       PET.apply_tactic (ET.rewrite_tac ~direction ~pattern equality []) status
48    in
49    PET.mk_tactic lift_rewrite_tac
50
51 let lift_destruct_tac ~context ~what =
52    let lift_destruct_tac status =
53       let (proof, goal) = status in
54       let (_, metasenv, _, _, _) = proof in
55       let _, new_context, _ = CicUtil.lookup_meta goal metasenv in
56       let n = List.length new_context - List.length context in
57       let what = S.lift n what in
58       PET.apply_tactic (DT.destruct_tac ~term:what) status
59    in
60    PET.mk_tactic lift_destruct_tac
61       
62 let msg0 = lazy "Subst: not found in context"
63 let msg1 = lazy "Subst: not an erasable equation"
64 let msg2 = lazy "Subst: recursive equation" 
65 let msg3 = lazy "Subst: no progress" 
66
67 let rec subst_tac ~try_tactic ~hyp =
68    let hole = C.Implicit (Some `Hole) in
69    let meta = C.Implicit None in
70    let rec constr = function
71       | C.MutConstruct _ -> true
72       | C.Appl (t :: tl) -> constr t
73       | _                -> false
74    in
75    let subst_tac status =
76       let (proof, goal) = status in
77       let (_, metasenv, _, _, _) = proof in
78       let _, context, _ = CicUtil.lookup_meta goal metasenv in
79       let what = match PEH.get_rel context hyp with
80          | Some t -> t
81          | None   -> raise (PET.Fail msg0)
82       in
83       let ty, _ = TC.type_of_aux' metasenv context what CicUniv.empty_ugraph in
84       let subst_g direction i t =
85          let rewrite pattern =
86             let tactic = lift_rewrite_tac ~context ~direction ~pattern what in
87             try_tactic ~tactic
88          in
89          let var = match PEH.get_name context i with
90             | Some name -> name
91             | None      -> raise (PET.Fail msg0)        
92          in
93          if DTI.does_not_occur i t then () else raise (PET.Fail msg2);
94          let map self = function
95             | Some (C.Name s, _) when s <> self -> 
96                Some (rewrite (None, [(s, hole)], None))
97             | _                                 -> None
98          in
99          let rew_hips = HEL.list_rev_map_filter (map hyp) context in
100          let rew_concl = rewrite (None, [], Some hole) in
101          let clear = PESR.clear ~hyps:[hyp; var] in
102          List.rev_append (rew_concl :: rew_hips) [clear]
103       in
104       let destruct_g () =
105          [lift_destruct_tac ~context ~what; PESR.clear ~hyps:[hyp]]
106       in
107       let whd_g () =
108          let whd_pattern = C.Appl [meta; meta; hole; hole] in
109          let pattern = None, [hyp, whd_pattern], None in
110          [RT.whd_tac ~pattern; subst_tac ~try_tactic ~hyp]
111       in
112       let tactics = match ty with
113          | (C.Appl [(C.MutInd (uri, 0, [])); _; C.Rel i; t]) 
114             when LO.is_eq_URI uri -> subst_g `LeftToRight i t
115          | (C.Appl [(C.MutInd (uri, 0, [])); _; t; C.Rel i]) 
116             when LO.is_eq_URI uri -> subst_g `RightToLeft i t
117          | (C.Appl [(C.MutInd (uri, 0, [])); _; t1; t2]) 
118             when LO.is_eq_URI uri && constr t1 && constr t2 -> destruct_g ()
119          | (C.Appl [(C.MutInd (uri, 0, [])); _; t1; t2])
120             when LO.is_eq_URI uri -> whd_g ()    
121          | _ -> raise (PET.Fail msg1)
122       in
123       PET.apply_tactic (T.seq ~tactics) status
124    in
125    PET.mk_tactic subst_tac
126
127 let subst_tac =
128    let subst_tac status =
129       let progress = ref false in
130       let try_tactic ~tactic =
131          let try_tactic status =
132             try 
133                let result = PET.apply_tactic tactic status in
134                progress := true; result
135             with
136                | PET.Fail _ -> PET.apply_tactic T.id_tac status
137          in
138          PET.mk_tactic try_tactic
139       in
140       let subst hyp = try_tactic ~tactic:(subst_tac ~try_tactic ~hyp) in
141       let map = function
142          | Some (C.Name s, _) -> Some (subst s)
143          | _                  -> None
144       in
145       let (proof, goal) = status in
146       let (_, metasenv, _, _, _) = proof in
147       let _, context, _ = CicUtil.lookup_meta goal metasenv in
148       let tactics = HEL.list_rev_map_filter map context in
149       let result = PET.apply_tactic (T.seq ~tactics) status in
150       if !progress then result else raise (PET.Fail msg3)
151    in
152    PET.mk_tactic subst_tac
153
154    let try_tac tactic = T.try_tactic ~tactic 
155    let then_tac start continuation = T.then_ ~start ~continuation 
156
157 let subst_tac = 
158    let tactic = T.repeat_tactic ~tactic:subst_tac in
159    T.try_tactic ~tactic