author | wenzelm |
Tue, 31 Jan 2006 18:19:34 +0100 | |
changeset 18878 | 08b06ec0ef74 |
parent 18843 | 23db974a0575 |
child 18908 | fb080097e436 |
permissions | -rw-r--r-- |
5820 | 1 |
(* Title: Pure/Isar/proof.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
15757 | 5 |
The Isar/VM proof language interpreter. |
5820 | 6 |
*) |
7 |
||
8 |
signature PROOF = |
|
9 |
sig |
|
17756 | 10 |
type context (*= Context.proof*) |
11 |
type method (*= Method.method*) |
|
5820 | 12 |
type state |
17359 | 13 |
val init: context -> state |
14 |
val level: state -> int |
|
15 |
val assert_bottom: bool -> state -> state |
|
5820 | 16 |
val context_of: state -> context |
17 |
val theory_of: state -> theory |
|
16450 | 18 |
val sign_of: state -> theory (*obsolete*) |
13377 | 19 |
val map_context: (context -> context) -> state -> state |
7924 | 20 |
val warn_extra_tfrees: state -> state -> state |
17976 | 21 |
val add_binds_i: (indexname * term option) list -> state -> state |
17359 | 22 |
val put_thms: string * thm list option -> state -> state |
6091 | 23 |
val the_facts: state -> thm list |
9469 | 24 |
val the_fact: state -> thm |
17450 | 25 |
val put_facts: thm list option -> state -> state |
6891 | 26 |
val assert_forward: state -> state |
17359 | 27 |
val assert_chain: state -> state |
9469 | 28 |
val assert_forward_or_chain: state -> state |
5820 | 29 |
val assert_backward: state -> state |
8206 | 30 |
val assert_no_chain: state -> state |
5820 | 31 |
val enter_forward: state -> state |
17359 | 32 |
val get_goal: state -> context * (thm list * thm) |
16539 | 33 |
val show_main_goal: bool ref |
6529 | 34 |
val verbose: bool ref |
12423 | 35 |
val pretty_state: int -> state -> Pretty.T list |
10360 | 36 |
val pretty_goals: bool -> state -> Pretty.T list |
17112 | 37 |
val refine: Method.text -> state -> state Seq.seq |
38 |
val refine_end: Method.text -> state -> state Seq.seq |
|
17359 | 39 |
val refine_goals: (context -> thm -> unit) -> context -> thm list -> state -> state Seq.seq |
5936 | 40 |
val match_bind: (string list * string) list -> state -> state |
41 |
val match_bind_i: (term list * term) list -> state -> state |
|
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
42 |
val let_bind: (string list * string) list -> state -> state |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
43 |
val let_bind_i: (term list * term) list -> state -> state |
18670 | 44 |
val fix: (string * string option) list -> state -> state |
45 |
val fix_i: (string * typ option) list -> state -> state |
|
46 |
val assm: ProofContext.export -> |
|
17359 | 47 |
((string * Attrib.src list) * (string * (string list * string list)) list) list -> |
48 |
state -> state |
|
18670 | 49 |
val assm_i: ProofContext.export -> |
18728 | 50 |
((string * attribute list) * (term * (term list * term list)) list) list |
17359 | 51 |
-> state -> state |
52 |
val assume: ((string * Attrib.src list) * (string * (string list * string list)) list) list -> |
|
53 |
state -> state |
|
18728 | 54 |
val assume_i: ((string * attribute list) * (term * (term list * term list)) list) list |
17359 | 55 |
-> state -> state |
56 |
val presume: ((string * Attrib.src list) * (string * (string list * string list)) list) list |
|
57 |
-> state -> state |
|
18728 | 58 |
val presume_i: ((string * attribute list) * (term * (term list * term list)) list) list |
17359 | 59 |
-> state -> state |
18308 | 60 |
val def: ((string * Attrib.src list) * (string * (string * string list))) list -> |
61 |
state -> state |
|
18728 | 62 |
val def_i: ((string * attribute list) * (string * (term * term list))) list -> |
18308 | 63 |
state -> state |
17359 | 64 |
val chain: state -> state |
65 |
val chain_facts: thm list -> state -> state |
|
66 |
val get_thmss: state -> (thmref * Attrib.src list) list -> thm list |
|
14564 | 67 |
val simple_note_thms: string -> thm list -> state -> state |
17359 | 68 |
val note_thmss: ((string * Attrib.src list) * |
17112 | 69 |
(thmref * Attrib.src list) list) list -> state -> state |
18728 | 70 |
val note_thmss_i: ((string * attribute list) * |
71 |
(thm list * attribute list) list) list -> state -> state |
|
17112 | 72 |
val from_thmss: ((thmref * Attrib.src list) list) list -> state -> state |
18728 | 73 |
val from_thmss_i: ((thm list * attribute list) list) list -> state -> state |
17112 | 74 |
val with_thmss: ((thmref * Attrib.src list) list) list -> state -> state |
18728 | 75 |
val with_thmss_i: ((thm list * attribute list) list) list -> state -> state |
18548 | 76 |
val using: ((thmref * Attrib.src list) list) list -> state -> state |
18728 | 77 |
val using_i: ((thm list * attribute list) list) list -> state -> state |
18548 | 78 |
val unfolding: ((thmref * Attrib.src list) list) list -> state -> state |
18728 | 79 |
val unfolding_i: ((thm list * attribute list) list) list -> state -> state |
17112 | 80 |
val invoke_case: string * string option list * Attrib.src list -> state -> state |
18728 | 81 |
val invoke_case_i: string * string option list * attribute list -> state -> state |
17359 | 82 |
val begin_block: state -> state |
83 |
val next_block: state -> state |
|
84 |
val end_block: state -> state Seq.seq |
|
17112 | 85 |
val proof: Method.text option -> state -> state Seq.seq |
86 |
val defer: int option -> state -> state Seq.seq |
|
87 |
val prefer: int -> state -> state Seq.seq |
|
88 |
val apply: Method.text -> state -> state Seq.seq |
|
89 |
val apply_end: Method.text -> state -> state Seq.seq |
|
17359 | 90 |
val goal_names: string option -> string -> string list -> string |
91 |
val local_goal: (context -> ((string * string) * (string * thm list) list) -> unit) -> |
|
18728 | 92 |
(theory -> 'a -> attribute) -> |
17359 | 93 |
(context * 'b list -> context * (term list list * (context -> context))) -> |
18124 | 94 |
string -> Method.text option -> (thm list list -> state -> state Seq.seq) -> |
17359 | 95 |
((string * 'a list) * 'b) list -> state -> state |
96 |
val local_qed: Method.text option * bool -> state -> state Seq.seq |
|
97 |
val global_goal: (context -> (string * string) * (string * thm list) list -> unit) -> |
|
18728 | 98 |
(theory -> 'a -> attribute) -> |
17450 | 99 |
(context * 'b list -> context * (term list list * (context -> context))) -> |
18124 | 100 |
string -> Method.text option -> (thm list list -> theory -> theory) -> |
17859 | 101 |
string option -> string * 'a list -> ((string * 'a list) * 'b) list -> context -> state |
18808 | 102 |
val global_qed: Method.text option * bool -> state -> context * theory |
17359 | 103 |
val local_terminal_proof: Method.text * Method.text option -> state -> state Seq.seq |
104 |
val local_default_proof: state -> state Seq.seq |
|
105 |
val local_immediate_proof: state -> state Seq.seq |
|
106 |
val local_done_proof: state -> state Seq.seq |
|
107 |
val local_skip_proof: bool -> state -> state Seq.seq |
|
18808 | 108 |
val global_terminal_proof: Method.text * Method.text option -> state -> context * theory |
109 |
val global_default_proof: state -> context * theory |
|
110 |
val global_immediate_proof: state -> context * theory |
|
111 |
val global_done_proof: state -> context * theory |
|
112 |
val global_skip_proof: bool -> state -> context * theory |
|
18124 | 113 |
val have: Method.text option -> (thm list list -> state -> state Seq.seq) -> |
17359 | 114 |
((string * Attrib.src list) * (string * (string list * string list)) list) list -> |
115 |
bool -> state -> state |
|
18124 | 116 |
val have_i: Method.text option -> (thm list list -> state -> state Seq.seq) -> |
18728 | 117 |
((string * attribute list) * (term * (term list * term list)) list) list -> |
17359 | 118 |
bool -> state -> state |
18124 | 119 |
val show: Method.text option -> (thm list list -> state -> state Seq.seq) -> |
17359 | 120 |
((string * Attrib.src list) * (string * (string list * string list)) list) list -> |
121 |
bool -> state -> state |
|
18124 | 122 |
val show_i: Method.text option -> (thm list list -> state -> state Seq.seq) -> |
18728 | 123 |
((string * attribute list) * (term * (term list * term list)) list) list -> |
17359 | 124 |
bool -> state -> state |
18124 | 125 |
val theorem: string -> Method.text option -> (thm list list -> theory -> theory) -> |
17359 | 126 |
string option -> string * Attrib.src list -> |
127 |
((string * Attrib.src list) * (string * (string list * string list)) list) list -> |
|
128 |
context -> state |
|
18124 | 129 |
val theorem_i: string -> Method.text option -> (thm list list -> theory -> theory) -> |
18728 | 130 |
string option -> string * attribute list -> |
131 |
((string * attribute list) * (term * (term list * term list)) list) list -> |
|
17359 | 132 |
context -> state |
5820 | 133 |
end; |
134 |
||
13377 | 135 |
structure Proof: PROOF = |
5820 | 136 |
struct |
137 |
||
16813 | 138 |
type context = ProofContext.context; |
17112 | 139 |
type method = Method.method; |
16813 | 140 |
|
5820 | 141 |
|
142 |
(** proof state **) |
|
143 |
||
17359 | 144 |
(* datatype state *) |
5820 | 145 |
|
17112 | 146 |
datatype mode = Forward | Chain | Backward; |
5820 | 147 |
|
17359 | 148 |
datatype state = |
149 |
State of node Stack.T |
|
150 |
and node = |
|
7176 | 151 |
Node of |
152 |
{context: context, |
|
153 |
facts: thm list option, |
|
154 |
mode: mode, |
|
17359 | 155 |
goal: goal option} |
156 |
and goal = |
|
157 |
Goal of |
|
158 |
{statement: string * term list list, (*goal kind and statement*) |
|
159 |
using: thm list, (*goal facts*) |
|
160 |
goal: thm, (*subgoals ==> statement*) |
|
17859 | 161 |
before_qed: Method.text option, |
18124 | 162 |
after_qed: |
163 |
(thm list list -> state -> state Seq.seq) * |
|
164 |
(thm list list -> theory -> theory)}; |
|
17359 | 165 |
|
17859 | 166 |
fun make_goal (statement, using, goal, before_qed, after_qed) = |
167 |
Goal {statement = statement, using = using, goal = goal, |
|
168 |
before_qed = before_qed, after_qed = after_qed}; |
|
5820 | 169 |
|
7176 | 170 |
fun make_node (context, facts, mode, goal) = |
171 |
Node {context = context, facts = facts, mode = mode, goal = goal}; |
|
172 |
||
17359 | 173 |
fun map_node f (Node {context, facts, mode, goal}) = |
174 |
make_node (f (context, facts, mode, goal)); |
|
5820 | 175 |
|
17359 | 176 |
fun init ctxt = State (Stack.init (make_node (ctxt, NONE, Forward, NONE))); |
5820 | 177 |
|
17359 | 178 |
fun current (State st) = Stack.top st |> (fn Node node => node); |
179 |
fun map_current f (State st) = State (Stack.map (map_node f) st); |
|
12045 | 180 |
|
5820 | 181 |
|
182 |
||
183 |
(** basic proof state operations **) |
|
184 |
||
17359 | 185 |
(* block structure *) |
186 |
||
187 |
fun open_block (State st) = State (Stack.push st); |
|
188 |
||
18678 | 189 |
fun close_block (State st) = State (Stack.pop st) |
190 |
handle Empty => error "Unbalanced block parentheses"; |
|
17359 | 191 |
|
192 |
fun level (State st) = Stack.level st; |
|
193 |
||
194 |
fun assert_bottom b state = |
|
195 |
let val b' = (level state <= 2) in |
|
18678 | 196 |
if b andalso not b' then error "Not at bottom of proof!" |
197 |
else if not b andalso b' then error "Already at bottom of proof!" |
|
17359 | 198 |
else state |
199 |
end; |
|
200 |
||
201 |
||
5820 | 202 |
(* context *) |
203 |
||
17359 | 204 |
val context_of = #context o current; |
5820 | 205 |
val theory_of = ProofContext.theory_of o context_of; |
16450 | 206 |
val sign_of = theory_of; |
5820 | 207 |
|
17359 | 208 |
fun map_context f = |
209 |
map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal)); |
|
5820 | 210 |
|
17359 | 211 |
fun map_context_result f state = |
17859 | 212 |
f (context_of state) ||> (fn ctxt => map_context (K ctxt) state); |
5820 | 213 |
|
7924 | 214 |
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of; |
10809 | 215 |
val add_binds_i = map_context o ProofContext.add_binds_i; |
6091 | 216 |
val put_thms = map_context o ProofContext.put_thms; |
8374 | 217 |
val get_case = ProofContext.get_case o context_of; |
5820 | 218 |
|
219 |
||
220 |
(* facts *) |
|
221 |
||
17359 | 222 |
val get_facts = #facts o current; |
223 |
||
224 |
fun the_facts state = |
|
225 |
(case get_facts state of SOME facts => facts |
|
18678 | 226 |
| NONE => error "No current facts available"); |
5820 | 227 |
|
9469 | 228 |
fun the_fact state = |
17359 | 229 |
(case the_facts state of [thm] => thm |
18678 | 230 |
| _ => error "Single theorem expected"); |
7605 | 231 |
|
17359 | 232 |
fun put_facts facts = |
233 |
map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) |
|
234 |
#> put_thms (AutoBind.thisN, facts); |
|
5820 | 235 |
|
17359 | 236 |
fun these_factss more_facts (named_factss, state) = |
237 |
(named_factss, state |> put_facts (SOME (List.concat (map snd named_factss) @ more_facts))); |
|
5820 | 238 |
|
17359 | 239 |
fun export_facts inner outer = |
240 |
(case get_facts inner of |
|
241 |
NONE => Seq.single (put_facts NONE outer) |
|
242 |
| SOME thms => |
|
243 |
thms |
|
18041
052622286158
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17976
diff
changeset
|
244 |
|> Seq.map_list (ProofContext.exports (context_of inner) (context_of outer)) |
17359 | 245 |
|> Seq.map (fn ths => put_facts (SOME ths) outer)); |
5820 | 246 |
|
247 |
||
248 |
(* mode *) |
|
249 |
||
17359 | 250 |
val get_mode = #mode o current; |
5820 | 251 |
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal)); |
252 |
||
17359 | 253 |
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove"); |
5820 | 254 |
|
255 |
fun assert_mode pred state = |
|
256 |
let val mode = get_mode state in |
|
257 |
if pred mode then state |
|
18678 | 258 |
else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode") |
5820 | 259 |
end; |
260 |
||
261 |
val assert_forward = assert_mode (equal Forward); |
|
17359 | 262 |
val assert_chain = assert_mode (equal Chain); |
17112 | 263 |
val assert_forward_or_chain = assert_mode (equal Forward orf equal Chain); |
5820 | 264 |
val assert_backward = assert_mode (equal Backward); |
17112 | 265 |
val assert_no_chain = assert_mode (not_equal Chain); |
5820 | 266 |
|
17359 | 267 |
val enter_forward = put_mode Forward; |
268 |
val enter_chain = put_mode Chain; |
|
269 |
val enter_backward = put_mode Backward; |
|
5820 | 270 |
|
17359 | 271 |
|
272 |
(* current goal *) |
|
273 |
||
274 |
fun current_goal state = |
|
275 |
(case current state of |
|
276 |
{context, goal = SOME (Goal goal), ...} => (context, goal) |
|
18678 | 277 |
| _ => error "No current goal!"); |
5820 | 278 |
|
17359 | 279 |
fun assert_current_goal g state = |
280 |
let val g' = can current_goal state in |
|
18678 | 281 |
if g andalso not g' then error "No goal in this block!" |
282 |
else if not g andalso g' then error "Goal present in this block!" |
|
17359 | 283 |
else state |
284 |
end; |
|
6776 | 285 |
|
17359 | 286 |
fun put_goal goal = map_current (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal)); |
287 |
||
17859 | 288 |
val before_qed = #before_qed o #2 o current_goal; |
289 |
||
17359 | 290 |
|
291 |
(* nested goal *) |
|
5820 | 292 |
|
17359 | 293 |
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) = |
294 |
let |
|
17859 | 295 |
val Goal {statement, using, goal, before_qed, after_qed} = goal; |
296 |
val goal' = make_goal (g (statement, using, goal, before_qed, after_qed)); |
|
17359 | 297 |
in State (make_node (f context, facts, mode, SOME goal'), nodes) end |
298 |
| map_goal f g (State (nd, node :: nodes)) = |
|
299 |
let val State (node', nodes') = map_goal f g (State (node, nodes)) |
|
300 |
in map_context f (State (nd, node' :: nodes')) end |
|
301 |
| map_goal _ _ state = state; |
|
5820 | 302 |
|
17859 | 303 |
fun using_facts using = map_goal I (fn (statement, _, goal, before_qed, after_qed) => |
304 |
(statement, using, goal, before_qed, after_qed)); |
|
17359 | 305 |
|
306 |
local |
|
307 |
fun find i state = |
|
308 |
(case try current_goal state of |
|
309 |
SOME (ctxt, goal) => (ctxt, (i, goal)) |
|
18678 | 310 |
| NONE => find (i + 1) (close_block state handle ERROR _ => error "No goal present")); |
17359 | 311 |
in val find_goal = find 0 end; |
312 |
||
313 |
fun get_goal state = |
|
314 |
let val (ctxt, (_, {using, goal, ...})) = find_goal state |
|
315 |
in (ctxt, (using, goal)) end; |
|
5820 | 316 |
|
317 |
||
318 |
||
12423 | 319 |
(** pretty_state **) |
5820 | 320 |
|
13869 | 321 |
val show_main_goal = ref false; |
16539 | 322 |
val verbose = ProofContext.verbose; |
13698 | 323 |
|
14876 | 324 |
val pretty_goals_local = Display.pretty_goals_aux o ProofContext.pp; |
12085 | 325 |
|
15531 | 326 |
fun pretty_facts _ _ NONE = [] |
327 |
| pretty_facts s ctxt (SOME ths) = |
|
12055 | 328 |
[Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""]; |
6756 | 329 |
|
17359 | 330 |
fun pretty_state nr state = |
5820 | 331 |
let |
17359 | 332 |
val {context, facts, mode, goal = _} = current state; |
333 |
val ref (_, _, bg) = Display.current_goals_markers; |
|
5820 | 334 |
|
335 |
fun levels_up 0 = "" |
|
17359 | 336 |
| levels_up 1 = "1 level up" |
337 |
| levels_up i = string_of_int i ^ " levels up"; |
|
5820 | 338 |
|
11556 | 339 |
fun subgoals 0 = "" |
17359 | 340 |
| subgoals 1 = "1 subgoal" |
341 |
| subgoals n = string_of_int n ^ " subgoals"; |
|
342 |
||
343 |
fun description strs = |
|
344 |
(case filter_out (equal "") strs of [] => "" |
|
345 |
| strs' => enclose " (" ")" (commas strs')); |
|
12146 | 346 |
|
17859 | 347 |
fun prt_goal (SOME (ctxt, (i, {statement = (kind, _), using, goal, before_qed, after_qed}))) = |
17359 | 348 |
pretty_facts "using " ctxt |
349 |
(if mode <> Backward orelse null using then NONE else SOME using) @ |
|
350 |
[Pretty.str ("goal" ^ description [kind, levels_up (i div 2), |
|
351 |
subgoals (Thm.nprems_of goal)] ^ ":")] @ |
|
352 |
pretty_goals_local ctxt bg (true, ! show_main_goal) (! Display.goals_limit) goal |
|
353 |
| prt_goal NONE = []; |
|
6848 | 354 |
|
17359 | 355 |
val prt_ctxt = |
356 |
if ! verbose orelse mode = Forward then ProofContext.pretty_context context |
|
18670 | 357 |
else if mode = Backward then ProofContext.pretty_ctxt context |
7575 | 358 |
else []; |
17359 | 359 |
in |
360 |
[Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^ |
|
361 |
(if ! verbose then ", depth " ^ string_of_int (level state div 2 - 1) else "")), |
|
362 |
Pretty.str ""] @ |
|
363 |
(if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @ |
|
364 |
(if ! verbose orelse mode = Forward then |
|
365 |
pretty_facts "" context facts @ prt_goal (try find_goal state) |
|
366 |
else if mode = Chain then pretty_facts "picking " context facts |
|
367 |
else prt_goal (try find_goal state)) |
|
368 |
end; |
|
5820 | 369 |
|
12085 | 370 |
fun pretty_goals main state = |
17359 | 371 |
let val (ctxt, (_, {goal, ...})) = find_goal state |
372 |
in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) goal end; |
|
10320 | 373 |
|
5820 | 374 |
|
375 |
||
376 |
(** proof steps **) |
|
377 |
||
17359 | 378 |
(* refine via method *) |
5820 | 379 |
|
8234 | 380 |
local |
381 |
||
16146 | 382 |
fun goalN i = "goal" ^ string_of_int i; |
383 |
fun goals st = map goalN (1 upto Thm.nprems_of st); |
|
384 |
||
385 |
fun no_goal_cases st = map (rpair NONE) (goals st); |
|
386 |
||
387 |
fun goal_cases st = |
|
18607 | 388 |
RuleCases.make_common true (Thm.theory_of_thm st, Thm.prop_of st) (map (rpair []) (goals st)); |
16146 | 389 |
|
16450 | 390 |
fun check_theory thy state = |
391 |
if subthy (thy, theory_of state) then state |
|
18678 | 392 |
else error ("Bad theory of method result: " ^ Context.str_of_thy thy); |
5820 | 393 |
|
17112 | 394 |
fun apply_method current_context meth_fun state = |
6848 | 395 |
let |
17859 | 396 |
val (goal_ctxt, (_, {statement, using, goal, before_qed, after_qed})) = find_goal state; |
17112 | 397 |
val meth = meth_fun (if current_context then context_of state else goal_ctxt); |
16146 | 398 |
in |
18228 | 399 |
Method.apply meth using goal |> Seq.map (fn (meth_cases, goal') => |
6848 | 400 |
state |
17359 | 401 |
|> check_theory (Thm.theory_of_thm goal') |
16146 | 402 |
|> map_goal |
18475 | 403 |
(ProofContext.add_cases false (no_goal_cases goal @ goal_cases goal') #> |
404 |
ProofContext.add_cases true meth_cases) |
|
17859 | 405 |
(K (statement, using, goal', before_qed, after_qed))) |
16146 | 406 |
end; |
5820 | 407 |
|
17112 | 408 |
fun apply_text cc text state = |
409 |
let |
|
410 |
val thy = theory_of state; |
|
411 |
||
412 |
fun eval (Method.Basic m) = apply_method cc m |
|
413 |
| eval (Method.Source src) = apply_method cc (Method.method thy src) |
|
414 |
| eval (Method.Then txts) = Seq.EVERY (map eval txts) |
|
415 |
| eval (Method.Orelse txts) = Seq.FIRST (map eval txts) |
|
416 |
| eval (Method.Try txt) = Seq.TRY (eval txt) |
|
417 |
| eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt); |
|
418 |
in eval text state end; |
|
419 |
||
8234 | 420 |
in |
421 |
||
17112 | 422 |
val refine = apply_text true; |
423 |
val refine_end = apply_text false; |
|
8234 | 424 |
|
425 |
end; |
|
426 |
||
5820 | 427 |
|
17359 | 428 |
(* refine via sub-proof *) |
429 |
||
430 |
local |
|
9469 | 431 |
|
11816 | 432 |
fun refine_tac rule = |
18186 | 433 |
Tactic.norm_hhf_tac THEN' Tactic.rtac rule THEN_ALL_NEW |
434 |
(Tactic.norm_hhf_tac THEN' (SUBGOAL (fn (goal, i) => |
|
435 |
if can Logic.unprotect (Logic.strip_assums_concl goal) then |
|
436 |
Tactic.etac Drule.protectI i |
|
437 |
else all_tac))); |
|
11816 | 438 |
|
17359 | 439 |
fun refine_goal print_rule inner raw_rule state = |
17859 | 440 |
let val (outer, (_, {statement, using, goal, before_qed, after_qed})) = find_goal state in |
17359 | 441 |
raw_rule |
18041
052622286158
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17976
diff
changeset
|
442 |
|> ProofContext.goal_exports inner outer |
17359 | 443 |
|> Seq.maps (fn rule => |
444 |
(print_rule outer rule; |
|
445 |
goal |
|
446 |
|> FINDGOAL (refine_tac rule) |
|
17859 | 447 |
|> Seq.map (fn goal' => |
448 |
map_goal I (K (statement, using, goal', before_qed, after_qed)) state))) |
|
17359 | 449 |
end; |
6932 | 450 |
|
17359 | 451 |
in |
12146 | 452 |
|
17359 | 453 |
fun refine_goals print_rule inner raw_rules = |
454 |
Seq.EVERY (map (refine_goal print_rule inner) raw_rules); |
|
455 |
||
456 |
end; |
|
6932 | 457 |
|
458 |
||
17359 | 459 |
(* conclude_goal *) |
6932 | 460 |
|
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
461 |
fun conclude_goal state goal propss = |
5820 | 462 |
let |
463 |
val ctxt = context_of state; |
|
464 |
||
17359 | 465 |
val ngoals = Thm.nprems_of goal; |
466 |
val _ = conditional (ngoals > 0) (fn () => |
|
18678 | 467 |
error (Pretty.string_of (Pretty.chunks |
17359 | 468 |
(pretty_goals_local ctxt "" (true, ! show_main_goal) (! Display.goals_limit) goal @ |
12085 | 469 |
[Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")])))); |
5820 | 470 |
|
17359 | 471 |
val {hyps, thy, ...} = Thm.rep_thm goal; |
17450 | 472 |
val bad_hyps = fold (remove (op aconv)) (ProofContext.assms_of ctxt) hyps; |
18678 | 473 |
val _ = conditional (not (null bad_hyps)) (fn () => error ("Additional hypotheses:\n" ^ |
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
474 |
cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps))); |
12146 | 475 |
|
17976 | 476 |
val th = Goal.conclude goal; |
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
477 |
val _ = conditional (not (Pattern.matches thy |
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
478 |
(Logic.mk_conjunction_list2 propss, Thm.prop_of th))) (fn () => |
18678 | 479 |
error ("Proved a different theorem:\n" ^ ProofContext.string_of_thm ctxt th)); |
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
480 |
in Drule.conj_elim_precise (map length propss) th end; |
5820 | 481 |
|
482 |
||
483 |
||
484 |
(*** structured proof commands ***) |
|
485 |
||
17112 | 486 |
(** context elements **) |
5820 | 487 |
|
17112 | 488 |
(* bindings *) |
5820 | 489 |
|
16813 | 490 |
local |
491 |
||
17359 | 492 |
fun gen_bind bind args state = |
5820 | 493 |
state |
494 |
|> assert_forward |
|
18308 | 495 |
|> map_context (bind args #> snd) |
17359 | 496 |
|> put_facts NONE; |
5820 | 497 |
|
16813 | 498 |
in |
499 |
||
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
500 |
val match_bind = gen_bind (ProofContext.match_bind false); |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
501 |
val match_bind_i = gen_bind (ProofContext.match_bind_i false); |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
502 |
val let_bind = gen_bind (ProofContext.match_bind true); |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
503 |
val let_bind_i = gen_bind (ProofContext.match_bind_i true); |
5820 | 504 |
|
16813 | 505 |
end; |
506 |
||
5820 | 507 |
|
17359 | 508 |
(* fix *) |
9196 | 509 |
|
12714 | 510 |
local |
511 |
||
18670 | 512 |
fun gen_fix add_fixes args = |
17359 | 513 |
assert_forward |
18670 | 514 |
#> map_context (snd o add_fixes (map Syntax.no_syn args)) |
17359 | 515 |
#> put_facts NONE; |
5820 | 516 |
|
16813 | 517 |
in |
518 |
||
18670 | 519 |
val fix = gen_fix ProofContext.add_fixes; |
520 |
val fix_i = gen_fix ProofContext.add_fixes_i; |
|
5820 | 521 |
|
16813 | 522 |
end; |
523 |
||
5820 | 524 |
|
17359 | 525 |
(* assume etc. *) |
5820 | 526 |
|
16813 | 527 |
local |
528 |
||
17112 | 529 |
fun gen_assume asm prep_att exp args state = |
5820 | 530 |
state |
531 |
|> assert_forward |
|
17112 | 532 |
|> map_context_result (asm exp (Attrib.map_specs (prep_att (theory_of state)) args)) |
17359 | 533 |
|> these_factss [] |> #2; |
6932 | 534 |
|
16813 | 535 |
in |
536 |
||
18728 | 537 |
val assm = gen_assume ProofContext.add_assms Attrib.attribute; |
18670 | 538 |
val assm_i = gen_assume ProofContext.add_assms_i (K I); |
539 |
val assume = assm ProofContext.assume_export; |
|
540 |
val assume_i = assm_i ProofContext.assume_export; |
|
541 |
val presume = assm ProofContext.presume_export; |
|
542 |
val presume_i = assm_i ProofContext.presume_export; |
|
5820 | 543 |
|
16813 | 544 |
end; |
545 |
||
7271 | 546 |
|
17359 | 547 |
(* def *) |
11891 | 548 |
|
16813 | 549 |
local |
550 |
||
18308 | 551 |
fun gen_def fix prep_att prep_binds args state = |
11891 | 552 |
let |
553 |
val _ = assert_forward state; |
|
17112 | 554 |
val thy = theory_of state; |
11891 | 555 |
|
18308 | 556 |
val ((raw_names, raw_atts), (xs, raw_rhss)) = args |> split_list |>> split_list ||> split_list; |
557 |
val names = map (fn ("", x) => Thm.def_name x | (name, _) => name) (raw_names ~~ xs); |
|
558 |
val atts = map (map (prep_att thy)) raw_atts; |
|
559 |
val (rhss, state') = state |> map_context_result (prep_binds false (map swap raw_rhss)); |
|
18826 | 560 |
val eqs = LocalDefs.mk_def (context_of state') (xs ~~ rhss); |
11891 | 561 |
in |
18308 | 562 |
state' |
18670 | 563 |
|> fix (map (rpair NONE) xs) |
18826 | 564 |
|> assm_i LocalDefs.def_export ((names ~~ atts) ~~ map (fn eq => [(eq, ([], []))]) eqs) |
11891 | 565 |
end; |
566 |
||
16813 | 567 |
in |
568 |
||
18728 | 569 |
val def = gen_def fix Attrib.attribute ProofContext.match_bind; |
18308 | 570 |
val def_i = gen_def fix_i (K I) ProofContext.match_bind_i; |
11891 | 571 |
|
16813 | 572 |
end; |
573 |
||
11891 | 574 |
|
8374 | 575 |
|
17112 | 576 |
(** facts **) |
5820 | 577 |
|
17359 | 578 |
(* chain *) |
5820 | 579 |
|
17359 | 580 |
val chain = |
581 |
assert_forward |
|
582 |
#> tap the_facts |
|
583 |
#> enter_chain; |
|
5820 | 584 |
|
17359 | 585 |
fun chain_facts facts = |
586 |
put_facts (SOME facts) |
|
587 |
#> chain; |
|
5820 | 588 |
|
589 |
||
17359 | 590 |
(* note etc. *) |
17112 | 591 |
|
17166 | 592 |
fun no_binding args = map (pair ("", [])) args; |
17112 | 593 |
|
594 |
local |
|
595 |
||
17359 | 596 |
fun gen_thmss note_ctxt more_facts opt_chain opt_result prep_atts args state = |
17112 | 597 |
state |
598 |
|> assert_forward |
|
599 |
|> map_context_result (note_ctxt (Attrib.map_facts (prep_atts (theory_of state)) args)) |
|
600 |
|> these_factss (more_facts state) |
|
17359 | 601 |
||> opt_chain |
602 |
|> opt_result; |
|
17112 | 603 |
|
604 |
in |
|
605 |
||
18728 | 606 |
val note_thmss = gen_thmss ProofContext.note_thmss (K []) I #2 Attrib.attribute; |
17359 | 607 |
val note_thmss_i = gen_thmss ProofContext.note_thmss_i (K []) I #2 (K I); |
17112 | 608 |
|
609 |
val from_thmss = |
|
18728 | 610 |
gen_thmss ProofContext.note_thmss (K []) chain #2 Attrib.attribute o no_binding; |
17359 | 611 |
val from_thmss_i = gen_thmss ProofContext.note_thmss_i (K []) chain #2 (K I) o no_binding; |
17112 | 612 |
|
613 |
val with_thmss = |
|
18728 | 614 |
gen_thmss ProofContext.note_thmss the_facts chain #2 Attrib.attribute o no_binding; |
17359 | 615 |
val with_thmss_i = gen_thmss ProofContext.note_thmss_i the_facts chain #2 (K I) o no_binding; |
616 |
||
18808 | 617 |
val local_results = |
618 |
gen_thmss ProofContext.note_thmss_i (K []) I I (K I) o map (apsnd Thm.simple_fact); |
|
619 |
fun global_results kind = PureThy.note_thmss_i kind o map (apsnd Thm.simple_fact); |
|
17359 | 620 |
|
621 |
fun get_thmss state srcs = the_facts (note_thmss [(("", []), srcs)] state); |
|
622 |
fun simple_note_thms name thms = note_thmss_i [((name, []), [(thms, [])])]; |
|
17112 | 623 |
|
624 |
end; |
|
625 |
||
626 |
||
18548 | 627 |
(* using/unfolding *) |
17112 | 628 |
|
629 |
local |
|
630 |
||
18548 | 631 |
fun gen_using f g note prep_atts args state = |
17112 | 632 |
state |
633 |
|> assert_backward |
|
17166 | 634 |
|> map_context_result (note (Attrib.map_facts (prep_atts (theory_of state)) (no_binding args))) |
18843 | 635 |
|> (fn (named_facts, state') => |
636 |
state' |> map_goal I (fn (statement, using, goal, before_qed, after_qed) => |
|
637 |
let |
|
638 |
val ctxt = context_of state'; |
|
639 |
val ths = List.concat (map snd named_facts); |
|
640 |
in (statement, f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end)); |
|
18548 | 641 |
|
18843 | 642 |
fun append_using _ ths using = using @ ths; |
18878 | 643 |
fun unfold_using ctxt ths = map (LocalDefs.unfold ctxt ths); |
644 |
val unfold_goals = LocalDefs.unfold_goals; |
|
17112 | 645 |
|
646 |
in |
|
647 |
||
18843 | 648 |
val using = gen_using append_using (K (K I)) ProofContext.note_thmss Attrib.attribute; |
649 |
val using_i = gen_using append_using (K (K I)) ProofContext.note_thmss_i (K I); |
|
18826 | 650 |
val unfolding = gen_using unfold_using unfold_goals ProofContext.note_thmss Attrib.attribute; |
651 |
val unfolding_i = gen_using unfold_using unfold_goals ProofContext.note_thmss_i (K I); |
|
17112 | 652 |
|
653 |
end; |
|
654 |
||
655 |
||
656 |
(* case *) |
|
657 |
||
658 |
local |
|
659 |
||
660 |
fun gen_invoke_case prep_att (name, xs, raw_atts) state = |
|
661 |
let |
|
662 |
val atts = map (prep_att (theory_of state)) raw_atts; |
|
18607 | 663 |
val (asms, state') = |
17112 | 664 |
map_context_result (ProofContext.apply_case (get_case state name xs)) state; |
18607 | 665 |
val assumptions = asms |> map (fn (a, ts) => ((a, atts), map (rpair ([], [])) ts)); |
17112 | 666 |
in |
667 |
state' |
|
17359 | 668 |
|> map_context (ProofContext.qualified_names #> ProofContext.no_base_names) |
17112 | 669 |
|> assume_i assumptions |
17976 | 670 |
|> add_binds_i AutoBind.no_facts |
17112 | 671 |
|> map_context (ProofContext.restore_naming (context_of state)) |
672 |
|> `the_facts |-> simple_note_thms name |
|
673 |
end; |
|
674 |
||
675 |
in |
|
676 |
||
18728 | 677 |
val invoke_case = gen_invoke_case Attrib.attribute; |
17112 | 678 |
val invoke_case_i = gen_invoke_case (K I); |
679 |
||
680 |
end; |
|
681 |
||
682 |
||
683 |
||
17359 | 684 |
(** proof structure **) |
685 |
||
686 |
(* blocks *) |
|
687 |
||
688 |
val begin_block = |
|
689 |
assert_forward |
|
690 |
#> open_block |
|
691 |
#> put_goal NONE |
|
692 |
#> open_block; |
|
693 |
||
694 |
val next_block = |
|
695 |
assert_forward |
|
696 |
#> close_block |
|
697 |
#> open_block |
|
698 |
#> put_goal NONE |
|
699 |
#> put_facts NONE; |
|
700 |
||
701 |
fun end_block state = |
|
702 |
state |
|
703 |
|> assert_forward |
|
704 |
|> close_block |
|
705 |
|> assert_current_goal false |
|
706 |
|> close_block |
|
707 |
|> export_facts state; |
|
708 |
||
709 |
||
710 |
(* sub-proofs *) |
|
711 |
||
712 |
fun proof opt_text = |
|
713 |
assert_backward |
|
17859 | 714 |
#> refine (the_default Method.default_text opt_text) |
17359 | 715 |
#> Seq.map (using_facts [] #> enter_forward); |
716 |
||
717 |
fun end_proof bot txt = |
|
718 |
assert_forward |
|
719 |
#> assert_bottom bot |
|
720 |
#> close_block |
|
721 |
#> assert_current_goal true |
|
722 |
#> using_facts [] |
|
17859 | 723 |
#> `before_qed #-> (refine o the_default Method.succeed_text) |
724 |
#> Seq.maps (refine (Method.finish_text txt)); |
|
17359 | 725 |
|
726 |
||
727 |
(* unstructured refinement *) |
|
728 |
||
729 |
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i))); |
|
730 |
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i))); |
|
731 |
||
732 |
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []); |
|
733 |
fun apply_end text = assert_forward #> refine_end text; |
|
734 |
||
735 |
||
736 |
||
17112 | 737 |
(** goals **) |
738 |
||
17359 | 739 |
(* goal names *) |
740 |
||
741 |
fun prep_names prep_att stmt = |
|
742 |
let |
|
743 |
val (names_attss, propp) = split_list (Attrib.map_specs prep_att stmt); |
|
744 |
val (names, attss) = split_list names_attss; |
|
745 |
in ((names, attss), propp) end; |
|
5820 | 746 |
|
17359 | 747 |
fun goal_names target name names = |
17450 | 748 |
(case target of NONE => "" | SOME "" => "" | SOME loc => " (in " ^ loc ^ ")") ^ |
17359 | 749 |
(if name = "" then "" else " " ^ name) ^ |
750 |
(case filter_out (equal "") names of [] => "" |
|
751 |
| nms => " " ^ enclose "(" ")" (space_implode " and " (Library.take (7, nms) @ |
|
752 |
(if length nms > 7 then ["..."] else [])))); |
|
753 |
||
16813 | 754 |
|
17359 | 755 |
(* generic goals *) |
756 |
||
757 |
fun check_tvars props state = |
|
758 |
(case fold Term.add_tvars props [] of [] => () |
|
18678 | 759 |
| tvars => error ("Goal statement contains illegal schematic type variable(s): " ^ |
760 |
commas (map (ProofContext.string_of_typ (context_of state) o TVar) tvars))); |
|
8991 | 761 |
|
17359 | 762 |
fun check_vars props state = |
763 |
(case fold Term.add_vars props [] of [] => () |
|
764 |
| vars => warning ("Goal statement contains unbound schematic variable(s): " ^ |
|
765 |
commas (map (ProofContext.string_of_term (context_of state) o Var) vars))); |
|
766 |
||
17859 | 767 |
fun generic_goal prepp kind before_qed after_qed raw_propp state = |
5820 | 768 |
let |
17359 | 769 |
val thy = theory_of state; |
770 |
val chaining = can assert_chain state; |
|
771 |
||
772 |
val ((propss, after_ctxt), goal_state) = |
|
5936 | 773 |
state |
774 |
|> assert_forward_or_chain |
|
775 |
|> enter_forward |
|
17359 | 776 |
|> open_block |
17859 | 777 |
|> map_context_result (fn ctxt => swap (prepp (ctxt, raw_propp))); |
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
778 |
val props = List.concat propss; |
15703 | 779 |
|
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
780 |
val goal = Goal.init (Thm.cterm_of thy (Logic.mk_conjunction_list2 propss)); |
18124 | 781 |
val after_qed' = after_qed |>> (fn after_local => |
782 |
fn results => map_context after_ctxt #> after_local results); |
|
5820 | 783 |
in |
17359 | 784 |
goal_state |
785 |
|> tap (check_tvars props) |
|
786 |
|> tap (check_vars props) |
|
18670 | 787 |
|> map_context (ProofContext.set_body true) |
17859 | 788 |
|> put_goal (SOME (make_goal ((kind, propss), [], goal, before_qed, after_qed'))) |
18475 | 789 |
|> map_context (ProofContext.add_cases false (AutoBind.cases thy props)) |
17359 | 790 |
|> map_context (ProofContext.auto_bind_goal props) |
791 |
|> K chaining ? (`the_facts #-> using_facts) |
|
792 |
|> put_facts NONE |
|
793 |
|> open_block |
|
794 |
|> put_goal NONE |
|
5820 | 795 |
|> enter_backward |
17450 | 796 |
|> K (null props) ? (refine (Method.Basic Method.assumption) #> Seq.hd) |
5820 | 797 |
end; |
798 |
||
17359 | 799 |
fun generic_qed state = |
12503 | 800 |
let |
18548 | 801 |
val (goal_ctxt, {statement = (_, stmt), using = _, goal, before_qed = _, after_qed}) = |
17859 | 802 |
current_goal state; |
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
803 |
val outer_state = state |> close_block; |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
804 |
val outer_ctxt = context_of outer_state; |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
805 |
|
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
806 |
val props = |
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
807 |
List.concat stmt |
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
808 |
|> ProofContext.generalize goal_ctxt outer_ctxt; |
17359 | 809 |
val results = |
18450
e57731ba01dd
discontinued unflat in favour of burrow and burrow_split
haftmann
parents:
18377
diff
changeset
|
810 |
stmt |
18503
841137f20307
goal/qed: proper treatment of two levels of conjunctions;
wenzelm
parents:
18475
diff
changeset
|
811 |
|> conclude_goal state goal |
18450
e57731ba01dd
discontinued unflat in favour of burrow and burrow_split
haftmann
parents:
18377
diff
changeset
|
812 |
|> (Seq.map_list o Seq.map_list) (ProofContext.exports goal_ctxt outer_ctxt); |
17359 | 813 |
in |
814 |
outer_state |
|
815 |
|> map_context (ProofContext.auto_bind_facts props) |
|
18124 | 816 |
|> pair (after_qed, results) |
17359 | 817 |
end; |
818 |
||
9469 | 819 |
|
17359 | 820 |
(* local goals *) |
821 |
||
17859 | 822 |
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state = |
17359 | 823 |
let |
824 |
val ((names, attss), propp) = prep_names (prep_att (theory_of state)) stmt; |
|
825 |
||
18124 | 826 |
fun after_qed' results = |
18808 | 827 |
local_results ((names ~~ attss) ~~ results) |
17359 | 828 |
#-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit)) |
18124 | 829 |
#> after_qed results; |
5820 | 830 |
in |
17359 | 831 |
state |
18124 | 832 |
|> generic_goal prepp (kind ^ goal_names NONE "" names) before_qed (after_qed', K I) propp |
17359 | 833 |
|> warn_extra_tfrees state |
5820 | 834 |
end; |
835 |
||
17359 | 836 |
fun local_qed txt = |
837 |
end_proof false txt |
|
838 |
#> Seq.map generic_qed |
|
18124 | 839 |
#> Seq.maps (uncurry (fn ((after_qed, _), results) => Seq.lifts after_qed results)); |
5820 | 840 |
|
841 |
||
17359 | 842 |
(* global goals *) |
843 |
||
844 |
fun global_goal print_results prep_att prepp |
|
17859 | 845 |
kind before_qed after_qed target (name, raw_atts) stmt ctxt = |
17359 | 846 |
let |
847 |
val thy = ProofContext.theory_of ctxt; |
|
848 |
val thy_ctxt = ProofContext.init thy; |
|
849 |
||
850 |
val atts = map (prep_att thy) raw_atts; |
|
851 |
val ((names, attss), propp) = prep_names (prep_att thy) stmt; |
|
852 |
||
17450 | 853 |
fun store_results prfx res = |
854 |
K (prfx <> "") ? (Sign.add_path prfx #> Sign.no_base_names) |
|
855 |
#> global_results kind ((names ~~ attss) ~~ res) |
|
17359 | 856 |
#-> (fn res' => |
857 |
(print_results thy_ctxt ((kind, name), res') : unit; |
|
858 |
#2 o global_results kind [((name, atts), List.concat (map #2 res'))])) |
|
17450 | 859 |
#> Sign.restore_naming thy; |
17359 | 860 |
|
18124 | 861 |
fun after_qed' results = |
17450 | 862 |
(case target of NONE => I |
863 |
| SOME prfx => store_results (NameSpace.base prfx) |
|
18784 | 864 |
(map (map (ProofContext.export_standard ctxt thy_ctxt |
17450 | 865 |
#> Drule.strip_shyps_warning)) results)) |
18124 | 866 |
#> after_qed results; |
17359 | 867 |
in |
868 |
init ctxt |
|
18670 | 869 |
|> generic_goal (prepp #> ProofContext.auto_fixes) (kind ^ goal_names target name names) |
18124 | 870 |
before_qed (K Seq.single, after_qed') propp |
17359 | 871 |
end; |
12065 | 872 |
|
18678 | 873 |
fun check_result msg sq = |
17112 | 874 |
(case Seq.pull sq of |
18678 | 875 |
NONE => error msg |
17112 | 876 |
| SOME res => Seq.cons res); |
877 |
||
17359 | 878 |
fun global_qeds txt = |
879 |
end_proof true txt |
|
880 |
#> Seq.map generic_qed |
|
18124 | 881 |
#> Seq.maps (fn (((_, after_qed), results), state) => |
882 |
Seq.lift after_qed results (theory_of state) |
|
18808 | 883 |
|> Seq.map (pair (context_of state))) |
17112 | 884 |
|> Seq.DETERM; (*backtracking may destroy theory!*) |
885 |
||
18678 | 886 |
fun global_qed txt = |
887 |
global_qeds txt |
|
888 |
#> check_result "Failed to finish proof" |
|
889 |
#> Seq.hd; |
|
12959 | 890 |
|
5820 | 891 |
|
17359 | 892 |
(* concluding steps *) |
17112 | 893 |
|
17359 | 894 |
fun local_terminal_proof (text, opt_text) = |
895 |
proof (SOME text) #> Seq.maps (local_qed (opt_text, true)); |
|
17112 | 896 |
|
897 |
val local_default_proof = local_terminal_proof (Method.default_text, NONE); |
|
898 |
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE); |
|
17359 | 899 |
val local_done_proof = proof (SOME Method.done_text) #> Seq.maps (local_qed (NONE, false)); |
900 |
fun local_skip_proof int = local_terminal_proof (Method.sorry_text int, NONE); |
|
17112 | 901 |
|
18678 | 902 |
fun global_term_proof immed (text, opt_text) = |
903 |
proof (SOME text) |
|
904 |
#> check_result "Terminal proof method failed" |
|
905 |
#> Seq.maps (global_qeds (opt_text, immed)) |
|
906 |
#> check_result "Failed to finish proof (after successful terminal method)" |
|
907 |
#> Seq.hd; |
|
17112 | 908 |
|
909 |
val global_terminal_proof = global_term_proof true; |
|
910 |
val global_default_proof = global_terminal_proof (Method.default_text, NONE); |
|
911 |
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE); |
|
912 |
val global_done_proof = global_term_proof false (Method.done_text, NONE); |
|
17359 | 913 |
fun global_skip_proof int = global_terminal_proof (Method.sorry_text int, NONE); |
5820 | 914 |
|
6896 | 915 |
|
17359 | 916 |
(* common goal statements *) |
917 |
||
918 |
local |
|
919 |
||
17859 | 920 |
fun gen_have prep_att prepp before_qed after_qed stmt int = |
921 |
local_goal (ProofDisplay.print_results int) prep_att prepp "have" before_qed after_qed stmt; |
|
6896 | 922 |
|
17859 | 923 |
fun gen_show prep_att prepp before_qed after_qed stmt int state = |
17359 | 924 |
let |
925 |
val testing = ref false; |
|
926 |
val rule = ref (NONE: thm option); |
|
927 |
fun fail_msg ctxt = |
|
928 |
"Local statement will fail to solve any pending goal" :: |
|
929 |
(case ! rule of NONE => [] | SOME th => [ProofDisplay.string_of_rule ctxt "Failed" th]) |
|
930 |
|> cat_lines; |
|
6896 | 931 |
|
17359 | 932 |
fun print_results ctxt res = |
933 |
if ! testing then () else ProofDisplay.print_results int ctxt res; |
|
934 |
fun print_rule ctxt th = |
|
935 |
if ! testing then rule := SOME th |
|
936 |
else if int then priority (ProofDisplay.string_of_rule ctxt "Successful" th) |
|
937 |
else (); |
|
938 |
val test_proof = |
|
939 |
(Seq.pull oo local_skip_proof) true |
|
940 |
|> setmp testing true |
|
941 |
|> setmp proofs 0 |
|
942 |
|> capture; |
|
6896 | 943 |
|
18124 | 944 |
fun after_qed' results = |
17359 | 945 |
refine_goals print_rule (context_of state) (List.concat results) |
18124 | 946 |
#> Seq.maps (after_qed results); |
17359 | 947 |
in |
948 |
state |
|
17859 | 949 |
|> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt |
17359 | 950 |
|> K int ? (fn goal_state => |
951 |
(case test_proof goal_state of |
|
952 |
Result (SOME _) => goal_state |
|
18678 | 953 |
| Result NONE => error (fail_msg (context_of goal_state)) |
17359 | 954 |
| Exn Interrupt => raise Interrupt |
955 |
| Exn exn => raise EXCEPTION (exn, fail_msg (context_of goal_state)))) |
|
956 |
end; |
|
957 |
||
17859 | 958 |
fun gen_theorem prep_att prepp kind before_qed after_qed target a = |
959 |
global_goal ProofDisplay.present_results prep_att prepp kind before_qed after_qed target a; |
|
17359 | 960 |
|
961 |
in |
|
962 |
||
18728 | 963 |
val have = gen_have Attrib.attribute ProofContext.bind_propp; |
17359 | 964 |
val have_i = gen_have (K I) ProofContext.bind_propp_i; |
18728 | 965 |
val show = gen_show Attrib.attribute ProofContext.bind_propp; |
17359 | 966 |
val show_i = gen_show (K I) ProofContext.bind_propp_i; |
18728 | 967 |
val theorem = gen_theorem Attrib.attribute ProofContext.bind_propp_schematic; |
17359 | 968 |
val theorem_i = gen_theorem (K I) ProofContext.bind_propp_schematic_i; |
6896 | 969 |
|
5820 | 970 |
end; |
17359 | 971 |
|
972 |
end; |