src/Pure/PIDE/command.ML
author wenzelm
Tue May 06 16:05:14 2014 +0200 (2014-05-06)
changeset 56875 f6259d6fb565
parent 56504 d71f4be7e287
child 56887 1ca814da47ae
permissions -rw-r--r--
explicit option parallel_print to downgrade parallel scheduling, which might occasionally help for big and heavy "scripts";
wenzelm@47336
     1
(*  Title:      Pure/PIDE/command.ML
wenzelm@47336
     2
    Author:     Makarius
wenzelm@47336
     3
wenzelm@52534
     4
Prover command execution: read -- eval -- print.
wenzelm@47336
     5
*)
wenzelm@47336
     6
wenzelm@47336
     7
signature COMMAND =
wenzelm@47336
     8
sig
wenzelm@56458
     9
  type blob = (string * (SHA1.digest * string list) option) Exn.result
wenzelm@54526
    10
  val read_file: Path.T -> Position.T -> Path.T -> Token.file
wenzelm@55798
    11
  val read: (unit -> theory) -> Path.T -> blob list -> Token.T list -> Toplevel.transition
wenzelm@52600
    12
  type eval
wenzelm@52607
    13
  val eval_eq: eval * eval -> bool
wenzelm@52784
    14
  val eval_running: eval -> bool
wenzelm@52772
    15
  val eval_finished: eval -> bool
wenzelm@52536
    16
  val eval_result_state: eval -> Toplevel.state
wenzelm@55798
    17
  val eval: (unit -> theory) -> Path.T -> blob list -> Token.T list -> eval -> eval
wenzelm@52600
    18
  type print
wenzelm@52850
    19
  val print: bool -> (string * string list) list -> string ->
wenzelm@52850
    20
    eval -> print list -> print list option
wenzelm@52526
    21
  type print_fn = Toplevel.transition -> Toplevel.state -> unit
wenzelm@52953
    22
  type print_function =
wenzelm@56291
    23
    {command_name: string, args: string list, exec_id: Document_ID.exec} ->
