| author | wenzelm | 
| Sat, 19 Dec 2020 12:05:17 +0100 | |
| changeset 72956 | c007d0fa0938 | 
| parent 72950 | ac6457a70db5 | 
| child 73044 | e7855739409e | 
| permissions | -rw-r--r-- | 
| 47336 | 1 | (* Title: Pure/PIDE/command.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 52534 | 4 | Prover command execution: read -- eval -- print. | 
| 47336 | 5 | *) | 
| 6 | ||
| 7 | signature COMMAND = | |
| 8 | sig | |
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 9 |   type blob = {file_node: string, src_path: Path.T, content: (SHA1.digest * string list) option}
 | 
| 72841 | 10 | val read_file: Path.T -> Position.T -> bool -> Path.T -> Token.file | 
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58923diff
changeset | 11 | val read_thy: Toplevel.state -> theory | 
| 58934 
385a4cc7426f
prefer externally provided keywords -- Command.read_thy may degenerate to bootstrap_thy in case of errors;
 wenzelm parents: 
58928diff
changeset | 12 | val read: Keyword.keywords -> theory -> Path.T-> (unit -> theory) -> | 
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 13 | blob Exn.result list * int -> Token.T list -> Toplevel.transition | 
| 52600 | 14 | type eval | 
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 15 | val eval_command_id: eval -> Document_ID.command | 
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66167diff
changeset | 16 | val eval_exec_id: eval -> Document_ID.exec | 
| 52607 
33a133d50616
clarified execution: maintain running execs only, check "stable" separately via memo (again);
 wenzelm parents: 
52606diff
changeset | 17 | val eval_eq: eval * eval -> bool | 
| 52784 
4ba2e8b9972f
de-assign execs that were not registered as running yet -- observe change of perspective more thoroughly;
 wenzelm parents: 
52775diff
changeset | 18 | val eval_running: eval -> bool | 
| 52772 | 19 | val eval_finished: eval -> bool | 
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 20 | val eval_result_command: eval -> Toplevel.transition | 
| 52536 | 21 | val eval_result_state: eval -> Toplevel.state | 
| 58934 
385a4cc7426f
prefer externally provided keywords -- Command.read_thy may degenerate to bootstrap_thy in case of errors;
 wenzelm parents: 
58928diff
changeset | 22 | val eval: Keyword.keywords -> Path.T -> (unit -> theory) -> | 
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 23 | blob Exn.result list * int -> Document_ID.command -> Token.T list -> eval -> eval | 
| 52600 | 24 | type print | 
| 68344 | 25 | type print_fn = Toplevel.transition -> Toplevel.state -> unit | 
| 26 |   val print0: {pri: int, print_fn: print_fn} -> eval -> print
 | |
| 58923 | 27 | val print: bool -> (string * string list) list -> Keyword.keywords -> string -> | 
| 52850 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 28 | eval -> print list -> print list option | 
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 29 | val parallel_print: print -> bool | 
| 52953 
2c927b7501c5
explicit "strict" flag for print functions (flipped internal meaning);
 wenzelm parents: 
