| author | wenzelm | 
| Mon, 29 Jul 2019 10:26:12 +0200 | |
| changeset 70435 | 52fbcf7a61f8 | 
| parent 70398 | 725438ceae7c | 
| child 71023 | 35a8e15b7e03 | 
| 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: 
26491diff
changeset | 4 | Isabelle/Isar toplevel transactions. | 
| 5828 | 5 | *) | 
| 6 | ||
| 7 | signature TOPLEVEL = | |
| 8 | sig | |
| 19063 | 9 | exception UNDEF | 
| 5828 | 10 | type state | 
| 69886 | 11 | val init_toplevel: unit -> state | 
| 62895 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62889diff
changeset | 12 | val theory_toplevel: theory -> state | 
| 7732 | 13 | val is_toplevel: state -> bool | 
| 18589 | 14 | val is_theory: state -> bool | 
| 15 | val is_proof: state -> bool | |
| 51555 | 16 | val is_skipped_proof: state -> bool | 
| 17076 | 17 | val level: state -> int | 
| 67391 | 18 | val previous_theory_of: state -> theory 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: 
37951diff
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: 
56867diff
changeset | 29 | val pretty_state: state -> Pretty.T list | 
| 61208 | 30 | val string_of_state: state -> string | 
| 37858 | 31 | val pretty_abstract: state -> Pretty.T | 
| 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 | 
| 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: 
44186diff
changeset | 42 | val init_theory: (unit -> theory) -> transition -> transition | 
| 
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
 wenzelm parents: 
44186diff
changeset | 43 | val is_init: transition -> bool | 
| 44186 
806f0ec1a43d
simplified Toplevel.init_theory: discontinued special master argument;
 wenzelm parents: 
44185diff
changeset | 44 | val modify_init: (unit -> theory) -> transition -> transition | 
| 6689 | 45 | val exit: transition -> transition | 
| 5828 | 46 | val keep: (state -> unit) -> transition -> transition | 
| 7612 | 47 | val keep': (bool -> state -> unit) -> transition -> transition | 
| 60190 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 48 | val keep_proof: (state -> unit) -> transition -> transition | 
| 27840 | 49 | val ignored: Position.T -> transition | 
| 51268 
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
 wenzelm parents: 
51241diff
changeset | 50 | val is_ignored: transition -> bool | 
| 27840 | 51 | val malformed: Position.T -> string -> transition | 
| 26491 | 52 | val generic_theory: (generic_theory -> generic_theory) -> transition -> transition | 
| 7612 | 53 | val theory': (bool -> theory -> theory) -> transition -> transition | 
| 49012 
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
 wenzelm parents: 
49011diff
changeset | 54 | val theory: (theory -> theory) -> transition -> transition | 
| 20985 | 55 | val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition | 
| 21007 | 56 | val end_local_theory: transition -> transition | 
| 47069 | 57 | val open_target: (generic_theory -> local_theory) -> transition -> transition | 
| 58 | val close_target: transition -> transition | |
| 59939 
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
 wenzelm parents: 
59923diff
changeset | 59 | val local_theory': (bool * Position.T) option -> (xstring * Position.T) option -> | 
| 59923 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 60 | (bool -> local_theory -> local_theory) -> transition -> transition | 
| 59939 
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
 wenzelm parents: 
59923diff
changeset | 61 | val local_theory: (bool * Position.T) option -> (xstring * Position.T) option -> | 
| 59923 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 62 | (local_theory -> local_theory) -> transition -> transition | 
| 45488 
6d71d9e52369
pass positions for named targets, for formal links in the document model;
 wenzelm parents: 
44304diff
changeset | 63 | val present_local_theory: (xstring * Position.T) option -> (state -> unit) -> | 
| 24453 | 64 | transition -> transition | 
| 59939 
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
 wenzelm parents: 
59923diff
changeset | 65 | 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: 
44304diff
changeset | 66 | (bool -> local_theory -> Proof.state) -> transition -> transition | 
| 59939 
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
 wenzelm parents: 
59923diff
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: 
44304diff
changeset | 68 | (local_theory -> Proof.state) -> transition -> transition | 
| 17363 | 69 | val theory_to_proof: (theory -> Proof.state) -> transition -> transition | 
| 21007 | 70 | 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: 
68875diff
changeset | 71 | val forget_proof: transition -> transition | 
| 49863 
b5fb6e7f8d81
more informative errors for 'proof' and 'apply' steps;
 wenzelm parents: 
49062diff
changeset | 72 | 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: 
17513diff
changeset | 73 | val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition | 
| 49863 
b5fb6e7f8d81
more informative errors for 'proof' and 'apply' steps;
 wenzelm parents: 
49062diff
changeset | 74 | val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition | 
| 21177 | 75 | val proof: (Proof.state -> Proof.state) -> transition -> transition | 
| 33390 | 76 | 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: 
60403diff
changeset | 77 | val skip_proof: (unit -> unit) -> transition -> transition | 
| 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 78 | val skip_proof_open: transition -> transition | 
| 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 79 | val skip_proof_close: transition -> transition | 
| 52536 | 80 | val exec_id: Document_ID.exec -> transition -> transition | 
| 28425 | 81 |   val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
 | 
| 28103 
b79e61861f0f
simplified Toplevel.add_hook: cover successful transactions only;
 wenzelm parents: 
28095diff
changeset | 82 | val add_hook: (transition -> state -> state -> unit) -> unit | 
| 59055 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 83 | val transition: bool -> transition -> state -> state * (exn * string) option | 
| 51323 | 84 | val command_errors: bool -> transition -> state -> Runtime.error list * state option | 
| 51284 
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
 wenzelm parents: 
51282diff
changeset | 85 | val command_exception: bool -> transition -> state -> state | 
| 56937 | 86 | val reset_theory: state -> state option | 
| 87 | val reset_proof: state -> state option | |
| 68877 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 88 | val reset_notepad: state -> state option | 
| 69887 | 89 | val fork_presentation: transition -> transition * transition | 
| 51332 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 90 | type result | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 91 | val join_results: result -> (transition * state) list | 
| 68839 | 92 | val element_result: Keyword.keywords -> transition Thy_Element.element -> state -> result * state | 
| 5828 | 93 | end; | 
| 94 | ||
| 6965 | 95 | structure Toplevel: TOPLEVEL = | 
| 5828 | 96 | struct | 
| 97 | ||
| 98 | (** toplevel state **) | |
| 99 | ||
| 31476 
c5d2899b6de9
moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
 wenzelm parents: 
31431diff
changeset | 100 | exception UNDEF = Runtime.UNDEF; | 
| 19063 | 101 | |
| 102 | ||
| 21958 
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
 wenzelm parents: 
