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