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