52853diff
changeset | 30 | type print_function = | 
| 58923 | 31 |     {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
 | 
| 52953 
2c927b7501c5
explicit "strict" flag for print functions (flipped internal meaning);
 wenzelm parents: 
52853diff
changeset | 32 |       {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option
 | 
| 
2c927b7501c5
explicit "strict" flag for print functions (flipped internal meaning);
 wenzelm parents: 
52853diff
changeset | 33 | val print_function: string -> print_function -> unit | 
| 52571 | 34 | val no_print_function: string -> unit | 
| 52600 | 35 | type exec = eval * print list | 
| 62895 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 36 | val init_exec: theory option -> exec | 
| 52600 | 37 | val no_exec: exec | 
| 38 | val exec_ids: exec option -> Document_ID.exec list | |
| 52606 | 39 | val exec: Document_ID.execution -> exec -> unit | 
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 40 | val exec_parallel_prints: Document_ID.execution -> Future.task list -> exec -> exec option | 
| 47336 | 41 | end; | 
| 42 | ||
| 43 | structure Command: COMMAND = | |
| 44 | struct | |
| 45 | ||
| 52536 | 46 | (** main phases of execution **) | 
| 47 | ||
| 59466 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 48 | fun task_context group f = | 
| 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 49 | f | 
| 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 50 | |> Future.interruptible_task | 
| 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 51 | |> Future.task_context "Command.run_process" group; | 
| 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 52 | |
| 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 53 | |
| 52510 | 54 | (* read *) | 
| 52509 | 55 | |
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 56 | type blob = {file_node: string, src_path: Path.T, content: (SHA1.digest * string list) option};
 | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
53976diff
changeset | 57 | |
| 72841 | 58 | fun read_file_node file_node master_dir pos delimited src_path = | 
| 54526 | 59 | let | 
| 71675 | 60 | val _ = | 
| 61 | if Context_Position.pide_reports () | |
| 72841 | 62 | then Position.report pos (Markup.language_path delimited) else (); | 
| 72950 | 63 | |
| 64 | fun read_file () = | |
| 65 | let | |
| 66 | val path = File.check_file (File.full_path master_dir src_path); | |
| 67 | val text = File.read path; | |
| 68 | val file_pos = Path.position path; | |
| 69 | in (text, file_pos) end; | |
| 70 | ||
| 71 | fun read_url () = | |
| 72 | let | |
| 73 | val text = Isabelle_System.download file_node; | |
| 74 | val file_pos = Position.file file_node; | |
| 75 | in (text, file_pos) end; | |
| 76 | ||
| 77 | val (text, file_pos) = | |
| 56504 
d71f4be7e287
tuned error -- allow user to click on hyperlink to open remote file;
 wenzelm parents: 
56458diff
changeset | 78 | (case try Url.explode file_node of | 
| 72950 | 79 | NONE => read_file () | 
| 80 | | SOME (Url.File _) => read_file () | |
| 81 | | _ => read_url ()); | |
| 82 | ||
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 83 | val lines = split_lines text; | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 84 | val digest = SHA1.digest text; | 
| 72950 | 85 |   in {src_path = src_path, lines = lines, digest = digest, pos = Position.copy_id pos file_pos} end
 | 
| 59685 
c043306d2598
clarified markup for embedded files, early before execution;
 wenzelm parents: 
59472diff
changeset | 86 | handle ERROR msg => error (msg ^ Position.here pos); | 
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 87 | |
| 56504 
d71f4be7e287
tuned error -- allow user to click on hyperlink to open remote file;
 wenzelm parents: 
56458diff
changeset | 88 | val read_file = read_file_node ""; | 
| 
d71f4be7e287
tuned error -- allow user to click on hyperlink to open remote file;
 wenzelm parents: 
56458diff
changeset | 89 | |
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 90 | local | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 91 | |
| 56447 
1e77ed11f2f7
separate file_node vs. file_path, e.g. relevant on Windows for hyperlink to the latter;
 wenzelm parents: 
56333diff
changeset | 92 | fun blob_file src_path lines digest file_node = | 
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 93 | let | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 94 | val file_pos = | 
| 56447 
1e77ed11f2f7
separate file_node vs. file_path, e.g. relevant on Windows for hyperlink to the latter;
 wenzelm parents: 
56333diff
changeset | 95 | Position.file file_node |> | 
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 96 | (case Position.get_id (Position.thread_data ()) of | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 97 | NONE => I | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 98 | | SOME exec_id => Position.put_id exec_id); | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 99 |   in {src_path = src_path, lines = lines, digest = digest, pos = file_pos} end
 | 
| 54526 | 100 | |
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 101 | fun resolve_files master_dir (blobs, blobs_index) toks = | 
| 57905 
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
 wenzelm parents: 
57844diff
changeset | 102 | (case Outer_Syntax.parse_spans toks of | 
| 72841 | 103 | [Command_Span.Span (Command_Span.Command_Span _, _)] => | 
| 59689 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 104 | (case try (nth toks) blobs_index of | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 105 | SOME tok => | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 106 | let | 
| 72841 | 107 | val source = Token.input_of tok; | 
| 108 | val pos = Input.pos_of source; | |
| 109 | val delimited = Input.is_delimited source; | |
| 110 | ||
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 111 |             fun make_file (Exn.Res {file_node, src_path, content = NONE}) =
 | 
| 59689 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 112 | Exn.interruptible_capture (fn () => | 
| 72841 | 113 | read_file_node file_node master_dir pos delimited src_path) () | 
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 114 |               | make_file (Exn.Res {file_node, src_path, content = SOME (digest, lines)}) =
 | 
| 72841 | 115 | (Position.report pos (Markup.language_path delimited); | 
| 60027 
c42d65e11b6e
clarified language_path markup (again): exactly once *after* static phase, see also 83071f4c8ae6 and c043306d2598;
 wenzelm parents: 
59944diff
changeset | 116 | Exn.Res (blob_file src_path lines digest file_node)) | 
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 117 | | make_file (Exn.Exn e) = Exn.Exn e; | 
| 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 118 | val files = map make_file blobs; | 
| 59689 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 119 | in | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 120 | toks |> map_index (fn (i, tok) => | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 121 | if i = blobs_index then Token.put_files files tok else tok) | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 122 | end | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 123 | | NONE => toks) | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
53976diff
changeset | 124 | | _ => toks); | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
53976diff
changeset | 125 | |
| 61379 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 126 | fun reports_of_token keywords tok = | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 127 | let | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 128 | val malformed_symbols = | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 129 | Input.source_explode (Token.input_of tok) | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 130 | |> map_filter (fn (sym, pos) => | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 131 | if Symbol.is_malformed sym | 
| 64677 
8dc24130e8fe
more uniform treatment of "bad" like other messages (with serial number);
 wenzelm parents: 
63475diff
changeset | 132 | then SOME ((pos, Markup.bad ()), "Malformed symbolic character") else NONE); | 
| 61379 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 133 | val is_malformed = Token.is_error tok orelse not (null malformed_symbols); | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 134 | val reports = Token.reports keywords tok @ Token.completion_report tok @ malformed_symbols; | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 135 | in (is_malformed, reports) end; | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 136 | |
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 137 | in | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 138 | |
| 60095 | 139 | fun read_thy st = Toplevel.theory_of st | 
| 140 | handle Toplevel.UNDEF => Pure_Syn.bootstrap_thy; | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58923diff
changeset | 141 | |
| 59689 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 142 | fun read keywords thy master_dir init blobs_info span = | 
| 52510 | 143 | let | 
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58923diff
changeset | 144 | val command_reports = Outer_Syntax.command_reports thy; | 
| 52509 | 145 | |
| 68729 
3a02b424d5fb
clarified ignored span / core range: include formal comments, e.g. relevant for error messages from antiquotations;
 wenzelm parents: 
68728diff
changeset | 146 | val core_range = Token.range_of (drop_suffix Token.is_ignored span); | 
| 52510 | 147 | val pos = | 
| 148 | (case find_first Token.is_command span of | |
| 55708 | 149 | SOME tok => Token.pos_of tok | 
| 68728 | 150 | | NONE => #1 core_range); | 
| 52509 | 151 | |
| 61379 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 152 | val token_reports = map (reports_of_token keywords) span; | 
| 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 153 | val _ = Position.reports_text (maps #2 token_reports @ maps command_reports span); | 
| 69506 | 154 | |
| 155 | val verbatim = | |
| 156 | span |> map_filter (fn tok => | |
| 157 | if Token.kind_of tok = Token.Verbatim then SOME (Token.pos_of tok) else NONE); | |
| 158 | val _ = | |
| 159 | if null verbatim then () | |
| 160 |       else legacy_feature ("Old-style {* verbatim *} token -- use \<open>cartouche\<close> instead" ^
 | |
| 161 | Position.here_list verbatim); | |
| 52510 | 162 | in | 
| 61379 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
61213diff
changeset | 163 | if exists #1 token_reports then Toplevel.malformed pos "Malformed command syntax" | 
| 72747 
5f9d66155081
clarified theory keywords: loaded_files are determined statically in Scala, but ML needs to do it semantically;
 wenzelm parents: 
71675diff
changeset | 164 | else Outer_Syntax.parse_span thy init (resolve_files master_dir blobs_info span) | 
| 52510 | 165 | end; | 
| 52509 | 166 | |
| 55788 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 167 | end; | 
| 
67699e08e969
store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
 wenzelm parents: 
55709diff
changeset | 168 | |
| 52509 | 169 | |
| 170 | (* eval *) | |
| 47336 | 171 | |
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 172 | type eval_state = {failed: bool, command: Toplevel.transition, state: Toplevel.state};
 | 
| 62895 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 173 | |
| 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 174 | fun init_eval_state opt_thy = | 
| 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 175 |  {failed = false,
 | 
| 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 176 | command = Toplevel.empty, | 
| 69883 | 177 | state = | 
| 178 | (case opt_thy of | |
| 69886 | 179 | NONE => Toplevel.init_toplevel () | 
| 69883 | 180 | | SOME thy => Toplevel.theory_toplevel thy)}; | 
| 52600 | 181 | |
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 182 | datatype eval = | 
| 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 183 | Eval of | 
| 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 184 |     {command_id: Document_ID.command, exec_id: Document_ID.exec, eval_process: eval_state lazy};
 | 
| 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 185 | |
| 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 186 | fun eval_command_id (Eval {command_id, ...}) = command_id;
 | 
| 52600 | 187 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 188 | fun eval_exec_id (Eval {exec_id, ...}) = exec_id;
 | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58934diff
changeset | 189 | val eval_eq = op = o apply2 eval_exec_id; | 
| 52607 
33a133d50616
clarified execution: maintain running execs only, check "stable" separately via memo (again);
 wenzelm parents: 
52606diff
changeset | 190 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 191 | val eval_running = Execution.is_running_exec o eval_exec_id; | 
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 192 | fun eval_finished (Eval {eval_process, ...}) = Lazy.is_finished eval_process;
 | 
| 52772 | 193 | |
| 59466 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 194 | fun eval_result (Eval {eval_process, ...}) =
 | 
| 68867 
a8728e3f9822
more robust eval_result: enforce finished result stemming from previous run_process, fail if that was interrupted (e.g. due to resource problems);
 wenzelm parents: 
68858diff
changeset | 195 | Exn.release (Lazy.finished_result eval_process); | 
| 59466 
6fab87db556c
ensure that running into older execution is interruptible (see also b91dc7ab3464);
 wenzelm parents: 
59348diff
changeset | 196 | |
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 197 | val eval_result_command = #command o eval_result; | 
| 52600 | 198 | val eval_result_state = #state o eval_result; | 
| 199 | ||
| 47336 | 200 | local | 
| 201 | ||
| 58923 | 202 | fun reset_state keywords tr st0 = Toplevel.setmp_thread_position tr (fn () => | 
| 56937 | 203 | let | 
| 204 | val name = Toplevel.name_of tr; | |
| 205 | val res = | |
| 58923 | 206 | if Keyword.is_theory_body keywords name then Toplevel.reset_theory st0 | 
| 207 | else if Keyword.is_proof keywords name then Toplevel.reset_proof st0 | |
| 68877 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68874diff
changeset | 208 | else if Keyword.is_theory_end keywords name then | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68874diff
changeset | 209 | (case Toplevel.reset_notepad st0 of | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68874diff
changeset | 210 | NONE => Toplevel.reset_theory st0 | 
| 
33d78e5e0a00
more robust reset_state: begin/end structure takes precedence over goal/proof structure;
 wenzelm parents: 
68874diff
changeset | 211 | | some => some) | 
| 56937 | 212 | else NONE; | 
| 213 | in | |
| 214 | (case res of | |
| 215 | NONE => st0 | |
| 60076 | 216 | | SOME st => (Output.error_message (Toplevel.type_error tr ^ " -- using reset state"); st)) | 
| 56937 | 217 | end) (); | 
| 218 | ||
| 58923 | 219 | fun run keywords int tr st = | 
| 68130 
6fb85346cb79
clarified future scheduling parameters, with support for parallel_limit;
 wenzelm parents: 
67570diff
changeset | 220 | if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (Toplevel.name_of tr) then | 
| 69887 | 221 | let | 
| 222 | val (tr1, tr2) = Toplevel.fork_presentation tr; | |
| 223 | val _ = | |
| 224 |         Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
 | |
| 225 | (fn () => Toplevel.command_exception int tr1 st); | |
| 226 | in Toplevel.command_errors int tr2 st end | |
| 51284 
59a03019f3bf
fork diagnostic commands (theory loader and PIDE interaction);
 wenzelm parents: 
51266diff
changeset | 227 | else Toplevel.command_errors int tr st; | 
| 47336 | 228 | |
| 67570 | 229 | fun check_token_comments ctxt tok = | 
| 230 | (Thy_Output.check_comments ctxt (Input.source_explode (Token.input_of tok)); []) | |
| 231 | handle exn => | |
| 232 | if Exn.is_interrupt exn then Exn.reraise exn | |
| 233 | else Runtime.exn_messages exn; | |
| 67377 
143665524d8e
check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
 wenzelm parents: 
67194diff
changeset | 234 | |
| 67570 | 235 | fun check_span_comments ctxt span tr = | 
| 236 | Toplevel.setmp_thread_position tr (fn () => maps (check_token_comments ctxt) span) (); | |
| 52510 | 237 | |
| 54678 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54671diff
changeset | 238 | fun report tr m = | 
| 56333 
38f1422ef473
support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
 wenzelm parents: 
56303diff
changeset | 239 | Toplevel.setmp_thread_position tr (fn () => Output.report [Markup.markup_only m]) (); | 
| 54678 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54671diff
changeset | 240 | |
| 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54671diff
changeset | 241 | fun status tr m = | 
| 70662 | 242 | Toplevel.setmp_thread_position tr (fn () => Output.status [Markup.markup_only m]) (); | 
| 54678 
87910da843d5
more uniform status -- accommodate spurious Exn.Interrupt from user code, allow ML_Compiler.exn_messages_id to crash;
 wenzelm parents: 
54671diff
changeset | 243 | |
| 63474 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 244 | fun command_indent tr st = | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 245 | (case try Toplevel.proof_of st of | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 246 | SOME prf => | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 247 | let val keywords = Thy_Header.get_keywords (Proof.theory_of prf) in | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 248 | if Keyword.command_kind keywords (Toplevel.name_of tr) = SOME Keyword.prf_script then | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 249 | (case try Proof.goal prf of | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 250 |             SOME {goal, ...} =>
 | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 251 | let val n = Thm.nprems_of goal | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 252 | in if n > 1 then report tr (Markup.command_indent (n - 1)) else () end | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 253 | | NONE => ()) | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 254 | else () | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 255 | end | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 256 | | NONE => ()); | 
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 257 | |
| 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 258 | |
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 259 | fun eval_state keywords span tr ({state, ...}: eval_state) =
 | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 260 | let | 
| 62924 
ce47945ce4fb
tuned signature -- closer to Exn.Interrupt.expose in Scala;
 wenzelm parents: 
62895diff
changeset | 261 | val _ = Thread_Attributes.expose_interrupt (); | 
| 47336 | 262 | |
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 263 | val st = reset_state keywords tr state; | 
| 56937 | 264 | |
| 63474 
f66e3c3b0fb1
semantic indentation for unstructured proof scripts;
 wenzelm parents: 
62924diff
changeset | 265 | val _ = command_indent tr st; | 
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 266 | val _ = status tr Markup.running; | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 267 | val (errs1, result) = run keywords true tr st; | 
| 67381 | 268 | val errs2 = | 
| 269 | (case result of | |
| 270 | NONE => [] | |
| 69892 | 271 | | SOME st' => check_span_comments (Toplevel.presentation_context st') span tr); | 
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 272 | val errs = errs1 @ errs2; | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 273 | val _ = List.app (Future.error_message (Toplevel.pos_of tr)) errs; | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 274 | in | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 275 | (case result of | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 276 | NONE => | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 277 | let | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 278 | val _ = status tr Markup.failed; | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 279 | val _ = status tr Markup.finished; | 
| 68871 
f5c76072db55
more explicit status for "canceled" command within theory node;
 wenzelm parents: 
68868diff
changeset | 280 | val _ = if null errs then (status tr Markup.canceled; Exn.interrupt ()) else (); | 
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 281 |         in {failed = true, command = tr, state = st} end
 | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 282 | | SOME st' => | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 283 | let | 
| 68884 
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
 wenzelm parents: 
68877diff
changeset | 284 | val _ = | 
| 
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
 wenzelm parents: 
68877diff
changeset | 285 | if Keyword.is_theory_end keywords (Toplevel.name_of tr) andalso | 
| 
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
 wenzelm parents: 
68877diff
changeset | 286 | can (Toplevel.end_theory Position.none) st' | 
| 
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
 wenzelm parents: 
68877diff
changeset | 287 | then status tr Markup.finalized else (); | 
| 68886 
1167f2d8a167
more robust: avoid race-condition of terminated vs. consolidated;
 wenzelm parents: 
68884diff
changeset | 288 | val _ = status tr Markup.finished; | 
| 59472 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 289 |         in {failed = false, command = tr, state = st'} end)
 | 
| 
513300fa2d09
discontinued special treatment of malformed commands (reverting e46cd0d26481), i.e. errors in outer syntax failure are treated like errors in inner syntax, name space lookup etc.;
 wenzelm parents: 
59466diff
changeset | 290 | end; | 
| 47336 | 291 | |
| 52534 | 292 | in | 
| 293 | ||
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 294 | fun eval keywords master_dir init blobs_info command_id span eval0 = | 
| 52534 | 295 | let | 
| 296 | val exec_id = Document_ID.make (); | |
| 297 | fun process () = | |
| 298 | let | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58923diff
changeset | 299 | val eval_state0 = eval_result eval0; | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
58923diff
changeset | 300 | val thy = read_thy (#state eval_state0); | 
| 52534 | 301 | val tr = | 
| 302 | Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id)) | |
| 59689 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 303 | (fn () => | 
| 
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
 wenzelm parents: 
59685diff
changeset | 304 | read keywords thy master_dir init blobs_info span |> Toplevel.exec_id exec_id) (); | 
| 58934 
385a4cc7426f
prefer externally provided keywords -- Command.read_thy may degenerate to bootstrap_thy in case of errors;
 wenzelm parents: 
58928diff
changeset | 305 | in eval_state keywords span tr eval_state0 end; | 
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 306 | in | 
| 68874 | 307 |     Eval {command_id = command_id, exec_id = exec_id,
 | 
| 308 | eval_process = Lazy.lazy_name "Command.eval" process} | |
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 309 | end; | 
| 52534 | 310 | |
| 47336 | 311 | end; | 
| 312 | ||
| 52509 | 313 | |
| 314 | (* print *) | |
| 315 | ||
| 52600 | 316 | datatype print = Print of | 
| 52850 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 317 |  {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
 | 
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 318 | exec_id: Document_ID.exec, print_process: unit lazy}; | 
| 52600 | 319 | |
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 320 | fun print_exec_id (Print {exec_id, ...}) = exec_id;
 | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58934diff
changeset | 321 | val print_eq = op = o apply2 print_exec_id; | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 322 | |
| 52526 | 323 | type print_fn = Toplevel.transition -> Toplevel.state -> unit; | 
| 52515 | 324 | |
| 52647 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 325 | type print_function = | 
| 58923 | 326 |   {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
 | 
| 52953 
2c927b7501c5
explicit "strict" flag for print functions (flipped internal meaning);
 wenzelm parents: 
52853diff
changeset | 327 |     {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
 | 
| 52647 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 328 | |
| 52511 | 329 | local | 
| 330 | ||
| 52647 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 331 | val print_functions = | 
| 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 332 | Synchronized.var "Command.print_functions" ([]: (string * print_function) list); | 
| 52511 | 333 | |
| 56265 
785569927666
discontinued Toplevel.debug in favour of system option "exception_trace";
 wenzelm parents: 
56034diff
changeset | 334 | fun print_error tr opt_context e = | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56292diff
changeset | 335 | (Toplevel.setmp_thread_position tr o Runtime.controlled_execution opt_context) e () | 
| 52619 
70d5f2f7d27f
reraise interrupts outside command regular transactions -- relevant for memo_stable;
 wenzelm parents: 
52609diff
changeset | 336 | handle exn => | 
| 62505 | 337 | if Exn.is_interrupt exn then Exn.reraise exn | 
| 65948 | 338 | else List.app (Future.error_message (Toplevel.pos_of tr)) (Runtime.exn_messages exn); | 
| 52516 
b5b3c888df9f
more exception handling -- make print functions total;
 wenzelm parents: 
52515diff
changeset | 339 | |
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 340 | fun print_finished (Print {print_process, ...}) = Lazy.is_finished print_process;
 | 
| 52656 
9437f440ef3f
keep persistent prints only if actually finished;
 wenzelm parents: 
52651diff
changeset | 341 | |
| 52600 | 342 | fun print_persistent (Print {persistent, ...}) = persistent;
 | 
| 52596 
40298d383463
global management of command execution fragments;
 wenzelm parents: 
52586diff
changeset | 343 | |
| 52853 
4ab66773a41f
prefer canonical order, to avoid potential fluctuation due to front-end edits;
 wenzelm parents: 
52850diff
changeset | 344 | val overlay_ord = prod_ord string_ord (list_ord string_ord); | 
| 
4ab66773a41f
prefer canonical order, to avoid potential fluctuation due to front-end edits;
 wenzelm parents: 
52850diff
changeset | 345 | |
| 68333 | 346 | fun make_print (name, args) {delay, pri, persistent, strict, print_fn} eval =
 | 
| 347 | let | |
| 348 | val exec_id = Document_ID.make (); | |
| 349 | fun process () = | |
| 350 | let | |
| 351 |         val {failed, command, state = st', ...} = eval_result eval;
 | |
| 352 | val tr = Toplevel.exec_id exec_id command; | |
| 353 | val opt_context = try Toplevel.generic_theory_of st'; | |
| 354 | in | |
| 355 | if failed andalso strict then () | |
| 356 | else print_error tr opt_context (fn () => print_fn tr st') | |
| 357 | end; | |
| 358 | in | |
| 359 |     Print {
 | |
| 360 | name = name, args = args, delay = delay, pri = pri, persistent = persistent, | |
| 361 | exec_id = exec_id, print_process = Lazy.lazy_name "Command.print" process} | |
| 362 | end; | |
| 363 | ||
| 364 | fun bad_print name_args exn = | |
| 365 |   make_print name_args {delay = NONE, pri = 0, persistent = false,
 | |
| 366 | strict = false, print_fn = fn _ => fn _ => Exn.reraise exn}; | |
| 367 | ||
| 52511 | 368 | in | 
| 52509 | 369 | |
| 68344 | 370 | fun print0 {pri, print_fn} =
 | 
| 68334 | 371 |   make_print ("", [serial_string ()])
 | 
| 68344 | 372 |     {delay = NONE, pri = pri, persistent = true, strict = true, print_fn = print_fn};
 | 
| 68334 | 373 | |
| 58923 | 374 | fun print command_visible command_overlays keywords command_name eval old_prints = | 
| 52570 | 375 | let | 
| 52850 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 376 | val print_functions = Synchronized.value print_functions; | 
| 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 377 | |
| 68333 | 378 | fun new_print (name, args) get_pr = | 
| 52570 | 379 | let | 
| 58923 | 380 | val params = | 
| 381 |          {keywords = keywords,
 | |
| 382 | command_name = command_name, | |
| 383 | args = args, | |
| 384 | exec_id = eval_exec_id eval}; | |
| 52570 | 385 | in | 
| 56303 
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
 wenzelm parents: 
56292diff
changeset | 386 | (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of | 
| 52570 | 387 | Exn.Res NONE => NONE | 
| 68333 | 388 | | Exn.Res (SOME pr) => SOME (make_print (name, args) pr eval) | 
| 389 | | Exn.Exn exn => SOME (bad_print (name, args) exn eval)) | |
| 52570 | 390 | end; | 
| 391 | ||
| 68333 | 392 | fun get_print (name, args) = | 
| 393 | (case find_first (fn Print print => (#name print, #args print) = (name, args)) old_prints of | |
| 52850 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 394 | NONE => | 
| 68333 | 395 | (case AList.lookup (op =) print_functions name of | 
| 396 | NONE => | |
| 397 |               SOME (bad_print (name, args) (ERROR ("Missing print function " ^ quote name)) eval)
 | |
| 398 | | SOME get_pr => new_print (name, args) get_pr) | |
| 52850 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 399 | | some => some); | 
| 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 400 | |
| 68334 | 401 | val retained_prints = | 
| 402 | filter (fn print => print_finished print andalso print_persistent print) old_prints; | |
| 403 | val visible_prints = | |
| 52570 | 404 | if command_visible then | 
| 68333 | 405 | fold (fn (name, _) => cons (name, [])) print_functions command_overlays | 
| 52853 
4ab66773a41f
prefer canonical order, to avoid potential fluctuation due to front-end edits;
 wenzelm parents: 
52850diff
changeset | 406 | |> sort_distinct overlay_ord | 
| 52850 
9fff9f78240a
support print functions with explicit arguments, as provided by overlays;
 wenzelm parents: 
52785diff
changeset | 407 | |> map_filter get_print | 
| 68334 | 408 | else []; | 
| 409 | val new_prints = visible_prints @ retained_prints; | |
| 52570 | 410 | in | 
| 52600 | 411 | if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints | 
| 52570 | 412 | end; | 
| 52511 | 413 | |
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 414 | fun parallel_print (Print {pri, ...}) =
 | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 415 | pri <= 0 orelse (Future.enabled () andalso Options.default_bool "parallel_print"); | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 416 | |
| 52647 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 417 | fun print_function name f = | 
| 52511 | 418 | Synchronized.change print_functions (fn funs => | 
| 68334 | 419 | (if name = "" then error "Unnamed print function" else (); | 
| 420 | if not (AList.defined (op =) funs name) then () | |
| 52511 | 421 |     else warning ("Redefining command print function: " ^ quote name);
 | 
| 52647 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 422 | AList.update (op =) (name, f) funs)); | 
| 52511 | 423 | |
| 52571 | 424 | fun no_print_function name = | 
| 425 | Synchronized.change print_functions (filter_out (equal name o #1)); | |
| 426 | ||
| 52511 | 427 | end; | 
| 428 | ||
| 52526 | 429 | val _ = | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 430 | print_function "Execution.print" | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 431 |     (fn {args, exec_id, ...} =>
 | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 432 | if null args then | 
| 60610 
f52b4b0c10c4
improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
 wenzelm parents: 
60095diff
changeset | 433 |         SOME {delay = NONE, pri = Task_Queue.urgent_pri + 2, persistent = false, strict = false,
 | 
| 56292 | 434 | print_fn = fn _ => fn _ => Execution.fork_prints exec_id} | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 435 | else NONE); | 
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 436 | |
| 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 437 | val _ = | 
| 52647 
45ce95b8bf69
determine print function parameters dynamically, e.g. depending on runtime options;
 wenzelm parents: 
52619diff
changeset | 438 | print_function "print_state" | 
| 58923 | 439 |     (fn {keywords, command_name, ...} =>
 | 
| 61213 | 440 | if Options.default_bool "editor_output_state" andalso Keyword.is_printed keywords command_name | 
| 441 | then | |
| 60610 
f52b4b0c10c4
improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
 wenzelm parents: 
60095diff
changeset | 442 |         SOME {delay = NONE, pri = Task_Queue.urgent_pri + 1, persistent = false, strict = false,
 | 
| 61208 | 443 | print_fn = fn _ => fn st => | 
| 444 | if Toplevel.is_proof st then Output.state (Toplevel.string_of_state st) | |
| 445 | else ()} | |
| 56895 
f058120aaad4
discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
 wenzelm parents: 
56887diff
changeset | 446 | else NONE); | 
| 52509 | 447 | |
| 52532 | 448 | |
| 52600 | 449 | (* combined execution *) | 
| 450 | ||
| 451 | type exec = eval * print list; | |
| 62895 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 452 | |
| 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 453 | fun init_exec opt_thy : exec = | 
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 454 | (Eval | 
| 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 455 |     {command_id = Document_ID.none, exec_id = Document_ID.none,
 | 
| 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 456 | eval_process = Lazy.value (init_eval_state opt_thy)}, []); | 
| 62895 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 457 | |
| 
54c2abe7e9a4
treat ROOT.ML as theory with header "theory ML_Root imports ML_Bootstrap begin";
 wenzelm parents: 
62826diff
changeset | 458 | val no_exec = init_exec NONE; | 
| 52532 | 459 | |
| 52600 | 460 | fun exec_ids NONE = [] | 
| 56291 
e79f76a48449
added Execution.print: accumulate print operations for some command execution, which are applied later and print time;
 wenzelm parents: 
56265diff
changeset | 461 | | exec_ids (SOME (eval, prints)) = eval_exec_id eval :: map print_exec_id prints; | 
| 52600 | 462 | |
| 463 | local | |
| 464 | ||
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 465 | fun run_process execution_id exec_id process = | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 466 | let val group = Future.worker_subgroup () in | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 467 | if Execution.running execution_id exec_id [group] then | 
| 68931 | 468 |       ignore (task_context group (fn () => Lazy.force_result {strict = true} process) ())
 | 
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 469 | else () | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 470 | end; | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 471 | |
| 59328 
b83d6c3c439a
ignore print process even after fork, to avoid loosing active worker threads;
 wenzelm parents: 
59193diff
changeset | 472 | fun ignore_process process = | 
| 
b83d6c3c439a
ignore print process even after fork, to avoid loosing active worker threads;
 wenzelm parents: 
59193diff
changeset | 473 | Lazy.is_running process orelse Lazy.is_finished process; | 
| 
b83d6c3c439a
ignore print process even after fork, to avoid loosing active worker threads;
 wenzelm parents: 
59193diff
changeset | 474 | |
| 68184 
6c693b2700b3
support for dynamic document output while editing;
 wenzelm parents: 
68130diff
changeset | 475 | fun run_eval execution_id (Eval {exec_id, eval_process, ...}) =
 | 
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 476 | if Lazy.is_finished eval_process then () | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 477 | else run_process execution_id exec_id eval_process; | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 478 | |
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 479 | fun fork_print execution_id deps (Print {name, delay, pri, exec_id, print_process, ...}) =
 | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 480 | let | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 481 | val group = Future.worker_subgroup (); | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 482 | fun fork () = | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 483 | ignore ((singleton o Future.forks) | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 484 |         {name = name, group = SOME group, deps = deps, pri = pri, interrupts = true}
 | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 485 | (fn () => | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 486 | if ignore_process print_process then () | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 487 | else run_process execution_id exec_id print_process)); | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 488 | in | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 489 | (case delay of | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 490 | NONE => fork () | 
| 69826 
1bea05713dde
physical vs. logical events, the latter takes GC time into account;
 wenzelm parents: 
69506diff
changeset | 491 |     | SOME d => ignore (Event_Timer.request {physical = true} (Time.now () + d) fork))
 | 
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 492 | end; | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 493 | |
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 494 | fun run_print execution_id (print as Print {exec_id, print_process, ...}) =
 | 
| 59328 
b83d6c3c439a
ignore print process even after fork, to avoid loosing active worker threads;
 wenzelm parents: 
59193diff
changeset | 495 | if ignore_process print_process then () | 
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 496 | else if parallel_print print then fork_print execution_id [] print | 
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 497 | else run_process execution_id exec_id print_process; | 
| 52559 
ddaf277e0d8c
more direct interleaving of eval/print and update/execution -- refrain from crude manipulation of max_threads;
 wenzelm parents: 
52536diff
changeset | 498 | |
| 52600 | 499 | in | 
| 500 | ||
| 59193 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 501 | fun exec execution_id (eval, prints) = | 
| 
59f1591a11cb
eliminated Document.execution frontier (again, see 627fb639a2d9): just run into older execution, potentially stalling worker thread, but without global delay due to long-running tasks (notably sledgehammer);
 wenzelm parents: 
59188diff
changeset | 502 | (run_eval execution_id eval; List.app (run_print execution_id) prints); | 
| 52532 | 503 | |
| 68366 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 504 | fun exec_parallel_prints execution_id deps (exec as (Eval {eval_process, ...}, prints)) =
 | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 505 | if Lazy.is_finished eval_process | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 506 | then (List.app (fork_print execution_id deps) prints; NONE) | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 507 | else SOME exec; | 
| 
cd387c55e085
fork parallel prints early in execution: avoid degradation of priority due to main eval task;
 wenzelm parents: 
68344diff
changeset | 508 | |
| 47336 | 509 | end; | 
| 510 | ||
| 52600 | 511 | end; |