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