paulson@15481
|
1 |
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
|
dixon@15538
|
2 |
(* Title: Provers/eqsubst.ML
|
paulson@15481
|
3 |
Author: Lucas Dixon, University of Edinburgh
|
paulson@15481
|
4 |
lucas.dixon@ed.ac.uk
|
dixon@15538
|
5 |
Modified: 18 Feb 2005 - Lucas -
|
paulson@15481
|
6 |
Created: 29 Jan 2005
|
paulson@15481
|
7 |
*)
|
paulson@15481
|
8 |
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
|
paulson@15481
|
9 |
(* DESCRIPTION:
|
paulson@15481
|
10 |
|
paulson@15481
|
11 |
A Tactic to perform a substiution using an equation.
|
paulson@15481
|
12 |
|
paulson@15481
|
13 |
*)
|
paulson@15481
|
14 |
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
|
paulson@15481
|
15 |
|
dixon@15538
|
16 |
(* Logic specific data stub *)
|
paulson@15481
|
17 |
signature EQRULE_DATA =
|
paulson@15481
|
18 |
sig
|
dixon@15538
|
19 |
|
paulson@15481
|
20 |
(* to make a meta equality theorem in the current logic *)
|
paulson@15481
|
21 |
val prep_meta_eq : thm -> thm list
|
dixon@15538
|
22 |
|
paulson@15481
|
23 |
end;
|
paulson@15481
|
24 |
|
dixon@15538
|
25 |
|
paulson@15481
|
26 |
(* the signature of an instance of the SQSUBST tactic *)
|
paulson@15481
|
27 |
signature EQSUBST_TAC =
|
paulson@15481
|
28 |
sig
|
dixon@15538
|
29 |
|
dixon@15550
|
30 |
type match =
|
dixon@15915
|
31 |
((Term.indexname * (Term.sort * Term.typ)) list (* type instantiations *)
|
dixon@15915
|
32 |
* (Term.indexname * (Term.typ * Term.term)) list) (* term instantiations *)
|
dixon@15814
|
33 |
* (string * Term.typ) list (* fake named type abs env *)
|
dixon@15814
|
34 |
* (string * Term.typ) list (* type abs env *)
|
dixon@15814
|
35 |
* Term.term (* outer term *)
|
dixon@15550
|
36 |
|
dixon@15538
|
37 |
val prep_subst_in_asm :
|
dixon@15538
|
38 |
(Sign.sg (* sign for matching *)
|
dixon@15538
|
39 |
-> int (* maxidx *)
|
dixon@15538
|
40 |
-> 'a (* input object kind *)
|
dixon@15538
|
41 |
-> BasicIsaFTerm.FcTerm (* focusterm to search under *)
|
dixon@15538
|
42 |
-> 'b) (* result type *)
|
dixon@15538
|
43 |
-> int (* subgoal to subst in *)
|
dixon@15538
|
44 |
-> Thm.thm (* target theorem with subgoals *)
|
dixon@15538
|
45 |
-> int (* premise to subst in *)
|
dixon@15538
|
46 |
-> (Thm.cterm list (* certified free var placeholders for vars *)
|
dixon@15538
|
47 |
* int (* premice no. to subst *)
|
dixon@15538
|
48 |
* int (* number of assumptions of premice *)
|
dixon@15538
|
49 |
* Thm.thm) (* premice as a new theorem for forward reasoning *)
|
dixon@15538
|
50 |
* ('a -> 'b) (* matchf *)
|
dixon@15538
|
51 |
|
dixon@15538
|
52 |
val prep_subst_in_asms :
|
dixon@15538
|
53 |
(Sign.sg -> int -> 'a -> BasicIsaFTerm.FcTerm -> 'b)
|
dixon@15538
|
54 |
-> int (* subgoal to subst in *)
|
dixon@15538
|
55 |
-> Thm.thm (* target theorem with subgoals *)
|
dixon@15538
|
56 |
-> ((Thm.cterm list (* certified free var placeholders for vars *)
|
dixon@15538
|
57 |
* int (* premice no. to subst *)
|
dixon@15538
|
58 |
* int (* number of assumptions of premice *)
|
dixon@15538
|
59 |
* Thm.thm) (* premice as a new theorem for forward reasoning *)
|
dixon@15538
|
60 |
* ('a -> 'b)) (* matchf *)
|
dixon@15538
|
61 |
Seq.seq
|
dixon@15538
|
62 |
|
dixon@15538
|
63 |
val apply_subst_in_asm :
|
dixon@15538
|
64 |
int (* subgoal *)
|
dixon@15538
|
65 |
-> Thm.thm (* overall theorem *)
|
dixon@15538
|
66 |
-> (Thm.cterm list (* certified free var placeholders for vars *)
|
dixon@15538
|
67 |
* int (* assump no being subst *)
|
dixon@15538
|
68 |
* int (* num of premises of asm *)
|
dixon@15538
|
69 |
* Thm.thm) (* premthm *)
|
dixon@15538
|
70 |
-> Thm.thm (* rule *)
|
dixon@15550
|
71 |
-> match
|
dixon@15538
|
72 |
-> Thm.thm Seq.seq
|
dixon@15538
|
73 |
|
dixon@15538
|
74 |
val prep_concl_subst :
|
dixon@15538
|
75 |
(Sign.sg -> int -> 'a -> BasicIsaFTerm.FcTerm -> 'b) (* searchf *)
|
dixon@15538
|
76 |
-> int (* subgoal *)
|
dixon@15538
|
77 |
-> Thm.thm (* overall goal theorem *)
|
dixon@15538
|
78 |
-> (Thm.cterm list * Thm.thm) * ('a -> 'b) (* (cvfs, conclthm), matchf *)
|
dixon@15538
|
79 |
|
dixon@15538
|
80 |
val apply_subst_in_concl :
|
dixon@15538
|
81 |
int (* subgoal *)
|
dixon@15538
|
82 |
-> Thm.thm (* thm with all goals *)
|
dixon@15538
|
83 |
-> Thm.cterm list (* certified free var placeholders for vars *)
|
dixon@15538
|
84 |
* Thm.thm (* trivial thm of goal concl *)
|
dixon@15538
|
85 |
(* possible matches/unifiers *)
|
dixon@15538
|
86 |
-> Thm.thm (* rule *)
|
dixon@15550
|
87 |
-> match
|
dixon@15538
|
88 |
-> Thm.thm Seq.seq (* substituted goal *)
|
dixon@15538
|
89 |
|
dixon@15814
|
90 |
val searchf_tlr_unify_all :
|
dixon@15814
|
91 |
(Sign.sg -> int ->
|
dixon@15814
|
92 |
Term.term ->
|
dixon@15814
|
93 |
BasicIsaFTerm.FcTerm ->
|
dixon@15929
|
94 |
match Seq.seq Seq.seq)
|
dixon@15814
|
95 |
val searchf_tlr_unify_valid :
|
dixon@15814
|
96 |
(Sign.sg -> int ->
|
dixon@15814
|
97 |
Term.term ->
|
dixon@15814
|
98 |
BasicIsaFTerm.FcTerm ->
|
dixon@15929
|
99 |
match Seq.seq Seq.seq)
|
dixon@15814
|
100 |
|
dixon@15929
|
101 |
val eqsubst_asm_meth : int -> Thm.thm list -> Proof.method
|
dixon@15929
|
102 |
val eqsubst_asm_tac : int -> Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
|
dixon@15814
|
103 |
val eqsubst_asm_tac' :
|
dixon@15814
|
104 |
(Sign.sg -> int ->
|
dixon@15814
|
105 |
Term.term ->
|
dixon@15814
|
106 |
BasicIsaFTerm.FcTerm ->
|
dixon@15814
|
107 |
match Seq.seq) -> Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
|
dixon@15538
|
108 |
|
dixon@15929
|
109 |
val eqsubst_meth : int -> Thm.thm list -> Proof.method
|
dixon@15929
|
110 |
val eqsubst_tac : int -> Thm.thm list -> int -> Thm.thm -> Thm.thm Seq.seq
|
dixon@15814
|
111 |
val eqsubst_tac' :
|
dixon@15814
|
112 |
(Sign.sg -> int ->
|
dixon@15814
|
113 |
Term.term ->
|
dixon@15814
|
114 |
BasicIsaFTerm.FcTerm ->
|
dixon@15814
|
115 |
match Seq.seq) -> Thm.thm -> int -> Thm.thm -> Thm.thm Seq.seq
|
dixon@15538
|
116 |
|
dixon@15929
|
117 |
val meth : (bool * int) * Thm.thm list -> Proof.context -> Proof.method
|
paulson@15481
|
118 |
val setup : (Theory.theory -> Theory.theory) list
|
paulson@15481
|
119 |
end;
|
paulson@15481
|
120 |
|
paulson@15481
|
121 |
functor EQSubstTacFUN (structure EqRuleData : EQRULE_DATA)
|
dixon@15538
|
122 |
: EQSUBST_TAC
|
paulson@15481
|
123 |
= struct
|
paulson@15481
|
124 |
|
dixon@15915
|
125 |
(* a type abriviation for match information *)
|
dixon@15550
|
126 |
type match =
|
dixon@15915
|
127 |
((Term.indexname * (Term.sort * Term.typ)) list (* type instantiations *)
|
dixon@15915
|
128 |
* (Term.indexname * (Term.typ * Term.term)) list) (* term instantiations *)
|
dixon@15915
|
129 |
* (string * Term.typ) list (* fake named type abs env *)
|
dixon@15915
|
130 |
* (string * Term.typ) list (* type abs env *)
|
dixon@15915
|
131 |
* Term.term (* outer term *)
|
dixon@15550
|
132 |
|
dixon@15550
|
133 |
|
dixon@15538
|
134 |
(* FOR DEBUGGING...
|
dixon@15538
|
135 |
type trace_subst_errT = int (* subgoal *)
|
dixon@15538
|
136 |
* Thm.thm (* thm with all goals *)
|
dixon@15538
|
137 |
* (Thm.cterm list (* certified free var placeholders for vars *)
|
dixon@15538
|
138 |
* Thm.thm) (* trivial thm of goal concl *)
|
dixon@15538
|
139 |
(* possible matches/unifiers *)
|
dixon@15538
|
140 |
* Thm.thm (* rule *)
|
dixon@15538
|
141 |
* (((Term.indexname * Term.typ) list (* type instantiations *)
|
dixon@15538
|
142 |
* (Term.indexname * Term.term) list ) (* term instantiations *)
|
dixon@15538
|
143 |
* (string * Term.typ) list (* Type abs env *)
|
dixon@15538
|
144 |
* Term.term) (* outer term *);
|
dixon@15538
|
145 |
|
dixon@15538
|
146 |
val trace_subst_err = (ref NONE : trace_subst_errT option ref);
|
dixon@15538
|
147 |
val trace_subst_search = ref false;
|
dixon@15538
|
148 |
exception trace_subst_exp of trace_subst_errT;
|
dixon@15538
|
149 |
*)
|
dixon@15538
|
150 |
|
dixon@15538
|
151 |
(* also defined in /HOL/Tools/inductive_codegen.ML,
|
dixon@15538
|
152 |
maybe move this to seq.ML ? *)
|
dixon@15538
|
153 |
infix 5 :->;
|
dixon@15538
|
154 |
fun s :-> f = Seq.flat (Seq.map f s);
|
dixon@15538
|
155 |
|
dixon@15814
|
156 |
(* search from top, left to right, then down *)
|
dixon@15814
|
157 |
fun search_tlr_all_f f ft =
|
paulson@15481
|
158 |
let
|
paulson@15481
|
159 |
fun maux ft =
|
paulson@15481
|
160 |
let val t' = (IsaFTerm.focus_of_fcterm ft)
|
dixon@15538
|
161 |
(* val _ =
|
dixon@15538
|
162 |
if !trace_subst_search then
|
dixon@15538
|
163 |
(writeln ("Examining: " ^ (TermLib.string_of_term t'));
|
dixon@15538
|
164 |
TermLib.writeterm t'; ())
|
dixon@15538
|
165 |
else (); *)
|
paulson@15481
|
166 |
in
|
paulson@15481
|
167 |
(case t' of
|
dixon@15538
|
168 |
(_ $ _) => Seq.append(maux (IsaFTerm.focus_left ft),
|
dixon@15929
|
169 |
Seq.cons(f ft,
|
paulson@15481
|
170 |
maux (IsaFTerm.focus_right ft)))
|
dixon@15929
|
171 |
| (Abs _) => Seq.cons(f ft, maux (IsaFTerm.focus_abs ft))
|
dixon@15929
|
172 |
| leaf => Seq.single (f ft)) end
|
paulson@15481
|
173 |
in maux ft end;
|
paulson@15481
|
174 |
|
dixon@15814
|
175 |
(* search from top, left to right, then down *)
|
dixon@15814
|
176 |
fun search_tlr_valid_f f ft =
|
dixon@15814
|
177 |
let
|
dixon@15814
|
178 |
fun maux ft =
|
dixon@15814
|
179 |
let
|
dixon@15814
|
180 |
val hereseq = if IsaFTerm.valid_match_start ft then f ft else Seq.empty
|
dixon@15814
|
181 |
in
|
dixon@15814
|
182 |
(case (IsaFTerm.focus_of_fcterm ft) of
|
dixon@15814
|
183 |
(_ $ _) => Seq.append(maux (IsaFTerm.focus_left ft),
|
dixon@15929
|
184 |
Seq.cons(hereseq,
|
dixon@15814
|
185 |
maux (IsaFTerm.focus_right ft)))
|
dixon@15929
|
186 |
| (Abs _) => Seq.cons(hereseq, maux (IsaFTerm.focus_abs ft))
|
dixon@15929
|
187 |
| leaf => Seq.single (hereseq))
|
dixon@15814
|
188 |
end
|
dixon@15814
|
189 |
in maux ft end;
|
dixon@15814
|
190 |
|
dixon@15814
|
191 |
(* search all unifications *)
|
dixon@15814
|
192 |
fun searchf_tlr_unify_all sgn maxidx lhs =
|
paulson@15481
|
193 |
IsaFTerm.find_fcterm_matches
|
dixon@15814
|
194 |
search_tlr_all_f
|
paulson@15481
|
195 |
(IsaFTerm.clean_unify_ft sgn maxidx lhs);
|
paulson@15481
|
196 |
|
dixon@15814
|
197 |
(* search only for 'valid' unifiers (non abs subterms and non vars) *)
|
dixon@15814
|
198 |
fun searchf_tlr_unify_valid sgn maxidx lhs =
|
dixon@15814
|
199 |
IsaFTerm.find_fcterm_matches
|
dixon@15814
|
200 |
search_tlr_valid_f
|
dixon@15814
|
201 |
(IsaFTerm.clean_unify_ft sgn maxidx lhs);
|
dixon@15814
|
202 |
|
dixon@15929
|
203 |
(* special tactic to skip the first "occ" occurances - ie start at the nth match *)
|
dixon@15929
|
204 |
fun skip_first_occs_search occ searchf sgn i t ft =
|
dixon@15929
|
205 |
let
|
dixon@15929
|
206 |
fun skip_occs n sq =
|
dixon@15929
|
207 |
if n <= 1 then sq
|
dixon@15929
|
208 |
else
|
dixon@15929
|
209 |
(case (Seq.pull sq) of NONE => Seq.empty
|
dixon@15929
|
210 |
| SOME (h,t) =>
|
dixon@15929
|
211 |
(case Seq.pull h of NONE => skip_occs n t
|
dixon@15929
|
212 |
| SOME _ => skip_occs (n - 1) t))
|
dixon@15929
|
213 |
in Seq.flat (skip_occs occ (searchf sgn i t ft)) end;
|
dixon@15929
|
214 |
|
dixon@15814
|
215 |
|
dixon@15538
|
216 |
(* apply a substitution in the conclusion of the theorem th *)
|
dixon@15538
|
217 |
(* cfvs are certified free var placeholders for goal params *)
|
dixon@15538
|
218 |
(* conclthm is a theorem of for just the conclusion *)
|
dixon@15538
|
219 |
(* m is instantiation/match information *)
|
dixon@15538
|
220 |
(* rule is the equation for substitution *)
|
dixon@15538
|
221 |
fun apply_subst_in_concl i th (cfvs, conclthm) rule m =
|
dixon@15538
|
222 |
(RWInst.rw m rule conclthm)
|
dixon@15855
|
223 |
|> IsaND.unfix_frees cfvs
|
dixon@15915
|
224 |
|> RWInst.beta_eta_contract
|
dixon@15538
|
225 |
|> (fn r => Tactic.rtac r i th);
|
paulson@15481
|
226 |
|
dixon@15538
|
227 |
(*
|
paulson@15481
|
228 |
|
dixon@15538
|
229 |
|> (fn r => Thm.bicompose false (false, r, Thm.nprems_of r) i th)
|
paulson@15481
|
230 |
|
dixon@15538
|
231 |
*)
|
paulson@15481
|
232 |
|
paulson@15481
|
233 |
(* substitute within the conclusion of goal i of gth, using a meta
|
dixon@15538
|
234 |
equation rule. Note that we assume rule has var indicies zero'd *)
|
dixon@15538
|
235 |
fun prep_concl_subst searchf i gth =
|
paulson@15481
|
236 |
let
|
paulson@15481
|
237 |
val th = Thm.incr_indexes 1 gth;
|
paulson@15481
|
238 |
val tgt_term = Thm.prop_of th;
|
paulson@15481
|
239 |
|
paulson@15481
|
240 |
val sgn = Thm.sign_of_thm th;
|
paulson@15481
|
241 |
val ctermify = Thm.cterm_of sgn;
|
paulson@15481
|
242 |
val trivify = Thm.trivial o ctermify;
|
paulson@15481
|
243 |
|
paulson@15481
|
244 |
val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
|
paulson@15481
|
245 |
val cfvs = rev (map ctermify fvs);
|
paulson@15481
|
246 |
|
dixon@15538
|
247 |
val conclterm = Logic.strip_imp_concl fixedbody;
|
dixon@15538
|
248 |
val conclthm = trivify conclterm;
|
dixon@15538
|
249 |
val maxidx = Term.maxidx_of_term conclterm;
|
paulson@15481
|
250 |
in
|
dixon@15538
|
251 |
((cfvs, conclthm),
|
dixon@15538
|
252 |
(fn lhs => searchf sgn maxidx lhs
|
dixon@15538
|
253 |
((IsaFTerm.focus_right
|
dixon@15538
|
254 |
o IsaFTerm.focus_left
|
dixon@15538
|
255 |
o IsaFTerm.fcterm_of_term
|
dixon@15538
|
256 |
o Thm.prop_of) conclthm)))
|
paulson@15481
|
257 |
end;
|
paulson@15481
|
258 |
|
dixon@15538
|
259 |
|
paulson@15481
|
260 |
(* substitute using an object or meta level equality *)
|
dixon@15814
|
261 |
fun eqsubst_tac' searchf instepthm i th =
|
dixon@15538
|
262 |
let
|
dixon@15538
|
263 |
val (cvfsconclthm, findmatchf) =
|
dixon@15814
|
264 |
prep_concl_subst searchf i th;
|
dixon@15538
|
265 |
|
dixon@15538
|
266 |
val stepthms =
|
dixon@15538
|
267 |
Seq.map Drule.zero_var_indexes
|
dixon@15538
|
268 |
(Seq.of_list (EqRuleData.prep_meta_eq instepthm));
|
dixon@15538
|
269 |
|
dixon@15538
|
270 |
fun rewrite_with_thm r =
|
dixon@15538
|
271 |
let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
|
dixon@15538
|
272 |
in (findmatchf lhs)
|
dixon@15538
|
273 |
:-> (apply_subst_in_concl i th cvfsconclthm r) end;
|
dixon@15538
|
274 |
|
dixon@15538
|
275 |
in (stepthms :-> rewrite_with_thm) end;
|
dixon@15538
|
276 |
|
dixon@15538
|
277 |
|
paulson@15481
|
278 |
(* substitute using one of the given theorems *)
|
dixon@15929
|
279 |
fun eqsubst_tac occ instepthms i th =
|
dixon@15538
|
280 |
if Thm.nprems_of th < i then Seq.empty else
|
dixon@15814
|
281 |
(Seq.of_list instepthms)
|
dixon@15929
|
282 |
:-> (fn r => eqsubst_tac' (skip_first_occs_search
|
dixon@15929
|
283 |
occ searchf_tlr_unify_valid) r i th);
|
paulson@15481
|
284 |
|
paulson@15481
|
285 |
(* inthms are the given arguments in Isar, and treated as eqstep with
|
paulson@15481
|
286 |
the first one, then the second etc *)
|
dixon@15929
|
287 |
fun eqsubst_meth occ inthms =
|
paulson@15481
|
288 |
Method.METHOD
|
dixon@15538
|
289 |
(fn facts =>
|
dixon@15929
|
290 |
HEADGOAL ( Method.insert_tac facts THEN' eqsubst_tac occ inthms ));
|
paulson@15481
|
291 |
|
paulson@15481
|
292 |
|
dixon@15538
|
293 |
fun apply_subst_in_asm i th (cfvs, j, nprems, pth) rule m =
|
dixon@15538
|
294 |
(RWInst.rw m rule pth)
|
dixon@15538
|
295 |
|> Thm.permute_prems 0 ~1
|
dixon@15855
|
296 |
|> IsaND.unfix_frees cfvs
|
dixon@15915
|
297 |
|> RWInst.beta_eta_contract
|
dixon@15538
|
298 |
|> (fn r => Tactic.dtac r i th);
|
paulson@15481
|
299 |
|
dixon@15538
|
300 |
(*
|
dixon@15538
|
301 |
? should I be using bicompose what if we match more than one
|
dixon@15538
|
302 |
assumption, even after instantiation ? (back will work, but it would
|
dixon@15538
|
303 |
be nice to avoid the redudent search)
|
paulson@15481
|
304 |
|
dixon@15538
|
305 |
something like...
|
dixon@15538
|
306 |
|> Thm.lift_rule (th, i)
|
dixon@15538
|
307 |
|> (fn r => Thm.bicompose false (false, r, Thm.nprems_of r - nprems) i th)
|
dixon@15538
|
308 |
|
dixon@15538
|
309 |
*)
|
dixon@15538
|
310 |
|
dixon@15538
|
311 |
|
dixon@15538
|
312 |
(* prepare to substitute within the j'th premise of subgoal i of gth,
|
dixon@15538
|
313 |
using a meta-level equation. Note that we assume rule has var indicies
|
dixon@15538
|
314 |
zero'd. Note that we also assume that premt is the j'th premice of
|
dixon@15538
|
315 |
subgoal i of gth. Note the repetition of work done for each
|
dixon@15538
|
316 |
assumption, i.e. this can be made more efficient for search over
|
dixon@15538
|
317 |
multiple assumptions. *)
|
dixon@15538
|
318 |
fun prep_subst_in_asm searchf i gth j =
|
paulson@15481
|
319 |
let
|
paulson@15481
|
320 |
val th = Thm.incr_indexes 1 gth;
|
paulson@15481
|
321 |
val tgt_term = Thm.prop_of th;
|
paulson@15481
|
322 |
|
paulson@15481
|
323 |
val sgn = Thm.sign_of_thm th;
|
paulson@15481
|
324 |
val ctermify = Thm.cterm_of sgn;
|
paulson@15481
|
325 |
val trivify = Thm.trivial o ctermify;
|
paulson@15481
|
326 |
|
paulson@15481
|
327 |
val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term;
|
paulson@15481
|
328 |
val cfvs = rev (map ctermify fvs);
|
paulson@15481
|
329 |
|
dixon@15538
|
330 |
val asmt = Library.nth_elem(j - 1,(Logic.strip_imp_prems fixedbody));
|
dixon@15538
|
331 |
val asm_nprems = length (Logic.strip_imp_prems asmt);
|
dixon@15538
|
332 |
|
dixon@15538
|
333 |
val pth = trivify asmt;
|
dixon@15538
|
334 |
val maxidx = Term.maxidx_of_term asmt;
|
dixon@15538
|
335 |
|
paulson@15481
|
336 |
in
|
dixon@15538
|
337 |
((cfvs, j, asm_nprems, pth),
|
dixon@15538
|
338 |
(fn lhs => (searchf sgn maxidx lhs
|
dixon@15538
|
339 |
((IsaFTerm.focus_right
|
dixon@15538
|
340 |
o IsaFTerm.fcterm_of_term
|
dixon@15538
|
341 |
o Thm.prop_of) pth))))
|
paulson@15481
|
342 |
end;
|
paulson@15481
|
343 |
|
dixon@15538
|
344 |
(* prepare subst in every possible assumption *)
|
dixon@15538
|
345 |
fun prep_subst_in_asms searchf i gth =
|
dixon@15538
|
346 |
Seq.map
|
dixon@15538
|
347 |
(prep_subst_in_asm searchf i gth)
|
dixon@15538
|
348 |
(Seq.of_list (IsaPLib.mk_num_list
|
dixon@15538
|
349 |
(length (Logic.prems_of_goal (Thm.prop_of gth) i))));
|
dixon@15538
|
350 |
|
dixon@15538
|
351 |
|
dixon@15538
|
352 |
(* substitute in an assumption using an object or meta level equality *)
|
dixon@15814
|
353 |
fun eqsubst_asm_tac' searchf instepthm i th =
|
dixon@15538
|
354 |
let
|
dixon@15814
|
355 |
val asmpreps = prep_subst_in_asms searchf i th;
|
dixon@15538
|
356 |
val stepthms =
|
dixon@15538
|
357 |
Seq.map Drule.zero_var_indexes
|
dixon@15538
|
358 |
(Seq.of_list (EqRuleData.prep_meta_eq instepthm))
|
dixon@15538
|
359 |
|
dixon@15538
|
360 |
fun rewrite_with_thm (asminfo, findmatchf) r =
|
dixon@15538
|
361 |
let val (lhs,_) = Logic.dest_equals (Thm.concl_of r);
|
dixon@15538
|
362 |
in (findmatchf lhs)
|
dixon@15538
|
363 |
:-> (apply_subst_in_asm i th asminfo r) end;
|
dixon@15538
|
364 |
in
|
dixon@15538
|
365 |
(asmpreps :-> (fn a => stepthms :-> rewrite_with_thm a))
|
paulson@15481
|
366 |
end;
|
paulson@15481
|
367 |
|
paulson@15481
|
368 |
(* substitute using one of the given theorems *)
|
dixon@15929
|
369 |
fun eqsubst_asm_tac occ instepthms i th =
|
dixon@15538
|
370 |
if Thm.nprems_of th < i then Seq.empty else
|
dixon@15814
|
371 |
(Seq.of_list instepthms)
|
dixon@15929
|
372 |
:-> (fn r => eqsubst_asm_tac' (skip_first_occs_search
|
dixon@15929
|
373 |
occ searchf_tlr_unify_valid) r i th);
|
paulson@15481
|
374 |
|
paulson@15481
|
375 |
(* inthms are the given arguments in Isar, and treated as eqstep with
|
paulson@15481
|
376 |
the first one, then the second etc *)
|
dixon@15929
|
377 |
fun eqsubst_asm_meth occ inthms =
|
paulson@15481
|
378 |
Method.METHOD
|
dixon@15538
|
379 |
(fn facts =>
|
dixon@15929
|
380 |
HEADGOAL (Method.insert_tac facts THEN' eqsubst_asm_tac occ inthms ));
|
paulson@15481
|
381 |
|
paulson@15481
|
382 |
(* combination method that takes a flag (true indicates that subst
|
paulson@15481
|
383 |
should be done to an assumption, false = apply to the conclusion of
|
paulson@15481
|
384 |
the goal) as well as the theorems to use *)
|
dixon@15929
|
385 |
fun meth ((asmflag, occ), inthms) ctxt =
|
dixon@15929
|
386 |
if asmflag then eqsubst_asm_meth occ inthms else eqsubst_meth occ inthms;
|
paulson@15481
|
387 |
|
paulson@15481
|
388 |
(* syntax for options, given "(asm)" will give back true, without
|
paulson@15481
|
389 |
gives back false *)
|
paulson@15481
|
390 |
val options_syntax =
|
paulson@15481
|
391 |
(Args.parens (Args.$$$ "asm") >> (K true)) ||
|
paulson@15481
|
392 |
(Scan.succeed false);
|
dixon@15929
|
393 |
val ith_syntax =
|
dixon@15929
|
394 |
(Args.parens ((Args.$$$ "occ") |-- Args.nat))
|
dixon@15929
|
395 |
|| (Scan.succeed 0);
|
paulson@15481
|
396 |
|
paulson@15481
|
397 |
(* method syntax, first take options, then theorems *)
|
paulson@15481
|
398 |
fun meth_syntax meth src ctxt =
|
paulson@15481
|
399 |
meth (snd (Method.syntax ((Scan.lift options_syntax)
|
dixon@15929
|
400 |
-- (Scan.lift ith_syntax)
|
paulson@15481
|
401 |
-- Attrib.local_thms) src ctxt))
|
paulson@15481
|
402 |
ctxt;
|
paulson@15481
|
403 |
|
paulson@15481
|
404 |
(* setup function for adding method to theory. *)
|
paulson@15481
|
405 |
val setup =
|
paulson@15481
|
406 |
[Method.add_method ("subst", meth_syntax meth, "Substiution with an equation. Use \"(asm)\" option to substitute in an assumption.")];
|
paulson@15481
|
407 |
|
paulson@15481
|
408 |
end; |