21861diff
changeset | 103 | (* datatype node *) | 
| 21294 | 104 | |
| 5828 | 105 | datatype node = | 
| 69883 | 106 | Toplevel | 
| 107 | (*toplevel outside of theory body*) | | |
| 69878 | 108 | Theory of generic_theory | 
| 109 | (*global or local theory*) | | |
| 33390 | 110 | 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: 
27564diff
changeset | 111 | (*proof node, finish, original theory*) | | 
| 51555 | 112 | Skipped_Proof of int * (generic_theory * generic_theory); | 
| 27564 
fc6d34e49e17
replaced obsolete ProofHistory by ProofNode (backtracking only);
 wenzelm parents: 
27500diff
changeset | 113 | (*proof depth, resulting theory, original theory*) | 
| 5828 | 114 | |
| 69878 | 115 | val theory_node = fn Theory gthy => SOME gthy | _ => NONE; | 
| 18589 | 116 | val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE; | 
| 51555 | 117 | val skipped_proof_node = fn Skipped_Proof _ => true | _ => false; | 
| 18589 | 118 | |
| 69883 | 119 | fun cases_node f _ _ Toplevel = f () | 
| 120 | | cases_node _ g _ (Theory gthy) = g gthy | |
| 121 | | cases_node _ _ h (Proof (prf, _)) = h (Proof_Node.current prf) | |
| 122 | | cases_node _ g _ (Skipped_Proof (_, (gthy, _))) = g gthy; | |
| 123 | ||
| 124 | fun cases_proper_node g h = cases_node (fn () => raise UNDEF) g h; | |
| 125 | ||
| 126 | val get_theory = cases_node (K NONE) (SOME o Context.theory_of) (SOME o Proof.theory_of); | |
| 19063 | 127 | |
| 21958 
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
 wenzelm parents: 
21861diff
changeset | 128 | |
| 
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
 wenzelm parents: 
21861diff
changeset | 129 | (* datatype state *) | 
| 
9dfd1ca4c0a0
refined notion of empty toplevel, admits undo of 'end';
 wenzelm parents: 
21861diff
changeset | 130 | |
| 69883 | 131 | type node_presentation = node * Proof.context; | 
| 5828 | 132 | |
| 69886 | 133 | fun init_presentation () = | 
| 134 | Proof_Context.init_global (Theory.get_pure_bootstrap ()); | |
| 135 | ||
| 136 | fun node_presentation node = | |
| 137 | (node, cases_node init_presentation Context.proof_of Proof.context_of node); | |
| 138 | ||
| 69878 | 139 | |
| 69883 | 140 | datatype state = | 
| 141 | State of node_presentation * theory option; | |
| 142 | (*current node with presentation context, previous theory*) | |
| 62895 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62889diff
changeset | 143 | |
| 69883 | 144 | fun node_of (State ((node, _), _)) = node; | 
| 145 | fun previous_theory_of (State (_, prev_thy)) = prev_thy; | |
| 5828 | 146 | |
| 69887 | 147 | fun init_toplevel () = State (node_presentation Toplevel, NONE); | 
| 148 | fun theory_toplevel thy = State (node_presentation (Theory (Context.Theory thy)), NONE); | |
| 69883 | 149 | |
| 69886 | 150 | |
| 69883 | 151 | fun level state = | 
| 152 | (case node_of state of | |
| 153 | Toplevel => 0 | |
| 154 | | Theory _ => 0 | |
| 155 | | Proof (prf, _) => Proof.level (Proof_Node.current prf) | |
| 156 | | Skipped_Proof (d, _) => d + 1); (*different notion of proof depth!*) | |
| 17076 | 157 | |
| 69883 | 158 | fun str_of_state state = | 
| 159 | (case node_of state of | |
| 160 | Toplevel => | |
| 161 | (case previous_theory_of state of | |
| 162 | NONE => "at top level" | |
| 163 | | SOME thy => "at top level, result theory " ^ quote (Context.theory_name thy)) | |
| 164 | | Theory (Context.Theory _) => "in theory mode" | |
| 165 | | Theory (Context.Proof _) => "in local theory mode" | |
| 166 | | Proof _ => "in proof mode" | |
| 167 | | Skipped_Proof _ => "in skipped proof mode"); | |
| 5946 
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
 wenzelm parents: 
5939diff
changeset | 168 | |
| 
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
 wenzelm parents: 
5939diff
changeset | 169 | |
| 27576 
7afff36043e6
eliminated internal command history -- superceeded by global Isar state (cf. isar.ML);
 wenzelm parents: 
27564diff
changeset | 170 | (* current node *) | 
| 5828 | 171 | |
| 69883 | 172 | fun is_toplevel state = (case node_of state of Toplevel => true | _ => false); | 
| 5828 | 173 | |
| 69883 | 174 | fun is_theory state = | 
| 175 | not (is_toplevel state) andalso is_some (theory_node (node_of state)); | |
| 18589 | 176 | |
| 69883 | 177 | fun is_proof state = | 
| 178 | not (is_toplevel state) andalso is_some (proof_node (node_of state)); | |
| 5828 | 179 | |
| 69883 | 180 | fun is_skipped_proof state = | 
| 181 | not (is_toplevel state) andalso skipped_proof_node (node_of state); | |
| 30801 | 182 | |
| 69883 | 183 | fun proper_node_of state = if is_toplevel state then raise UNDEF else node_of state; | 
| 184 | fun proper_node_case f g state = cases_proper_node f g (proper_node_of state); | |
| 185 | ||
| 186 | val context_of = proper_node_case Context.proof_of Proof.context_of; | |
| 187 | val generic_theory_of = proper_node_case I (Context.Proof o Proof.context_of); | |
| 188 | val theory_of = proper_node_case Context.theory_of Proof.theory_of; | |
| 189 | val proof_of = proper_node_case (fn _ => error "No proof state") I; | |
| 17208 | 190 | |
| 18589 | 191 | fun proof_position_of state = | 
| 69883 | 192 | (case proper_node_of state of | 
| 33390 | 193 | Proof (prf, _) => Proof_Node.position prf | 
| 60094 | 194 | | _ => ~1); | 
| 6664 | 195 | |
| 69883 | 196 | fun is_end_theory (State ((Toplevel, _), SOME _)) = true | 
| 68505 | 197 | | is_end_theory _ = false; | 
| 198 | ||
| 69883 | 199 | fun end_theory _ (State ((Toplevel, _), SOME thy)) = thy | 
| 68869 | 200 |   | end_theory pos _ = error ("Malformed theory" ^ Position.here pos);
 | 
| 37953 
ddc3b72f9a42
simplified handling of theory begin/end wrt. toplevel and theory loader;
 wenzelm parents: 
