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, _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
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, _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
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 ind = function
72 | C.Appl (t :: tl) -> ind t
75 let rec constr = function
76 | C.MutConstruct _ -> true
77 | C.Appl (t :: tl) -> constr t
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
86 | None -> raise (PET.Fail msg0)
88 let ty, _ = TC.type_of_aux' metasenv context what CicUniv.empty_ugraph in
89 let subst_g direction i t =
91 let tactic = lift_rewrite_tac ~context ~direction ~pattern what in
94 let var = match PEH.get_name context i with
96 | None -> raise (PET.Fail msg0)
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))
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]
110 [lift_destruct_tac ~context ~what; PESR.clear ~hyps:[hyp]]
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]
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)
128 PET.apply_tactic (T.seq ~tactics) status
130 PET.mk_tactic subst_tac
133 let subst_tac status =
134 let progress = ref false in
135 let try_tactic ~tactic =
136 let try_tactic status =
138 let result = PET.apply_tactic tactic status in
139 progress := true; result
141 | PET.Fail _ -> PET.apply_tactic T.id_tac status
143 PET.mk_tactic try_tactic
145 let subst hyp = try_tactic ~tactic:(subst_tac ~try_tactic ~hyp) in
147 | Some (C.Name s, _) -> Some (subst s)
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)
157 PET.mk_tactic subst_tac
159 let try_tac tactic = T.try_tactic ~tactic
160 let then_tac start continuation = T.then_ ~start ~continuation
163 let tactic = T.repeat_tactic ~tactic:subst_tac in