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