37951diff
changeset | 201 | |
| 5828 | 202 | |
| 67381 | 203 | (* presentation context *) | 
| 204 | ||
| 205 | structure Presentation_State = Proof_Data | |
| 206 | ( | |
| 207 | type T = state option; | |
| 208 | fun init _ = NONE; | |
| 209 | ); | |
| 210 | ||
| 69883 | 211 | fun presentation_context0 (State ((_, pr_ctxt), _)) = pr_ctxt; | 
| 67642 
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
 wenzelm parents: 
67641diff
changeset | 212 | |
| 
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
 wenzelm parents: 
67641diff
changeset | 213 | fun presentation_context (state as State (current, _)) = | 
| 
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
 wenzelm parents: 
67641diff
changeset | 214 | presentation_context0 state | 
| 
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
 wenzelm parents: 
67641diff
changeset | 215 | |> Presentation_State.put (SOME (State (current, NONE))); | 
| 67381 | 216 | |
| 217 | fun presentation_state ctxt = | |
| 218 | (case Presentation_State.get ctxt of | |
| 69886 | 219 | NONE => State (node_presentation (Theory (Context.Proof ctxt)), NONE) | 
| 67381 | 220 | | SOME state => state); | 
| 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: 
56867diff
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 | |
| 61208 | 245 | val string_of_state = pretty_state #> Pretty.chunks #> Pretty.string_of; | 
| 16815 | 246 | |
| 37858 | 247 | fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
 | 
| 248 | ||
| 62819 
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
 wenzelm parents: 
62663diff
changeset | 249 | val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty_abstract); | 
| 62663 | 250 | |
| 16815 | 251 | |
| 15668 | 252 | |
| 5828 | 253 | (** toplevel transitions **) | 
| 254 | ||
| 69888 | 255 | (* primitive transitions *) | 
| 256 | ||
| 257 | datatype trans = | |
| 258 | (*init theory*) | |
| 259 | Init of unit -> theory | | |
| 260 | (*formal exit of theory*) | |
| 261 | Exit | | |
| 262 | (*peek at state*) | |
| 263 | Keep of bool -> state -> unit | | |
| 264 | (*node transaction and presentation*) | |
| 265 | Transaction of (bool -> node -> node_presentation) * (state -> unit); | |
| 266 | ||
| 267 | local | |
| 7022 | 268 | |
| 31476 
c5d2899b6de9
moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
 wenzelm parents: 
31431diff
changeset | 269 | exception FAILURE of state * exn; | 
| 
c5d2899b6de9
moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
 wenzelm parents: 
31431diff
changeset | 270 | |
| 69888 | 271 | fun apply f g node = | 
| 20128 
8f0e07d7cf92
keep/transaction: unified execution model (with debugging etc.);
 wenzelm parents: 
19996diff
changeset | 272 | let | 
| 69886 | 273 | val node_pr = node_presentation node; | 
| 69883 | 274 | val context = cases_proper_node I (Context.Proof o Proof.context_of) node; | 
| 275 | fun state_error e node_pr' = (State (node_pr', get_theory node), e); | |
| 6689 | 276 | |
| 20128 
8f0e07d7cf92
keep/transaction: unified execution model (with debugging etc.);
 wenzelm parents: 
19996diff
changeset | 277 | val (result, err) = | 
| 69878 | 278 | node | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56265diff
changeset | 279 | |> Runtime.controlled_execution (SOME context) f | 
| 26624 
770265032999
transaction/init: ensure stable theory (non-draft);
 wenzelm parents: 
26621diff
changeset | 280 | |> state_error NONE | 
| 69878 | 281 | handle exn => state_error (SOME exn) node_pr; | 
| 20128 
8f0e07d7cf92
keep/transaction: unified execution model (with debugging etc.);
 wenzelm parents: 
19996diff
changeset | 282 | in | 
| 52696 
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
 wenzelm parents: 
52565diff
changeset | 283 | (case err of | 
| 
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
 wenzelm parents: 
52565diff
changeset | 284 | NONE => tap g result | 
| 
38466f4f3483
immutable theory values with full stamp record of every update (increase of stamp size for HOL: 20000 -> 100000, JinjaThreads: 65000 -> 300000) -- minimal measurable impact on inference kernel performance;
 wenzelm parents: 
52565diff
changeset | 285 | | SOME exn => raise FAILURE (result, exn)) | 
| 20128 
8f0e07d7cf92
keep/transaction: unified execution model (with debugging etc.);
 wenzelm parents: 
