]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/substTactic.ml
cic_acic should be compiled before cic_exportation
[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, _subst, _, _, _) = 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, _subst, _, _, _) = 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 ind = function
71       | C.MutInd _       -> true
72       | C.Appl (t :: tl) -> ind t
73       | _                -> false
74    in
75    let rec constr = function
76       | C.MutConstruct _ -> true
77       | C.Appl (t :: tl) -> constr t
78       | _                -> false
79    in
80    let subst_tac status =
81       let (proof, goal) = status in
82       let (_, metasenv, _subst, _, _, _) = proof in
83       let _, context, _ = CicUtil.lookup_meta goal metasenv in
84       let what = match PEH.get_rel context hyp with
85          | Some t -> t
86          | None   -> raise (PET.Fail msg0)
87       in
88       let ty, _ = TC.type_of_aux' metasenv context what CicUniv.empty_ugraph in
89       let subst_g direction i t =
90          let rewrite pattern =
91             let tactic = lift_rewrite_tac ~context ~direction ~pattern what in
92             try_tactic ~tactic
93          in
94          let var = match PEH.get_name context i with
95             | Some name -> name
96             | None      -> raise (PET.Fail msg0)        
97          in
98          if DTI.does_not_occur i t then () else raise (PET.Fail msg2);
99          let map self = function
100             | Some (C.Name s, _) when s <> self -> 
101                Some (rewrite (None, [(s, hole)], None))
102             | _                                 -> None
103          in
104          let rew_hips = HEL.list_rev_map_filter (map hyp) context in
105          let rew_concl = rewrite (None, [], Some hole) in
106          let clear = PESR.clear ~hyps:[hyp; var] in
107          List.rev_append (rew_concl :: rew_hips) [clear]
108       in
109       let destruct_g () =
110          [lift_destruct_tac ~context ~what; PESR.clear ~hyps:[hyp]]
111       in
112       let whd_g () =
113          let whd_pattern = C.Appl [meta; hole; hole; hole] in
114          let pattern = None, [hyp, whd_pattern], None in
115          [RT.whd_tac ~pattern; subst_tac ~try_tactic ~hyp]
116       in
117       let tactics = match ty with
118          | (C.Appl [(C.MutInd (uri, 0, [])); _; C.Rel i; t]) 
119             when LO.is_eq_URI uri -> subst_g `LeftToRight i t
120          | (C.Appl [(C.MutInd (uri, 0, [])); _; t; C.Rel i]) 
121             when LO.is_eq_URI uri -> subst_g `RightToLeft i t
122          | (C.Appl [(C.MutInd (uri, 0, [])); t; t1; t2]) 
123             when LO.is_eq_URI uri && ind t && constr t1 && constr t2 -> destruct_g ()
124          | (C.Appl [(C.MutInd (uri, 0, [])); _; t1; t2])
125             when LO.is_eq_URI uri -> whd_g ()    
126          | _ -> raise (PET.Fail msg1)
127       in
128       PET.apply_tactic (T.seq ~tactics) status
129    in
130    PET.mk_tactic subst_tac
131
132 let subst_tac =
133    let subst_tac status =
134       let progress = ref false in
135       let try_tactic ~tactic =
136          let try_tactic status =
137             try 
138                let result = PET.apply_tactic tactic status in
139                progress := true; result
140             with
141                | PET.Fail _ -> PET.apply_tactic T.id_tac status
142          in
143          PET.mk_tactic try_tactic
144       in
145       let subst hyp = try_tactic ~tactic:(subst_tac ~try_tactic ~hyp) in
146       let map = function
147          | Some (C.Name s, _) -> Some (subst s)
148          | _                  -> None
149       in
150       let (proof, goal) = status in
151       let (_, metasenv, _subst, _, _, _) = proof in
152       let _, context, _ = CicUtil.lookup_meta goal metasenv in
153       let tactics = HEL.list_rev_map_filter map context in
154       let result = PET.apply_tactic (T.seq ~tactics) status in
155       if !progress then result else raise (PET.Fail msg3)
156    in
157    PET.mk_tactic subst_tac
158
159    let try_tac tactic = T.try_tactic ~tactic 
160    let then_tac start continuation = T.then_ ~start ~continuation 
161
162 let subst_tac = 
163    let tactic = T.repeat_tactic ~tactic:subst_tac in
164    T.try_tactic ~tactic