1 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
27 module DT = DiscriminationTactics
28 module DTI = DoubleTypeInference
29 module ET = EqualityTactics
31 module LO = LibraryObjects
32 module PEH = ProofEngineHelpers
33 module PESR = ProofEngineStructuralRules
34 module PET = ProofEngineTypes
35 module RT = ReductionTactics
36 module S = CicSubstitution
38 module TC = CicTypeChecker
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
49 PET.mk_tactic lift_rewrite_tac
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
60 PET.mk_tactic lift_destruct_tac
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"
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
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
81 | None -> raise (PET.Fail msg0)
83 let ty, _ = TC.type_of_aux' metasenv context what CicUniv.empty_ugraph in
84 let subst_g direction i t =
86 let tactic = lift_rewrite_tac ~context ~direction ~pattern what in
89 let var = match PEH.get_name context i with
91 | None -> raise (PET.Fail msg0)
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))
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]
105 [lift_destruct_tac ~context ~what; PESR.clear ~hyps:[hyp]]
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]
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)
123 PET.apply_tactic (T.seq ~tactics) status
125 PET.mk_tactic subst_tac
128 let subst_tac status =
129 let progress = ref false in
130 let try_tactic ~tactic =
131 let try_tactic status =
133 let result = PET.apply_tactic tactic status in
134 progress := true; result
136 | PET.Fail _ -> PET.apply_tactic T.id_tac status
138 PET.mk_tactic try_tactic
140 let subst hyp = try_tactic ~tactic:(subst_tac ~try_tactic ~hyp) in
142 | Some (C.Name s, _) -> Some (subst s)
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)
152 PET.mk_tactic subst_tac
154 let try_tac tactic = T.try_tactic ~tactic
155 let then_tac start continuation = T.then_ ~start ~continuation
157 let rec repeat_tactic ~tactic =
158 try_tac (then_tac tactic (repeat_tactic ~tactic))
161 let subst_tac status =
162 PET.apply_tactic (repeat_tactic ~tactic:subst_tac) status
164 PET.mk_tactic subst_tac