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