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