author | haftmann |
Tue, 18 Sep 2007 07:36:14 +0200 | |
changeset 24623 | 7b2bc73405b8 |
parent 24244 | d7ee11ba1534 |
child 24862 | 6b7258da912b |
permissions | -rw-r--r-- |
8364 | 1 |
(* Title: Pure/Isar/rule_cases.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
5 |
Annotations and local contexts of rules. |
8364 | 6 |
*) |
7 |
||
18188 | 8 |
infix 1 THEN_ALL_NEW_CASES; |
9 |
||
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
10 |
signature BASIC_RULE_CASES = |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
11 |
sig |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
12 |
type cases |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
13 |
type cases_tactic |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
14 |
val CASES: cases -> tactic -> cases_tactic |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
15 |
val NO_CASES: tactic -> cases_tactic |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
16 |
val SUBGOAL_CASES: ((term * int) -> cases_tactic) -> int -> cases_tactic |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
17 |
val THEN_ALL_NEW_CASES: (int -> cases_tactic) * (int -> tactic) -> int -> cases_tactic |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
18 |
end |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
19 |
|
8364 | 20 |
signature RULE_CASES = |
21 |
sig |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
22 |
include BASIC_RULE_CASES |
18608 | 23 |
datatype T = Case of |
24 |
{fixes: (string * typ) list, |
|
25 |
assumes: (string * term list) list, |
|
26 |
binds: (indexname * term option) list, |
|
27 |
cases: (string * T) list} |
|
28 |
val strip_params: term -> (string * typ) list |
|
29 |
val make_common: bool -> theory * term -> (string * string list) list -> cases |
|
30 |
val make_nested: bool -> term -> theory * term -> (string * string list) list -> cases |
|
31 |
val make_simple: bool -> theory * term -> string -> cases |
|
32 |
val apply: term list -> T -> T |
|
18237 | 33 |
val consume: thm list -> thm list -> ('a * int) * thm -> |
34 |
(('a * (int * thm list)) * thm) Seq.seq |
|
18477 | 35 |
val add_consumes: int -> thm -> thm |
18728 | 36 |
val consumes: int -> attribute |
37 |
val consumes_default: int -> attribute |
|
8364 | 38 |
val name: string list -> thm -> thm |
18728 | 39 |
val case_names: string list -> attribute |
40 |
val case_conclusion: string * string list -> attribute |
|
12761 | 41 |
val save: thm -> thm -> thm |
18237 | 42 |
val get: thm -> (string * string list) list * int |
8427 | 43 |
val rename_params: string list list -> thm -> thm |
18728 | 44 |
val params: string list list -> attribute |
20289 | 45 |
val mutual_rule: Proof.context -> thm list -> (int list * thm) option |
46 |
val strict_mutual_rule: Proof.context -> thm list -> int list * thm |
|
8364 | 47 |
end; |
48 |
||
49 |
structure RuleCases: RULE_CASES = |
|
50 |
struct |
|
51 |
||
18608 | 52 |
(** cases **) |
53 |
||
54 |
datatype T = Case of |
|
55 |
{fixes: (string * typ) list, |
|
56 |
assumes: (string * term list) list, |
|
57 |
binds: (indexname * term option) list, |
|
58 |
cases: (string * T) list}; |
|
59 |
||
60 |
type cases = (string * T option) list; |
|
61 |
||
62 |
val case_conclN = "case"; |
|
63 |
val case_hypsN = "hyps"; |
|
64 |
val case_premsN = "prems"; |
|
65 |
||
20087 | 66 |
val strip_params = map (apfst (perhaps (try Name.dest_skolem))) o Logic.strip_params; |
18608 | 67 |
|
68 |
local |
|
69 |
||
70 |
fun abs xs t = Term.list_abs (xs, t); |
|
71 |
fun app us t = Term.betapplys (t, us); |
|
72 |
||
73 |
fun dest_binops cs tm = |
|
74 |
let |
|
75 |
val n = length cs; |
|
76 |
fun dest 0 _ = [] |
|
77 |
| dest 1 t = [t] |
|
78 |
| dest k (_ $ t $ u) = t :: dest (k - 1) u |
|
79 |
| dest _ _ = raise TERM ("Expected " ^ string_of_int n ^ " binop arguments", [tm]); |
|
80 |
in cs ~~ dest n tm end; |
|
81 |
||
82 |
fun extract_fixes NONE prop = (strip_params prop, []) |
|
83 |
| extract_fixes (SOME outline) prop = |
|
19012 | 84 |
chop (length (Logic.strip_params outline)) (strip_params prop); |
18608 | 85 |
|
86 |
fun extract_assumes _ NONE prop = ([("", Logic.strip_assums_hyp prop)], []) |
|
87 |
| extract_assumes qual (SOME outline) prop = |
|
88 |
let val (hyps, prems) = |
|
19012 | 89 |
chop (length (Logic.strip_assums_hyp outline)) (Logic.strip_assums_hyp prop) |
18608 | 90 |
in ([(qual case_hypsN, hyps)], [(qual case_premsN, prems)]) end; |
91 |
||
92 |
fun extract_case is_open thy (case_outline, raw_prop) name concls = |
|
93 |
let |
|
21745 | 94 |
val rename = if is_open then I else (apfst (Name.internal o Name.clean)); |
18608 | 95 |
|
96 |
val props = Logic.dest_conjunctions (Drule.norm_hhf thy raw_prop); |
|
97 |
val len = length props; |
|
98 |
val nested = is_some case_outline andalso len > 1; |
|
99 |
||
100 |
fun extract prop = |
|
101 |
let |
|
102 |
val (fixes1, fixes2) = extract_fixes case_outline prop |
|
103 |
|> apfst (map rename); |
|
104 |
val abs_fixes = abs (fixes1 @ fixes2); |
|
105 |
fun abs_fixes1 t = |
|
106 |
if not nested then abs_fixes t |
|
107 |
else abs fixes1 (app (map (Term.dummy_pattern o #2) fixes2) (abs fixes2 t)); |
|
108 |
||
109 |
val (assumes1, assumes2) = extract_assumes (NameSpace.qualified name) case_outline prop |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19423
diff
changeset
|
110 |
|> pairself (map (apsnd (maps Logic.dest_conjunctions))); |
18608 | 111 |
|
112 |
val concl = ObjectLogic.drop_judgment thy (Logic.strip_assums_concl prop); |
|
113 |
val binds = |
|
114 |
(case_conclN, concl) :: dest_binops concls concl |
|
115 |
|> map (fn (x, t) => ((x, 0), SOME (abs_fixes t))); |
|
116 |
in |
|
117 |
((fixes1, map (apsnd (map abs_fixes1)) assumes1), |
|
118 |
((fixes2, map (apsnd (map abs_fixes)) assumes2), binds)) |
|
119 |
end; |
|
120 |
||
121 |
val cases = map extract props; |
|
122 |
||
123 |
fun common_case ((fixes1, assumes1), ((fixes2, assumes2), binds)) = |
|
124 |
Case {fixes = fixes1 @ fixes2, assumes = assumes1 @ assumes2, binds = binds, cases = []}; |
|
125 |
fun inner_case (_, ((fixes2, assumes2), binds)) = |
|
126 |
Case {fixes = fixes2, assumes = assumes2, binds = binds, cases = []}; |
|
127 |
fun nested_case ((fixes1, assumes1), _) = |
|
128 |
Case {fixes = fixes1, assumes = assumes1, binds = [], |
|
129 |
cases = map string_of_int (1 upto len) ~~ map inner_case cases}; |
|
130 |
in |
|
131 |
if len = 0 then NONE |
|
132 |
else if len = 1 then SOME (common_case (hd cases)) |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
133 |
else if is_none case_outline orelse length (distinct (op =) (map fst cases)) > 1 then NONE |
18608 | 134 |
else SOME (nested_case (hd cases)) |
135 |
end; |
|
136 |
||
137 |
fun make is_open rule_struct (thy, prop) cases = |
|
138 |
let |
|
139 |
val n = length cases; |
|
21576 | 140 |
val nprems = Logic.count_prems prop; |
18608 | 141 |
fun add_case (name, concls) (cs, i) = |
142 |
((case try (fn () => |
|
143 |
(Option.map (curry Logic.nth_prem i) rule_struct, Logic.nth_prem (i, prop))) () of |
|
144 |
NONE => (name, NONE) |
|
145 |
| SOME p => (name, extract_case is_open thy p name concls)) :: cs, i - 1); |
|
146 |
in fold_rev add_case (Library.drop (n - nprems, cases)) ([], n) |> #1 end; |
|
147 |
||
148 |
in |
|
149 |
||
150 |
fun make_common is_open = make is_open NONE; |
|
151 |
fun make_nested is_open rule_struct = make is_open (SOME rule_struct); |
|
152 |
fun make_simple is_open (thy, prop) name = [(name, extract_case is_open thy (NONE, prop) "" [])]; |
|
153 |
||
154 |
fun apply args = |
|
155 |
let |
|
156 |
fun appl (Case {fixes, assumes, binds, cases}) = |
|
157 |
let |
|
158 |
val assumes' = map (apsnd (map (app args))) assumes; |
|
159 |
val binds' = map (apsnd (Option.map (app args))) binds; |
|
160 |
val cases' = map (apsnd appl) cases; |
|
161 |
in Case {fixes = fixes, assumes = assumes', binds = binds', cases = cases'} end; |
|
162 |
in appl end; |
|
163 |
||
164 |
end; |
|
165 |
||
166 |
||
167 |
||
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
168 |
(** tactics with cases **) |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
169 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
170 |
type cases_tactic = thm -> (cases * thm) Seq.seq; |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
171 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
172 |
fun CASES cases tac st = Seq.map (pair cases) (tac st); |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
173 |
fun NO_CASES tac = CASES [] tac; |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
174 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
175 |
fun SUBGOAL_CASES tac i st = |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
176 |
(case try Logic.nth_prem (i, Thm.prop_of st) of |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
177 |
SOME goal => tac (goal, i) st |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
178 |
| NONE => Seq.empty); |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
179 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
180 |
fun (tac1 THEN_ALL_NEW_CASES tac2) i st = |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
181 |
st |> tac1 i |> Seq.maps (fn (cases, st') => |
18237 | 182 |
CASES cases (Seq.INTERVAL tac2 i (i + nprems_of st' - nprems_of st)) st'); |
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
183 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
184 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
185 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
186 |
(** consume facts **) |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
187 |
|
18477 | 188 |
local |
189 |
||
190 |
fun unfold_prems n defs th = |
|
191 |
if null defs then th |
|
23584 | 192 |
else Conv.fconv_rule (Conv.prems_conv n (MetaSimplifier.rewrite true defs)) th; |
18477 | 193 |
|
194 |
fun unfold_prems_concls defs th = |
|
195 |
if null defs orelse not (can Logic.dest_conjunction (Thm.concl_of th)) then th |
|
196 |
else |
|
22900 | 197 |
Conv.fconv_rule |
23418 | 198 |
(Conv.concl_conv ~1 (Conjunction.convs |
23584 | 199 |
(Conv.prems_conv ~1 (MetaSimplifier.rewrite true defs)))) th; |
18477 | 200 |
|
201 |
in |
|
202 |
||
203 |
fun consume defs facts ((xx, n), th) = |
|
18237 | 204 |
let val m = Int.min (length facts, n) in |
18477 | 205 |
th |
206 |
|> unfold_prems n defs |
|
207 |
|> unfold_prems_concls defs |
|
18237 | 208 |
|> Drule.multi_resolve (Library.take (m, facts)) |
18477 | 209 |
|> Seq.map (pair (xx, (n - m, Library.drop (m, facts)))) |
18237 | 210 |
end; |
10811 | 211 |
|
18477 | 212 |
end; |
213 |
||
10811 | 214 |
val consumes_tagN = "consumes"; |
215 |
||
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
216 |
fun lookup_consumes th = |
23657 | 217 |
(case AList.lookup (op =) (Thm.get_tags th) (consumes_tagN) of |
218 |
NONE => NONE |
|
219 |
| SOME s => |
|
24244 | 220 |
(case Lexicon.read_nat s of SOME n => SOME n |
23657 | 221 |
| _ => raise THM ("Malformed 'consumes' tag of theorem", 0, [th]))); |
10530 | 222 |
|
18477 | 223 |
fun get_consumes th = the_default 0 (lookup_consumes th); |
224 |
||
15531 | 225 |
fun put_consumes NONE th = th |
226 |
| put_consumes (SOME n) th = th |
|
18799 | 227 |
|> PureThy.untag_rule consumes_tagN |
18909
f1333b0ff9e5
consumes: negative argument relative to total number of prems;
wenzelm
parents:
18799
diff
changeset
|
228 |
|> PureThy.tag_rule |
23657 | 229 |
(consumes_tagN, Library.string_of_int (if n < 0 then Thm.nprems_of th + n else n)); |
10530 | 230 |
|
18477 | 231 |
fun add_consumes k th = put_consumes (SOME (k + get_consumes th)) th; |
232 |
||
12761 | 233 |
val save_consumes = put_consumes o lookup_consumes; |
234 |
||
18728 | 235 |
fun consumes n x = Thm.rule_attribute (K (put_consumes (SOME n))) x; |
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
20289
diff
changeset
|
236 |
|
16148
5f15a14122dc
make: T option -- actually remove undefined cases;
wenzelm
parents:
15973
diff
changeset
|
237 |
fun consumes_default n x = |
21395
f34ac19659ae
moved some fundamental concepts to General/basics.ML;
wenzelm
parents:
20289
diff
changeset
|
238 |
if is_some (lookup_consumes (#2 x)) then x else consumes n x; |
8364 | 239 |
|
240 |
||
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
241 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
242 |
(** case names **) |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
243 |
|
23657 | 244 |
val implode_args = space_implode ";"; |
245 |
val explode_args = space_explode ";"; |
|
246 |
||
18237 | 247 |
val case_names_tagN = "case_names"; |
8364 | 248 |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
249 |
fun add_case_names NONE = I |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
250 |
| add_case_names (SOME names) = |
18799 | 251 |
PureThy.untag_rule case_names_tagN |
23657 | 252 |
#> PureThy.tag_rule (case_names_tagN, implode_args names); |
8364 | 253 |
|
23657 | 254 |
fun lookup_case_names th = |
255 |
AList.lookup (op =) (Thm.get_tags th) case_names_tagN |
|
256 |
|> Option.map explode_args; |
|
10530 | 257 |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
258 |
val save_case_names = add_case_names o lookup_case_names; |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
259 |
val name = add_case_names o SOME; |
18728 | 260 |
fun case_names ss = Thm.rule_attribute (K (name ss)); |
8364 | 261 |
|
10530 | 262 |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
263 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
264 |
(** case conclusions **) |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
265 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
266 |
val case_concl_tagN = "case_conclusion"; |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
267 |
|
23657 | 268 |
fun get_case_concl name (a, b) = |
269 |
if a = case_concl_tagN then |
|
270 |
(case explode_args b of c :: cs => if c = name then SOME cs else NONE) |
|
271 |
else NONE; |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
272 |
|
21646
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
wenzelm
parents:
21576
diff
changeset
|
273 |
fun add_case_concl (name, cs) = Thm.map_tags (fn tags => |
23657 | 274 |
filter_out (is_some o get_case_concl name) tags @ |
275 |
[(case_concl_tagN, implode_args (name :: cs))]); |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
276 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
277 |
fun get_case_concls th name = |
23657 | 278 |
these (get_first (get_case_concl name) (Thm.get_tags th)); |
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
279 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
280 |
fun save_case_concls th = |
21646
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
wenzelm
parents:
21576
diff
changeset
|
281 |
let val concls = Thm.get_tags th |> map_filter |
23657 | 282 |
(fn (a, b) => |
283 |
if a = case_concl_tagN then (case explode_args b of c :: cs => SOME (c, cs) | _ => NONE) |
|
284 |
else NONE) |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
285 |
in fold add_case_concl concls end; |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
286 |
|
18728 | 287 |
fun case_conclusion concl = Thm.rule_attribute (fn _ => add_case_concl concl); |
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
288 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
289 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
290 |
|
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
291 |
(** case declarations **) |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
292 |
|
10530 | 293 |
(* access hints *) |
294 |
||
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
295 |
fun save th = save_consumes th #> save_case_names th #> save_case_concls th; |
12761 | 296 |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
297 |
fun get th = |
12761 | 298 |
let |
18477 | 299 |
val n = get_consumes th; |
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
300 |
val cases = |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
301 |
(case lookup_case_names th of |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
302 |
NONE => map (rpair [] o Library.string_of_int) (1 upto (Thm.nprems_of th - n)) |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
303 |
| SOME names => map (fn name => (name, get_case_concls th name)) names); |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
304 |
in (cases, n) end; |
10530 | 305 |
|
8364 | 306 |
|
8427 | 307 |
(* params *) |
8364 | 308 |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
309 |
fun rename_params xss th = |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
310 |
th |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
311 |
|> fold_index (fn (i, xs) => Thm.rename_params_rule (xs, i + 1)) xss |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
312 |
|> save th; |
8364 | 313 |
|
18728 | 314 |
fun params xss = Thm.rule_attribute (K (rename_params xss)); |
8364 | 315 |
|
18477 | 316 |
|
317 |
||
318 |
(** mutual_rule **) |
|
319 |
||
320 |
local |
|
321 |
||
322 |
fun equal_cterms ts us = |
|
323 |
list_ord (Term.fast_term_ord o pairself Thm.term_of) (ts, us) = EQUAL; |
|
324 |
||
19917 | 325 |
fun prep_rule n th = |
18477 | 326 |
let |
19917 | 327 |
val th' = Thm.permute_prems 0 n th; |
18477 | 328 |
val prems = Library.take (Thm.nprems_of th' - n, Drule.cprems_of th'); |
329 |
val th'' = Drule.implies_elim_list th' (map Thm.assume prems); |
|
330 |
in (prems, (n, th'')) end; |
|
331 |
||
332 |
in |
|
333 |
||
19917 | 334 |
fun mutual_rule _ [] = NONE |
335 |
| mutual_rule _ [th] = SOME ([0], th) |
|
336 |
| mutual_rule ctxt (ths as th :: _) = |
|
18477 | 337 |
let |
22568
ed7aa5a350ef
renamed Variable.import to import_thms (avoid clash with Alice keywords);
wenzelm
parents:
21745
diff
changeset
|
338 |
val ((_, ths'), ctxt') = Variable.import_thms true ths ctxt; |
19917 | 339 |
val rules as (prems, _) :: _ = map (prep_rule (get_consumes th)) ths'; |
340 |
val (ns, rls) = split_list (map #2 rules); |
|
18477 | 341 |
in |
342 |
if not (forall (equal_cterms prems o #1) rules) then NONE |
|
343 |
else |
|
344 |
SOME (ns, |
|
19917 | 345 |
rls |
23418 | 346 |
|> Conjunction.intr_balanced |
18477 | 347 |
|> Drule.implies_intr_list prems |
19917 | 348 |
|> singleton (Variable.export ctxt' ctxt) |
349 |
|> save th |
|
18477 | 350 |
|> put_consumes (SOME 0)) |
351 |
end; |
|
352 |
||
353 |
end; |
|
354 |
||
19917 | 355 |
fun strict_mutual_rule ctxt ths = |
356 |
(case mutual_rule ctxt ths of |
|
18581 | 357 |
NONE => error "Failed to join given rules into one mutual rule" |
358 |
| SOME res => res); |
|
359 |
||
8364 | 360 |
end; |
17113 | 361 |
|
18229
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
362 |
structure BasicRuleCases: BASIC_RULE_CASES = RuleCases; |
e5a624483a23
added type cases/cases_tactic, and CASES, SUBGOAL_CASES;
wenzelm
parents:
18188
diff
changeset
|
363 |
open BasicRuleCases; |