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