6442
|
1 |
(* Title: HOL/Tools/induct_method.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
8376
|
5 |
Proof by cases and induction on types and sets.
|
6442
|
6 |
*)
|
|
7 |
|
|
8 |
signature INDUCT_METHOD =
|
|
9 |
sig
|
8376
|
10 |
val dest_global_rules: theory ->
|
|
11 |
{type_cases: (string * thm) list, set_cases: (string * thm) list,
|
|
12 |
type_induct: (string * thm) list, set_induct: (string * thm) list}
|
8308
|
13 |
val print_global_rules: theory -> unit
|
8376
|
14 |
val dest_local_rules: Proof.context ->
|
|
15 |
{type_cases: (string * thm) list, set_cases: (string * thm) list,
|
|
16 |
type_induct: (string * thm) list, set_induct: (string * thm) list}
|
8308
|
17 |
val print_local_rules: Proof.context -> unit
|
8431
|
18 |
val vars_of: term -> term list
|
8308
|
19 |
val cases_type_global: string -> theory attribute
|
|
20 |
val cases_set_global: string -> theory attribute
|
|
21 |
val cases_type_local: string -> Proof.context attribute
|
|
22 |
val cases_set_local: string -> Proof.context attribute
|
|
23 |
val induct_type_global: string -> theory attribute
|
|
24 |
val induct_set_global: string -> theory attribute
|
|
25 |
val induct_type_local: string -> Proof.context attribute
|
|
26 |
val induct_set_local: string -> Proof.context attribute
|
8337
|
27 |
val con_elim_tac: simpset -> tactic
|
|
28 |
val con_elim_solved_tac: simpset -> tactic
|
6442
|
29 |
val setup: (theory -> theory) list
|
|
30 |
end;
|
|
31 |
|
|
32 |
structure InductMethod: INDUCT_METHOD =
|
|
33 |
struct
|
|
34 |
|
8337
|
35 |
|
8308
|
36 |
(** global and local induct data **)
|
6442
|
37 |
|
8308
|
38 |
(* rules *)
|
|
39 |
|
|
40 |
type rules = (string * thm) NetRules.T;
|
|
41 |
|
|
42 |
fun eq_rule ((s1:string, th1), (s2, th2)) = s1 = s2 andalso Thm.eq_thm (th1, th2);
|
|
43 |
|
|
44 |
val type_rules = NetRules.init eq_rule (Thm.concl_of o #2);
|
|
45 |
val set_rules = NetRules.init eq_rule (Thm.major_prem_of o #2);
|
|
46 |
|
|
47 |
fun lookup_rule (rs:rules) name = Library.assoc (NetRules.rules rs, name);
|
|
48 |
|
|
49 |
fun print_rules kind rs =
|
|
50 |
let val thms = map snd (NetRules.rules rs)
|
8315
|
51 |
in Pretty.writeln (Pretty.big_list kind (map Display.pretty_thm thms)) end;
|
8308
|
52 |
|
|
53 |
|
|
54 |
(* theory data kind 'HOL/induct_method' *)
|
|
55 |
|
|
56 |
structure GlobalInductArgs =
|
|
57 |
struct
|
|
58 |
val name = "HOL/induct_method";
|
|
59 |
type T = (rules * rules) * (rules * rules);
|
|
60 |
|
|
61 |
val empty = ((type_rules, set_rules), (type_rules, set_rules));
|
|
62 |
val copy = I;
|
|
63 |
val prep_ext = I;
|
|
64 |
fun merge (((casesT1, casesS1), (inductT1, inductS1)),
|
|
65 |
((casesT2, casesS2), (inductT2, inductS2))) =
|
|
66 |
((NetRules.merge (casesT1, casesT2), NetRules.merge (casesS1, casesS2)),
|
|
67 |
(NetRules.merge (inductT1, inductT2), NetRules.merge (inductS1, inductS2)));
|
|
68 |
|
|
69 |
fun print _ ((casesT, casesS), (inductT, inductS)) =
|
8315
|
70 |
(print_rules "type cases:" casesT;
|
|
71 |
print_rules "set cases:" casesS;
|
|
72 |
print_rules "type induct:" inductT;
|
|
73 |
print_rules "set induct:" inductS);
|
8376
|
74 |
|
|
75 |
fun dest ((casesT, casesS), (inductT, inductS)) =
|
|
76 |
{type_cases = NetRules.rules casesT,
|
|
77 |
set_cases = NetRules.rules casesS,
|
|
78 |
type_induct = NetRules.rules inductT,
|
|
79 |
set_induct = NetRules.rules inductS};
|
8308
|
80 |
end;
|
|
81 |
|
|
82 |
structure GlobalInduct = TheoryDataFun(GlobalInductArgs);
|
|
83 |
val print_global_rules = GlobalInduct.print;
|
8376
|
84 |
val dest_global_rules = GlobalInductArgs.dest o GlobalInduct.get;
|
8308
|
85 |
|
|
86 |
|
|
87 |
(* proof data kind 'HOL/induct_method' *)
|
|
88 |
|
|
89 |
structure LocalInductArgs =
|
|
90 |
struct
|
|
91 |
val name = "HOL/induct_method";
|
|
92 |
type T = GlobalInductArgs.T;
|
8278
|
93 |
|
8308
|
94 |
fun init thy = GlobalInduct.get thy;
|
|
95 |
fun print x = GlobalInductArgs.print x;
|
|
96 |
end;
|
|
97 |
|
|
98 |
structure LocalInduct = ProofDataFun(LocalInductArgs);
|
|
99 |
val print_local_rules = LocalInduct.print;
|
8376
|
100 |
val dest_local_rules = GlobalInductArgs.dest o LocalInduct.get;
|
8308
|
101 |
|
|
102 |
|
|
103 |
(* access rules *)
|
|
104 |
|
|
105 |
val get_cases = #1 o LocalInduct.get;
|
|
106 |
val get_induct = #2 o LocalInduct.get;
|
|
107 |
|
|
108 |
val lookup_casesT = lookup_rule o #1 o get_cases;
|
|
109 |
val lookup_casesS = lookup_rule o #2 o get_cases;
|
|
110 |
val lookup_inductT = lookup_rule o #1 o get_induct;
|
|
111 |
val lookup_inductS = lookup_rule o #2 o get_induct;
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
(** attributes **)
|
|
116 |
|
|
117 |
local
|
|
118 |
|
|
119 |
fun mk_att f g name (x, thm) = (f (g (name, thm)) x, thm);
|
|
120 |
|
|
121 |
fun add_casesT rule x = apfst (apfst (NetRules.insert rule)) x;
|
|
122 |
fun add_casesS rule x = apfst (apsnd (NetRules.insert rule)) x;
|
|
123 |
fun add_inductT rule x = apsnd (apfst (NetRules.insert rule)) x;
|
|
124 |
fun add_inductS rule x = apsnd (apsnd (NetRules.insert rule)) x;
|
|
125 |
|
|
126 |
in
|
|
127 |
|
|
128 |
val cases_type_global = mk_att GlobalInduct.map add_casesT;
|
|
129 |
val cases_set_global = mk_att GlobalInduct.map add_casesS;
|
|
130 |
val induct_type_global = mk_att GlobalInduct.map add_inductT;
|
|
131 |
val induct_set_global = mk_att GlobalInduct.map add_inductS;
|
|
132 |
|
|
133 |
val cases_type_local = mk_att LocalInduct.map add_casesT;
|
|
134 |
val cases_set_local = mk_att LocalInduct.map add_casesS;
|
|
135 |
val induct_type_local = mk_att LocalInduct.map add_inductT;
|
|
136 |
val induct_set_local = mk_att LocalInduct.map add_inductS;
|
|
137 |
|
|
138 |
end;
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
(** misc utils **)
|
8278
|
143 |
|
8344
|
144 |
(* terms *)
|
|
145 |
|
8278
|
146 |
fun vars_of tm = (*ordered left-to-right, preferring right!*)
|
8308
|
147 |
Term.foldl_aterms (fn (ts, t as Var _) => t :: ts | (ts, _) => ts) ([], tm)
|
8278
|
148 |
|> Library.distinct |> rev;
|
|
149 |
|
8308
|
150 |
fun type_name t =
|
|
151 |
#1 (Term.dest_Type (Term.type_of t))
|
|
152 |
handle TYPE _ => raise TERM ("Bad type of term argument", [t]);
|
8278
|
153 |
|
|
154 |
|
8337
|
155 |
(* simplifying cases rules *)
|
|
156 |
|
|
157 |
local
|
|
158 |
|
|
159 |
(*delete needless equality assumptions*)
|
|
160 |
val refl_thin = prove_goal HOL.thy "!!P. [| a=a; P |] ==> P"
|
|
161 |
(fn _ => [assume_tac 1]);
|
|
162 |
|
|
163 |
val elim_rls = [asm_rl, FalseE, refl_thin, conjE, exE, Pair_inject];
|
|
164 |
|
|
165 |
val elim_tac = REPEAT o Tactic.eresolve_tac elim_rls;
|
|
166 |
|
|
167 |
fun simp_case_tac ss =
|
|
168 |
EVERY' [elim_tac, asm_full_simp_tac ss, elim_tac, REPEAT o bound_hyp_subst_tac];
|
|
169 |
|
|
170 |
in
|
|
171 |
|
|
172 |
fun con_elim_tac ss = ALLGOALS (simp_case_tac ss) THEN prune_params_tac;
|
|
173 |
|
|
174 |
fun con_elim_solved_tac ss =
|
|
175 |
ALLGOALS (fn i => TRY (simp_case_tac ss i THEN_MAYBE no_tac)) THEN prune_params_tac;
|
|
176 |
|
|
177 |
end;
|
|
178 |
|
|
179 |
|
8278
|
180 |
|
|
181 |
(** cases method **)
|
|
182 |
|
8308
|
183 |
(*
|
|
184 |
rule selection:
|
|
185 |
cases - classical case split
|
8376
|
186 |
cases t - datatype exhaustion
|
|
187 |
<x:A> cases ... - set elimination
|
8451
|
188 |
... cases ... R - explicit rule
|
8308
|
189 |
*)
|
8278
|
190 |
|
8344
|
191 |
local
|
|
192 |
|
8308
|
193 |
fun cases_var thm =
|
|
194 |
(case try (hd o vars_of o hd o Logic.strip_assums_hyp o Library.last_elem o Thm.prems_of) thm of
|
|
195 |
None => raise THM ("Malformed cases rule", 0, [thm])
|
|
196 |
| Some x => x);
|
|
197 |
|
8337
|
198 |
fun simplify_cases ctxt =
|
|
199 |
Tactic.rule_by_tactic (con_elim_solved_tac (Simplifier.get_local_simpset ctxt));
|
|
200 |
|
8344
|
201 |
fun cases_tac (ctxt, (simplified, args)) facts =
|
8308
|
202 |
let
|
|
203 |
val sg = ProofContext.sign_of ctxt;
|
|
204 |
val cert = Thm.cterm_of sg;
|
8278
|
205 |
|
8308
|
206 |
fun inst_rule t thm =
|
|
207 |
Drule.cterm_instantiate [(cert (cases_var thm), cert t)] thm;
|
6442
|
208 |
|
8376
|
209 |
val cond_simp = if simplified then simplify_cases ctxt else I;
|
|
210 |
|
|
211 |
fun find_cases th =
|
|
212 |
NetRules.may_unify (#2 (get_cases ctxt))
|
|
213 |
(Logic.strip_assums_concl (#prop (Thm.rep_thm th)));
|
|
214 |
|
|
215 |
val rules =
|
8344
|
216 |
(case (args, facts) of
|
8400
|
217 |
((None, None), []) => [RuleCases.none case_split_thm] (* FIXME add cases!? *)
|
8376
|
218 |
| ((Some t, None), []) =>
|
8308
|
219 |
let val name = type_name t in
|
|
220 |
(case lookup_casesT ctxt name of
|
|
221 |
None => error ("No cases rule for type: " ^ quote name)
|
8376
|
222 |
| Some thm => [(inst_rule t thm, RuleCases.get thm)])
|
8308
|
223 |
end
|
8376
|
224 |
| ((None, None), th :: _) => map (RuleCases.add o #2) (find_cases th)
|
|
225 |
| ((Some t, None), th :: _) =>
|
|
226 |
(case find_cases th of (*may instantiate first rule only!*)
|
|
227 |
(_, thm) :: _ => [(inst_rule t thm, RuleCases.get thm)]
|
|
228 |
| [] => [])
|
|
229 |
| ((None, Some thm), _) => [RuleCases.add thm]
|
|
230 |
| ((Some t, Some thm), _) => [(inst_rule t thm, RuleCases.get thm)]);
|
|
231 |
|
|
232 |
fun prep_rule (thm, cases) =
|
|
233 |
Seq.map (rpair cases o cond_simp) (Method.multi_resolves facts [thm]);
|
|
234 |
in Method.resolveq_cases_tac (Seq.flat (Seq.map prep_rule (Seq.of_list rules))) end;
|
8278
|
235 |
|
8344
|
236 |
in
|
|
237 |
|
8376
|
238 |
val cases_meth = Method.METHOD_CASES o (FINDGOAL oo cases_tac);
|
8278
|
239 |
|
8344
|
240 |
end;
|
|
241 |
|
8278
|
242 |
|
|
243 |
|
|
244 |
(** induct method **)
|
|
245 |
|
8308
|
246 |
(*
|
|
247 |
rule selection:
|
|
248 |
induct - mathematical induction
|
8376
|
249 |
induct x - datatype induction
|
|
250 |
<x:A> induct ... - set induction
|
8451
|
251 |
... induct ... R - explicit rule
|
8308
|
252 |
*)
|
8278
|
253 |
|
8344
|
254 |
local
|
|
255 |
|
8376
|
256 |
infix 1 THEN_ALL_NEW_CASES;
|
|
257 |
|
|
258 |
fun (tac1 THEN_ALL_NEW_CASES tac2) i st =
|
|
259 |
st |> Seq.THEN (tac1 i, (fn (st', cases) =>
|
|
260 |
Seq.map (rpair cases) (ALLGOALS_RANGE tac2 i (i + nprems_of st' - nprems_of st) st')));
|
|
261 |
|
|
262 |
|
8330
|
263 |
fun induct_rule ctxt t =
|
|
264 |
let val name = type_name t in
|
|
265 |
(case lookup_inductT ctxt name of
|
|
266 |
None => error ("No induct rule for type: " ^ quote name)
|
8332
|
267 |
| Some thm => (name, thm))
|
8330
|
268 |
end;
|
|
269 |
|
8332
|
270 |
fun join_rules [(_, thm)] = thm
|
8330
|
271 |
| join_rules raw_thms =
|
|
272 |
let
|
8332
|
273 |
val thms = (map (apsnd Drule.freeze_all) raw_thms);
|
|
274 |
fun eq_prems ((_, th1), (_, th2)) =
|
|
275 |
Term.aconvs (Thm.prems_of th1, Thm.prems_of th2);
|
8330
|
276 |
in
|
8332
|
277 |
(case Library.gen_distinct eq_prems thms of
|
|
278 |
[(_, thm)] =>
|
|
279 |
let
|
|
280 |
val cprems = Drule.cprems_of thm;
|
|
281 |
val asms = map Thm.assume cprems;
|
|
282 |
fun strip (_, th) = Drule.implies_elim_list th asms;
|
|
283 |
in
|
|
284 |
foldr1 (fn (th, th') => [th, th'] MRS conjI) (map strip thms)
|
|
285 |
|> Drule.implies_intr_list cprems
|
|
286 |
|> Drule.standard
|
|
287 |
end
|
|
288 |
| [] => error "No rule given"
|
|
289 |
| bads => error ("Incompatible rules for " ^ commas_quote (map #1 bads)))
|
8330
|
290 |
end;
|
|
291 |
|
8376
|
292 |
|
8344
|
293 |
fun induct_tac (ctxt, (stripped, args)) facts =
|
8308
|
294 |
let
|
|
295 |
val sg = ProofContext.sign_of ctxt;
|
|
296 |
val cert = Thm.cterm_of sg;
|
|
297 |
|
|
298 |
fun prep_inst (concl, ts) =
|
|
299 |
let val xs = vars_of concl; val n = length xs - length ts in
|
|
300 |
if n < 0 then error "More arguments given than in induction rule"
|
|
301 |
else map cert (Library.drop (n, xs)) ~~ map cert ts
|
|
302 |
end;
|
8278
|
303 |
|
8308
|
304 |
fun inst_rule insts thm =
|
|
305 |
Drule.cterm_instantiate (flat (map2 prep_inst
|
|
306 |
(HOLogic.dest_conj (HOLogic.dest_Trueprop (Thm.concl_of thm)), insts))) thm;
|
8278
|
307 |
|
8376
|
308 |
fun find_induct th =
|
|
309 |
NetRules.may_unify (#2 (get_induct ctxt))
|
|
310 |
(Logic.strip_assums_concl (#prop (Thm.rep_thm th)));
|
|
311 |
|
|
312 |
val rules =
|
8308
|
313 |
(case (args, facts) of
|
8376
|
314 |
(([], None), []) => [RuleCases.none nat_induct] (* FIXME add cases!? *)
|
|
315 |
| ((insts, None), []) =>
|
|
316 |
let val thms = map (induct_rule ctxt o last_elem) insts
|
|
317 |
in [(inst_rule insts (join_rules thms), RuleCases.get (#2 (hd thms)))] end
|
|
318 |
| (([], None), th :: _) => map (RuleCases.add o #2) (find_induct th)
|
|
319 |
| ((insts, None), th :: _) =>
|
|
320 |
(case find_induct th of (*may instantiate first rule only!*)
|
|
321 |
(_, thm) :: _ => [(inst_rule insts thm, RuleCases.get thm)]
|
|
322 |
| [] => [])
|
|
323 |
| (([], Some thm), _) => [RuleCases.add thm]
|
|
324 |
| ((insts, Some thm), _) => [(inst_rule insts thm, RuleCases.get thm)]);
|
|
325 |
|
|
326 |
fun prep_rule (thm, cases) =
|
|
327 |
Seq.map (rpair cases) (Method.multi_resolves facts [thm]);
|
|
328 |
val tac = Method.resolveq_cases_tac (Seq.flat (Seq.map prep_rule (Seq.of_list rules)));
|
8344
|
329 |
in
|
8376
|
330 |
if stripped then tac THEN_ALL_NEW_CASES (REPEAT o resolve_tac [impI, allI, ballI])
|
|
331 |
else tac
|
8344
|
332 |
end;
|
|
333 |
|
|
334 |
in
|
8278
|
335 |
|
8376
|
336 |
val induct_meth = Method.METHOD_CASES o (FINDGOAL oo induct_tac);
|
8278
|
337 |
|
8344
|
338 |
end;
|
|
339 |
|
8278
|
340 |
|
|
341 |
|
|
342 |
(** concrete syntax **)
|
|
343 |
|
8308
|
344 |
val casesN = "cases";
|
|
345 |
val inductN = "induct";
|
8344
|
346 |
|
8337
|
347 |
val simplifiedN = "simplified";
|
8344
|
348 |
val strippedN = "stripped";
|
|
349 |
|
8308
|
350 |
val typeN = "type";
|
|
351 |
val setN = "set";
|
|
352 |
val ruleN = "rule";
|
|
353 |
|
|
354 |
|
|
355 |
(* attributes *)
|
|
356 |
|
|
357 |
fun spec k = (Args.$$$ k -- Args.$$$ ":") |-- Args.!!! Args.name;
|
|
358 |
|
|
359 |
fun attrib sign_of add_type add_set = Scan.depend (fn x =>
|
|
360 |
let val sg = sign_of x in
|
|
361 |
spec typeN >> (add_type o Sign.intern_tycon sg) ||
|
|
362 |
spec setN >> (add_set o Sign.intern_const sg)
|
|
363 |
end >> pair x);
|
|
364 |
|
|
365 |
val cases_attr =
|
|
366 |
(Attrib.syntax (attrib Theory.sign_of cases_type_global cases_set_global),
|
|
367 |
Attrib.syntax (attrib ProofContext.sign_of cases_type_local cases_set_local));
|
|
368 |
|
|
369 |
val induct_attr =
|
|
370 |
(Attrib.syntax (attrib Theory.sign_of induct_type_global induct_set_global),
|
|
371 |
Attrib.syntax (attrib ProofContext.sign_of induct_type_local induct_set_local));
|
|
372 |
|
|
373 |
|
|
374 |
(* methods *)
|
|
375 |
|
8278
|
376 |
local
|
6442
|
377 |
|
8308
|
378 |
fun err k get name =
|
|
379 |
(case get name of Some x => x
|
|
380 |
| None => error ("No rule for " ^ k ^ " " ^ quote name));
|
6442
|
381 |
|
8308
|
382 |
fun rule get_type get_set =
|
|
383 |
Scan.depend (fn ctxt =>
|
|
384 |
let val sg = ProofContext.sign_of ctxt in
|
|
385 |
spec typeN >> (err typeN (get_type ctxt) o Sign.intern_tycon sg) ||
|
|
386 |
spec setN >> (err setN (get_set ctxt) o Sign.intern_const sg)
|
|
387 |
end >> pair ctxt) ||
|
|
388 |
Scan.lift (Args.$$$ ruleN -- Args.$$$ ":") |-- Attrib.local_thm;
|
6442
|
389 |
|
8308
|
390 |
val cases_rule = rule lookup_casesT lookup_casesS;
|
|
391 |
val induct_rule = rule lookup_inductT lookup_inductS;
|
6442
|
392 |
|
8308
|
393 |
val kind = (Args.$$$ typeN || Args.$$$ setN || Args.$$$ ruleN) -- Args.$$$ ":";
|
|
394 |
val term = Scan.unless (Scan.lift kind) Args.local_term;
|
6446
|
395 |
|
8344
|
396 |
fun mode name =
|
|
397 |
Scan.lift (Scan.optional (Args.$$$ name -- Args.$$$ ":" >> K true) false);
|
8337
|
398 |
|
8278
|
399 |
in
|
|
400 |
|
8344
|
401 |
val cases_args =
|
|
402 |
Method.syntax (mode simplifiedN -- (Scan.option term -- Scan.option cases_rule));
|
|
403 |
val induct_args =
|
|
404 |
Method.syntax (mode strippedN -- (Args.and_list (Scan.repeat1 term) -- Scan.option induct_rule));
|
8278
|
405 |
|
|
406 |
end;
|
6446
|
407 |
|
|
408 |
|
6442
|
409 |
|
8278
|
410 |
(** theory setup **)
|
6446
|
411 |
|
8278
|
412 |
val setup =
|
8308
|
413 |
[GlobalInduct.init, LocalInduct.init,
|
|
414 |
Attrib.add_attributes
|
|
415 |
[(casesN, cases_attr, "cases rule for type or set"),
|
|
416 |
(inductN, induct_attr, "induction rule for type or set")],
|
|
417 |
Method.add_methods
|
|
418 |
[("cases", cases_meth oo cases_args, "case analysis on types or sets"),
|
|
419 |
("induct", induct_meth oo induct_args, "induction on types or sets")]];
|
6442
|
420 |
|
|
421 |
end;
|