15481
|
1 |
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
|
|
2 |
(* Title: sys/eqsubst_tac.ML
|
|
3 |
Author: Lucas Dixon, University of Edinburgh
|
|
4 |
lucas.dixon@ed.ac.uk
|
|
5 |
Created: 29 Jan 2005
|
|
6 |
*)
|
|
7 |
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
|
|
8 |
(* DESCRIPTION:
|
|
9 |
|
|
10 |
A Tactic to perform a substiution using an equation.
|
|
11 |
|
|
12 |
*)
|
|
13 |
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
|
|
14 |
|
|
15 |
(* Logic specific data *)
|
|
16 |
signature EQRULE_DATA =
|
|
17 |
sig
|
|
18 |
(* to make a meta equality theorem in the current logic *)
|
|
19 |
val prep_meta_eq : thm -> thm list
|
|
20 |
end;
|
|
21 |
|
|
22 |
(* the signature of an instance of the SQSUBST tactic *)
|
|
23 |
signature EQSUBST_TAC =
|
|
24 |
sig
|
|
25 |
val eqsubst_asm_meth : Thm.thm list -> Proof.method
|
|
26 |
val eqsubst_asm_tac : Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
|
|
27 |
val eqsubst_asm_tac' : Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
|
|
28 |
val eqsubst_meth : Thm.thm list -> Proof.method
|
|
29 |
val eqsubst_tac : Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
|
|
30 |
val eqsubst_tac' : Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
|
|
31 |
val meth : bool * Thm.thm list -> Proof.context -> Proof.method
|
|
32 |
val subst : Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
|
|
33 |
val subst_asm : Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
|
|
34 |
|
|
35 |
val setup : (Theory.theory -> Theory.theory) list
|
|
36 |
end;
|
|
37 |
|
|
38 |
functor EQSubstTacFUN (structure EqRuleData : EQRULE_DATA)
|
|
39 |
(* : EQSUBST_TAC *)
|
|
40 |
= struct
|
|
41 |
|
|
42 |
fun search_tb_lr_f f ft =
|
|
43 |
let
|
|
44 |
fun maux ft =
|
|
45 |
let val t' = (IsaFTerm.focus_of_fcterm ft)
|
|
46 |
(* val _ = writeln ("Examining: " ^ (TermLib.string_of_term t')) *)
|
|
47 |
in
|
|
48 |
(case t' of
|
|
49 |
(_ $ _) => Seq.append(f ft,
|
|
50 |
Seq.append(maux (IsaFTerm.focus_left ft),
|
|
51 |
maux (IsaFTerm.focus_right ft)))
|
|
52 |
| (Abs _) => Seq.append (f ft, maux (IsaFTerm.focus_abs ft))
|
|
53 |
| leaf => f ft) end
|
|
54 |
in maux ft end;
|
|
55 |
|
|
56 |
fun search_for_match sgn lhs maxidx =
|
|
57 |
IsaFTerm.find_fcterm_matches
|
|
58 |
search_tb_lr_f
|
|
59 |
(IsaFTerm.clean_unify_ft sgn maxidx lhs);
|
|
60 |
|
|
61 |
|
|
62 |
(* CLEANUP: lots of duplication of code for substituting in
|
|
63 |
assumptions and conclusion - this could be cleaned up a little. *)
|
|
64 |
|
|
65 |
fun subst_concl rule cfvs i th (conclthm, concl_matches)=
|
|
66 |
let
|
|
67 |
fun apply_subst m =
|
|
68 |
(RWInst.rw m rule conclthm)
|
|
69 |
|> IsaND.schemify_frees_to_vars cfvs
|
|
70 |
|> RWInst.beta_eta_contract_tac
|
|
71 |
|> (fn r => Tactic.rtac r i th)
|
|
72 |
|> Seq.map Drule.zero_var_indexes
|
|
73 |
in
|
|
74 |
Seq.flat (Seq.map apply_subst concl_matches)
|
|
75 |
end;
|
|
76 |
|
|
77 |
|
|
78 |
(* substitute within the conclusion of goal i of gth, using a meta
|
|
79 |
equation rule *)
|
|
80 |
fun subst rule i gth =
|
|
81 |
let
|
|
82 |
val th = Thm.incr_indexes 1 gth;
|
|
83 |
val tgt_term = Thm.prop_of th;
|
|
84 |
val maxidx = Term.maxidx_of_term tgt_term;
|
|
85 |
|
|
86 |
val rule' = Drule.zero_var_indexes rule;
|
|
87 |
val (lhs,_) = Logic.dest_equals (Thm.concl_of rule');
|
|
88 |
|
|
89 |
val sgn = Thm.sign_of_thm th;
|
|
90 |
val ctermify = Thm.cterm_of sgn;
|
|
91 |
val trivify = Thm.trivial o ctermify;
|
|
92 |
|
|
93 |
val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
|
|
94 |
val cfvs = rev (map ctermify fvs);
|
|
95 |
|
|
96 |
val conclthm = trivify (Logic.strip_imp_concl fixedbody);
|
|
97 |
val concl_matches =
|
|
98 |
search_for_match sgn lhs maxidx
|
|
99 |
((IsaFTerm.focus_right
|
|
100 |
o IsaFTerm.focus_left
|
|
101 |
o IsaFTerm.fcterm_of_term
|
|
102 |
o Thm.prop_of) conclthm);
|
|
103 |
in
|
|
104 |
subst_concl rule' cfvs i th (conclthm, concl_matches)
|
|
105 |
end;
|
|
106 |
|
|
107 |
(* substitute using an object or meta level equality *)
|
|
108 |
fun eqsubst_tac' instepthm i th =
|
|
109 |
let val stepthms = Seq.of_list (EqRuleData.prep_meta_eq instepthm) in
|
|
110 |
Seq.flat (Seq.map (fn rule => subst rule i th) stepthms)
|
|
111 |
end;
|
|
112 |
(* substitute using one of the given theorems *)
|
|
113 |
fun eqsubst_tac instepthms i th =
|
|
114 |
Seq.flat (Seq.map (fn r => eqsubst_tac' r i th) (Seq.of_list instepthms));
|
|
115 |
|
|
116 |
(* inthms are the given arguments in Isar, and treated as eqstep with
|
|
117 |
the first one, then the second etc *)
|
|
118 |
fun eqsubst_meth inthms =
|
|
119 |
Method.METHOD
|
15486
|
120 |
(fn facts => (*first, insert chained facts*)
|
|
121 |
HEADGOAL (Method.insert_tac facts THEN' eqsubst_tac inthms));
|
15481
|
122 |
|
|
123 |
|
|
124 |
fun apply_subst_in_asm rule cfvs i th matchseq =
|
|
125 |
let
|
|
126 |
fun apply_subst ((j, pth), mseq) =
|
|
127 |
Seq.flat (Seq.map
|
|
128 |
(fn m =>
|
|
129 |
(RWInst.rw m rule pth)
|
|
130 |
|> Thm.permute_prems 0 ~1
|
|
131 |
|> IsaND.schemify_frees_to_vars cfvs
|
|
132 |
|> RWInst.beta_eta_contract_tac
|
|
133 |
|> (fn r => Tactic.dtac r i th)
|
|
134 |
|> Seq.map Drule.zero_var_indexes)
|
|
135 |
mseq)
|
|
136 |
in
|
|
137 |
Seq.flat (Seq.map apply_subst matchseq)
|
|
138 |
end;
|
|
139 |
|
|
140 |
|
|
141 |
(* substitute within an assumption of goal i of gth, using a meta
|
|
142 |
equation rule *)
|
|
143 |
fun subst_asm rule i gth =
|
|
144 |
let
|
|
145 |
val th = Thm.incr_indexes 1 gth;
|
|
146 |
val tgt_term = Thm.prop_of th;
|
|
147 |
val maxidx = Term.maxidx_of_term tgt_term;
|
|
148 |
|
|
149 |
val rule' = Drule.zero_var_indexes rule;
|
|
150 |
val (lhs,_) = Logic.dest_equals (Thm.concl_of rule');
|
|
151 |
|
|
152 |
val sgn = Thm.sign_of_thm th;
|
|
153 |
val ctermify = Thm.cterm_of sgn;
|
|
154 |
val trivify = Thm.trivial o ctermify;
|
|
155 |
|
|
156 |
val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
|
|
157 |
val cfvs = rev (map ctermify fvs);
|
|
158 |
|
|
159 |
val premthms = Seq.of_list (IsaPLib.number_list 1
|
|
160 |
(map trivify (Logic.strip_imp_prems fixedbody)));
|
|
161 |
val prem_matches =
|
|
162 |
Seq.map (fn (i, pth) =>
|
|
163 |
((i, pth), search_for_match sgn lhs maxidx
|
|
164 |
((IsaFTerm.focus_right
|
|
165 |
o IsaFTerm.fcterm_of_term
|
|
166 |
o Thm.prop_of) pth)))
|
|
167 |
premthms;
|
|
168 |
in
|
|
169 |
apply_subst_in_asm rule' cfvs i th prem_matches
|
|
170 |
end;
|
|
171 |
|
|
172 |
(* substitute using an object or meta level equality *)
|
|
173 |
fun eqsubst_asm_tac' instepthm i th =
|
|
174 |
let val stepthms = Seq.of_list (EqRuleData.prep_meta_eq instepthm) in
|
|
175 |
Seq.flat (Seq.map (fn rule => subst_asm rule i th) stepthms)
|
|
176 |
end;
|
|
177 |
|
|
178 |
(* substitute using one of the given theorems *)
|
|
179 |
fun eqsubst_asm_tac instepthms i th =
|
|
180 |
Seq.flat (Seq.map (fn r => eqsubst_asm_tac' r i th)
|
|
181 |
(Seq.of_list instepthms));
|
|
182 |
|
|
183 |
(* inthms are the given arguments in Isar, and treated as eqstep with
|
|
184 |
the first one, then the second etc *)
|
|
185 |
fun eqsubst_asm_meth inthms =
|
|
186 |
Method.METHOD
|
15486
|
187 |
(fn facts => (*first, insert chained facts*)
|
|
188 |
HEADGOAL (Method.insert_tac facts THEN' eqsubst_asm_tac inthms));
|
|
189 |
|
15481
|
190 |
|
|
191 |
(* combination method that takes a flag (true indicates that subst
|
|
192 |
should be done to an assumption, false = apply to the conclusion of
|
|
193 |
the goal) as well as the theorems to use *)
|
|
194 |
fun meth (asmflag, inthms) ctxt =
|
|
195 |
if asmflag then eqsubst_asm_meth inthms else eqsubst_meth inthms;
|
|
196 |
|
|
197 |
(* syntax for options, given "(asm)" will give back true, without
|
|
198 |
gives back false *)
|
|
199 |
val options_syntax =
|
|
200 |
(Args.parens (Args.$$$ "asm") >> (K true)) ||
|
|
201 |
(Scan.succeed false);
|
|
202 |
|
|
203 |
(* method syntax, first take options, then theorems *)
|
|
204 |
fun meth_syntax meth src ctxt =
|
|
205 |
meth (snd (Method.syntax ((Scan.lift options_syntax)
|
|
206 |
-- Attrib.local_thms) src ctxt))
|
|
207 |
ctxt;
|
|
208 |
|
|
209 |
(* setup function for adding method to theory. *)
|
|
210 |
val setup =
|
|
211 |
[Method.add_method ("subst", meth_syntax meth, "Substiution with an equation. Use \"(asm)\" option to substitute in an assumption.")];
|
|
212 |
|
|
213 |
end; |