author | haftmann |
Thu, 24 Jul 2025 17:46:29 +0200 | |
changeset 82902 | 99a720d3ed8f |
parent 82589 | 255dcbe53c50 |
permissions | -rw-r--r-- |
5828 | 1 |
(* Title: Pure/Isar/toplevel.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
26602
5534b6a6b810
made purely value-oriented, moved global state to structure Isar (cf. isar.ML);
wenzelm
parents:
26491
diff
changeset
|
4 |
Isabelle/Isar toplevel transactions. |
5828 | 5 |
*) |
6 |
||
7 |
signature TOPLEVEL = |
|
8 |
sig |
|
19063 | 9 |
exception UNDEF |
5828 | 10 |
type state |
76415 | 11 |
val make_state: theory option -> state |
7732 | 12 |
val is_toplevel: state -> bool |
18589 | 13 |
val is_theory: state -> bool |
14 |
val is_proof: state -> bool |
|
51555 | 15 |
val is_skipped_proof: state -> bool |
17076 | 16 |
val level: state -> int |
67391 | 17 |
val previous_theory_of: state -> theory option |
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
18 |
val output_of: state -> Latex.text option |
21506 | 19 |
val context_of: state -> Proof.context |
22089 | 20 |
val generic_theory_of: state -> generic_theory |
5828 | 21 |
val theory_of: state -> theory |
22 |
val proof_of: state -> Proof.state |
|
18589 | 23 |
val proof_position_of: state -> int |
68505 | 24 |
val is_end_theory: state -> bool |
37953
ddc3b72f9a42
simplified handling of theory begin/end wrt. toplevel and theory loader;
wenzelm
parents:
37951
diff
changeset
|
25 |
val end_theory: Position.T -> state -> theory |
67381 | 26 |
val presentation_context: state -> Proof.context |
27 |
val presentation_state: Proof.context -> state |
|
56893 | 28 |
val pretty_context: state -> Pretty.T list |
56887
1ca814da47ae
clarified print_state, which goes back to TTY loop before Proof General, and before separate print_context;
wenzelm
parents:
56867
diff
changeset
|
29 |
val pretty_state: state -> Pretty.T list |
37858 | 30 |
val pretty_abstract: state -> Pretty.T |
76815 | 31 |
type presentation = state -> Latex.text |
16682 | 32 |
type transition |
5828 | 33 |
val empty: transition |
27427 | 34 |
val name_of: transition -> string |
28105 | 35 |
val pos_of: transition -> Position.T |
69877 | 36 |
val timing_of: transition -> Time.time |
60076 | 37 |
val type_error: transition -> string |
5828 | 38 |
val name: string -> transition -> transition |
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
39 |
val position: Position.T -> transition -> transition |
69887 | 40 |
val markers: Input.source list -> transition -> transition |
69877 | 41 |
val timing: Time.time -> transition -> transition |
44187
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
42 |
val init_theory: (unit -> theory) -> transition -> transition |
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
43 |
val is_init: transition -> bool |
44186
806f0ec1a43d
simplified Toplevel.init_theory: discontinued special master argument;
wenzelm
parents:
44185
diff
changeset
|
44 |
val modify_init: (unit -> theory) -> transition -> transition |
6689 | 45 |
val exit: transition -> transition |
76815 | 46 |
val present: presentation -> transition -> transition |
5828 | 47 |
val keep: (state -> unit) -> transition -> transition |
7612 | 48 |
val keep': (bool -> state -> unit) -> transition -> transition |
60190
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
49 |
val keep_proof: (state -> unit) -> transition -> transition |
51268
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents:
51241
diff
changeset
|
50 |
val is_ignored: transition -> bool |
74672 | 51 |
val is_malformed: transition -> bool |
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
52 |
val ignored: Position.T -> transition |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
53 |
val malformed: Position.T -> string -> transition |
26491 | 54 |
val generic_theory: (generic_theory -> generic_theory) -> transition -> transition |
76815 | 55 |
val theory': (bool -> theory -> theory) -> presentation option -> transition -> transition |
49012
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
wenzelm
parents:
49011
diff
changeset
|
56 |
val theory: (theory -> theory) -> transition -> transition |
72434 | 57 |
val begin_main_target: bool -> (theory -> local_theory) -> transition -> transition |
58 |
val end_main_target: transition -> transition |
|
72453 | 59 |
val begin_nested_target: (Context.generic -> local_theory) -> transition -> transition |
72434 | 60 |
val end_nested_target: transition -> transition |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59923
diff
changeset
|
61 |
val local_theory': (bool * Position.T) option -> (xstring * Position.T) option -> |
76815 | 62 |
(bool -> local_theory -> local_theory) -> presentation option -> transition -> transition |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59923
diff
changeset
|
63 |
val local_theory: (bool * Position.T) option -> (xstring * Position.T) option -> |
59923
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59472
diff
changeset
|
64 |
(local_theory -> local_theory) -> transition -> transition |
76815 | 65 |
val present_local_theory: (xstring * Position.T) option -> presentation -> |
24453 | 66 |
transition -> transition |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59923
diff
changeset
|
67 |
val local_theory_to_proof': (bool * Position.T) option -> (xstring * Position.T) option -> |
45488
6d71d9e52369
pass positions for named targets, for formal links in the document model;
wenzelm
parents:
44304
diff
changeset
|
68 |
(bool -> local_theory -> Proof.state) -> transition -> transition |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59923
diff
changeset
|
69 |
val local_theory_to_proof: (bool * Position.T) option -> (xstring * Position.T) option -> |
45488
6d71d9e52369
pass positions for named targets, for formal links in the document model;
wenzelm
parents:
44304
diff
changeset
|
70 |
(local_theory -> Proof.state) -> transition -> transition |
17363 | 71 |
val theory_to_proof: (theory -> Proof.state) -> transition -> transition |
21007 | 72 |
val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition |
68876
cefaac3d24ff
no reset_proof for notepad: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68875
diff
changeset
|
73 |
val forget_proof: transition -> transition |
49863
b5fb6e7f8d81
more informative errors for 'proof' and 'apply' steps;
wenzelm
parents:
49062
diff
changeset
|
74 |
val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition |
17904
21c6894b5998
simplified interfaces proof/proof' etc.: perform ProofHistory.apply(s)/current internally;
wenzelm
parents:
17513
diff
changeset
|
75 |
val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition |
49863
b5fb6e7f8d81
more informative errors for 'proof' and 'apply' steps;
wenzelm
parents:
49062
diff
changeset
|
76 |
val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition |
21177 | 77 |
val proof: (Proof.state -> Proof.state) -> transition -> transition |
33390 | 78 |
val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition |
60693
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
79 |
val skip_proof: (unit -> unit) -> transition -> transition |
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
80 |
val skip_proof_open: transition -> transition |
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
81 |
val skip_proof_close: transition -> transition |
52536 | 82 |
val exec_id: Document_ID.exec -> transition -> transition |
28425 | 83 |
val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b |
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
84 |
val transition: bool -> transition -> state -> state * (exn * string) option |
51323 | 85 |
val command_errors: bool -> transition -> state -> Runtime.error list * state option |
51284
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51282
diff
changeset
|
86 |
val command_exception: bool -> transition -> state -> state |
56937 | 87 |
val reset_theory: state -> state option |
88 |
val reset_proof: state -> state option |
|
68877
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
89 |
val reset_notepad: state -> state option |
69887 | 90 |
val fork_presentation: transition -> transition * transition |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
91 |
type result |
73687
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
92 |
val join_results: result -> (state * transition * state) list |
68839 | 93 |
val element_result: Keyword.keywords -> transition Thy_Element.element -> state -> result * state |
5828 | 94 |
end; |
95 |
||
6965 | 96 |
structure Toplevel: TOPLEVEL = |
5828 | 97 |
struct |
98 |
||
99 |
(** toplevel state **) |
|
100 |
||
31476
c5d2899b6de9
moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
31431
diff
changeset
|
101 |
exception UNDEF = Runtime.UNDEF; |
19063 | 102 |
|
103 |
||
21958
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
wenzelm
parents:
21861
diff
changeset
|
104 |
(* datatype node *) |
21294 | 105 |
|
5828 | 106 |
datatype node = |
69883 | 107 |
Toplevel |
108 |
(*toplevel outside of theory body*) | |
|
69878 | 109 |
Theory of generic_theory |
110 |
(*global or local theory*) | |
|
33390 | 111 |
Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory) |
27576
7afff36043e6
eliminated internal command history -- superceeded by global Isar state (cf. isar.ML);
wenzelm
parents:
27564
diff
changeset
|
112 |
(*proof node, finish, original theory*) | |
51555 | 113 |
Skipped_Proof of int * (generic_theory * generic_theory); |
27564
fc6d34e49e17
replaced obsolete ProofHistory by ProofNode (backtracking only);
wenzelm
parents:
27500
diff
changeset
|
114 |
(*proof depth, resulting theory, original theory*) |
5828 | 115 |
|
69878 | 116 |
val theory_node = fn Theory gthy => SOME gthy | _ => NONE; |
18589 | 117 |
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE; |
51555 | 118 |
val skipped_proof_node = fn Skipped_Proof _ => true | _ => false; |
18589 | 119 |
|
69883 | 120 |
fun cases_node f _ _ Toplevel = f () |
121 |
| cases_node _ g _ (Theory gthy) = g gthy |
|
122 |
| cases_node _ _ h (Proof (prf, _)) = h (Proof_Node.current prf) |
|
123 |
| cases_node _ g _ (Skipped_Proof (_, (gthy, _))) = g gthy; |
|
124 |
||
125 |
fun cases_proper_node g h = cases_node (fn () => raise UNDEF) g h; |
|
126 |
||
127 |
val get_theory = cases_node (K NONE) (SOME o Context.theory_of) (SOME o Proof.theory_of); |
|
19063 | 128 |
|
21958
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
wenzelm
parents:
21861
diff
changeset
|
129 |
|
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
wenzelm
parents:
21861
diff
changeset
|
130 |
(* datatype state *) |
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
wenzelm
parents:
21861
diff
changeset
|
131 |
|
69883 | 132 |
type node_presentation = node * Proof.context; |
5828 | 133 |
|
69886 | 134 |
fun init_presentation () = |
135 |
Proof_Context.init_global (Theory.get_pure_bootstrap ()); |
|
136 |
||
137 |
fun node_presentation node = |
|
138 |
(node, cases_node init_presentation Context.proof_of Proof.context_of node); |
|
139 |
||
69883 | 140 |
datatype state = |
76817
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
141 |
State of node_presentation * (theory option * Latex.text future option); |
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
142 |
(*current node with presentation context, previous theory, document output*) |
62895
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
wenzelm
parents:
62889
diff
changeset
|
143 |
|
69883 | 144 |
fun node_of (State ((node, _), _)) = node; |
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
145 |
fun previous_theory_of (State (_, (prev_thy, _))) = prev_thy; |
76817
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
146 |
fun output_of (State (_, (_, output))) = Option.map Future.join output; |
5828 | 147 |
|
76415 | 148 |
fun make_state opt_thy = |
149 |
let val node = (case opt_thy of NONE => Toplevel | SOME thy => Theory (Context.Theory thy)) |
|
150 |
in State (node_presentation node, (NONE, NONE)) end; |
|
69883 | 151 |
|
152 |
fun level state = |
|
153 |
(case node_of state of |
|
154 |
Toplevel => 0 |
|
155 |
| Theory _ => 0 |
|
156 |
| Proof (prf, _) => Proof.level (Proof_Node.current prf) |
|
157 |
| Skipped_Proof (d, _) => d + 1); (*different notion of proof depth!*) |
|
17076 | 158 |
|
69883 | 159 |
fun str_of_state state = |
160 |
(case node_of state of |
|
161 |
Toplevel => |
|
162 |
(case previous_theory_of state of |
|
163 |
NONE => "at top level" |
|
77889
5db014c36f42
clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents:
76817
diff
changeset
|
164 |
| SOME thy => "at top level, result theory " ^ quote (Context.theory_base_name thy)) |
69883 | 165 |
| Theory (Context.Theory _) => "in theory mode" |
166 |
| Theory (Context.Proof _) => "in local theory mode" |
|
167 |
| Proof _ => "in proof mode" |
|
168 |
| Skipped_Proof _ => "in skipped proof mode"); |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
169 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
170 |
|
27576
7afff36043e6
eliminated internal command history -- superceeded by global Isar state (cf. isar.ML);
wenzelm
parents:
27564
diff
changeset
|
171 |
(* current node *) |
5828 | 172 |
|
69883 | 173 |
fun is_toplevel state = (case node_of state of Toplevel => true | _ => false); |
5828 | 174 |
|
69883 | 175 |
fun is_theory state = |
176 |
not (is_toplevel state) andalso is_some (theory_node (node_of state)); |
|
18589 | 177 |
|
69883 | 178 |
fun is_proof state = |
179 |
not (is_toplevel state) andalso is_some (proof_node (node_of state)); |
|
5828 | 180 |
|
69883 | 181 |
fun is_skipped_proof state = |
182 |
not (is_toplevel state) andalso skipped_proof_node (node_of state); |
|
30801 | 183 |
|
69883 | 184 |
fun proper_node_of state = if is_toplevel state then raise UNDEF else node_of state; |
185 |
fun proper_node_case f g state = cases_proper_node f g (proper_node_of state); |
|
186 |
||
187 |
val context_of = proper_node_case Context.proof_of Proof.context_of; |
|
188 |
val generic_theory_of = proper_node_case I (Context.Proof o Proof.context_of); |
|
189 |
val theory_of = proper_node_case Context.theory_of Proof.theory_of; |
|
190 |
val proof_of = proper_node_case (fn _ => error "No proof state") I; |
|
17208 | 191 |
|
18589 | 192 |
fun proof_position_of state = |
69883 | 193 |
(case proper_node_of state of |
33390 | 194 |
Proof (prf, _) => Proof_Node.position prf |
60094 | 195 |
| _ => ~1); |
6664 | 196 |
|
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
197 |
fun is_end_theory (State ((Toplevel, _), (SOME _, _))) = true |
68505 | 198 |
| is_end_theory _ = false; |
199 |
||
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
200 |
fun end_theory _ (State ((Toplevel, _), (SOME thy, _))) = thy |
68869 | 201 |
| end_theory pos _ = error ("Malformed theory" ^ Position.here pos); |
37953
ddc3b72f9a42
simplified handling of theory begin/end wrt. toplevel and theory loader;
wenzelm
parents:
37951
diff
changeset
|
202 |
|
5828 | 203 |
|
67381 | 204 |
(* presentation context *) |
205 |
||
206 |
structure Presentation_State = Proof_Data |
|
207 |
( |
|
208 |
type T = state option; |
|
209 |
fun init _ = NONE; |
|
210 |
); |
|
211 |
||
69883 | 212 |
fun presentation_context0 (State ((_, pr_ctxt), _)) = pr_ctxt; |
67642
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
wenzelm
parents:
67641
diff
changeset
|
213 |
|
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
wenzelm
parents:
67641
diff
changeset
|
214 |
fun presentation_context (state as State (current, _)) = |
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
wenzelm
parents:
67641
diff
changeset
|
215 |
presentation_context0 state |
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
216 |
|> Presentation_State.put (SOME (State (current, (NONE, NONE)))); |
67381 | 217 |
|
218 |
fun presentation_state ctxt = |
|
77908
a6bd716a6124
tuned: concise combinators instead of bulky case-expressions;
wenzelm
parents:
77889
diff
changeset
|
219 |
Presentation_State.get ctxt |
a6bd716a6124
tuned: concise combinators instead of bulky case-expressions;
wenzelm
parents:
77889
diff
changeset
|
220 |
|> \<^if_none>\<open>State (node_presentation (Theory (Context.Proof ctxt)), (NONE, NONE))\<close>; |
67381 | 221 |
|
222 |
||
16815 | 223 |
(* print state *) |
224 |
||
56893 | 225 |
fun pretty_context state = |
69883 | 226 |
if is_toplevel state then [] |
227 |
else |
|
228 |
let |
|
229 |
val gthy = |
|
230 |
(case node_of state of |
|
231 |
Toplevel => raise Match |
|
232 |
| Theory gthy => gthy |
|
233 |
| Proof (_, (_, gthy)) => gthy |
|
234 |
| Skipped_Proof (_, (_, gthy)) => gthy); |
|
235 |
val lthy = Context.cases Named_Target.theory_init I gthy; |
|
236 |
in Local_Theory.pretty lthy end; |
|
16815 | 237 |
|
56887
1ca814da47ae
clarified print_state, which goes back to TTY loop before Proof General, and before separate print_context;
wenzelm
parents:
56867
diff
changeset
|
238 |
fun pretty_state state = |
69883 | 239 |
(case node_of state of |
240 |
Toplevel => [] |
|
241 |
| Theory _ => [] |
|
242 |
| Proof (prf, _) => Proof.pretty_state (Proof_Node.current prf) |
|
243 |
| Skipped_Proof (d, _) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)]); |
|
56867 | 244 |
|
37858 | 245 |
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">"); |
246 |
||
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
79113
diff
changeset
|
247 |
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_ML o pretty_abstract); |
62663 | 248 |
|
16815 | 249 |
|
15668 | 250 |
|
5828 | 251 |
(** toplevel transitions **) |
252 |
||
76815 | 253 |
(* primitive transitions *) |
74831
32490add64b4
tuned signature: more explicit types for presentation;
wenzelm
parents:
74672
diff
changeset
|
254 |
|
76815 | 255 |
type presentation = state -> Latex.text; |
69888 | 256 |
|
257 |
datatype trans = |
|
258 |
(*init theory*) |
|
259 |
Init of unit -> theory | |
|
260 |
(*formal exit of theory*) |
|
261 |
Exit | |
|
76411 | 262 |
(*keep state unchanged*) |
76815 | 263 |
Keep of (bool -> state -> unit) * presentation option | |
69888 | 264 |
(*node transaction and presentation*) |
76815 | 265 |
Transaction of (bool -> node -> node_presentation) * presentation option; |
69888 | 266 |
|
267 |
local |
|
7022 | 268 |
|
76817
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
269 |
fun present_state fork g node_pr prev_thy = |
76815 | 270 |
let |
271 |
val state = State (node_pr, (prev_thy, NONE)); |
|
76817
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
272 |
fun present pr = |
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
273 |
if fork andalso Future.proofs_enabled 1 then |
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
274 |
Execution.fork {name = "Toplevel.present_state", pos = Position.thread_data (), pri = ~1} |
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
275 |
(fn () => pr state) |
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
276 |
else Future.value (pr state); |
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
277 |
in State (node_pr, (prev_thy, Option.map present g)) end; |
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
278 |
|
76814 | 279 |
fun no_update f g state = |
76809 | 280 |
Runtime.controlled_execution (try generic_theory_of state) |
281 |
(fn () => |
|
282 |
let |
|
283 |
val prev_thy = previous_theory_of state; |
|
76814 | 284 |
val () = f state; |
76815 | 285 |
val node_pr = node_presentation (node_of state); |
76817
0eb3ea050fa9
support asynchronous presentation commands, but not for "no_update" / "Keep", which is usually forked via "Toplevel.diag";
wenzelm
parents:
76816
diff
changeset
|
286 |
in present_state false g node_pr prev_thy end) () |
76809 | 287 |
|
76813 | 288 |
fun update f g state = |
289 |
Runtime.controlled_execution (try generic_theory_of state) |
|
290 |
(fn () => |
|
291 |
let |
|
78488
44ffc06187e0
proper prev_thy (amending 92a547feec88), notably for the sake of 'print_theorems', which is the only use of Toplevel.previous_theory_of;
wenzelm
parents:
78469
diff
changeset
|
292 |
val node = node_of state; |
44ffc06187e0
proper prev_thy (amending 92a547feec88), notably for the sake of 'print_theorems', which is the only use of Toplevel.previous_theory_of;
wenzelm
parents:
78469
diff
changeset
|
293 |
val prev_thy = get_theory node; |
44ffc06187e0
proper prev_thy (amending 92a547feec88), notably for the sake of 'print_theorems', which is the only use of Toplevel.previous_theory_of;
wenzelm
parents:
78469
diff
changeset
|
294 |
in present_state true g (f node) prev_thy end) (); |
6689 | 295 |
|
69887 | 296 |
fun apply_tr int trans state = |
297 |
(case (trans, node_of state) of |
|
298 |
(Init f, Toplevel) => |
|
299 |
Runtime.controlled_execution NONE (fn () => |
|
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
300 |
State (node_presentation (Theory (Context.Theory (f ()))), (NONE, NONE))) () |
76813 | 301 |
| (Exit, Theory (Context.Theory thy)) => |
69883 | 302 |
let |
303 |
val State ((node', pr_ctxt), _) = |
|
76813 | 304 |
state |> update |
71023
35a8e15b7e03
more robust Thm.expose_theory -- ensure that PIDE export happens in the proper theory context;
wenzelm
parents:
70398
diff
changeset
|
305 |
(fn _ => |
35a8e15b7e03
more robust Thm.expose_theory -- ensure that PIDE export happens in the proper theory context;
wenzelm
parents:
70398
diff
changeset
|
306 |
node_presentation |
35a8e15b7e03
more robust Thm.expose_theory -- ensure that PIDE export happens in the proper theory context;
wenzelm
parents:
70398
diff
changeset
|
307 |
(Theory (Context.Theory (tap Thm.expose_theory (Theory.end_theory thy))))) |
76815 | 308 |
NONE; |
74833
fe9e590ae52f
output for document commands like 'section', 'text' is defined in user-space, as part of the command transaction;
wenzelm
parents:
74831
diff
changeset
|
309 |
in State ((Toplevel, pr_ctxt), (get_theory node', NONE)) end |
76814 | 310 |
| (Keep (f, g), _) => no_update (fn x => f int x) g state |
69887 | 311 |
| (Transaction _, Toplevel) => raise UNDEF |
76813 | 312 |
| (Transaction (f, g), _) => update (fn x => f int x) g state |
69887 | 313 |
| _ => raise UNDEF); |
5828 | 314 |
|
76812 | 315 |
fun apply_body _ [] _ = raise UNDEF |
316 |
| apply_body int [tr] state = apply_tr int tr state |
|
317 |
| apply_body int (tr :: trs) state = |
|
318 |
apply_body int trs state |
|
76811
56d76e8cecf4
discontinued somewhat pointless exception FAILURE with its "alt_state", which was originally due to quasi-mutable states (see 169e5b07ec06);
wenzelm
parents:
76810
diff
changeset
|
319 |
handle Runtime.UNDEF => apply_tr int tr state; |
6689 | 320 |
|
70134 | 321 |
fun apply_markers name markers (state as State ((node, pr_ctxt), prev_thy)) = |
69887 | 322 |
let |
323 |
val state' = |
|
324 |
Runtime.controlled_execution (try generic_theory_of state) |
|
70134 | 325 |
(fn () => State ((node, fold (Document_Marker.evaluate name) markers pr_ctxt), prev_thy)) (); |
76811
56d76e8cecf4
discontinued somewhat pointless exception FAILURE with its "alt_state", which was originally due to quasi-mutable states (see 169e5b07ec06);
wenzelm
parents:
76810
diff
changeset
|
326 |
in (state', NONE) end; |
69887 | 327 |
|
6689 | 328 |
in |
329 |
||
76811
56d76e8cecf4
discontinued somewhat pointless exception FAILURE with its "alt_state", which was originally due to quasi-mutable states (see 169e5b07ec06);
wenzelm
parents:
76810
diff
changeset
|
330 |
fun apply_capture int name markers trans state = |
78757 | 331 |
(case Exn.capture_body (fn () => apply_body int trans state |> apply_markers name markers) of |
78725
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
wenzelm
parents:
78488
diff
changeset
|
332 |
Exn.Res res => res |
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
wenzelm
parents:
78488
diff
changeset
|
333 |
| Exn.Exn exn => (state, SOME exn)); |
6689 | 334 |
|
335 |
end; |
|
5828 | 336 |
|
337 |
||
338 |
(* datatype transition *) |
|
339 |
||
340 |
datatype transition = Transition of |
|
69887 | 341 |
{name: string, (*command name*) |
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
342 |
pos: Position.T, (*source position*) |
69887 | 343 |
markers: Input.source list, (*semantic document markers*) |
344 |
timing: Time.time, (*prescient timing information*) |
|
345 |
trans: trans list}; (*primitive transitions (union)*) |
|
5828 | 346 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
347 |
fun make_transition (name, pos, markers, timing, trans) = |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
348 |
Transition {name = name, pos = pos, markers = markers, timing = timing, trans = trans}; |
5828 | 349 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
350 |
fun map_transition f (Transition {name, pos, markers, timing, trans}) = |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
351 |
make_transition (f (name, pos, markers, timing, trans)); |
5828 | 352 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
353 |
val empty = make_transition ("", Position.none, [], Time.zeroTime, []); |
5828 | 354 |
|
355 |
||
356 |
(* diagnostics *) |
|
357 |
||
27427 | 358 |
fun name_of (Transition {name, ...}) = name; |
28105 | 359 |
fun pos_of (Transition {pos, ...}) = pos; |
69877 | 360 |
fun timing_of (Transition {timing, ...}) = timing; |
5828 | 361 |
|
60076 | 362 |
fun command_msg msg tr = |
363 |
msg ^ "command " ^ quote (Markup.markup Markup.keyword1 (name_of tr)) ^ |
|
364 |
Position.here (pos_of tr); |
|
5828 | 365 |
|
60076 | 366 |
fun at_command tr = command_msg "At " tr; |
367 |
fun type_error tr = command_msg "Bad context for " tr; |
|
5828 | 368 |
|
369 |
||
370 |
(* modify transitions *) |
|
371 |
||
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
372 |
fun name name = map_transition (fn (_, pos, markers, timing, trans) => |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
373 |
(name, pos, markers, timing, trans)); |
9010 | 374 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
375 |
fun position pos = map_transition (fn (name, _, markers, timing, trans) => |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
376 |
(name, pos, markers, timing, trans)); |
69887 | 377 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
378 |
fun markers markers = map_transition (fn (name, pos, _, timing, trans) => |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
379 |
(name, pos, markers, timing, trans)); |
14923 | 380 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
381 |
fun timing timing = map_transition (fn (name, pos, markers, _, trans) => |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
382 |
(name, pos, markers, timing, trans)); |
69877 | 383 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
384 |
fun add_trans tr = map_transition (fn (name, pos, markers, timing, trans) => |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
385 |
(name, pos, markers, timing, tr :: trans)); |
16607 | 386 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
387 |
val reset_trans = map_transition (fn (name, pos, markers, timing, _) => |
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
388 |
(name, pos, markers, timing, [])); |
5828 | 389 |
|
390 |
||
21007 | 391 |
(* basic transitions *) |
5828 | 392 |
|
44187
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
393 |
fun init_theory f = add_trans (Init f); |
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37953
diff
changeset
|
394 |
|
44187
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
395 |
fun is_init (Transition {trans = [Init _], ...}) = true |
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
396 |
| is_init _ = false; |
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
397 |
|
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents:
44186
diff
changeset
|
398 |
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr; |
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37953
diff
changeset
|
399 |
|
6689 | 400 |
val exit = add_trans Exit; |
30366
e3d788b9dffb
simplified presentation: built into transaction, pass state directly;
wenzelm
parents:
29516
diff
changeset
|
401 |
|
e3d788b9dffb
simplified presentation: built into transaction, pass state directly;
wenzelm
parents:
29516
diff
changeset
|
402 |
fun present_transaction f g = add_trans (Transaction (f, g)); |
76815 | 403 |
fun transaction f = present_transaction f NONE; |
404 |
fun transaction0 f = present_transaction (node_presentation oo f) NONE; |
|
5828 | 405 |
|
76815 | 406 |
fun present g = add_trans (Keep (fn _ => fn _ => (), SOME g)); |
407 |
fun keep f = add_trans (Keep (K f, NONE)); |
|
408 |
fun keep' f = add_trans (Keep (f, NONE)); |
|
5828 | 409 |
|
60190
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
410 |
fun keep_proof f = |
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
411 |
keep (fn st => |
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
412 |
if is_proof st then f st |
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
413 |
else if is_skipped_proof st then () |
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
414 |
else warning "No proof state"); |
906de96ba68a
allow diagnostic proof commands with skip_proofs;
wenzelm
parents:
60189
diff
changeset
|
415 |
|
74672 | 416 |
val ignoredN = "<ignored>"; |
417 |
val malformedN = "<malformed>"; |
|
418 |
||
419 |
fun is_ignored tr = name_of tr = ignoredN; |
|
420 |
fun is_malformed tr = name_of tr = malformedN; |
|
48772 | 421 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
422 |
fun ignored pos = |
74672 | 423 |
empty |> name ignoredN |> position pos |> keep (fn _ => ()); |
73098
8a20737e4ebf
support more command positions, analogous to Command.core_range in Isabelle/Scala;
wenzelm
parents:
72505
diff
changeset
|
424 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
425 |
fun malformed pos msg = |
74672 | 426 |
empty |> name malformedN |> position pos |> keep (fn _ => error msg); |
27840 | 427 |
|
21007 | 428 |
|
49012
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
wenzelm
parents:
49011
diff
changeset
|
429 |
(* theory transitions *) |
44304
7ee000ce5390
maintain recent future proofs at transaction boundaries;
wenzelm
parents:
44270
diff
changeset
|
430 |
|
27601
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
wenzelm
parents:
27583
diff
changeset
|
431 |
fun generic_theory f = transaction (fn _ => |
69886 | 432 |
(fn Theory gthy => node_presentation (Theory (f gthy)) |
26491 | 433 |
| _ => raise UNDEF)); |
434 |
||
74964
77a96ed74340
allow general command transactions with presentation;
wenzelm
parents:
74833
diff
changeset
|
435 |
fun theory' f = present_transaction (fn int => |
69878 | 436 |
(fn Theory (Context.Theory thy) => |
33725
a8481da77270
implicit name space grouping for theory/local_theory transactions;
wenzelm
parents:
33671
diff
changeset
|
437 |
let val thy' = thy |
49012
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
wenzelm
parents:
49011
diff
changeset
|
438 |
|> Sign.new_group |
33725
a8481da77270
implicit name space grouping for theory/local_theory transactions;
wenzelm
parents:
33671
diff
changeset
|
439 |
|> f int |
a8481da77270
implicit name space grouping for theory/local_theory transactions;
wenzelm
parents:
33671
diff
changeset
|
440 |
|> Sign.reset_group; |
69886 | 441 |
in node_presentation (Theory (Context.Theory thy')) end |
20963
a7fd8f05a2be
added type global_theory -- theory or local_theory;
wenzelm
parents:
20928
diff
changeset
|
442 |
| _ => raise UNDEF)); |
a7fd8f05a2be
added type global_theory -- theory or local_theory;
wenzelm
parents:
20928
diff
changeset
|
443 |
|
76815 | 444 |
fun theory f = theory' (K f) NONE; |
20963
a7fd8f05a2be
added type global_theory -- theory or local_theory;
wenzelm
parents:
20928
diff
changeset
|
445 |
|
72434 | 446 |
fun begin_main_target begin f = transaction (fn _ => |
69878 | 447 |
(fn Theory (Context.Theory thy) => |
20963
a7fd8f05a2be
added type global_theory -- theory or local_theory;
wenzelm
parents:
20928
diff
changeset
|
448 |
let |
20985 | 449 |
val lthy = f thy; |
72453 | 450 |
val gthy = |
451 |
if begin |
|
452 |
then Context.Proof lthy |
|
72505 | 453 |
else Target_Context.end_named_cmd lthy; |
56897
c668735fb8b5
print results as "state", to avoid intrusion into the source text;
wenzelm
parents:
56895
diff
changeset
|
454 |
val _ = |
60245 | 455 |
(case Local_Theory.pretty lthy of |
456 |
[] => () |
|
78469
53b59fa42696
prefer Output.writeln for theory "results", as opposed to Output.state for genuine proof states (see f8c412a45af8, c668735fb8b5, ecf80e37ed1a);
wenzelm
parents:
78021
diff
changeset
|
457 |
| prts => Pretty.writeln (Pretty.chunks prts)); |
69878 | 458 |
in (Theory gthy, lthy) end |
20963
a7fd8f05a2be
added type global_theory -- theory or local_theory;
wenzelm
parents:
20928
diff
changeset
|
459 |
| _ => raise UNDEF)); |
17076 | 460 |
|
72434 | 461 |
val end_main_target = transaction (fn _ => |
72505 | 462 |
(fn Theory (Context.Proof lthy) => (Theory (Target_Context.end_named_cmd lthy), lthy) |
21007 | 463 |
| _ => raise UNDEF)); |
464 |
||
72434 | 465 |
fun begin_nested_target f = transaction0 (fn _ => |
69878 | 466 |
(fn Theory gthy => |
72449
e1ee4a9902bd
centralized case distinction for beginning and ending nested targets in one place
haftmann
parents:
72436
diff
changeset
|
467 |
let |
72453 | 468 |
val lthy' = f gthy; |
72449
e1ee4a9902bd
centralized case distinction for beginning and ending nested targets in one place
haftmann
parents:
72436
diff
changeset
|
469 |
in Theory (Context.Proof lthy') end |
47069 | 470 |
| _ => raise UNDEF)); |
471 |
||
72434 | 472 |
val end_nested_target = transaction (fn _ => |
69878 | 473 |
(fn Theory (Context.Proof lthy) => |
72505 | 474 |
(case try Target_Context.end_nested_cmd lthy of |
475 |
SOME gthy' => (Theory gthy', lthy) |
|
47069 | 476 |
| NONE => raise UNDEF) |
477 |
| _ => raise UNDEF)); |
|
478 |
||
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
479 |
fun restricted_context (SOME (strict, scope)) = |
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
480 |
Proof_Context.map_naming (Name_Space.restricted strict scope) |
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
481 |
| restricted_context NONE = I; |
59939
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents:
59923
diff
changeset
|
482 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
483 |
fun local_theory' restricted target f = present_transaction (fn int => |
69878 | 484 |
(fn Theory gthy => |
21294 | 485 |
let |
72453 | 486 |
val (finish, lthy) = Target_Context.switch_named_cmd target gthy; |
47274 | 487 |
val lthy' = lthy |
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
488 |
|> restricted_context restricted |
49012
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
wenzelm
parents:
49011
diff
changeset
|
489 |
|> Local_Theory.new_group |
33725
a8481da77270
implicit name space grouping for theory/local_theory transactions;
wenzelm
parents:
33671
diff
changeset
|
490 |
|> f int |
a8481da77270
implicit name space grouping for theory/local_theory transactions;
wenzelm
parents:
33671
diff
changeset
|
491 |
|> Local_Theory.reset_group; |
69878 | 492 |
in (Theory (finish lthy'), lthy') end |
74964
77a96ed74340
allow general command transactions with presentation;
wenzelm
parents:
74833
diff
changeset
|
493 |
| _ => raise UNDEF)); |
15668 | 494 |
|
74964
77a96ed74340
allow general command transactions with presentation;
wenzelm
parents:
74833
diff
changeset
|
495 |
fun local_theory restricted target f = |
76815 | 496 |
local_theory' restricted target (K f) NONE; |
21007 | 497 |
|
74831
32490add64b4
tuned signature: more explicit types for presentation;
wenzelm
parents:
74672
diff
changeset
|
498 |
fun present_local_theory target g = present_transaction (fn _ => |
69878 | 499 |
(fn Theory gthy => |
72453 | 500 |
let val (finish, lthy) = Target_Context.switch_named_cmd target gthy; |
69878 | 501 |
in (Theory (finish lthy), lthy) end |
74831
32490add64b4
tuned signature: more explicit types for presentation;
wenzelm
parents:
74672
diff
changeset
|
502 |
| _ => raise UNDEF)) |
76815 | 503 |
(SOME g); |
21007 | 504 |
|
505 |
||
506 |
(* proof transitions *) |
|
507 |
||
27601
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
wenzelm
parents:
27583
diff
changeset
|
508 |
fun end_proof f = transaction (fn int => |
24795
6f5cb7885fd7
print_state_context: local theory context, not proof context;
wenzelm
parents:
24780
diff
changeset
|
509 |
(fn Proof (prf, (finish, _)) => |
33390 | 510 |
let val state = Proof_Node.current prf in |
21007 | 511 |
if can (Proof.assert_bottom true) state then |
512 |
let |
|
513 |
val ctxt' = f int state; |
|
514 |
val gthy' = finish ctxt'; |
|
69878 | 515 |
in (Theory gthy', ctxt') end |
21007 | 516 |
else raise UNDEF |
517 |
end |
|
69886 | 518 |
| Skipped_Proof (0, (gthy, _)) => node_presentation (Theory gthy) |
21007 | 519 |
| _ => raise UNDEF)); |
520 |
||
21294 | 521 |
local |
522 |
||
69882 | 523 |
fun begin_proof init_proof = transaction0 (fn int => |
69878 | 524 |
(fn Theory gthy => |
21294 | 525 |
let |
69882 | 526 |
val (finish, prf) = init_proof int gthy; |
67157 | 527 |
val document = Options.default_string "document"; |
528 |
val skip = (document = "" orelse document = "false") andalso Goal.skip_proofs_enabled (); |
|
58795 | 529 |
val schematic_goal = try Proof.schematic_goal prf; |
47274 | 530 |
val _ = |
58795 | 531 |
if skip andalso schematic_goal = SOME true then |
47274 | 532 |
warning "Cannot skip proof of schematic goal statement" |
533 |
else (); |
|
21294 | 534 |
in |
58795 | 535 |
if skip andalso schematic_goal = SOME false then |
51555 | 536 |
Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy)) |
47274 | 537 |
else Proof (Proof_Node.init prf, (finish, gthy)) |
21294 | 538 |
end |
539 |
| _ => raise UNDEF)); |
|
540 |
||
541 |
in |
|
542 |
||
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
543 |
fun local_theory_to_proof' restricted target f = begin_proof |
47274 | 544 |
(fn int => fn gthy => |
59923
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59472
diff
changeset
|
545 |
let |
72453 | 546 |
val (finish, lthy) = Target_Context.switch_named_cmd target gthy; |
59923
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59472
diff
changeset
|
547 |
val prf = lthy |
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
548 |
|> restricted_context restricted |
59923
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59472
diff
changeset
|
549 |
|> Local_Theory.new_group |
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59472
diff
changeset
|
550 |
|> f int; |
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59472
diff
changeset
|
551 |
in (finish o Local_Theory.reset_group, prf) end); |
24780
47bb1e380d83
local_theory transactions: more careful treatment of context position;
wenzelm
parents:
24634
diff
changeset
|
552 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
553 |
fun local_theory_to_proof restricted target f = |
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59939
diff
changeset
|
554 |
local_theory_to_proof' restricted target (K f); |
21294 | 555 |
|
556 |
fun theory_to_proof f = begin_proof |
|
47274 | 557 |
(fn _ => fn gthy => |
56057
ad6bd8030d88
more explicit Sign.change_check -- detect structural mistakes where they emerge, not at later theory merges;
wenzelm
parents:
56055
diff
changeset
|
558 |
(Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of, |
49062
7e31dfd99ce7
discontinued complicated/unreliable notion of recent proofs within context;
wenzelm
parents:
49042
diff
changeset
|
559 |
(case gthy of |
52788 | 560 |
Context.Theory thy => f (Sign.new_group thy) |
49012
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
wenzelm
parents:
49011
diff
changeset
|
561 |
| _ => raise UNDEF))); |
21294 | 562 |
|
563 |
end; |
|
564 |
||
69878 | 565 |
val forget_proof = transaction0 (fn _ => |
58798
49ed5eea15d4
'oops' requires proper goal statement -- exclude 'notepad' to avoid disrupting begin/end structure;
wenzelm
parents:
58795
diff
changeset
|
566 |
(fn Proof (prf, (_, orig_gthy)) => |
68876
cefaac3d24ff
no reset_proof for notepad: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68875
diff
changeset
|
567 |
if Proof.is_notepad (Proof_Node.current prf) then raise UNDEF |
69878 | 568 |
else Theory orig_gthy |
569 |
| Skipped_Proof (_, (_, orig_gthy)) => Theory orig_gthy |
|
21007 | 570 |
| _ => raise UNDEF)); |
571 |
||
69878 | 572 |
fun proofs' f = transaction0 (fn int => |
49062
7e31dfd99ce7
discontinued complicated/unreliable notion of recent proofs within context;
wenzelm
parents:
49042
diff
changeset
|
573 |
(fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x) |
51555 | 574 |
| skip as Skipped_Proof _ => skip |
16815 | 575 |
| _ => raise UNDEF)); |
15668 | 576 |
|
49863
b5fb6e7f8d81
more informative errors for 'proof' and 'apply' steps;
wenzelm
parents:
49062
diff
changeset
|
577 |
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f); |
17904
21c6894b5998
simplified interfaces proof/proof' etc.: perform ProofHistory.apply(s)/current internally;
wenzelm
parents:
17513
diff
changeset
|
578 |
val proofs = proofs' o K; |
6689 | 579 |
val proof = proof' o K; |
16815 | 580 |
|
60693
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
581 |
|
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
582 |
(* skipped proofs *) |
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
583 |
|
69878 | 584 |
fun actual_proof f = transaction0 (fn _ => |
21007 | 585 |
(fn Proof (prf, x) => Proof (f prf, x) |
20963
a7fd8f05a2be
added type global_theory -- theory or local_theory;
wenzelm
parents:
20928
diff
changeset
|
586 |
| _ => raise UNDEF)); |
16815 | 587 |
|
69878 | 588 |
fun skip_proof f = transaction0 (fn _ => |
60693
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
589 |
(fn skip as Skipped_Proof _ => (f (); skip) |
18563 | 590 |
| _ => raise UNDEF)); |
591 |
||
69878 | 592 |
val skip_proof_open = transaction0 (fn _ => |
60693
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
593 |
(fn Skipped_Proof (d, x) => Skipped_Proof (d + 1, x) |
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
594 |
| _ => raise UNDEF)); |
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
595 |
|
69878 | 596 |
val skip_proof_close = transaction0 (fn _ => |
597 |
(fn Skipped_Proof (0, (gthy, _)) => Theory gthy |
|
60693
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
wenzelm
parents:
60403
diff
changeset
|
598 |
| Skipped_Proof (d, x) => Skipped_Proof (d - 1, x) |
33725
a8481da77270
implicit name space grouping for theory/local_theory transactions;
wenzelm
parents:
33671
diff
changeset
|
599 |
| _ => raise UNDEF)); |
5828 | 600 |
|
601 |
||
602 |
||
603 |
(** toplevel transactions **) |
|
604 |
||
52527
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52499
diff
changeset
|
605 |
(* runtime position *) |
27427 | 606 |
|
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
607 |
fun exec_id id (tr as Transition {pos, ...}) = |
73098
8a20737e4ebf
support more command positions, analogous to Command.core_range in Isabelle/Scala;
wenzelm
parents:
72505
diff
changeset
|
608 |
let val put_id = Position.put_id (Document_ID.print id) |
73106
3df45de0c079
discontinued body_range (again): does not quite work, because Position.thread_data is plain Toplevel.pos_of only;
wenzelm
parents:
73098
diff
changeset
|
609 |
in position (put_id pos) tr end; |
25799 | 610 |
|
78021 | 611 |
fun setmp_thread_position (Transition {pos, name, ...}) f x = |
612 |
Position.setmp_thread_data (Position.label (Long_Name.qualify Markup.commandN name) pos) f x; |
|
25799 | 613 |
|
614 |
||
5828 | 615 |
(* apply transitions *) |
616 |
||
6664 | 617 |
local |
618 |
||
76073
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
619 |
fun show_state (st', opt_err) = |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
620 |
let |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
621 |
val enabled = is_none opt_err andalso Options.default_bool \<^system_option>\<open>show_states\<close>; |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
622 |
val opt_err' = |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
623 |
if enabled then |
82589 | 624 |
(case Exn.capture (Output.state o Pretty.strings_of o Pretty.chunks o pretty_state) st' of |
76073
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
625 |
Exn.Exn exn => SOME exn |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
626 |
| Exn.Res _ => opt_err) |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
627 |
else opt_err; |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
628 |
in (st', opt_err') end; |
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
629 |
|
70134 | 630 |
fun app int (tr as Transition {name, markers, trans, ...}) = |
67932 | 631 |
setmp_thread_position tr |
76811
56d76e8cecf4
discontinued somewhat pointless exception FAILURE with its "alt_state", which was originally due to quasi-mutable states (see 169e5b07ec06);
wenzelm
parents:
76810
diff
changeset
|
632 |
(Timing.protocol (name_of tr) (pos_of tr) (apply_capture int name markers trans) |
76810 | 633 |
##> Option.map (fn Runtime.UNDEF => ERROR (type_error tr) | exn => exn) |
76073
951abf9db857
option "show_states" for more verbosity of batch-builds;
wenzelm
parents:
75077
diff
changeset
|
634 |
#> show_state); |
6664 | 635 |
|
636 |
in |
|
5828 | 637 |
|
26602
5534b6a6b810
made purely value-oriented, moved global state to structure Isar (cf. isar.ML);
wenzelm
parents:
26491
diff
changeset
|
638 |
fun transition int tr st = |
28095
7eaf0813bdc3
added add_hook interface for post-transition hooks;
wenzelm
parents:
27859
diff
changeset
|
639 |
let |
60895
501be4aa75b4
default ML context for all command transactions, e.g. relevant for debugging and toplevel pretty-printing;
wenzelm
parents:
60693
diff
changeset
|
640 |
val (st', opt_err) = |
67642
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
wenzelm
parents:
67641
diff
changeset
|
641 |
Context.setmp_generic_context (try (Context.Proof o presentation_context0) st) |
60895
501be4aa75b4
default ML context for all command transactions, e.g. relevant for debugging and toplevel pretty-printing;
wenzelm
parents:
60693
diff
changeset
|
642 |
(fn () => app int tr st) (); |
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
643 |
val opt_err' = opt_err |> Option.map |
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
644 |
(fn Runtime.EXCURSION_FAIL exn_info => exn_info |
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
645 |
| exn => (Runtime.exn_context (try context_of st) exn, at_command tr)); |
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
646 |
in (st', opt_err') end; |
6664 | 647 |
|
648 |
end; |
|
5828 | 649 |
|
650 |
||
51284
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51282
diff
changeset
|
651 |
(* managed commands *) |
5828 | 652 |
|
51323 | 653 |
fun command_errors int tr st = |
654 |
(case transition int tr st of |
|
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
655 |
(st', NONE) => ([], SOME st') |
65948 | 656 |
| (_, SOME (exn, _)) => (Runtime.exn_messages exn, NONE)); |
51323 | 657 |
|
51284
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51282
diff
changeset
|
658 |
fun command_exception int tr st = |
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51282
diff
changeset
|
659 |
(case transition int tr st of |
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
660 |
(st', NONE) => st' |
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
661 |
| (_, SOME (exn, info)) => |
78764
a3dcae9a2ebe
distinguish proper interrupts from Poly/ML RTS breakdown;
wenzelm
parents:
78757
diff
changeset
|
662 |
if Exn.is_interrupt_proper exn then Exn.reraise exn |
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
59032
diff
changeset
|
663 |
else raise Runtime.EXCURSION_FAIL (exn, info)); |
27576
7afff36043e6
eliminated internal command history -- superceeded by global Isar state (cf. isar.ML);
wenzelm
parents:
27564
diff
changeset
|
664 |
|
58848 | 665 |
val command = command_exception false; |
51284
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents:
51282
diff
changeset
|
666 |
|
28433
b3dab95f098f
begin_proof: avoid race condition wrt. skip_proofs flag;
wenzelm
parents:
28425
diff
changeset
|
667 |
|
56937 | 668 |
(* reset state *) |
669 |
||
670 |
local |
|
671 |
||
672 |
fun reset_state check trans st = |
|
673 |
if check st then NONE |
|
674 |
else #2 (command_errors false (trans empty) st); |
|
675 |
||
676 |
in |
|
677 |
||
68876
cefaac3d24ff
no reset_proof for notepad: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68875
diff
changeset
|
678 |
val reset_theory = reset_state is_theory forget_proof; |
56937 | 679 |
|
680 |
val reset_proof = |
|
681 |
reset_state is_proof |
|
69878 | 682 |
(transaction0 (fn _ => |
683 |
(fn Theory gthy => Skipped_Proof (0, (gthy, gthy)) |
|
56937 | 684 |
| _ => raise UNDEF))); |
685 |
||
68877
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
686 |
val reset_notepad = |
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
687 |
reset_state |
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
688 |
(fn st => |
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
689 |
(case try proof_of st of |
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
690 |
SOME state => not (Proof.is_notepad state) orelse can Proof.end_notepad state |
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
691 |
| NONE => true)) |
68878 | 692 |
(proof Proof.reset_notepad); |
68877
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
wenzelm
parents:
68876
diff
changeset
|
693 |
|
56937 | 694 |
end; |
695 |
||
696 |
||
46959
cdc791910460
defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents:
45666
diff
changeset
|
697 |
(* scheduled proof result *) |
28433
b3dab95f098f
begin_proof: avoid race condition wrt. skip_proofs flag;
wenzelm
parents:
28425
diff
changeset
|
698 |
|
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
699 |
datatype result = |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
700 |
Result of transition * state | |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
701 |
Result_List of result list | |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
702 |
Result_Future of result future; |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
703 |
|
73687
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
704 |
fun join_results result = |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
705 |
let |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
706 |
fun add (tr, st') res = |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
707 |
(case res of |
76415 | 708 |
[] => [(make_state NONE, tr, st')] |
73687
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
709 |
| (_, _, st) :: _ => (st, tr, st') :: res); |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
710 |
fun acc (Result r) = add r |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
711 |
| acc (Result_List rs) = fold acc rs |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
712 |
| acc (Result_Future x) = acc (Future.join x); |
54fe8cc0e1c6
clarified signature: provide access to previous state;
wenzelm
parents:
73614
diff
changeset
|
713 |
in rev (acc result []) end; |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
714 |
|
51323 | 715 |
local |
716 |
||
47417 | 717 |
structure Result = Proof_Data |
28974
d6b190efa01a
excursion: pass explicit proof states as result of future proof, replaced low-level Thm.join_futures by PureThy.force_proofs;
wenzelm
parents:
28645
diff
changeset
|
718 |
( |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
719 |
type T = result; |
59150 | 720 |
fun init _ = Result_List []; |
28974
d6b190efa01a
excursion: pass explicit proof states as result of future proof, replaced low-level Thm.join_futures by PureThy.force_proofs;
wenzelm
parents:
28645
diff
changeset
|
721 |
); |
d6b190efa01a
excursion: pass explicit proof states as result of future proof, replaced low-level Thm.join_futures by PureThy.force_proofs;
wenzelm
parents:
28645
diff
changeset
|
722 |
|
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
723 |
val get_result = Result.get o Proof.context_of; |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
724 |
val put_result = Proof.map_context o Result.put; |
51324 | 725 |
|
66169
8cfa8c7ee1f6
keep original bottom-up order of proof forks, which potentially reduces thread congestion due to Proofterm.consolidate;
wenzelm
parents:
65948
diff
changeset
|
726 |
fun timing_estimate elem = |
68839 | 727 |
let val trs = tl (Thy_Element.flat_element elem) |
69877 | 728 |
in fold (fn tr => fn t => timing_of tr + t) trs Time.zeroTime end; |
51423
e5f9a6d9ca82
clarified parallel_subproofs_saturation (blind guess) vs. parallel_subproofs_threshold (precient timing estimate);
wenzelm
parents:
51332
diff
changeset
|
729 |
|
68130
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
wenzelm
parents:
67932
diff
changeset
|
730 |
fun future_proofs_enabled estimate st = |
51324 | 731 |
(case try proof_of st of |
732 |
NONE => false |
|
733 |
| SOME state => |
|
79113
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
78764
diff
changeset
|
734 |
not (Proofterm.any_proofs_enabled ()) andalso |
51324 | 735 |
not (Proof.is_relevant state) andalso |
736 |
(if can (Proof.assert_bottom true) state |
|
68130
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
wenzelm
parents:
67932
diff
changeset
|
737 |
then Future.proofs_enabled 1 |
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
wenzelm
parents:
67932
diff
changeset
|
738 |
else Future.proofs_enabled 2 orelse Future.proofs_enabled_timing estimate)); |
51278 | 739 |
|
69887 | 740 |
val empty_markers = markers []; |
741 |
val empty_trans = reset_trans #> keep (K ()); |
|
742 |
||
743 |
in |
|
744 |
||
745 |
fun fork_presentation tr = (tr |> empty_markers, tr |> empty_trans); |
|
746 |
||
58923 | 747 |
fun atom_result keywords tr st = |
51323 | 748 |
let |
749 |
val st' = |
|
68130
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
wenzelm
parents:
67932
diff
changeset
|
750 |
if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then |
69887 | 751 |
let |
752 |
val (tr1, tr2) = fork_presentation tr; |
|
753 |
val _ = |
|
754 |
Execution.fork {name = "Toplevel.diag", pos = pos_of tr, pri = ~1} |
|
755 |
(fn () => command tr1 st); |
|
756 |
in command tr2 st end |
|
51323 | 757 |
else command tr st; |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
758 |
in (Result (tr, st'), st') end; |
51323 | 759 |
|
68839 | 760 |
fun element_result keywords (Thy_Element.Element (tr, NONE)) st = atom_result keywords tr st |
761 |
| element_result keywords (elem as Thy_Element.Element (head_tr, SOME element_rest)) st = |
|
48633
7cd32f9d4293
recovered comination of Toplevel.skip_proofs and Goal.parallel_proofs from 9679bab23f93 (NB: skip_proofs leads to failure of Toplevel.proof_of);
wenzelm
parents:
47881
diff
changeset
|
762 |
let |
58923 | 763 |
val (head_result, st') = atom_result keywords head_tr st; |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
764 |
val (body_elems, end_tr) = element_rest; |
66169
8cfa8c7ee1f6
keep original bottom-up order of proof forks, which potentially reduces thread congestion due to Proofterm.consolidate;
wenzelm
parents:
65948
diff
changeset
|
765 |
val estimate = timing_estimate elem; |
51324 | 766 |
in |
68130
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
wenzelm
parents:
67932
diff
changeset
|
767 |
if not (future_proofs_enabled estimate st') |
51324 | 768 |
then |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
769 |
let |
68839 | 770 |
val proof_trs = maps Thy_Element.flat_element body_elems @ [end_tr]; |
58923 | 771 |
val (proof_results, st'') = fold_map (atom_result keywords) proof_trs st'; |
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
772 |
in (Result_List (head_result :: proof_results), st'') end |
51324 | 773 |
else |
774 |
let |
|
69887 | 775 |
val (end_tr1, end_tr2) = fork_presentation end_tr; |
776 |
||
51324 | 777 |
val finish = Context.Theory o Proof_Context.theory_of; |
28974
d6b190efa01a
excursion: pass explicit proof states as result of future proof, replaced low-level Thm.join_futures by PureThy.force_proofs;
wenzelm
parents:
28645
diff
changeset
|
778 |
|
51605
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
wenzelm
parents:
51595
diff
changeset
|
779 |
val future_proof = |
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
wenzelm
parents:
51595
diff
changeset
|
780 |
Proof.future_proof (fn state => |
76816 | 781 |
Execution.fork {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = ~1} |
51605
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
wenzelm
parents:
51595
diff
changeset
|
782 |
(fn () => |
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
wenzelm
parents:
51595
diff
changeset
|
783 |
let |
69883 | 784 |
val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st'; |
69878 | 785 |
val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy)); |
69887 | 786 |
val (results, result_state) = |
69886 | 787 |
State (node_presentation node', prev_thy) |
69887 | 788 |
|> fold_map (element_result keywords) body_elems ||> command end_tr1; |
789 |
in (Result_List results, presentation_context0 result_state) end)) |
|
51332
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
790 |
#> (fn (res, state') => state' |> put_result (Result_Future res)); |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
791 |
|
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
792 |
val forked_proof = |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
793 |
proof (future_proof #> |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
794 |
(fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
795 |
end_proof (fn _ => future_proof #> |
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
wenzelm
parents:
51324
diff
changeset
|
796 |
(fn state => state |> Proof.global_done_proof |> Result.put (get_result state))); |
28974
d6b190efa01a
excursion: pass explicit proof states as result of future proof, replaced low-level Thm.join_futures by PureThy.force_proofs;
wenzelm
parents:
28645
diff
changeset
|
797 |
|
69887 | 798 |
val st'' = st' |> command (head_tr |> reset_trans |> forked_proof); |
799 |
val end_st = st'' |> command end_tr2; |
|
800 |
val end_result = Result (end_tr, end_st); |
|
51324 | 801 |
val result = |
67642
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
wenzelm
parents:
67641
diff
changeset
|
802 |
Result_List [head_result, Result.get (presentation_context0 st''), end_result]; |
69887 | 803 |
in (result, end_st) end |
51324 | 804 |
end; |
28433
b3dab95f098f
begin_proof: avoid race condition wrt. skip_proofs flag;
wenzelm
parents:
28425
diff
changeset
|
805 |
|
6664 | 806 |
end; |
51323 | 807 |
|
808 |
end; |