author | wenzelm |
Sun, 28 Aug 2005 16:04:50 +0200 | |
changeset 17166 | dc3b8cec8bba |
parent 17112 | 736f4b7841a8 |
child 17203 | 29b2563f5c11 |
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 |
|
10 |
type context |
|
17112 | 11 |
type method |
5820 | 12 |
type state |
13 |
exception STATE of string * state |
|
17112 | 14 |
val init: theory -> state |
5820 | 15 |
val context_of: state -> context |
16 |
val theory_of: state -> theory |
|
16450 | 17 |
val sign_of: state -> theory (*obsolete*) |
13377 | 18 |
val map_context: (context -> context) -> state -> state |
7924 | 19 |
val warn_extra_tfrees: state -> state -> state |
14554 | 20 |
val put_thms: string * thm list -> state -> state |
7605 | 21 |
val reset_thms: string -> state -> state |
6091 | 22 |
val the_facts: state -> thm list |
9469 | 23 |
val the_fact: state -> thm |
16813 | 24 |
val thisN: string |
11079
a378479996f7
val get_goal: state -> context * (thm list * thm);
wenzelm
parents:
10938
diff
changeset
|
25 |
val get_goal: state -> context * (thm list * thm) |
6091 | 26 |
val goal_facts: (state -> thm list) -> state -> state |
9469 | 27 |
val is_chain: state -> bool |
6891 | 28 |
val assert_forward: state -> state |
9469 | 29 |
val assert_forward_or_chain: state -> state |
5820 | 30 |
val assert_backward: state -> state |
8206 | 31 |
val assert_no_chain: state -> state |
5820 | 32 |
val enter_forward: state -> state |
17112 | 33 |
val level: state -> int |
16539 | 34 |
val show_main_goal: bool ref |
6529 | 35 |
val verbose: bool ref |
12423 | 36 |
val pretty_state: int -> state -> Pretty.T list |
10360 | 37 |
val pretty_goals: bool -> state -> Pretty.T list |
17112 | 38 |
val refine: Method.text -> state -> state Seq.seq |
39 |
val refine_end: Method.text -> state -> state Seq.seq |
|
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 |
14564 | 44 |
val simple_note_thms: string -> thm list -> state -> state |
17112 | 45 |
val note_thmss: ((bstring * Attrib.src list) * |
46 |
(thmref * Attrib.src list) list) list -> state -> state |
|
14564 | 47 |
val note_thmss_i: ((bstring * context attribute list) * |
9196 | 48 |
(thm list * context attribute list) list) list -> state -> state |
17112 | 49 |
val from_thmss: ((thmref * Attrib.src list) list) list -> state -> state |
50 |
val from_thmss_i: ((thm list * context attribute list) list) list -> state -> state |
|
51 |
val with_thmss: ((thmref * Attrib.src list) list) list -> state -> state |
|
52 |
val with_thmss_i: ((thm list * context attribute list) list) list -> state -> state |
|
53 |
val using_thmss: ((thmref * Attrib.src list) list) list -> state -> state |
|
12930 | 54 |
val using_thmss_i: ((thm list * context attribute list) list) list -> state -> state |
7412 | 55 |
val fix: (string list * string option) list -> state -> state |
7665 | 56 |
val fix_i: (string list * typ option) list -> state -> state |
9469 | 57 |
val assm: ProofContext.exporter |
17112 | 58 |
-> ((string * Attrib.src list) * (string * (string list * string list)) list) list |
7271 | 59 |
-> state -> state |
9469 | 60 |
val assm_i: ProofContext.exporter |
10464 | 61 |
-> ((string * context attribute list) * (term * (term list * term list)) list) list |
6932 | 62 |
-> state -> state |
10464 | 63 |
val assume: |
17112 | 64 |
((string * Attrib.src list) * (string * (string list * string list)) list) list |
7271 | 65 |
-> state -> state |
10464 | 66 |
val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list |
6932 | 67 |
-> state -> state |
10464 | 68 |
val presume: |
17112 | 69 |
((string * Attrib.src list) * (string * (string list * string list)) list) list |
6932 | 70 |
-> state -> state |
10464 | 71 |
val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list |
6932 | 72 |
-> state -> state |
17112 | 73 |
val def: string * Attrib.src list -> string * (string * string list) -> state -> state |
74 |
val def_i: string * context attribute list -> string * (term * term list) -> state -> state |
|
75 |
val invoke_case: string * string option list * Attrib.src list -> state -> state |
|
76 |
val invoke_case_i: string * string option list * context attribute list -> state -> state |
|
77 |
val chain: state -> state |
|
78 |
val from_facts: thm list -> state -> state |
|
17034 | 79 |
val multi_theorem: string -> (thm list * thm list -> theory -> theory) -> |
15696 | 80 |
(cterm list -> context -> context -> thm -> thm) -> |
15703 | 81 |
(xstring * (Attrib.src list * Attrib.src list list)) option -> |
17112 | 82 |
bstring * Attrib.src list -> Locale.element Locale.elem_expr list |
83 |
-> ((bstring * Attrib.src list) * (string * (string list * string list)) list) list |
|
12146 | 84 |
-> theory -> state |
17034 | 85 |
val multi_theorem_i: string -> (thm list * thm list -> theory -> theory) -> |
15696 | 86 |
(cterm list -> context -> context -> thm -> thm) -> |
15703 | 87 |
(string * (Attrib.src list * Attrib.src list list)) option |
12959 | 88 |
-> bstring * theory attribute list |
15703 | 89 |
-> Locale.element_i Locale.elem_expr list |
12146 | 90 |
-> ((bstring * theory attribute list) * (term * (term list * term list)) list) list |
91 |
-> theory -> state |
|
17034 | 92 |
val show: (bool -> state -> state) -> (thm list -> state -> state Seq.seq) -> bool |
17112 | 93 |
-> ((string * Attrib.src list) * (string * (string list * string list)) list) list |
12146 | 94 |
-> bool -> state -> state |
17034 | 95 |
val show_i: (bool -> state -> state) -> (thm list -> state -> state Seq.seq) -> bool |
12146 | 96 |
-> ((string * context attribute list) * (term * (term list * term list)) list) list |
97 |
-> bool -> state -> state |
|
17034 | 98 |
val have: (thm list -> state -> state Seq.seq) -> bool |
17112 | 99 |
-> ((string * Attrib.src list) * (string * (string list * string list)) list) list |
12146 | 100 |
-> state -> state |
17034 | 101 |
val have_i: (thm list -> state -> state Seq.seq) -> bool |
12146 | 102 |
-> ((string * context attribute list) * (term * (term list * term list)) list) list |
103 |
-> state -> state |
|
6404 | 104 |
val at_bottom: state -> bool |
17112 | 105 |
val local_qed: bool -> Method.text option |
106 |
-> (context -> string * (string * thm list) list -> unit) * |
|
107 |
(context -> thm -> unit) -> state -> state Seq.seq |
|
108 |
val global_qed: bool -> Method.text option |
|
109 |
-> state -> (theory * context) * ((string * string) * (string * thm list) list) |
|
110 |
val proof: Method.text option -> state -> state Seq.seq |
|
111 |
val local_terminal_proof: Method.text * Method.text option |
|
112 |
-> (context -> string * (string * thm list) list -> unit) * |
|
113 |
(context -> thm -> unit) -> state -> state Seq.seq |
|
114 |
val local_default_proof: (context -> string * (string * thm list) list -> unit) * |
|
115 |
(context -> thm -> unit) -> state -> state Seq.seq |
|
116 |
val local_immediate_proof: (context -> string * (string * thm list) list -> unit) * |
|
117 |
(context -> thm -> unit) -> state -> state Seq.seq |
|
118 |
val local_done_proof: (context -> string * (string * thm list) list -> unit) * |
|
119 |
(context -> thm -> unit) -> state -> state Seq.seq |
|
120 |
val global_terminal_proof: Method.text * Method.text option |
|
121 |
-> state -> (theory * context) * ((string * string) * (string * thm list) list) |
|
122 |
val global_default_proof: state -> |
|
123 |
(theory * context) * ((string * string) * (string * thm list) list) |
|
124 |
val global_immediate_proof: state -> |
|
125 |
(theory * context) * ((string * string) * (string * thm list) list) |
|
126 |
val global_done_proof: state -> |
|
127 |
(theory * context) * ((string * string) * (string * thm list) list) |
|
128 |
val defer: int option -> state -> state Seq.seq |
|
129 |
val prefer: int -> state -> state Seq.seq |
|
130 |
val apply: Method.text -> state -> state Seq.seq |
|
131 |
val apply_end: Method.text -> state -> state Seq.seq |
|
6896 | 132 |
val begin_block: state -> state |
16813 | 133 |
val next_block: state -> state |
6932 | 134 |
val end_block: state -> state Seq.seq |
5820 | 135 |
end; |
136 |
||
13377 | 137 |
structure Proof: PROOF = |
5820 | 138 |
struct |
139 |
||
16813 | 140 |
type context = ProofContext.context; |
17112 | 141 |
type method = Method.method; |
16813 | 142 |
|
5820 | 143 |
|
144 |
(** proof state **) |
|
145 |
||
146 |
(* type goal *) |
|
147 |
||
148 |
datatype kind = |
|
12959 | 149 |
Theorem of {kind: string, |
150 |
theory_spec: (bstring * theory attribute list) * theory attribute list list, |
|
15703 | 151 |
locale_spec: (string * (Attrib.src list * Attrib.src list list)) option, |
152 |
view: cterm list * cterm list, (* target view and includes view *) |
|
153 |
export: cterm list -> context -> context -> thm -> thm} | |
|
15696 | 154 |
(* exporter to be used in finish_global *) |
12959 | 155 |
Show of context attribute list list | |
15703 | 156 |
Have of context attribute list list; |
15099 | 157 |
|
13415 | 158 |
fun kind_name _ (Theorem {kind = s, theory_spec = ((a, _), _), |
15596 | 159 |
locale_spec = NONE, ...}) = s ^ (if a = "" then "" else " " ^ a) |
16450 | 160 |
| kind_name thy (Theorem {kind = s, theory_spec = ((a, _), _), |
15596 | 161 |
locale_spec = SOME (name, _), ...}) = |
16450 | 162 |
s ^ " (in " ^ Locale.extern thy name ^ ")" ^ (if a = "" then "" else " " ^ a) |
12015 | 163 |
| kind_name _ (Show _) = "show" |
15703 | 164 |
| kind_name _ (Have _) = "have"; |
5820 | 165 |
|
166 |
type goal = |
|
12146 | 167 |
(kind * (*result kind*) |
168 |
string list * (*result names*) |
|
169 |
term list list) * (*result statements*) |
|
170 |
(thm list * (*use facts*) |
|
171 |
thm); (*goal: subgoals ==> statement*) |
|
5820 | 172 |
|
173 |
||
174 |
(* type mode *) |
|
175 |
||
17112 | 176 |
datatype mode = Forward | Chain | Backward; |
177 |
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove"); |
|
5820 | 178 |
|
179 |
||
180 |
(* datatype state *) |
|
181 |
||
7176 | 182 |
datatype node = |
183 |
Node of |
|
184 |
{context: context, |
|
185 |
facts: thm list option, |
|
186 |
mode: mode, |
|
15703 | 187 |
goal: (goal * (after_qed * bool)) option} |
7176 | 188 |
and state = |
5820 | 189 |
State of |
17112 | 190 |
node * (*current node*) |
191 |
node list (*parent nodes wrt. block structure*) |
|
15703 | 192 |
and after_qed = |
17034 | 193 |
AfterQed of |
17112 | 194 |
(thm list -> state -> state Seq.seq) * (*after local qed*) |
195 |
(thm list * thm list -> theory -> theory); (*after global qed*) |
|
5820 | 196 |
|
7176 | 197 |
fun make_node (context, facts, mode, goal) = |
198 |
Node {context = context, facts = facts, mode = mode, goal = goal}; |
|
199 |
||
200 |
||
5820 | 201 |
exception STATE of string * state; |
202 |
||
203 |
fun err_malformed name state = |
|
204 |
raise STATE (name ^ ": internal error -- malformed proof state", state); |
|
205 |
||
206 |
||
7176 | 207 |
fun map_current f (State (Node {context, facts, mode, goal}, nodes)) = |
5820 | 208 |
State (make_node (f (context, facts, mode, goal)), nodes); |
209 |
||
17112 | 210 |
fun init thy = |
15531 | 211 |
State (make_node (ProofContext.init thy, NONE, Forward, NONE), []); |
12045 | 212 |
|
5820 | 213 |
|
214 |
||
215 |
(** basic proof state operations **) |
|
216 |
||
217 |
(* context *) |
|
218 |
||
7176 | 219 |
fun context_of (State (Node {context, ...}, _)) = context; |
5820 | 220 |
val theory_of = ProofContext.theory_of o context_of; |
16450 | 221 |
val sign_of = theory_of; |
5820 | 222 |
|
223 |
fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal)); |
|
224 |
||
7176 | 225 |
fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) = |
5820 | 226 |
let val (context', result) = f context |
227 |
in (State (make_node (context', facts, mode, goal), nodes), result) end; |
|
228 |
||
229 |
||
7924 | 230 |
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of; |
10809 | 231 |
val add_binds_i = map_context o ProofContext.add_binds_i; |
6790 | 232 |
val auto_bind_goal = map_context o ProofContext.auto_bind_goal; |
12146 | 233 |
val auto_bind_facts = map_context o ProofContext.auto_bind_facts; |
6091 | 234 |
val put_thms = map_context o ProofContext.put_thms; |
235 |
val put_thmss = map_context o ProofContext.put_thmss; |
|
7605 | 236 |
val reset_thms = map_context o ProofContext.reset_thms; |
8374 | 237 |
val get_case = ProofContext.get_case o context_of; |
5820 | 238 |
|
239 |
||
240 |
(* facts *) |
|
241 |
||
15531 | 242 |
fun the_facts (State (Node {facts = SOME facts, ...}, _)) = facts |
5820 | 243 |
| the_facts state = raise STATE ("No current facts available", state); |
244 |
||
9469 | 245 |
fun the_fact state = |
246 |
(case the_facts state of |
|
247 |
[thm] => thm |
|
248 |
| _ => raise STATE ("Single fact expected", state)); |
|
249 |
||
6848 | 250 |
fun assert_facts state = (the_facts state; state); |
7176 | 251 |
fun get_facts (State (Node {facts, ...}, _)) = facts; |
6848 | 252 |
|
7605 | 253 |
|
254 |
val thisN = "this"; |
|
255 |
||
5820 | 256 |
fun put_facts facts state = |
257 |
state |
|
258 |
|> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) |
|
15531 | 259 |
|> (case facts of NONE => reset_thms thisN | SOME ths => put_thms (thisN, ths)); |
5820 | 260 |
|
15531 | 261 |
val reset_facts = put_facts NONE; |
5820 | 262 |
|
17112 | 263 |
fun these_factss more_facts (state, named_factss) = |
264 |
state |> put_facts (SOME (List.concat (map snd named_factss) @ more_facts)); |
|
5820 | 265 |
|
266 |
||
267 |
(* goal *) |
|
268 |
||
10553 | 269 |
local |
15531 | 270 |
fun find i (state as State (Node {goal = SOME goal, ...}, _)) = (context_of state, (i, goal)) |
17112 | 271 |
| find i (State (Node {goal = NONE, ...}, node :: nodes)) = |
272 |
find (i + 1) (State (node, nodes)) |
|
273 |
| find _ (state as State (_, [])) = raise STATE ("No goal present", state); |
|
10553 | 274 |
in val find_goal = find 0 end; |
7176 | 275 |
|
11079
a378479996f7
val get_goal: state -> context * (thm list * thm);
wenzelm
parents:
10938
diff
changeset
|
276 |
fun get_goal state = |
a378479996f7
val get_goal: state -> context * (thm list * thm);
wenzelm
parents:
10938
diff
changeset
|
277 |
let val (ctxt, (_, ((_, x), _))) = find_goal state |
a378479996f7
val get_goal: state -> context * (thm list * thm);
wenzelm
parents:
10938
diff
changeset
|
278 |
in (ctxt, x) end; |
a378479996f7
val get_goal: state -> context * (thm list * thm);
wenzelm
parents:
10938
diff
changeset
|
279 |
|
5820 | 280 |
fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal)); |
281 |
||
15531 | 282 |
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) = |
283 |
State (make_node (f context, facts, mode, SOME (g goal)), nodes) |
|
8374 | 284 |
| map_goal f g (State (nd, node :: nodes)) = |
285 |
let val State (node', nodes') = map_goal f g (State (node, nodes)) |
|
286 |
in map_context f (State (nd, node' :: nodes')) end |
|
287 |
| map_goal _ _ state = state; |
|
5820 | 288 |
|
289 |
fun goal_facts get state = |
|
290 |
state |
|
8374 | 291 |
|> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f)); |
5820 | 292 |
|
293 |
fun use_facts state = |
|
294 |
state |
|
295 |
|> goal_facts the_facts |
|
296 |
|> reset_facts; |
|
297 |
||
298 |
||
299 |
(* mode *) |
|
300 |
||
7176 | 301 |
fun get_mode (State (Node {mode, ...}, _)) = mode; |
5820 | 302 |
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal)); |
303 |
||
16813 | 304 |
val enter_forward = put_mode Forward; |
17112 | 305 |
val enter_forward_chain = put_mode Chain; |
5820 | 306 |
val enter_backward = put_mode Backward; |
307 |
||
308 |
fun assert_mode pred state = |
|
309 |
let val mode = get_mode state in |
|
310 |
if pred mode then state |
|
12010 | 311 |
else raise STATE ("Illegal application of proof command in " |
312 |
^ quote (mode_name mode) ^ " mode", state) |
|
5820 | 313 |
end; |
314 |
||
17112 | 315 |
fun is_chain state = get_mode state = Chain; |
5820 | 316 |
val assert_forward = assert_mode (equal Forward); |
17112 | 317 |
val assert_forward_or_chain = assert_mode (equal Forward orf equal Chain); |
5820 | 318 |
val assert_backward = assert_mode (equal Backward); |
17112 | 319 |
val assert_no_chain = assert_mode (not_equal Chain); |
5820 | 320 |
|
321 |
||
322 |
(* blocks *) |
|
323 |
||
6776 | 324 |
fun level (State (_, nodes)) = length nodes; |
325 |
||
5820 | 326 |
fun open_block (State (node, nodes)) = State (node, node :: nodes); |
327 |
||
328 |
fun new_block state = |
|
329 |
state |
|
330 |
|> open_block |
|
15531 | 331 |
|> put_goal NONE; |
5820 | 332 |
|
7487
c0f9b956a3e7
close_block: removed ProofContext.transfer_used_names;
wenzelm
parents:
7478
diff
changeset
|
333 |
fun close_block (state as State (_, node :: nodes)) = State (node, nodes) |
5820 | 334 |
| close_block state = raise STATE ("Unbalanced block parentheses", state); |
335 |
||
336 |
||
337 |
||
12423 | 338 |
(** pretty_state **) |
5820 | 339 |
|
13869 | 340 |
val show_main_goal = ref false; |
16539 | 341 |
val verbose = ProofContext.verbose; |
13698 | 342 |
|
14876 | 343 |
val pretty_goals_local = Display.pretty_goals_aux o ProofContext.pp; |
12085 | 344 |
|
15531 | 345 |
fun pretty_facts _ _ NONE = [] |
346 |
| pretty_facts s ctxt (SOME ths) = |
|
12055 | 347 |
[Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""]; |
6756 | 348 |
|
16813 | 349 |
fun pretty_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) = |
5820 | 350 |
let |
11891 | 351 |
val ref (_, _, begin_goal) = Display.current_goals_markers; |
5820 | 352 |
|
353 |
fun levels_up 0 = "" |
|
7575 | 354 |
| levels_up 1 = ", 1 level up" |
355 |
| levels_up i = ", " ^ string_of_int i ^ " levels up"; |
|
5820 | 356 |
|
11556 | 357 |
fun subgoals 0 = "" |
358 |
| subgoals 1 = ", 1 subgoal" |
|
359 |
| subgoals n = ", " ^ string_of_int n ^ " subgoals"; |
|
12146 | 360 |
|
361 |
fun prt_names names = |
|
12242
282a92bc5655
multi_theorem: common statement header (covers *all* results);
wenzelm
parents:
12223
diff
changeset
|
362 |
(case filter_out (equal "") names of [] => "" |
15570 | 363 |
| nms => " " ^ enclose "(" ")" (space_implode " and " (Library.take (7, nms) @ |
12308 | 364 |
(if length nms > 7 then ["..."] else [])))); |
12146 | 365 |
|
366 |
fun prt_goal (_, (i, (((kind, names, _), (goal_facts, thm)), _))) = |
|
12085 | 367 |
pretty_facts "using " ctxt |
15531 | 368 |
(if mode <> Backward orelse null goal_facts then NONE else SOME goal_facts) @ |
16450 | 369 |
[Pretty.str ("goal (" ^ kind_name (theory_of state) kind ^ prt_names names ^ |
12146 | 370 |
levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @ |
16146 | 371 |
pretty_goals_local ctxt begin_goal (true, ! show_main_goal) (! Display.goals_limit) thm; |
6848 | 372 |
|
8462 | 373 |
val ctxt_prts = |
12085 | 374 |
if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt |
375 |
else if mode = Backward then ProofContext.pretty_asms ctxt |
|
7575 | 376 |
else []; |
8462 | 377 |
|
378 |
val prts = |
|
8582 | 379 |
[Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^ |
16813 | 380 |
(if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1) |
8561 | 381 |
else "")), Pretty.str ""] @ |
8462 | 382 |
(if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @ |
383 |
(if ! verbose orelse mode = Forward then |
|
12146 | 384 |
(pretty_facts "" ctxt facts @ prt_goal (find_goal state)) |
17112 | 385 |
else if mode = Chain then pretty_facts "picking " ctxt facts |
12146 | 386 |
else prt_goal (find_goal state)) |
12423 | 387 |
in prts end; |
5820 | 388 |
|
12085 | 389 |
fun pretty_goals main state = |
390 |
let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state |
|
391 |
in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end; |
|
10320 | 392 |
|
5820 | 393 |
|
394 |
||
395 |
(** proof steps **) |
|
396 |
||
397 |
(* refine goal *) |
|
398 |
||
8234 | 399 |
local |
400 |
||
16146 | 401 |
fun goalN i = "goal" ^ string_of_int i; |
402 |
fun goals st = map goalN (1 upto Thm.nprems_of st); |
|
403 |
||
404 |
fun no_goal_cases st = map (rpair NONE) (goals st); |
|
405 |
||
406 |
fun goal_cases st = |
|
16450 | 407 |
RuleCases.make true NONE (Thm.theory_of_thm st, Thm.prop_of st) (goals st); |
16146 | 408 |
|
16450 | 409 |
fun check_theory thy state = |
410 |
if subthy (thy, theory_of state) then state |
|
411 |
else raise STATE ("Bad theory of method result: " ^ Context.str_of_thy thy, state); |
|
5820 | 412 |
|
17112 | 413 |
fun apply_method current_context meth_fun state = |
6848 | 414 |
let |
16146 | 415 |
val (goal_ctxt, (_, ((result, (facts, st)), x))) = find_goal state; |
17112 | 416 |
val meth = meth_fun (if current_context then context_of state else goal_ctxt); |
16146 | 417 |
in |
17112 | 418 |
Method.apply meth facts st |> Seq.map (fn (st', meth_cases) => |
6848 | 419 |
state |
16450 | 420 |
|> check_theory (Thm.theory_of_thm st') |
16146 | 421 |
|> map_goal |
422 |
(ProofContext.add_cases (no_goal_cases st @ goal_cases st' @ meth_cases)) |
|
423 |
(K ((result, (facts, st')), x))) |
|
424 |
end; |
|
5820 | 425 |
|
17112 | 426 |
fun apply_text cc text state = |
427 |
let |
|
428 |
val thy = theory_of state; |
|
429 |
||
430 |
fun eval (Method.Basic m) = apply_method cc m |
|
431 |
| eval (Method.Source src) = apply_method cc (Method.method thy src) |
|
432 |
| eval (Method.Then txts) = Seq.EVERY (map eval txts) |
|
433 |
| eval (Method.Orelse txts) = Seq.FIRST (map eval txts) |
|
434 |
| eval (Method.Try txt) = Seq.TRY (eval txt) |
|
435 |
| eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt); |
|
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 |
|
443 |
end; |
|
444 |
||
5820 | 445 |
|
9469 | 446 |
(* export results *) |
447 |
||
11816 | 448 |
fun refine_tac rule = |
12703
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents:
12698
diff
changeset
|
449 |
Tactic.norm_hhf_tac THEN' Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) => |
11816 | 450 |
if can Logic.dest_goal (Logic.strip_assums_concl goal) then |
451 |
Tactic.etac Drule.triv_goal i |
|
452 |
else all_tac)); |
|
453 |
||
12146 | 454 |
fun export_goal inner print_rule raw_rule state = |
6932 | 455 |
let |
12971 | 456 |
val (outer, (_, ((result, (facts, thm)), x))) = find_goal state; |
12010 | 457 |
val exp_tac = ProofContext.export true inner outer; |
9469 | 458 |
fun exp_rule rule = |
459 |
let |
|
12055 | 460 |
val _ = print_rule outer rule; |
11816 | 461 |
val thmq = FINDGOAL (refine_tac rule) thm; |
12971 | 462 |
in Seq.map (fn th => map_goal I (K ((result, (facts, th)), x)) state) thmq end; |
9469 | 463 |
in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end; |
6932 | 464 |
|
12146 | 465 |
fun export_goals inner print_rule raw_rules = |
466 |
Seq.EVERY (map (export_goal inner print_rule) raw_rules); |
|
467 |
||
6932 | 468 |
fun transfer_facts inner_state state = |
469 |
(case get_facts inner_state of |
|
15531 | 470 |
NONE => Seq.single (reset_facts state) |
471 |
| SOME thms => |
|
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
472 |
let |
12010 | 473 |
val exp_tac = ProofContext.export false (context_of inner_state) (context_of state); |
11816 | 474 |
val thmqs = map exp_tac thms; |
15531 | 475 |
in Seq.map (fn ths => put_facts (SOME ths) state) (Seq.commute thmqs) end); |
6932 | 476 |
|
477 |
||
478 |
(* prepare result *) |
|
479 |
||
12146 | 480 |
fun prep_result state ts raw_th = |
5820 | 481 |
let |
482 |
val ctxt = context_of state; |
|
483 |
fun err msg = raise STATE (msg, state); |
|
484 |
||
12146 | 485 |
val ngoals = Thm.nprems_of raw_th; |
5820 | 486 |
val _ = |
487 |
if ngoals = 0 then () |
|
16146 | 488 |
else (err (Pretty.string_of (Pretty.chunks |
489 |
(pretty_goals_local ctxt "" (true, !show_main_goal) (! Display.goals_limit) raw_th @ |
|
12085 | 490 |
[Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")])))); |
5820 | 491 |
|
16450 | 492 |
val {hyps, thy, ...} = Thm.rep_thm raw_th; |
493 |
val tsig = Sign.tsig_of thy; |
|
15570 | 494 |
val casms = List.concat (map #1 (ProofContext.assumptions_of (context_of state))); |
11816 | 495 |
val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms); |
12146 | 496 |
|
497 |
val th = raw_th RS Drule.rev_triv_goal; |
|
498 |
val ths = Drule.conj_elim_precise (length ts) th |
|
499 |
handle THM _ => err "Main goal structure corrupted"; |
|
5820 | 500 |
in |
12146 | 501 |
conditional (not (null bad_hyps)) (fn () => err ("Additional hypotheses:\n" ^ |
502 |
cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps))); |
|
12808
a529b4b91888
RuleCases.make interface based on term instead of thm;
wenzelm
parents:
12760
diff
changeset
|
503 |
conditional (exists (not o Pattern.matches tsig) (ts ~~ map Thm.prop_of ths)) (fn () => |
12146 | 504 |
err ("Proved a different theorem: " ^ Pretty.string_of (ProofContext.pretty_thm ctxt th))); |
505 |
ths |
|
5820 | 506 |
end; |
507 |
||
508 |
||
509 |
||
510 |
(*** structured proof commands ***) |
|
511 |
||
17112 | 512 |
(** context elements **) |
5820 | 513 |
|
17112 | 514 |
(* bindings *) |
5820 | 515 |
|
16813 | 516 |
local |
517 |
||
518 |
fun gen_bind f args state = |
|
5820 | 519 |
state |
520 |
|> assert_forward |
|
16813 | 521 |
|> map_context (f args) |
5820 | 522 |
|> reset_facts; |
523 |
||
16813 | 524 |
in |
525 |
||
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
526 |
val match_bind = gen_bind (ProofContext.match_bind false); |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
527 |
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
|
528 |
val let_bind = gen_bind (ProofContext.match_bind true); |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
529 |
val let_bind_i = gen_bind (ProofContext.match_bind_i true); |
5820 | 530 |
|
16813 | 531 |
end; |
532 |
||
5820 | 533 |
|
17112 | 534 |
(* fixes *) |
9196 | 535 |
|
12714 | 536 |
local |
537 |
||
17112 | 538 |
fun gen_fix fix_ctxt args state = |
5820 | 539 |
state |
540 |
|> assert_forward |
|
17112 | 541 |
|> map_context (fix_ctxt args) |
5820 | 542 |
|> reset_facts; |
543 |
||
16813 | 544 |
in |
545 |
||
5820 | 546 |
val fix = gen_fix ProofContext.fix; |
547 |
val fix_i = gen_fix ProofContext.fix_i; |
|
548 |
||
16813 | 549 |
end; |
550 |
||
5820 | 551 |
|
17112 | 552 |
(* assumptions *) |
5820 | 553 |
|
16813 | 554 |
local |
555 |
||
17112 | 556 |
fun gen_assume asm prep_att exp args state = |
5820 | 557 |
state |
558 |
|> assert_forward |
|
17112 | 559 |
|> map_context_result (asm exp (Attrib.map_specs (prep_att (theory_of state)) args)) |
560 |
|> these_factss []; |
|
6932 | 561 |
|
16813 | 562 |
in |
563 |
||
17112 | 564 |
val assm = gen_assume ProofContext.assume Attrib.local_attribute; |
565 |
val assm_i = gen_assume ProofContext.assume_i (K I); |
|
566 |
val assume = assm ProofContext.export_assume; |
|
567 |
val assume_i = assm_i ProofContext.export_assume; |
|
568 |
val presume = assm ProofContext.export_presume; |
|
11917 | 569 |
val presume_i = assm_i ProofContext.export_presume; |
5820 | 570 |
|
16813 | 571 |
end; |
572 |
||
7271 | 573 |
|
17112 | 574 |
(* local definitions *) |
11891 | 575 |
|
16813 | 576 |
local |
577 |
||
17112 | 578 |
fun gen_def fix prep_att prep_term prep_pats |
579 |
(raw_name, raw_atts) (x, (raw_rhs, raw_pats)) state = |
|
11891 | 580 |
let |
581 |
val _ = assert_forward state; |
|
17112 | 582 |
val thy = theory_of state; |
11891 | 583 |
val ctxt = context_of state; |
584 |
||
585 |
val rhs = prep_term ctxt raw_rhs; |
|
586 |
val T = Term.fastype_of rhs; |
|
587 |
val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats; |
|
17112 | 588 |
val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T)); |
11891 | 589 |
|
17112 | 590 |
val name = if raw_name = "" then Thm.def_name x else raw_name; |
591 |
val atts = map (prep_att thy) raw_atts; |
|
11891 | 592 |
in |
593 |
state |
|
15531 | 594 |
|> fix [([x], NONE)] |
11891 | 595 |
|> match_bind_i [(pats, rhs)] |
11917 | 596 |
|> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])] |
11891 | 597 |
end; |
598 |
||
16813 | 599 |
in |
600 |
||
17112 | 601 |
val def = gen_def fix Attrib.local_attribute ProofContext.read_term ProofContext.read_term_pats; |
602 |
val def_i = gen_def fix_i (K I) ProofContext.cert_term ProofContext.cert_term_pats; |
|
11891 | 603 |
|
16813 | 604 |
end; |
605 |
||
11891 | 606 |
|
8374 | 607 |
|
17112 | 608 |
(** facts **) |
5820 | 609 |
|
610 |
(* forward chaining *) |
|
611 |
||
612 |
fun chain state = |
|
613 |
state |
|
614 |
|> assert_forward |
|
6848 | 615 |
|> assert_facts |
5820 | 616 |
|> enter_forward_chain; |
617 |
||
618 |
fun from_facts facts state = |
|
619 |
state |
|
15531 | 620 |
|> put_facts (SOME facts) |
5820 | 621 |
|> chain; |
622 |
||
623 |
||
17112 | 624 |
(* note / from / with *) |
625 |
||
17166 | 626 |
fun no_binding args = map (pair ("", [])) args; |
17112 | 627 |
|
628 |
local |
|
629 |
||
630 |
fun gen_thmss note_ctxt more_facts opt_chain prep_atts args state = |
|
631 |
state |
|
632 |
|> assert_forward |
|
633 |
|> map_context_result (note_ctxt (Attrib.map_facts (prep_atts (theory_of state)) args)) |
|
634 |
|> these_factss (more_facts state) |
|
635 |
|> opt_chain; |
|
636 |
||
637 |
in |
|
638 |
||
639 |
val note_thmss = gen_thmss ProofContext.note_thmss (K []) I Attrib.local_attribute; |
|
640 |
val note_thmss_i = gen_thmss ProofContext.note_thmss_i (K []) I (K I); |
|
641 |
fun simple_note_thms name thms = note_thmss_i [((name, []), [(thms, [])])]; |
|
642 |
||
643 |
val from_thmss = |
|
17166 | 644 |
gen_thmss ProofContext.note_thmss (K []) chain Attrib.local_attribute o no_binding; |
645 |
val from_thmss_i = gen_thmss ProofContext.note_thmss_i (K []) chain (K I) o no_binding; |
|
17112 | 646 |
|
647 |
val with_thmss = |
|
17166 | 648 |
gen_thmss ProofContext.note_thmss the_facts chain Attrib.local_attribute o no_binding; |
649 |
val with_thmss_i = gen_thmss ProofContext.note_thmss_i the_facts chain (K I) o no_binding; |
|
17112 | 650 |
|
651 |
end; |
|
652 |
||
653 |
||
654 |
(* using *) |
|
655 |
||
656 |
local |
|
657 |
||
658 |
fun gen_using_thmss note prep_atts args state = |
|
659 |
state |
|
660 |
|> assert_backward |
|
17166 | 661 |
|> map_context_result (note (Attrib.map_facts (prep_atts (theory_of state)) (no_binding args))) |
17112 | 662 |
|> (fn (st, named_facts) => |
663 |
let val (_, (_, ((result, (facts, thm)), x))) = find_goal st; |
|
664 |
in st |> map_goal I (K ((result, (facts @ List.concat (map snd named_facts), thm)), x)) end); |
|
665 |
||
666 |
in |
|
667 |
||
668 |
val using_thmss = gen_using_thmss ProofContext.note_thmss Attrib.local_attribute; |
|
669 |
val using_thmss_i = gen_using_thmss ProofContext.note_thmss_i (K I); |
|
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; |
|
681 |
val (state', (lets, asms)) = |
|
682 |
map_context_result (ProofContext.apply_case (get_case state name xs)) state; |
|
683 |
val assumptions = asms |> map (fn (a, ts) => |
|
684 |
((NameSpace.qualified name a, atts), map (rpair ([], [])) ts)); |
|
685 |
in |
|
686 |
state' |
|
687 |
|> add_binds_i lets |
|
688 |
|> map_context (ProofContext.no_base_names o ProofContext.qualified_names) |
|
689 |
|> assume_i assumptions |
|
690 |
|> map_context (ProofContext.restore_naming (context_of state)) |
|
691 |
|> `the_facts |-> simple_note_thms name |
|
692 |
end; |
|
693 |
||
694 |
in |
|
695 |
||
696 |
val invoke_case = gen_invoke_case Attrib.local_attribute; |
|
697 |
val invoke_case_i = gen_invoke_case (K I); |
|
698 |
||
699 |
end; |
|
700 |
||
701 |
||
702 |
||
703 |
(** goals **) |
|
704 |
||
5820 | 705 |
(* setup goals *) |
706 |
||
16813 | 707 |
local |
708 |
||
11549 | 709 |
val rule_contextN = "rule_context"; |
8991 | 710 |
|
12971 | 711 |
fun setup_goal opt_block prepp autofix kind after_qed pr names raw_propp state = |
5820 | 712 |
let |
12146 | 713 |
val (state', (propss, gen_binds)) = |
5936 | 714 |
state |
715 |
|> assert_forward_or_chain |
|
716 |
|> enter_forward |
|
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
717 |
|> opt_block |
12534 | 718 |
|> map_context_result (fn ctxt => prepp (ctxt, raw_propp)); |
16450 | 719 |
val thy' = theory_of state'; |
12146 | 720 |
|
15703 | 721 |
val AfterQed (after_local, after_global) = after_qed; |
17112 | 722 |
val after_qed' = AfterQed (fn res => after_local res o map_context gen_binds, after_global); |
15703 | 723 |
|
15570 | 724 |
val props = List.concat propss; |
16450 | 725 |
val cprop = Thm.cterm_of thy' (Logic.mk_conjunction_list props); |
7556 | 726 |
val goal = Drule.mk_triv_goal cprop; |
10553 | 727 |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
728 |
val tvars = foldr Term.add_term_tvars [] props; |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
729 |
val vars = foldr Term.add_term_vars [] props; |
5820 | 730 |
in |
10553 | 731 |
if null tvars then () |
732 |
else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^ |
|
733 |
commas (map (Syntax.string_of_vname o #1) tvars), state); |
|
734 |
if null vars then () |
|
735 |
else warning ("Goal statement contains unbound schematic variable(s): " ^ |
|
12055 | 736 |
commas (map (ProofContext.string_of_term (context_of state')) vars)); |
5936 | 737 |
state' |
12146 | 738 |
|> map_context (autofix props) |
15703 | 739 |
|> put_goal (SOME (((kind, names, propss), ([], goal)), (after_qed', pr))) |
12167 | 740 |
|> map_context (ProofContext.add_cases |
12808
a529b4b91888
RuleCases.make interface based on term instead of thm;
wenzelm
parents:
12760
diff
changeset
|
741 |
(if length props = 1 then |
16450 | 742 |
RuleCases.make true NONE (Thm.theory_of_thm goal, Thm.prop_of goal) [rule_contextN] |
16146 | 743 |
else [(rule_contextN, NONE)])) |
12146 | 744 |
|> auto_bind_goal props |
5820 | 745 |
|> (if is_chain state then use_facts else reset_facts) |
746 |
|> new_block |
|
747 |
|> enter_backward |
|
748 |
end; |
|
749 |
||
17112 | 750 |
fun global_goal prep_att prep kind after_global export raw_locale (name, atts) elems concl thy = |
12503 | 751 |
let |
17112 | 752 |
val prep_atts = map (prep_att thy); |
753 |
val init_state = init thy; |
|
13399
c136276dc847
support locale ``views'' (for cumulative predicates);
wenzelm
parents:
13377
diff
changeset
|
754 |
val (opt_name, view, locale_ctxt, elems_ctxt, propp) = |
17112 | 755 |
prep (Option.map fst raw_locale) elems (map snd concl) (context_of init_state); |
12503 | 756 |
in |
17112 | 757 |
init_state |
12549 | 758 |
|> open_block |
12511 | 759 |
|> map_context (K locale_ctxt) |
12065 | 760 |
|> open_block |
12511 | 761 |
|> map_context (K elems_ctxt) |
12959 | 762 |
|> setup_goal I ProofContext.bind_propp_schematic_i ProofContext.fix_frees |
13415 | 763 |
(Theorem {kind = kind, |
17112 | 764 |
theory_spec = ((name, prep_atts atts), map (prep_atts o snd o fst) concl), |
15973 | 765 |
locale_spec = (case raw_locale of NONE => NONE | SOME (_, x) => SOME (the opt_name, x)), |
15596 | 766 |
view = view, |
15696 | 767 |
export = export}) |
17034 | 768 |
(AfterQed (K Seq.single, after_global)) |
15703 | 769 |
true (map (fst o fst) concl) propp |
12065 | 770 |
end; |
5820 | 771 |
|
17112 | 772 |
fun local_goal' prep_att prepp kind (check: bool -> state -> state) |
773 |
after_local pr raw_args int state = |
|
774 |
let val args = Attrib.map_specs (prep_att (theory_of state)) raw_args in |
|
775 |
state |
|
776 |
|> setup_goal open_block prepp (K I) (kind (map (snd o fst) args)) |
|
777 |
(AfterQed (after_local, K I)) pr (map (fst o fst) args) (map snd args) |
|
778 |
|> warn_extra_tfrees state |
|
779 |
|> check int |
|
780 |
end; |
|
5820 | 781 |
|
17112 | 782 |
fun local_goal prep_att prepp kind f pr args = |
783 |
local_goal' prep_att prepp kind (K I) f pr args false; |
|
10936 | 784 |
|
16813 | 785 |
in |
786 |
||
17112 | 787 |
val multi_theorem = global_goal Attrib.global_attribute Locale.read_context_statement; |
788 |
val multi_theorem_i = global_goal (K I) Locale.cert_context_statement; |
|
789 |
val show = local_goal' Attrib.local_attribute ProofContext.bind_propp Show; |
|
790 |
val show_i = local_goal' (K I) ProofContext.bind_propp_i Show; |
|
791 |
val have = local_goal Attrib.local_attribute ProofContext.bind_propp Have; |
|
792 |
val have_i = local_goal (K I) ProofContext.bind_propp_i Have; |
|
15703 | 793 |
|
16813 | 794 |
end; |
795 |
||
5820 | 796 |
|
797 |
||
798 |
(** conclusions **) |
|
799 |
||
800 |
(* current goal *) |
|
801 |
||
15531 | 802 |
fun current_goal (State (Node {context, goal = SOME goal, ...}, _)) = (context, goal) |
5820 | 803 |
| current_goal state = raise STATE ("No current goal!", state); |
804 |
||
15531 | 805 |
fun assert_current_goal true (state as State (Node {goal = NONE, ...}, _)) = |
6404 | 806 |
raise STATE ("No goal in this block!", state) |
15531 | 807 |
| assert_current_goal false (state as State (Node {goal = SOME _, ...}, _)) = |
6404 | 808 |
raise STATE ("Goal present in this block!", state) |
809 |
| assert_current_goal _ state = state; |
|
5820 | 810 |
|
12010 | 811 |
fun assert_bottom b (state as State (_, nodes)) = |
812 |
let val bot = (length nodes <= 2) in |
|
813 |
if b andalso not bot then raise STATE ("Not at bottom of proof!", state) |
|
814 |
else if not b andalso bot then raise STATE ("Already at bottom of proof!", state) |
|
815 |
else state |
|
816 |
end; |
|
5820 | 817 |
|
6404 | 818 |
val at_bottom = can (assert_bottom true o close_block); |
819 |
||
6932 | 820 |
fun end_proof bot state = |
5820 | 821 |
state |
822 |
|> assert_forward |
|
823 |
|> close_block |
|
824 |
|> assert_bottom bot |
|
7011 | 825 |
|> assert_current_goal true |
826 |
|> goal_facts (K []); |
|
5820 | 827 |
|
828 |
||
6404 | 829 |
(* local_qed *) |
5820 | 830 |
|
6982 | 831 |
fun finish_local (print_result, print_rule) state = |
5820 | 832 |
let |
15703 | 833 |
val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), (AfterQed (after_local, _), pr))) = |
834 |
current_goal state; |
|
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
835 |
val outer_state = state |> close_block; |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
836 |
val outer_ctxt = context_of outer_state; |
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
837 |
|
15570 | 838 |
val ts = List.concat tss; |
12146 | 839 |
val results = prep_result state ts raw_thm; |
840 |
val resultq = |
|
841 |
results |> map (ProofContext.export false goal_ctxt outer_ctxt) |
|
842 |
|> Seq.commute |> Seq.map (Library.unflat tss); |
|
9469 | 843 |
|
15703 | 844 |
val (attss, opt_solve) = |
5820 | 845 |
(case kind of |
12971 | 846 |
Show attss => (attss, |
15703 | 847 |
export_goals goal_ctxt (if pr then print_rule else (K (K ()))) results) |
848 |
| Have attss => (attss, Seq.single) |
|
6932 | 849 |
| _ => err_malformed "finish_local" state); |
5820 | 850 |
in |
12971 | 851 |
conditional pr (fn () => print_result goal_ctxt |
16450 | 852 |
(kind_name (theory_of state) kind, names ~~ Library.unflat tss results)); |
8617
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8582
diff
changeset
|
853 |
outer_state |
12549 | 854 |
|> auto_bind_facts (ProofContext.generalize goal_ctxt outer_ctxt ts) |
9469 | 855 |
|> (fn st => Seq.map (fn res => |
15703 | 856 |
note_thmss_i ((names ~~ attss) ~~ map (single o Thm.no_attributes) res) st) resultq) |
9469 | 857 |
|> (Seq.flat o Seq.map opt_solve) |
17034 | 858 |
|> (Seq.flat o Seq.map (after_local results)) |
5820 | 859 |
end; |
860 |
||
17112 | 861 |
fun local_qed asm opt_text print state = |
6404 | 862 |
state |
6932 | 863 |
|> end_proof false |
17112 | 864 |
|> refine (Method.finish_text asm opt_text) |
6982 | 865 |
|> (Seq.flat o Seq.map (finish_local print)); |
5820 | 866 |
|
867 |
||
12703
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents:
12698
diff
changeset
|
868 |
(* global_qed *) |
12065 | 869 |
|
17112 | 870 |
fun check_result msg state sq = |
871 |
(case Seq.pull sq of |
|
872 |
NONE => raise STATE (msg, state) |
|
873 |
| SOME res => Seq.cons res); |
|
874 |
||
6950 | 875 |
fun finish_global state = |
5820 | 876 |
let |
15703 | 877 |
val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), (AfterQed (_, after_global), _))) = |
878 |
current_goal state; |
|
12065 | 879 |
val locale_ctxt = context_of (state |> close_block); |
880 |
val theory_ctxt = context_of (state |> close_block |> close_block); |
|
15703 | 881 |
val {kind = k, theory_spec = ((name, atts), attss), |
882 |
locale_spec, view = (target_view, elems_view), export} = |
|
13399
c136276dc847
support locale ``views'' (for cumulative predicates);
wenzelm
parents:
13377
diff
changeset
|
883 |
(case kind of Theorem x => x | _ => err_malformed "finish_global" state); |
15703 | 884 |
val locale_name = Option.map fst locale_spec; |
5820 | 885 |
|
15570 | 886 |
val ts = List.concat tss; |
15696 | 887 |
val locale_results = map (export elems_view goal_ctxt locale_ctxt) |
13377 | 888 |
(prep_result state ts raw_thm); |
14215
ebf291f3b449
Improvements to Isar/Locales: premises generated by "includes" elements
ballarin
parents:
14129
diff
changeset
|
889 |
|
13377 | 890 |
val results = map (Drule.strip_shyps_warning o |
15696 | 891 |
export target_view locale_ctxt theory_ctxt) locale_results; |
12703
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents:
12698
diff
changeset
|
892 |
|
12959 | 893 |
val (theory', results') = |
894 |
theory_of state |
|
15696 | 895 |
|> (case locale_spec of NONE => I |
896 |
| SOME (loc, (loc_atts, loc_attss)) => fn thy => |
|
12959 | 897 |
if length names <> length loc_attss then |
898 |
raise THEORY ("Bad number of locale attributes", [thy]) |
|
13335 | 899 |
else (thy, locale_ctxt) |
15696 | 900 |
|> Locale.add_thmss k loc ((names ~~ Library.unflat tss locale_results) ~~ loc_attss) |
13335 | 901 |
|> (fn ((thy', ctxt'), res) => |
12959 | 902 |
if name = "" andalso null loc_atts then thy' |
13335 | 903 |
else (thy', ctxt') |
15696 | 904 |
|> (#1 o #1 o Locale.add_thmss k loc [((name, List.concat (map #2 res)), loc_atts)]))) |
15703 | 905 |
|> Locale.smart_note_thmss k locale_name |
12703
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
wenzelm
parents:
12698
diff
changeset
|
906 |
((names ~~ attss) ~~ map (single o Thm.no_attributes) (Library.unflat tss results)) |
12959 | 907 |
|> (fn (thy, res) => (thy, res) |
15703 | 908 |
|>> (#1 o Locale.smart_note_thmss k locale_name |
15570 | 909 |
[((name, atts), [(List.concat (map #2 res), [])])])); |
17112 | 910 |
in ((after_global (locale_results, results) theory', locale_ctxt), ((k, name), results')) end; |
911 |
||
912 |
fun global_qeds asm opt_text = |
|
913 |
end_proof true |
|
914 |
#> refine (Method.finish_text asm opt_text) |
|
915 |
#> Seq.map finish_global |
|
916 |
|> Seq.DETERM; (*backtracking may destroy theory!*) |
|
917 |
||
918 |
fun global_qed asm opt_text state = |
|
919 |
state |
|
920 |
|> global_qeds asm opt_text |
|
921 |
|> check_result "Failed to finish proof" state |
|
922 |
|> Seq.hd; |
|
12959 | 923 |
|
5820 | 924 |
|
17112 | 925 |
(* structured proof steps *) |
926 |
||
927 |
fun proof opt_text state = |
|
928 |
state |
|
929 |
|> assert_backward |
|
930 |
|> refine (if_none opt_text Method.default_text) |
|
931 |
|> Seq.map (goal_facts (K [])) |
|
932 |
|> Seq.map enter_forward; |
|
933 |
||
934 |
fun local_terminal_proof (text, opt_text) pr = |
|
935 |
Seq.THEN (proof (SOME text), local_qed true opt_text pr); |
|
936 |
||
937 |
val local_default_proof = local_terminal_proof (Method.default_text, NONE); |
|
938 |
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE); |
|
939 |
fun local_done_proof pr = Seq.THEN (proof (SOME Method.done_text), local_qed false NONE pr); |
|
940 |
||
941 |
fun global_term_proof asm (text, opt_text) state = |
|
5820 | 942 |
state |
17112 | 943 |
|> proof (SOME text) |
944 |
|> check_result "Terminal proof method failed" state |
|
945 |
|> (Seq.flat o Seq.map (global_qeds asm opt_text)) |
|
946 |
|> check_result "Failed to finish proof (after successful terminal method)" state |
|
947 |
|> Seq.hd; |
|
948 |
||
949 |
val global_terminal_proof = global_term_proof true; |
|
950 |
val global_default_proof = global_terminal_proof (Method.default_text, NONE); |
|
951 |
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE); |
|
952 |
val global_done_proof = global_term_proof false (Method.done_text, NONE); |
|
953 |
||
954 |
||
955 |
(* unstructured proof steps *) |
|
956 |
||
957 |
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i))); |
|
958 |
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i))); |
|
959 |
||
17166 | 960 |
fun apply text = assert_backward #> refine text #> Seq.map (goal_facts (K [])); |
961 |
fun apply_end text = assert_forward #> refine_end text; |
|
5820 | 962 |
|
963 |
||
6896 | 964 |
|
965 |
(** blocks **) |
|
966 |
||
967 |
fun begin_block state = |
|
968 |
state |
|
969 |
|> assert_forward |
|
970 |
|> new_block |
|
971 |
|> open_block; |
|
972 |
||
16813 | 973 |
fun next_block state = |
974 |
state |
|
975 |
|> assert_forward |
|
976 |
|> close_block |
|
977 |
|> new_block |
|
978 |
|> reset_facts; |
|
6896 | 979 |
|
980 |
fun end_block state = |
|
981 |
state |
|
982 |
|> assert_forward |
|
983 |
|> close_block |
|
984 |
|> assert_current_goal false |
|
985 |
|> close_block |
|
6932 | 986 |
|> transfer_facts state; |
6896 | 987 |
|
5820 | 988 |
end; |