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