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