wenzelm@52953
    24
      {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option
wenzelm@52953
    25
  val print_function: string -> print_function -> unit
wenzelm@52571
    26
  val no_print_function: string -> unit
wenzelm@52600
    27
  type exec = eval * print list
wenzelm@52600
    28
  val no_exec: exec
wenzelm@52600
    29
  val exec_ids: exec option -> Document_ID.exec list
wenzelm@52606
    30
  val exec: Document_ID.execution -> exec -> unit
wenzelm@47336
    31
end;
wenzelm@47336
    32
wenzelm@47336
    33
structure Command: COMMAND =
wenzelm@47336
    34
struct
wenzelm@47336
    35
wenzelm@52775
    36
(** memo results **)
wenzelm@47341
    37
wenzelm@47341
    38
datatype 'a expr =
wenzelm@52596
    39
  Expr of Document_ID.exec * (unit -> 'a) |
wenzelm@47341
    40
  Result of 'a Exn.result;
wenzelm@47341
    41
wenzelm@47341
    42
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    43
with
wenzelm@47341
    44
wenzelm@52596
    45
fun memo exec_id e = Memo (Synchronized.var "Command.memo" (Expr (exec_id, e)));
wenzelm@47341
    46
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    47
wenzelm@47342
    48
fun memo_result (Memo v) =
wenzelm@47342
    49
  (case Synchronized.value v of
wenzelm@52607
    50
    Expr (exec_id, _) => error ("Unfinished execution result: " ^ Document_ID.print exec_id)
wenzelm@52607
    51
  | Result res => Exn.release res);
wenzelm@47341
    52
wenzelm@52656
    53
fun memo_finished (Memo v) =
wenzelm@52785
    54
  (case Synchronized.value v of Expr _ => false | Result _ => true);
wenzelm@52656
    55
wenzelm@52607
    56
fun memo_exec execution_id (Memo v) =
wenzelm@52774
    57
  Synchronized.timed_access v (K (SOME Time.zeroTime))
wenzelm@52609
    58
    (fn expr =>
wenzelm@52609
    59
      (case expr of
wenzelm@52775
    60
        Expr (exec_id, body) =>
wenzelm@52609
    61
          uninterruptible (fn restore_attributes => fn () =>
wenzelm@54637
    62
            let val group = Future.worker_subgroup () in
wenzelm@54637
    63
              if Execution.running execution_id exec_id [group] then
wenzelm@54637
    64
                let
wenzelm@54637
    65
                  val res =
wenzelm@54637
    66
                    (body
wenzelm@54637
    67
                      |> restore_attributes
wenzelm@54649
    68
                      |> Future.task_context "Command.memo_exec" group
wenzelm@54637
    69
                      |> Exn.interruptible_capture) ();
wenzelm@54637
    70
                in SOME ((), Result res) end
wenzelm@54637
    71
              else SOME ((), expr)
wenzelm@54637
    72
            end) ()
wenzelm@52775
    73
      | Result _ => SOME ((), expr)))
wenzelm@52775
    74
  |> (fn NONE => error "Conflicting command execution" | _ => ());
wenzelm@52596
    75
wenzelm@52607
    76
fun memo_fork params execution_id (Memo v) =
wenzelm@52596
    77
  (case Synchronized.value v of
wenzelm@52596
    78
    Result _ => ()
wenzelm@52607
    79
  | _ => ignore ((singleton o Future.forks) params (fn () => memo_exec execution_id (Memo v))));
wenzelm@52526
    80
wenzelm@47341
    81
end;
wenzelm@47341
    82
wenzelm@47341
    83
wenzelm@52607
    84
wenzelm@52536
    85
(** main phases of execution **)
wenzelm@52536
    86
wenzelm@52510
    87
(* read *)
wenzelm@52509
    88
wenzelm@55798
    89
type blob =
wenzelm@56458
    90
  (string * (SHA1.digest * string list) option) Exn.result;  (*file node name, digest, lines*)
wenzelm@54519
    91
wenzelm@56504
    92
fun read_file_node file_node master_dir pos src_path =
wenzelm@54526
    93
  let
wenzelm@56034
    94
    val _ = Position.report pos Markup.language_path;
wenzelm@56504
    95
    val _ =
wenzelm@56504
    96
      (case try Url.explode file_node of
wenzelm@56504
    97
        NONE => ()
wenzelm@56504
    98
      | SOME (Url.File _) => ()
wenzelm@56504
    99
      | _ =>
wenzelm@56504
   100
         (Position.report pos (Markup.path file_node);
wenzelm@56504
   101
          error ("Prover cannot load remote file " ^
wenzelm@56504
   102
            Markup.markup (Markup.path file_node) (quote file_node) ^ Position.here pos)));
wenzelm@54526
   103
    val full_path = File.check_file (File.full_path master_dir src_path);
wenzelm@54526
   104
    val _ = Position.report pos (Markup.path (Path.implode full_path));
wenzelm@55788
   105
    val text = File.read full_path;
wenzelm@55788
   106
    val lines = split_lines text;
wenzelm@55788
   107
    val digest = SHA1.digest text;
wenzelm@55788
   108
  in {src_path = src_path, lines = lines, digest = digest, pos = Path.position full_path} end;
wenzelm@55788
   109
wenzelm@56504
   110
val read_file = read_file_node "";
wenzelm@56504
   111
wenzelm@55788
   112
local
wenzelm@55788
   113
wenzelm@56447
   114
fun blob_file src_path lines digest file_node =
wenzelm@55788
   115
  let
wenzelm@55788
   116
    val file_pos =
wenzelm@56447
   117
      Position.file file_node |>
wenzelm@55788
   118
      (case Position.get_id (Position.thread_data ()) of
wenzelm@55788
   119
        NONE => I
wenzelm@55788
   120
      | SOME exec_id => Position.put_id exec_id);
wenzelm@55788
   121
  in {src_path = src_path, lines = lines, digest = digest, pos = file_pos} end
wenzelm@54526
   122
wenzelm@54526
   123
fun resolve_files master_dir blobs toks =
wenzelm@54519
   124
  (case Thy_Syntax.parse_spans toks of
wenzelm@54519
   125
    [span] => span
wenzelm@54523
   126
      |> Thy_Syntax.resolve_files (fn cmd => fn (path, pos) =>
wenzelm@54523
   127
        let
wenzelm@56504
   128
          fun make_file src_path (Exn.Res (file_node, NONE)) =
wenzelm@56504
   129
                Exn.interruptible_capture (fn () =>
wenzelm@56504
   130
                  read_file_node file_node master_dir pos src_path) ()
wenzelm@56458
   131
            | make_file src_path (Exn.Res (file_node, SOME (digest, lines))) =
wenzelm@56458
   132
               (Position.reports [(pos, Markup.language_path), (pos, Markup.path file_node)];
wenzelm@56447
   133
                Exn.Res (blob_file src_path lines digest file_node))
wenzelm@54523
   134
            | make_file _ (Exn.Exn e) = Exn.Exn e;
wenzelm@54523
   135
          val src_paths = Keyword.command_files cmd path;
wenzelm@54523
   136
        in
wenzelm@54527
   137
          if null blobs then
wenzelm@56458
   138
            map2 make_file src_paths (map (K (Exn.Res ("", NONE))) src_paths)
wenzelm@54527
   139
          else if length src_paths = length blobs then
wenzelm@54527
   140
            map2 make_file src_paths blobs
wenzelm@54527
   141
          else error ("Misalignment of inlined files" ^ Position.here pos)
wenzelm@54523
   142
        end)
wenzelm@54519
   143
      |> Thy_Syntax.span_content
wenzelm@54519
   144
  | _ => toks);
wenzelm@54519
   145
wenzelm@55788
   146
in
wenzelm@55788
   147
wenzelm@54526
   148
fun read init master_dir blobs span =
wenzelm@52510
   149
  let
wenzelm@52510
   150
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
   151
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
   152
wenzelm@55709
   153
    val proper_range = Token.range_of (#1 (take_suffix Token.is_improper span));
wenzelm@52510
   154
    val pos =
wenzelm@52510
   155
      (case find_first Token.is_command span of
wenzelm@55708
   156
        SOME tok => Token.pos_of tok
wenzelm@55709
   157
      | NONE => #1 proper_range);
wenzelm@52509
   158
wenzelm@52510
   159
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
   160
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
   161
  in
wenzelm@52510
   162
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   163
    else
wenzelm@54526
   164
      (case Outer_Syntax.read_spans outer_syntax (resolve_files master_dir blobs span) of
wenzelm@52510
   165
        [tr] =>
wenzelm@52510
   166
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
   167
            Toplevel.malformed pos "Illegal control command"
wenzelm@52534
   168
          else Toplevel.modify_init init tr
wenzelm@55709
   169
      | [] => Toplevel.ignored (#1 (Token.range_of span))
wenzelm@55709
   170
      | _ => Toplevel.malformed (#1 proper_range) "Exactly one command expected")
wenzelm@55709
   171
      handle ERROR msg => Toplevel.malformed (#1 proper_range) msg
wenzelm@52510
   172
  end;
wenzelm@52509
   173
wenzelm@55788
   174
end;
wenzelm@55788
   175
wenzelm@52509
   176
wenzelm@52509
   177
(* eval *)
wenzelm@47336
   178
wenzelm@52600
   179
type eval_state =
wenzelm@52600
   180
  {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@52600
   181
val init_eval_state =
wenzelm@52600
   182
  {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52600
   183
wenzelm@52600
   184
datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
wenzelm@52600
   185
wenzelm@56291
   186
fun eval_exec_id (Eval {exec_id, ...}) = exec_id;
wenzelm@56291
   187
val eval_eq = op = o pairself eval_exec_id;
wenzelm@52607
   188
wenzelm@56291
   189
val eval_running = Execution.is_running_exec o eval_exec_id;
wenzelm@52772
   190
fun eval_finished (Eval {eval_process, ...}) = memo_finished eval_process;
wenzelm@52772
   191
wenzelm@52600
   192
fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
wenzelm@52600
   193
val eval_result_state = #state o eval_result;
wenzelm@52600
   194
wenzelm@47336
   195
local
wenzelm@47336
   196
wenzelm@47336
   197
fun run int tr st =
wenzelm@53189
   198
  if Goal.future_enabled 1 andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@53192
   199
    (Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   200
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   201
  else Toplevel.command_errors int tr st;
wenzelm@47336
   202
wenzelm@52510
   203
fun check_cmts span tr st' =
wenzelm@52510
   204
  Toplevel.setmp_thread_position tr
wenzelm@52510
   205
    (fn () =>
wenzelm@52510
   206
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   207
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52619
   208
          handle exn =>
wenzelm@52619
   209
            if Exn.is_interrupt exn then reraise exn
wenzelm@56303
   210
            else Runtime.exn_messages_ids exn)) ();
wenzelm@52510
   211
wenzelm@54678
   212
fun report tr m =
wenzelm@56333
   213
  Toplevel.setmp_thread_position tr (fn () => Output.report [Markup.markup_only m]) ();
wenzelm@54678
   214
wenzelm@54678
   215
fun status tr m =
wenzelm@54678
   216
  Toplevel.setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
wenzelm@54678
   217
wenzelm@47336
   218
fun proof_status tr st =
wenzelm@47336
   219
  (case try Toplevel.proof_of st of
wenzelm@54678
   220
    SOME prf => status tr (Proof.status_markup prf)
wenzelm@47336
   221
  | NONE => ());
wenzelm@47336
   222
wenzelm@52534
   223
fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   224
  if malformed then
wenzelm@52526
   225
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   226
  else
wenzelm@48772
   227
    let
wenzelm@48772
   228
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   229
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   230
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   231
wenzelm@48772
   232
      val _ = Multithreading.interrupted ();
wenzelm@54678
   233
      val _ = status tr Markup.running;
wenzelm@48918
   234
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   235
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   236
      val errs = errs1 @ errs2;
wenzelm@56333
   237
      val _ = List.app (Future.error_message (Toplevel.pos_of tr)) errs;
wenzelm@48772
   238
    in
wenzelm@48772
   239
      (case result of
wenzelm@48772
   240
        NONE =>
wenzelm@48772
   241
          let
wenzelm@54678
   242
            val _ = status tr Markup.failed;
wenzelm@54678
   243
            val _ = status tr Markup.finished;
wenzelm@54678
   244
            val _ = if null errs then (report tr Markup.bad; Exn.interrupt ()) else ();
wenzelm@52526
   245
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   246
      | SOME st' =>
wenzelm@48772
   247
          let
wenzelm@48772
   248
            val _ = proof_status tr st';
wenzelm@54678
   249
            val _ = status tr Markup.finished;
wenzelm@52526
   250
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   251
    end;
wenzelm@47336
   252
wenzelm@52534
   253
in
wenzelm@52534
   254
wenzelm@54526
   255
fun eval init master_dir blobs span eval0 =
wenzelm@52534
   256
  let
wenzelm@52534
   257
    val exec_id = Document_ID.make ();
wenzelm@52534
   258
    fun process () =
wenzelm@52534
   259
      let
wenzelm@52534
   260
        val tr =
wenzelm@52534
   261
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@54526
   262
            (fn () => read init master_dir blobs span |> Toplevel.exec_id exec_id) ();
wenzelm@52534
   263
      in eval_state span tr (eval_result eval0) end;
wenzelm@52600
   264
  in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
wenzelm@52534
   265
wenzelm@47336
   266
end;
wenzelm@47336
   267
wenzelm@52509
   268
wenzelm@52509
   269
(* print *)
wenzelm@52509
   270
wenzelm@52600
   271
datatype print = Print of
wenzelm@52850
   272
 {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
wenzelm@52600
   273
  exec_id: Document_ID.exec, print_process: unit memo};
wenzelm@52600
   274
wenzelm@56291
   275
fun print_exec_id (Print {exec_id, ...}) = exec_id;
wenzelm@56291
   276
val print_eq = op = o pairself print_exec_id;
wenzelm@56291
   277
wenzelm@52526
   278
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   279
wenzelm@52647
   280
type print_function =
wenzelm@56291
   281
  {command_name: string, args: string list, exec_id: Document_ID.exec} ->
wenzelm@52953
   282
    {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
wenzelm@52647
   283
wenzelm@52511
   284
local
wenzelm@52511
   285
wenzelm@52647
   286
val print_functions =
wenzelm@52647
   287
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   288
wenzelm@56265
   289
fun print_error tr opt_context e =
wenzelm@56303
   290
  (Toplevel.setmp_thread_position tr o Runtime.controlled_execution opt_context) e ()
wenzelm@52619
   291
    handle exn =>
wenzelm@52619
   292
      if Exn.is_interrupt exn then reraise exn
wenzelm@56333
   293
      else List.app (Future.error_message (Toplevel.pos_of tr)) (Runtime.exn_messages_ids exn);
wenzelm@52516
   294
wenzelm@52775
   295
fun print_finished (Print {print_process, ...}) = memo_finished print_process;
wenzelm@52656
   296
wenzelm@52600
   297
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   298
wenzelm@52853
   299
val overlay_ord = prod_ord string_ord (list_ord string_ord);
wenzelm@52853
   300
wenzelm@52511
   301
in
wenzelm@52509
   302
wenzelm@52850
   303
fun print command_visible command_overlays command_name eval old_prints =
wenzelm@52570
   304
  let
wenzelm@52850
   305
    val print_functions = Synchronized.value print_functions;
wenzelm@52850
   306
wenzelm@52999
   307
    fun make_print name args {delay, pri, persistent, strict, print_fn} =
wenzelm@52999
   308
      let
wenzelm@52999
   309
        val exec_id = Document_ID.make ();
wenzelm@52999
   310
        fun process () =
wenzelm@52999
   311
          let
wenzelm@52999
   312
            val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52999
   313
            val tr = Toplevel.exec_id exec_id command;
wenzelm@56265
   314
            val opt_context = try Toplevel.generic_theory_of st';
wenzelm@52999
   315
          in
wenzelm@52999
   316
            if failed andalso strict then ()
wenzelm@56265
   317
            else print_error tr opt_context (fn () => print_fn tr st')
wenzelm@52999
   318
          end;
wenzelm@52999
   319
      in
wenzelm@52999
   320
        Print {
wenzelm@52999
   321
          name = name, args = args, delay = delay, pri = pri, persistent = persistent,
wenzelm@52999
   322
          exec_id = exec_id, print_process = memo exec_id process}
wenzelm@52999
   323
      end;
wenzelm@52999
   324
wenzelm@52999
   325
    fun bad_print name args exn =
wenzelm@52999
   326
      make_print name args {delay = NONE, pri = 0, persistent = false,
wenzelm@52999
   327
        strict = false, print_fn = fn _ => fn _ => reraise exn};
wenzelm@52999
   328
wenzelm@52850
   329
    fun new_print name args get_pr =
wenzelm@52570
   330
      let
wenzelm@56291
   331
        val params = {command_name = command_name, args = args, exec_id = eval_exec_id eval};
wenzelm@52570
   332
      in
wenzelm@56303
   333
        (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of
wenzelm@52570
   334
          Exn.Res NONE => NONE
wenzelm@52999
   335
        | Exn.Res (SOME pr) => SOME (make_print name args pr)
wenzelm@52999
   336
        | Exn.Exn exn => SOME (bad_print name args exn))
wenzelm@52570
   337
      end;
wenzelm@52570
   338
wenzelm@52850
   339
    fun get_print (a, b) =
wenzelm@52850
   340
      (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of
wenzelm@52850
   341
        NONE =>
wenzelm@52850
   342
          (case AList.lookup (op =) print_functions a of
wenzelm@52999
   343
            NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a)))
wenzelm@52850
   344
          | SOME get_pr => new_print a b get_pr)
wenzelm@52850
   345
      | some => some);
wenzelm@52850
   346
wenzelm@52570
   347
    val new_prints =
wenzelm@52570
   348
      if command_visible then
wenzelm@52853
   349
        fold (fn (a, _) => cons (a, [])) print_functions command_overlays
wenzelm@52853
   350
        |> sort_distinct overlay_ord
wenzelm@52850
   351
        |> map_filter get_print
wenzelm@52656
   352
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   353
  in
wenzelm@52600
   354
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   355
  end;
wenzelm@52511
   356
wenzelm@52647
   357
fun print_function name f =
wenzelm@52511
   358
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   359
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   360
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   361
    AList.update (op =) (name, f) funs));
wenzelm@52511
   362
wenzelm@52571
   363
fun no_print_function name =
wenzelm@52571
   364
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   365
wenzelm@52511
   366
end;
wenzelm@52511
   367
wenzelm@52526
   368
val _ =
wenzelm@56291
   369
  print_function "Execution.print"
wenzelm@56291
   370
    (fn {args, exec_id, ...} =>
wenzelm@56291
   371
      if null args then
wenzelm@56291
   372
        SOME {delay = NONE, pri = 1, persistent = false, strict = true,
wenzelm@56292
   373
          print_fn = fn _ => fn _ => Execution.fork_prints exec_id}
wenzelm@56291
   374
      else NONE);
wenzelm@56291
   375
wenzelm@56291
   376
val _ =
wenzelm@52647
   377
  print_function "print_state"
wenzelm@52850
   378
    (fn {command_name, ...} =>
wenzelm@53404
   379
      SOME {delay = NONE, pri = 1, persistent = false, strict = true,
wenzelm@52651
   380
        print_fn = fn tr => fn st' =>
wenzelm@52651
   381
          let
wenzelm@52651
   382
            val is_init = Keyword.is_theory_begin command_name;
wenzelm@52651
   383
            val is_proof = Keyword.is_proof command_name;
wenzelm@52651
   384
            val do_print =
wenzelm@52651
   385
              not is_init andalso
wenzelm@52651
   386
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52651
   387
          in if do_print then Toplevel.print_state false st' else () end});
wenzelm@52509
   388
wenzelm@52532
   389
wenzelm@52600
   390
(* combined execution *)
wenzelm@52600
   391
wenzelm@52600
   392
type exec = eval * print list;
wenzelm@52600
   393
val no_exec: exec =
wenzelm@52600
   394
  (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
wenzelm@52532
   395
wenzelm@52600
   396
fun exec_ids NONE = []
wenzelm@56291
   397
  | exec_ids (SOME (eval, prints)) = eval_exec_id eval :: map print_exec_id prints;
wenzelm@52600
   398
wenzelm@52600
   399
local
wenzelm@52600
   400
wenzelm@52651
   401
fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
wenzelm@56875
   402
  if pri <= 0 orelse (Multithreading.enabled () andalso Options.default_bool "parallel_print")
wenzelm@56875
   403
  then
wenzelm@52651
   404
    let
wenzelm@52651
   405
      val group = Future.worker_subgroup ();
wenzelm@52651
   406
      fun fork () =
wenzelm@52651
   407
        memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@52651
   408
          execution_id print_process;
wenzelm@52651
   409
    in
wenzelm@52762
   410
      (case delay of
wenzelm@52762
   411
        NONE => fork ()
wenzelm@52762
   412
      | SOME d => ignore (Event_Timer.request (Time.+ (Time.now (), d)) fork))
wenzelm@52651
   413
    end
wenzelm@52651
   414
  else memo_exec execution_id print_process;
wenzelm@52559
   415
wenzelm@52600
   416
in
wenzelm@52600
   417
wenzelm@52607
   418
fun exec execution_id (Eval {eval_process, ...}, prints) =
wenzelm@52607
   419
  (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
wenzelm@52532
   420
wenzelm@47336
   421
end;
wenzelm@47336
   422
wenzelm@52600
   423
end;
wenzelm@52600
   424