19996diff
changeset | 286 | end; | 
| 6689 | 287 | |
| 69887 | 288 | fun apply_tr int trans state = | 
| 289 | (case (trans, node_of state) of | |
| 290 | (Init f, Toplevel) => | |
| 291 | Runtime.controlled_execution NONE (fn () => | |
| 292 | State (node_presentation (Theory (Context.Theory (f ()))), NONE)) () | |
| 293 | | (Exit, node as Theory (Context.Theory thy)) => | |
| 69883 | 294 | let | 
| 295 | val State ((node', pr_ctxt), _) = | |
| 69888 | 296 | node |> apply | 
| 69886 | 297 | (fn _ => node_presentation (Theory (Context.Theory (Theory.end_theory thy)))) | 
| 69883 | 298 | (K ()); | 
| 299 | in State ((Toplevel, pr_ctxt), get_theory node') end | |
| 69887 | 300 | | (Keep f, node) => | 
| 301 | Runtime.controlled_execution (try generic_theory_of state) | |
| 302 | (fn () => (f int state; State (node_presentation node, previous_theory_of state))) () | |
| 303 | | (Transaction _, Toplevel) => raise UNDEF | |
| 69888 | 304 | | (Transaction (f, g), node) => apply (fn x => f int x) g node | 
| 69887 | 305 | | _ => raise UNDEF); | 
| 5828 | 306 | |
| 32792 | 307 | fun apply_union _ [] state = raise FAILURE (state, UNDEF) | 
| 308 | | apply_union int (tr :: trs) state = | |
| 309 | apply_union int trs state | |
| 310 | handle Runtime.UNDEF => apply_tr int tr state | |
| 311 | | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state | |
| 6689 | 312 | | exn as FAILURE _ => raise exn | 
| 313 | | exn => raise FAILURE (state, exn); | |
| 314 | ||
| 70134 | 315 | fun apply_markers name markers (state as State ((node, pr_ctxt), prev_thy)) = | 
| 69887 | 316 | let | 
| 317 | val state' = | |
| 318 | Runtime.controlled_execution (try generic_theory_of state) | |
| 70134 | 319 | (fn () => State ((node, fold (Document_Marker.evaluate name) markers pr_ctxt), prev_thy)) (); | 
| 69887 | 320 | in (state', NONE) end | 
| 321 | handle exn => (state, SOME exn); | |
| 322 | ||
| 6689 | 323 | in | 
| 324 | ||
| 70134 | 325 | fun apply_trans int name markers trans state = | 
| 326 | (apply_union int trans state |> apply_markers name markers) | |
| 15531 | 327 | handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn); | 
| 6689 | 328 | |
| 329 | end; | |
| 5828 | 330 | |
| 331 | ||
| 332 | (* datatype transition *) | |
| 333 | ||
| 334 | datatype transition = Transition of | |
| 69887 | 335 |  {name: string,               (*command name*)
 | 
| 336 | pos: Position.T, (*source position*) | |
| 337 | markers: Input.source list, (*semantic document markers*) | |
| 338 | timing: Time.time, (*prescient timing information*) | |
| 339 | trans: trans list}; (*primitive transitions (union)*) | |
| 5828 | 340 | |
| 69887 | 341 | fun make_transition (name, pos, markers, timing, trans) = | 
| 342 |   Transition {name = name, pos = pos, markers = markers, timing = timing, trans = trans};
 | |
| 5828 | 343 | |
| 69887 | 344 | fun map_transition f (Transition {name, pos, markers, timing, trans}) =
 | 
| 345 | make_transition (f (name, pos, markers, timing, trans)); | |
| 5828 | 346 | |
| 69887 | 347 | val empty = make_transition ("", Position.none, [], Time.zeroTime, []);
 | 
| 5828 | 348 | |
| 349 | ||
| 350 | (* diagnostics *) | |
| 351 | ||
| 27427 | 352 | fun name_of (Transition {name, ...}) = name;
 | 
| 28105 | 353 | fun pos_of (Transition {pos, ...}) = pos;
 | 
| 69877 | 354 | fun timing_of (Transition {timing, ...}) = timing;
 | 
| 5828 | 355 | |
| 60076 | 356 | fun command_msg msg tr = | 
| 357 | msg ^ "command " ^ quote (Markup.markup Markup.keyword1 (name_of tr)) ^ | |
| 358 | Position.here (pos_of tr); | |
| 5828 | 359 | |
| 60076 | 360 | fun at_command tr = command_msg "At " tr; | 
| 361 | fun type_error tr = command_msg "Bad context for " tr; | |
| 5828 | 362 | |
| 363 | ||
| 364 | (* modify transitions *) | |
| 365 | ||
| 69887 | 366 | fun name name = map_transition (fn (_, pos, markers, timing, trans) => | 
| 367 | (name, pos, markers, timing, trans)); | |
| 9010 | 368 | |
| 69887 | 369 | fun position pos = map_transition (fn (name, _, markers, timing, trans) => | 
| 370 | (name, pos, markers, timing, trans)); | |
| 371 | ||
| 372 | fun markers markers = map_transition (fn (name, pos, _, timing, trans) => | |
| 373 | (name, pos, markers, timing, trans)); | |
| 14923 | 374 | |
| 69887 | 375 | fun timing timing = map_transition (fn (name, pos, markers, _, trans) => | 
| 376 | (name, pos, markers, timing, trans)); | |
| 69877 | 377 | |
| 69887 | 378 | fun add_trans tr = map_transition (fn (name, pos, markers, timing, trans) => | 
| 379 | (name, pos, markers, timing, tr :: trans)); | |
| 16607 | 380 | |
| 69887 | 381 | val reset_trans = map_transition (fn (name, pos, markers, timing, _) => | 
| 382 | (name, pos, markers, timing, [])); | |
| 5828 | 383 | |
| 384 | ||
| 21007 | 385 | (* basic transitions *) | 
| 5828 | 386 | |
| 44187 
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
 wenzelm parents: 
44186diff
changeset | 387 | 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: 
37953diff
changeset | 388 | |
| 44187 
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
 wenzelm parents: 
44186diff
changeset | 389 | fun is_init (Transition {trans = [Init _], ...}) = true
 | 
| 
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
 wenzelm parents: 
44186diff
changeset | 390 | | is_init _ = false; | 
| 
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
 wenzelm parents: 
44186diff
changeset | 391 | |
| 
88d770052bac
simplified Toplevel.init_theory: discontinued special name argument;
 wenzelm parents: 
44186diff
changeset | 392 | 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: 
37953diff
changeset | 393 | |
| 6689 | 394 | val exit = add_trans Exit; | 
| 7612 | 395 | val keep' = add_trans o Keep; | 
| 30366 
e3d788b9dffb
simplified presentation: built into transaction, pass state directly;
 wenzelm parents: 
29516diff
changeset | 396 | |
| 
e3d788b9dffb
simplified presentation: built into transaction, pass state directly;
 wenzelm parents: 
29516diff
changeset | 397 | fun present_transaction f g = add_trans (Transaction (f, g)); | 
| 
e3d788b9dffb
simplified presentation: built into transaction, pass state directly;
 wenzelm parents: 
29516diff
changeset | 398 | fun transaction f = present_transaction f (K ()); | 
| 69886 | 399 | fun transaction0 f = present_transaction (node_presentation oo f) (K ()); | 
| 5828 | 400 | |
| 7612 | 401 | fun keep f = add_trans (Keep (fn _ => f)); | 
| 5828 | 402 | |
| 60190 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 403 | fun keep_proof f = | 
| 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 404 | keep (fn st => | 
| 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 405 | if is_proof st then f st | 
| 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 406 | else if is_skipped_proof st then () | 
| 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 407 | else warning "No proof state"); | 
| 
906de96ba68a
allow diagnostic proof commands with skip_proofs;
 wenzelm parents: 
60189diff
changeset | 408 | |
| 60189 | 409 | fun ignored pos = empty |> name "<ignored>" |> position pos |> keep (fn _ => ()); | 
| 51268 
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
 wenzelm parents: 
51241diff
changeset | 410 | fun is_ignored tr = name_of tr = "<ignored>"; | 
| 48772 | 411 | |
| 27840 | 412 | fun malformed pos msg = | 
| 60189 | 413 | empty |> name "<malformed>" |> position pos |> keep (fn _ => error msg); | 
| 27840 | 414 | |
| 21007 | 415 | |
| 49012 
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
 wenzelm parents: 
49011diff
changeset | 416 | (* theory transitions *) | 
| 44304 
7ee000ce5390
maintain recent future proofs at transaction boundaries;
 wenzelm parents: 
44270diff
changeset | 417 | |
| 27601 
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
 wenzelm parents: 
27583diff
changeset | 418 | fun generic_theory f = transaction (fn _ => | 
| 69886 | 419 | (fn Theory gthy => node_presentation (Theory (f gthy)) | 
| 26491 | 420 | | _ => raise UNDEF)); | 
| 421 | ||
| 27601 
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
 wenzelm parents: 
27583diff
changeset | 422 | fun theory' f = transaction (fn int => | 
| 69878 | 423 | (fn Theory (Context.Theory thy) => | 
| 33725 
a8481da77270
implicit name space grouping for theory/local_theory transactions;
 wenzelm parents: 
33671diff
changeset | 424 | let val thy' = thy | 
| 49012 
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
 wenzelm parents: 
49011diff
changeset | 425 | |> Sign.new_group | 
| 33725 
a8481da77270
implicit name space grouping for theory/local_theory transactions;
 wenzelm parents: 
33671diff
changeset | 426 | |> f int | 
| 
a8481da77270
implicit name space grouping for theory/local_theory transactions;
 wenzelm parents: 
33671diff
changeset | 427 | |> Sign.reset_group; | 
| 69886 | 428 | in node_presentation (Theory (Context.Theory thy')) end | 
| 20963 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 429 | | _ => raise UNDEF)); | 
| 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 430 | |
| 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 431 | fun theory f = theory' (K f); | 
| 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 432 | |
| 27601 
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
 wenzelm parents: 
27583diff
changeset | 433 | fun begin_local_theory begin f = transaction (fn _ => | 
| 69878 | 434 | (fn Theory (Context.Theory thy) => | 
| 20963 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 435 | let | 
| 20985 | 436 | val lthy = f thy; | 
| 57483 
950dc7446454
centralized (ad-hoc) switching of targets in named_target.ML
 haftmann parents: 
57184diff
changeset | 437 | val gthy = if begin then Context.Proof lthy else Context.Theory (Named_Target.exit lthy); | 
| 56897 
c668735fb8b5
print results as "state", to avoid intrusion into the source text;
 wenzelm parents: 
56895diff
changeset | 438 | val _ = | 
| 60245 | 439 | (case Local_Theory.pretty lthy of | 
| 440 | [] => () | |
| 441 | | prts => Output.state (Pretty.string_of (Pretty.chunks prts))); | |
| 69878 | 442 | in (Theory gthy, lthy) end | 
| 20963 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 443 | | _ => raise UNDEF)); | 
| 17076 | 444 | |
| 27601 
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
 wenzelm parents: 
27583diff
changeset | 445 | val end_local_theory = transaction (fn _ => | 
| 69878 | 446 | (fn Theory (Context.Proof lthy) => (Theory (Context.Theory (Named_Target.exit lthy)), lthy) | 
| 21007 | 447 | | _ => raise UNDEF)); | 
| 448 | ||
| 69878 | 449 | fun open_target f = transaction0 (fn _ => | 
| 450 | (fn Theory gthy => | |
| 47069 | 451 | let val lthy = f gthy | 
| 69878 | 452 | in Theory (Context.Proof lthy) end | 
| 47069 | 453 | | _ => raise UNDEF)); | 
| 454 | ||
| 455 | val close_target = transaction (fn _ => | |
| 69878 | 456 | (fn Theory (Context.Proof lthy) => | 
| 47069 | 457 | (case try Local_Theory.close_target lthy of | 
| 50739 
5165d7e6d3b9
more precise Local_Theory.level: 1 really means main target and >= 2 nested context;
 wenzelm parents: 
50201diff
changeset | 458 | SOME ctxt' => | 
| 
5165d7e6d3b9
more precise Local_Theory.level: 1 really means main target and >= 2 nested context;
 wenzelm parents: 
50201diff
changeset | 459 | let | 
| 
5165d7e6d3b9
more precise Local_Theory.level: 1 really means main target and >= 2 nested context;
 wenzelm parents: 
50201diff
changeset | 460 | val gthy' = | 
| 
5165d7e6d3b9
more precise Local_Theory.level: 1 really means main target and >= 2 nested context;
 wenzelm parents: 
50201diff
changeset | 461 | if can Local_Theory.assert ctxt' | 
| 
5165d7e6d3b9
more precise Local_Theory.level: 1 really means main target and >= 2 nested context;
 wenzelm parents: 
50201diff
changeset | 462 | then Context.Proof ctxt' | 
| 
5165d7e6d3b9
more precise Local_Theory.level: 1 really means main target and >= 2 nested context;
 wenzelm parents: 
50201diff
changeset | 463 | else Context.Theory (Proof_Context.theory_of ctxt'); | 
| 69878 | 464 | in (Theory gthy', lthy) end | 
| 47069 | 465 | | NONE => raise UNDEF) | 
| 466 | | _ => raise UNDEF)); | |
| 467 | ||
| 59990 
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
 wenzelm parents: 
59939diff
changeset | 468 | 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: 
59939diff
changeset | 469 | 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: 
59939diff
changeset | 470 | | restricted_context NONE = I; | 
| 59939 
7d46aa03696e
support for 'restricted' modifier: only qualified accesses outside the local scope;
 wenzelm parents: 
59923diff
changeset | 471 | |
| 59990 
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
 wenzelm parents: 
59939diff
changeset | 472 | fun local_theory' restricted target f = present_transaction (fn int => | 
| 69878 | 473 | (fn Theory gthy => | 
| 21294 | 474 | let | 
| 59923 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 475 | val (finish, lthy) = Named_Target.switch target gthy; | 
| 47274 | 476 | 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: 
59939diff
changeset | 477 | |> restricted_context restricted | 
| 49012 
8686c36fa27d
refined treatment of forked proofs at transaction boundaries, including proof commands (see also 7ee000ce5390);
 wenzelm parents: 
49011diff
changeset | 478 | |> Local_Theory.new_group | 
| 33725 
a8481da77270
implicit name space grouping for theory/local_theory transactions;
 wenzelm parents: 
33671diff
changeset | 479 | |> f int | 
| 
a8481da77270
implicit name space grouping for theory/local_theory transactions;
 wenzelm parents: 
33671diff
changeset | 480 | |> Local_Theory.reset_group; | 
| 69878 | 481 | in (Theory (finish lthy'), lthy') end | 
| 59032 | 482 | | _ => raise UNDEF)) | 
| 483 | (K ()); | |
| 15668 | 484 | |
| 59990 
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
 wenzelm parents: 
59939diff
changeset | 485 | fun local_theory restricted target f = local_theory' restricted target (K f); | 
| 21007 | 486 | |
| 65054 | 487 | fun present_local_theory target = present_transaction (fn _ => | 
| 69878 | 488 | (fn Theory gthy => | 
| 59923 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 489 | let val (finish, lthy) = Named_Target.switch target gthy; | 
| 69878 | 490 | in (Theory (finish lthy), lthy) end | 
| 59032 | 491 | | _ => raise UNDEF)); | 
| 21007 | 492 | |
| 493 | ||
| 494 | (* proof transitions *) | |
| 495 | ||
| 27601 
6683cdb94af8
simplified commit_exit: operate on previous node of final state, include warning here;
 wenzelm parents: 
27583diff
changeset | 496 | fun end_proof f = transaction (fn int => | 
| 24795 
6f5cb7885fd7
print_state_context: local theory context, not proof context;
 wenzelm parents: 
24780diff
changeset | 497 | (fn Proof (prf, (finish, _)) => | 
| 33390 | 498 | let val state = Proof_Node.current prf in | 
| 21007 | 499 | if can (Proof.assert_bottom true) state then | 
| 500 | let | |
| 501 | val ctxt' = f int state; | |
| 502 | val gthy' = finish ctxt'; | |
| 69878 | 503 | in (Theory gthy', ctxt') end | 
| 21007 | 504 | else raise UNDEF | 
| 505 | end | |
| 69886 | 506 | | Skipped_Proof (0, (gthy, _)) => node_presentation (Theory gthy) | 
| 21007 | 507 | | _ => raise UNDEF)); | 
| 508 | ||
| 21294 | 509 | local | 
| 510 | ||
| 69882 | 511 | fun begin_proof init_proof = transaction0 (fn int => | 
| 69878 | 512 | (fn Theory gthy => | 
| 21294 | 513 | let | 
| 69882 | 514 | val (finish, prf) = init_proof int gthy; | 
| 67157 | 515 | val document = Options.default_string "document"; | 
| 516 | val skip = (document = "" orelse document = "false") andalso Goal.skip_proofs_enabled (); | |
| 58795 | 517 | val schematic_goal = try Proof.schematic_goal prf; | 
| 47274 | 518 | val _ = | 
| 58795 | 519 | if skip andalso schematic_goal = SOME true then | 
| 47274 | 520 | warning "Cannot skip proof of schematic goal statement" | 
| 521 | else (); | |
| 21294 | 522 | in | 
| 58795 | 523 | if skip andalso schematic_goal = SOME false then | 
| 51555 | 524 | Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy)) | 
| 47274 | 525 | else Proof (Proof_Node.init prf, (finish, gthy)) | 
| 21294 | 526 | end | 
| 527 | | _ => raise UNDEF)); | |
| 528 | ||
| 529 | in | |
| 530 | ||
| 59990 
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
 wenzelm parents: 
59939diff
changeset | 531 | fun local_theory_to_proof' restricted target f = begin_proof | 
| 47274 | 532 | (fn int => fn gthy => | 
| 59923 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 533 | let | 
| 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 534 | val (finish, lthy) = Named_Target.switch target gthy; | 
| 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 535 | 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: 
59939diff
changeset | 536 | |> restricted_context restricted | 
| 59923 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 537 | |> Local_Theory.new_group | 
| 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 538 | |> f int; | 
| 
b21c82422d65
support private scope for individual local theory commands;
 wenzelm parents: 
59472diff
changeset | 539 | in (finish o Local_Theory.reset_group, prf) end); | 
| 24780 
47bb1e380d83
local_theory transactions: more careful treatment of context position;
 wenzelm parents: 
24634diff
changeset | 540 | |
| 59990 
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
 wenzelm parents: 
59939diff
changeset | 541 | 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: 
59939diff
changeset | 542 | local_theory_to_proof' restricted target (K f); | 
| 21294 | 543 | |
| 544 | fun theory_to_proof f = begin_proof | |
| 47274 | 545 | (fn _ => fn gthy => | 
| 56057 
ad6bd8030d88
more explicit Sign.change_check -- detect structural mistakes where they emerge, not at later theory merges;
 wenzelm parents: 
56055diff
changeset | 546 | (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: 
49042diff
changeset | 547 | (case gthy of | 
| 52788 | 548 | 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: 
49011diff
changeset | 549 | | _ => raise UNDEF))); | 
| 21294 | 550 | |
| 551 | end; | |
| 552 | ||
| 69878 | 553 | val forget_proof = transaction0 (fn _ => | 
| 58798 
49ed5eea15d4
'oops' requires proper goal statement -- exclude 'notepad' to avoid disrupting begin/end structure;
 wenzelm parents: 
58795diff
changeset | 554 | (fn Proof (prf, (_, orig_gthy)) => | 
| 68876 
cefaac3d24ff
no reset_proof for notepad: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68875diff
changeset | 555 | if Proof.is_notepad (Proof_Node.current prf) then raise UNDEF | 
| 69878 | 556 | else Theory orig_gthy | 
| 557 | | Skipped_Proof (_, (_, orig_gthy)) => Theory orig_gthy | |
| 21007 | 558 | | _ => raise UNDEF)); | 
| 559 | ||
| 69878 | 560 | fun proofs' f = transaction0 (fn int => | 
| 49062 
7e31dfd99ce7
discontinued complicated/unreliable notion of recent proofs within context;
 wenzelm parents: 
49042diff
changeset | 561 | (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x) | 
| 51555 | 562 | | skip as Skipped_Proof _ => skip | 
| 16815 | 563 | | _ => raise UNDEF)); | 
| 15668 | 564 | |
| 49863 
b5fb6e7f8d81
more informative errors for 'proof' and 'apply' steps;
 wenzelm parents: 
49062diff
changeset | 565 | 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: 
17513diff
changeset | 566 | val proofs = proofs' o K; | 
| 6689 | 567 | val proof = proof' o K; | 
| 16815 | 568 | |
| 60693 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 569 | |
| 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 570 | (* skipped proofs *) | 
| 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 571 | |
| 69878 | 572 | fun actual_proof f = transaction0 (fn _ => | 
| 21007 | 573 | (fn Proof (prf, x) => Proof (f prf, x) | 
| 20963 
a7fd8f05a2be
added type global_theory -- theory or local_theory;
 wenzelm parents: 
20928diff
changeset | 574 | | _ => raise UNDEF)); | 
| 16815 | 575 | |
| 69878 | 576 | fun skip_proof f = transaction0 (fn _ => | 
| 60693 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 577 | (fn skip as Skipped_Proof _ => (f (); skip) | 
| 18563 | 578 | | _ => raise UNDEF)); | 
| 579 | ||
| 69878 | 580 | val skip_proof_open = transaction0 (fn _ => | 
| 60693 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 581 | (fn Skipped_Proof (d, x) => Skipped_Proof (d + 1, x) | 
| 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 582 | | _ => raise UNDEF)); | 
| 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 583 | |
| 69878 | 584 | val skip_proof_close = transaction0 (fn _ => | 
| 585 | (fn Skipped_Proof (0, (gthy, _)) => Theory gthy | |
| 60693 
044f8bb3dd30
more accurate skip_proofs nesting, e.g. relevant for 'subgoal' command;
 wenzelm parents: 
60403diff
changeset | 586 | | Skipped_Proof (d, x) => Skipped_Proof (d - 1, x) | 
| 33725 
a8481da77270
implicit name space grouping for theory/local_theory transactions;
 wenzelm parents: 
33671diff
changeset | 587 | | _ => raise UNDEF)); | 
| 5828 | 588 | |
| 589 | ||
| 590 | ||
| 591 | (** toplevel transactions **) | |
| 592 | ||
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52499diff
changeset | 593 | (* runtime position *) | 
| 27427 | 594 | |
| 52536 | 595 | fun exec_id id (tr as Transition {pos, ...}) =
 | 
| 596 | position (Position.put_id (Document_ID.print id) pos) tr; | |
| 25799 | 597 | |
| 25960 | 598 | fun setmp_thread_position (Transition {pos, ...}) f x =
 | 
| 25819 
e6feb08b7f4b
replaced thread_properties by simplified version in position.ML;
 wenzelm parents: 
25809diff
changeset | 599 | Position.setmp_thread_data pos f x; | 
| 25799 | 600 | |
| 601 | ||
| 28095 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 602 | (* post-transition hooks *) | 
| 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 603 | |
| 37905 | 604 | local | 
| 56147 | 605 | val hooks = | 
| 606 | Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list); | |
| 37905 | 607 | in | 
| 28095 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 608 | |
| 56147 | 609 | fun add_hook hook = Synchronized.change hooks (cons hook); | 
| 610 | fun get_hooks () = Synchronized.value hooks; | |
| 28095 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 611 | |
| 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 612 | end; | 
| 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 613 | |
| 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 614 | |
| 5828 | 615 | (* apply transitions *) | 
| 616 | ||
| 6664 | 617 | local | 
| 618 | ||
| 70134 | 619 | fun app int (tr as Transition {name, markers, trans, ...}) =
 | 
| 67932 | 620 | setmp_thread_position tr | 
| 70134 | 621 | (Timing.protocol (name_of tr) (pos_of tr) (apply_trans int name markers trans) | 
| 67932 | 622 | ##> Option.map (fn UNDEF => ERROR (type_error tr) | exn => exn)); | 
| 6664 | 623 | |
| 624 | in | |
| 5828 | 625 | |
| 26602 
5534b6a6b810
made purely value-oriented, moved global state to structure Isar (cf. isar.ML);
 wenzelm parents: 
26491diff
changeset | 626 | fun transition int tr st = | 
| 28095 
7eaf0813bdc3
added add_hook interface for post-transition hooks;
 wenzelm parents: 
27859diff
changeset | 627 | let | 
| 60895 
501be4aa75b4
default ML context for all command transactions, e.g. relevant for debugging and toplevel pretty-printing;
 wenzelm parents: 
60693diff
changeset | 628 | val (st', opt_err) = | 
| 67642 
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
 wenzelm parents: 
67641diff
changeset | 629 | 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: 
60693diff
changeset | 630 | (fn () => app int tr st) (); | 
| 59055 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 631 | val opt_err' = opt_err |> Option.map | 
| 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 632 | (fn Runtime.EXCURSION_FAIL exn_info => exn_info | 
| 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 633 | | exn => (Runtime.exn_context (try context_of st) exn, at_command tr)); | 
| 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 634 | val _ = get_hooks () |> List.app (fn f => (try (fn () => f tr st st') (); ())); | 
| 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 635 | in (st', opt_err') end; | 
| 6664 | 636 | |
| 637 | end; | |
| 5828 | 638 | |
| 639 | ||
| 51284 
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
 wenzelm parents: 
51282diff
changeset | 640 | (* managed commands *) | 
| 5828 | 641 | |
| 51323 | 642 | fun command_errors int tr st = | 
| 643 | (case transition int tr st of | |
| 59055 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 644 | (st', NONE) => ([], SOME st') | 
| 65948 | 645 | | (_, SOME (exn, _)) => (Runtime.exn_messages exn, NONE)); | 
| 51323 | 646 | |
| 51284 
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
 wenzelm parents: 
51282diff
changeset | 647 | fun command_exception int tr st = | 
| 
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
 wenzelm parents: 
51282diff
changeset | 648 | (case transition int tr st of | 
| 59055 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 649 | (st', NONE) => st' | 
| 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 650 | | (_, SOME (exn, info)) => | 
| 62505 | 651 | if Exn.is_interrupt exn then Exn.reraise exn | 
| 59055 
5a7157b8e870
more informative failure of protocol commands, with exception trace;
 wenzelm parents: 
59032diff
changeset | 652 | else raise Runtime.EXCURSION_FAIL (exn, info)); | 
| 27576 
7afff36043e6
eliminated internal command history -- superceeded by global Isar state (cf. isar.ML);
 wenzelm parents: 
27564diff
changeset | 653 | |
| 58848 | 654 | val command = command_exception false; | 
| 51284 
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
 wenzelm parents: 
51282diff
changeset | 655 | |
| 28433 
b3dab95f098f
begin_proof: avoid race condition wrt. skip_proofs flag;
 wenzelm parents: 
28425diff
changeset | 656 | |
| 56937 | 657 | (* reset state *) | 
| 658 | ||
| 659 | local | |
| 660 | ||
| 661 | fun reset_state check trans st = | |
| 662 | if check st then NONE | |
| 663 | else #2 (command_errors false (trans empty) st); | |
| 664 | ||
| 665 | in | |
| 666 | ||
| 68876 
cefaac3d24ff
no reset_proof for notepad: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68875diff
changeset | 667 | val reset_theory = reset_state is_theory forget_proof; | 
| 56937 | 668 | |
| 669 | val reset_proof = | |
| 670 | reset_state is_proof | |
| 69878 | 671 | (transaction0 (fn _ => | 
| 672 | (fn Theory gthy => Skipped_Proof (0, (gthy, gthy)) | |
| 56937 | 673 | | _ => raise UNDEF))); | 
| 674 | ||
| 68877 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 675 | val reset_notepad = | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 676 | reset_state | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 677 | (fn st => | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 678 | (case try proof_of st of | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 679 | 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: 
68876diff
changeset | 680 | | NONE => true)) | 
| 68878 | 681 | (proof Proof.reset_notepad); | 
| 68877 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68876diff
changeset | 682 | |
| 56937 | 683 | end; | 
| 684 | ||
| 685 | ||
| 46959 
cdc791910460
defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
 wenzelm parents: 
45666diff
changeset | 686 | (* scheduled proof result *) | 
| 28433 
b3dab95f098f
begin_proof: avoid race condition wrt. skip_proofs flag;
 wenzelm parents: 
28425diff
changeset | 687 | |
| 51332 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 688 | datatype result = | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 689 | Result of transition * state | | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 690 | Result_List of result list | | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 691 | Result_Future of result future; | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 692 | |
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 693 | fun join_results (Result x) = [x] | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 694 | | join_results (Result_List xs) = maps join_results xs | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 695 | | join_results (Result_Future x) = join_results (Future.join x); | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 696 | |
| 51323 | 697 | local | 
| 698 | ||
| 47417 | 699 | 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: 
28645diff
changeset | 700 | ( | 
| 51332 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 701 | type T = result; | 
| 59150 | 702 | 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: 
28645diff
changeset | 703 | ); | 
| 
d6b190efa01a
excursion: pass explicit proof states as result of future proof, replaced low-level Thm.join_futures by PureThy.force_proofs;
 wenzelm parents: 
28645diff
changeset | 704 | |
| 51332 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 705 | 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: 
51324diff
changeset | 706 | val put_result = Proof.map_context o Result.put; | 
| 51324 | 707 | |
| 66169 
8cfa8c7ee1f6
keep original bottom-up order of proof forks, which potentially reduces thread congestion due to Proofterm.consolidate;
 wenzelm parents: 
65948diff
changeset | 708 | fun timing_estimate elem = | 
| 68839 | 709 | let val trs = tl (Thy_Element.flat_element elem) | 
| 69877 | 710 | 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: 
51332diff
changeset | 711 | |
| 68130 
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
 wenzelm parents: 
67932diff
changeset | 712 | fun future_proofs_enabled estimate st = | 
| 51324 | 713 | (case try proof_of st of | 
| 714 | NONE => false | |
| 715 | | SOME state => | |
| 70398 | 716 | not (Proofterm.proofs_enabled ()) andalso | 
| 51324 | 717 | not (Proof.is_relevant state) andalso | 
| 718 | (if can (Proof.assert_bottom true) state | |
| 68130 
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
 wenzelm parents: 
67932diff
changeset | 719 | then Future.proofs_enabled 1 | 
| 
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
 wenzelm parents: 
67932diff
changeset | 720 | else Future.proofs_enabled 2 orelse Future.proofs_enabled_timing estimate)); | 
| 51278 | 721 | |
| 69887 | 722 | val empty_markers = markers []; | 
| 723 | val empty_trans = reset_trans #> keep (K ()); | |
| 724 | ||
| 725 | in | |
| 726 | ||
| 727 | fun fork_presentation tr = (tr |> empty_markers, tr |> empty_trans); | |
| 728 | ||
| 58923 | 729 | fun atom_result keywords tr st = | 
| 51323 | 730 | let | 
| 731 | val st' = | |
| 68130 
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
 wenzelm parents: 
67932diff
changeset | 732 | if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then | 
| 69887 | 733 | let | 
| 734 | val (tr1, tr2) = fork_presentation tr; | |
| 735 | val _ = | |
| 736 |             Execution.fork {name = "Toplevel.diag", pos = pos_of tr, pri = ~1}
 | |
| 737 | (fn () => command tr1 st); | |
| 738 | in command tr2 st end | |
| 51323 | 739 | else command tr st; | 
| 51332 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 740 | in (Result (tr, st'), st') end; | 
| 51323 | 741 | |
| 68839 | 742 | fun element_result keywords (Thy_Element.Element (tr, NONE)) st = atom_result keywords tr st | 
| 743 | | 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: 
47881diff
changeset | 744 | let | 
| 58923 | 745 | 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: 
51324diff
changeset | 746 | 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: 
65948diff
changeset | 747 | val estimate = timing_estimate elem; | 
| 51324 | 748 | in | 
| 68130 
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
 wenzelm parents: 
67932diff
changeset | 749 | if not (future_proofs_enabled estimate st') | 
| 51324 | 750 | then | 
| 51332 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 751 | let | 
| 68839 | 752 | val proof_trs = maps Thy_Element.flat_element body_elems @ [end_tr]; | 
| 58923 | 753 | 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: 
51324diff
changeset | 754 | in (Result_List (head_result :: proof_results), st'') end | 
| 51324 | 755 | else | 
| 756 | let | |
| 69887 | 757 | val (end_tr1, end_tr2) = fork_presentation end_tr; | 
| 758 | ||
| 51324 | 759 | 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: 
28645diff
changeset | 760 | |
| 51605 
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
 wenzelm parents: 
51595diff
changeset | 761 | val future_proof = | 
| 
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
 wenzelm parents: 
51595diff
changeset | 762 | Proof.future_proof (fn state => | 
| 53192 | 763 | Execution.fork | 
| 66169 
8cfa8c7ee1f6
keep original bottom-up order of proof forks, which potentially reduces thread congestion due to Proofterm.consolidate;
 wenzelm parents: 
65948diff
changeset | 764 |                   {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: 
51595diff
changeset | 765 | (fn () => | 
| 
eca8acb42e4a
more explicit Goal.fork_params -- avoid implicit arguments via thread data;
 wenzelm parents: 
51595diff
changeset | 766 | let | 
| 69883 | 767 | val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st'; | 
| 69878 | 768 | val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy)); | 
| 69887 | 769 | val (results, result_state) = | 
| 69886 | 770 | State (node_presentation node', prev_thy) | 
| 69887 | 771 | |> fold_map (element_result keywords) body_elems ||> command end_tr1; | 
| 772 | 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: 
51324diff
changeset | 773 | #> (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: 
51324diff
changeset | 774 | |
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 775 | val forked_proof = | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 776 | proof (future_proof #> | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 777 | (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: 
51324diff
changeset | 778 | end_proof (fn _ => future_proof #> | 
| 
8707df0b0255
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
 wenzelm parents: 
51324diff
changeset | 779 | (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: 
28645diff
changeset | 780 | |
| 69887 | 781 | val st'' = st' |> command (head_tr |> reset_trans |> forked_proof); | 
| 782 | val end_st = st'' |> command end_tr2; | |
| 783 | val end_result = Result (end_tr, end_st); | |
| 51324 | 784 | val result = | 
| 67642 
10ff1f077119
more tight presentation context: avoid storing full Toplevel.state;
 wenzelm parents: 
67641diff
changeset | 785 | Result_List [head_result, Result.get (presentation_context0 st''), end_result]; | 
| 69887 | 786 | in (result, end_st) end | 
| 51324 | 787 | end; | 
| 28433 
b3dab95f098f
begin_proof: avoid race condition wrt. skip_proofs flag;
 wenzelm parents: 
28425diff
changeset | 788 | |
| 6664 | 789 | end; | 
| 51323 | 790 | |
| 791 | end; | |
| 69719 
331ef175a112
Backed out changeset 1bc422c08209 -- obsolete in AFP/5d11846ac6ab;
 wenzelm parents: 
69715diff
changeset | 792 | |
| 69708 
1c201e4792cb
Local_Theory.reset only required for toplevel interaction, attempt to withhold it from user space
 haftmann parents: 
68878diff
changeset | 793 | structure Local_Theory : LOCAL_THEORY = struct open Local_Theory; end; |