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