8364
|
1 |
(* Title: Pure/Isar/rule_cases.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
8807
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
8364
|
5 |
|
|
6 |
Manage local contexts of rules.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature RULE_CASES =
|
|
10 |
sig
|
10530
|
11 |
val consumes: int -> 'a attribute
|
|
12 |
val consumes_default: int -> 'a attribute
|
8364
|
13 |
val name: string list -> thm -> thm
|
8427
|
14 |
val case_names: string list -> 'a attribute
|
10530
|
15 |
val get: thm -> string list * int
|
|
16 |
val add: thm -> thm * (string list * int)
|
|
17 |
val save: thm -> thm -> thm
|
10811
|
18 |
type T
|
9290
|
19 |
val make: bool -> thm -> string list -> (string * T) list
|
8427
|
20 |
val rename_params: string list list -> thm -> thm
|
8364
|
21 |
val params: string list list -> 'a attribute
|
|
22 |
end;
|
|
23 |
|
|
24 |
structure RuleCases: RULE_CASES =
|
|
25 |
struct
|
|
26 |
|
10811
|
27 |
(* names *)
|
|
28 |
|
|
29 |
val consumes_tagN = "consumes";
|
|
30 |
val cases_tagN = "cases";
|
|
31 |
val case_conclN = "case";
|
|
32 |
|
|
33 |
|
10530
|
34 |
(* number of consumed facts *)
|
8364
|
35 |
|
10811
|
36 |
fun lookup_consumes thm = Library.assoc (Thm.tags_of_thm thm, consumes_tagN);
|
8364
|
37 |
|
10530
|
38 |
fun get_consumes thm =
|
|
39 |
let fun err () = raise THM ("Malformed 'consumes' tag of theorem", 0, [thm]) in
|
|
40 |
(case lookup_consumes thm of
|
|
41 |
None => 0
|
|
42 |
| Some [s] => (case Syntax.read_nat s of Some n => n | _ => err ())
|
|
43 |
| _ => err ())
|
|
44 |
end;
|
|
45 |
|
10811
|
46 |
fun put_consumes n = Drule.tag_rule (consumes_tagN, [Library.string_of_int n]);
|
10530
|
47 |
val save_consumes = put_consumes o get_consumes;
|
|
48 |
|
|
49 |
fun consumes n x = Drule.rule_attribute (K (put_consumes n)) x;
|
|
50 |
fun consumes_default n x = if Library.is_some (lookup_consumes (#2 x)) then x else consumes n x;
|
8364
|
51 |
|
|
52 |
|
|
53 |
(* case names *)
|
|
54 |
|
|
55 |
fun name names thm =
|
|
56 |
thm
|
10811
|
57 |
|> Drule.untag_rule cases_tagN
|
|
58 |
|> Drule.tag_rule (cases_tagN, names);
|
8364
|
59 |
|
10811
|
60 |
fun get_case_names thm = Library.assocs (Thm.tags_of_thm thm) cases_tagN;
|
10530
|
61 |
val save_case_names = name o get_case_names;
|
|
62 |
|
8427
|
63 |
fun case_names ss = Drule.rule_attribute (K (name ss));
|
8364
|
64 |
|
10530
|
65 |
|
|
66 |
(* access hints *)
|
|
67 |
|
|
68 |
fun get thm = (get_case_names thm, get_consumes thm);
|
8364
|
69 |
fun add thm = (thm, get thm);
|
10530
|
70 |
|
|
71 |
fun save thm = save_case_names thm o save_consumes thm;
|
8364
|
72 |
|
|
73 |
|
|
74 |
(* prepare cases *)
|
|
75 |
|
10811
|
76 |
type T = {fixes: (string * typ) list, assumes: term list, binds: (indexname * term option) list};
|
10530
|
77 |
|
10407
|
78 |
fun prep_case open_parms thm name i =
|
8364
|
79 |
let
|
|
80 |
val (_, _, Bi, _) = Thm.dest_state (thm, i)
|
|
81 |
handle THM _ => raise THM ("More cases than premises in rule", 0, [thm]);
|
10811
|
82 |
val xs =
|
|
83 |
(rev (rename_wrt_term Bi (Logic.strip_params Bi))) (* FIXME avoid rename_wrt_term? *)
|
|
84 |
|> map (if open_parms then I else apfst Syntax.internal);
|
|
85 |
val asms = map (curry Term.list_abs xs) (Logic.strip_assums_hyp Bi);
|
|
86 |
val concl_bind = ((case_conclN, 0),
|
|
87 |
Some (Term.list_abs (xs, AutoBind.drop_judgment (Logic.strip_assums_concl Bi))));
|
|
88 |
in (name, {fixes = xs, assumes = asms, binds = [concl_bind]}) end;
|
8364
|
89 |
|
10811
|
90 |
fun make open_parms raw_thm names =
|
|
91 |
let val thm = Tactic.norm_hhf raw_thm in
|
|
92 |
#1 (foldr (fn (name, (cases, i)) => (prep_case open_parms thm name i :: cases, i - 1))
|
|
93 |
(Library.drop (length names - Thm.nprems_of thm, names), ([], Thm.nprems_of thm)))
|
|
94 |
end;
|
8364
|
95 |
|
|
96 |
|
8427
|
97 |
(* params *)
|
8364
|
98 |
|
|
99 |
fun rename_params xss thm =
|
8636
|
100 |
#1 (foldl (fn ((th, i), xs) => (Thm.rename_params_rule (xs, i) th, i + 1)) ((thm, 1), xss))
|
10530
|
101 |
|> save thm;
|
8364
|
102 |
|
|
103 |
fun params xss = Drule.rule_attribute (K (rename_params xss));
|
|
104 |
|
|
105 |
|
|
106 |
end;
|