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
|
12166
|
19 |
val empty: T
|
9290
|
20 |
val make: bool -> thm -> string list -> (string * T) list
|
8427
|
21 |
val rename_params: string list list -> thm -> thm
|
8364
|
22 |
val params: string list list -> 'a attribute
|
|
23 |
end;
|
|
24 |
|
|
25 |
structure RuleCases: RULE_CASES =
|
|
26 |
struct
|
|
27 |
|
10811
|
28 |
(* names *)
|
|
29 |
|
|
30 |
val consumes_tagN = "consumes";
|
|
31 |
val cases_tagN = "cases";
|
|
32 |
val case_conclN = "case";
|
|
33 |
|
|
34 |
|
10530
|
35 |
(* number of consumed facts *)
|
8364
|
36 |
|
10811
|
37 |
fun lookup_consumes thm = Library.assoc (Thm.tags_of_thm thm, consumes_tagN);
|
8364
|
38 |
|
10530
|
39 |
fun get_consumes thm =
|
|
40 |
let fun err () = raise THM ("Malformed 'consumes' tag of theorem", 0, [thm]) in
|
|
41 |
(case lookup_consumes thm of
|
|
42 |
None => 0
|
|
43 |
| Some [s] => (case Syntax.read_nat s of Some n => n | _ => err ())
|
|
44 |
| _ => err ())
|
|
45 |
end;
|
|
46 |
|
11993
|
47 |
fun put_consumes n =
|
|
48 |
Drule.tag_rule (consumes_tagN, [Library.string_of_int n]) o Drule.untag_rule consumes_tagN;
|
10530
|
49 |
val save_consumes = put_consumes o get_consumes;
|
|
50 |
|
|
51 |
fun consumes n x = Drule.rule_attribute (K (put_consumes n)) x;
|
|
52 |
fun consumes_default n x = if Library.is_some (lookup_consumes (#2 x)) then x else consumes n x;
|
8364
|
53 |
|
|
54 |
|
|
55 |
(* case names *)
|
|
56 |
|
|
57 |
fun name names thm =
|
|
58 |
thm
|
10811
|
59 |
|> Drule.untag_rule cases_tagN
|
|
60 |
|> Drule.tag_rule (cases_tagN, names);
|
8364
|
61 |
|
10811
|
62 |
fun get_case_names thm = Library.assocs (Thm.tags_of_thm thm) cases_tagN;
|
10530
|
63 |
val save_case_names = name o get_case_names;
|
|
64 |
|
8427
|
65 |
fun case_names ss = Drule.rule_attribute (K (name ss));
|
8364
|
66 |
|
10530
|
67 |
|
|
68 |
(* access hints *)
|
|
69 |
|
|
70 |
fun get thm = (get_case_names thm, get_consumes thm);
|
8364
|
71 |
fun add thm = (thm, get thm);
|
10530
|
72 |
|
|
73 |
fun save thm = save_case_names thm o save_consumes thm;
|
8364
|
74 |
|
|
75 |
|
|
76 |
(* prepare cases *)
|
|
77 |
|
10811
|
78 |
type T = {fixes: (string * typ) list, assumes: term list, binds: (indexname * term option) list};
|
10530
|
79 |
|
12166
|
80 |
val empty = {fixes = [], assumes = [], binds = []};
|
|
81 |
|
11983
|
82 |
fun prep_case open_parms thm name i =
|
8364
|
83 |
let
|
|
84 |
val (_, _, Bi, _) = Thm.dest_state (thm, i)
|
|
85 |
handle THM _ => raise THM ("More cases than premises in rule", 0, [thm]);
|
10830
|
86 |
val xs = map (if open_parms then I else apfst Syntax.internal) (Logic.strip_params Bi);
|
10811
|
87 |
val asms = map (curry Term.list_abs xs) (Logic.strip_assums_hyp Bi);
|
10872
|
88 |
val concl = Term.list_abs (xs, Logic.strip_assums_concl Bi);
|
11983
|
89 |
val bind = ((case_conclN, 0), Some (ObjectLogic.drop_judgment (Thm.sign_of_thm thm) concl));
|
10872
|
90 |
in (name, {fixes = xs, assumes = asms, binds = [bind]}) end;
|
8364
|
91 |
|
11983
|
92 |
fun make open_parms raw_thm names =
|
10811
|
93 |
let val thm = Tactic.norm_hhf raw_thm in
|
11983
|
94 |
#1 (foldr (fn (name, (cases, i)) => (prep_case open_parms thm name i :: cases, i - 1))
|
|
95 |
(Library.drop (length names - Thm.nprems_of thm, names), ([], length names)))
|
10811
|
96 |
end;
|
8364
|
97 |
|
|
98 |
|
8427
|
99 |
(* params *)
|
8364
|
100 |
|
11002
|
101 |
fun rename_params xss thm = foldln Thm.rename_params_rule xss thm
|
10530
|
102 |
|> save thm;
|
8364
|
103 |
|
|
104 |
fun params xss = Drule.rule_attribute (K (rename_params xss));
|
|
105 |
|
|
106 |
|
|
107 |
end;
|