src/Pure/PIDE/command.ML
author wenzelm
Mon Mar 10 21:15:29 2014 +0100 (2014-03-10)
changeset 56034 1c59b555ac4a
parent 55798 985bd3a325ab
child 56265 785569927666
permissions -rw-r--r--
some Markup.language_path to prevent completion of symbols (notably "~") -- always "delimited" for simplicity in contrast to 42ac3cfb89f6;
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@55798
     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@52953
    23
    {command_name: string, args: string list} ->
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@55798
    90
  (string * (SHA1.digest * string list) option) Exn.result;  (*file node name, digest, lines*)
wenzelm@54519
    91
wenzelm@54526
    92
fun read_file master_dir pos src_path =
wenzelm@54526
    93
  let
wenzelm@56034
    94
    val _ = Position.report pos Markup.language_path;
wenzelm@54526
    95
    val full_path = File.check_file (File.full_path master_dir src_path);
wenzelm@54526
    96
    val _ = Position.report pos (Markup.path (Path.implode full_path));
wenzelm@55788
    97
    val text = File.read full_path;
wenzelm@55788
    98
    val lines = split_lines text;
wenzelm@55788
    99
    val digest = SHA1.digest text;
wenzelm@55788
   100
  in {src_path = src_path, lines = lines, digest = digest, pos = Path.position full_path} end;
wenzelm@55788
   101
wenzelm@55788
   102
local
wenzelm@55788
   103
wenzelm@55798
   104
fun blob_file src_path lines digest file =
wenzelm@55788
   105
  let
wenzelm@55788
   106
    val file_pos =
wenzelm@55788
   107
      Position.file file (*sic!*) |>
wenzelm@55788
   108
      (case Position.get_id (Position.thread_data ()) of
wenzelm@55788
   109
        NONE => I
wenzelm@55788
   110
      | SOME exec_id => Position.put_id exec_id);
wenzelm@55788
   111
  in {src_path = src_path, lines = lines, digest = digest, pos = file_pos} end
wenzelm@54526
   112
wenzelm@54526
   113
fun resolve_files master_dir blobs toks =
wenzelm@54519
   114
  (case Thy_Syntax.parse_spans toks of
wenzelm@54519
   115
    [span] => span
wenzelm@54523
   116
      |> Thy_Syntax.resolve_files (fn cmd => fn (path, pos) =>
wenzelm@54523
   117
        let
wenzelm@54526
   118
          fun make_file src_path (Exn.Res (_, NONE)) =
wenzelm@54526
   119
                Exn.interruptible_capture (fn () => read_file master_dir pos src_path) ()
wenzelm@55798
   120
            | make_file src_path (Exn.Res (file, SOME (digest, lines))) =
wenzelm@56034
   121
               (Position.reports [(pos, Markup.language_path), (pos, Markup.path file)];
wenzelm@55798
   122
                Exn.Res (blob_file src_path lines digest file))
wenzelm@54523
   123
            | make_file _ (Exn.Exn e) = Exn.Exn e;
wenzelm@54523
   124
          val src_paths = Keyword.command_files cmd path;
wenzelm@54523
   125
        in
wenzelm@54527
   126
          if null blobs then
wenzelm@54527
   127
            map2 make_file src_paths (map (K (Exn.Res ("", NONE))) src_paths)
wenzelm@54527
   128
          else if length src_paths = length blobs then
wenzelm@54527
   129
            map2 make_file src_paths blobs
wenzelm@54527
   130
          else error ("Misalignment of inlined files" ^ Position.here pos)
wenzelm@54523
   131
        end)
wenzelm@54519
   132
      |> Thy_Syntax.span_content
wenzelm@54519
   133
  | _ => toks);
wenzelm@54519
   134
wenzelm@55788
   135
in
wenzelm@55788
   136
wenzelm@54526
   137
fun read init master_dir blobs span =
wenzelm@52510
   138
  let
wenzelm@52510
   139
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
   140
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
   141
wenzelm@55709
   142
    val proper_range = Token.range_of (#1 (take_suffix Token.is_improper span));
wenzelm@52510
   143
    val pos =
wenzelm@52510
   144
      (case find_first Token.is_command span of
wenzelm@55708
   145
        SOME tok => Token.pos_of tok
wenzelm@55709
   146
      | NONE => #1 proper_range);
wenzelm@52509
   147
wenzelm@52510
   148
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
   149
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
   150
  in
wenzelm@52510
   151
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   152
    else
wenzelm@54526
   153
      (case Outer_Syntax.read_spans outer_syntax (resolve_files master_dir blobs span) of
wenzelm@52510
   154
        [tr] =>
wenzelm@52510
   155
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
   156
            Toplevel.malformed pos "Illegal control command"
wenzelm@52534
   157
          else Toplevel.modify_init init tr
wenzelm@55709
   158
      | [] => Toplevel.ignored (#1 (Token.range_of span))
wenzelm@55709
   159
      | _ => Toplevel.malformed (#1 proper_range) "Exactly one command expected")
wenzelm@55709
   160
      handle ERROR msg => Toplevel.malformed (#1 proper_range) msg
wenzelm@52510
   161
  end;
wenzelm@52509
   162
wenzelm@55788
   163
end;
wenzelm@55788
   164
wenzelm@52509
   165
wenzelm@52509
   166
(* eval *)
wenzelm@47336
   167
wenzelm@52600
   168
type eval_state =
wenzelm@52600
   169
  {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@52600
   170
val init_eval_state =
wenzelm@52600
   171
  {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52600
   172
wenzelm@52600
   173
datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
wenzelm@52600
   174
wenzelm@52607
   175
fun eval_eq (Eval {exec_id, ...}, Eval {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   176
wenzelm@52784
   177
fun eval_running (Eval {exec_id, ...}) = Execution.is_running_exec exec_id;
wenzelm@52772
   178
fun eval_finished (Eval {eval_process, ...}) = memo_finished eval_process;
wenzelm@52772
   179
wenzelm@52600
   180
fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
wenzelm@52600
   181
val eval_result_state = #state o eval_result;
wenzelm@52600
   182
wenzelm@47336
   183
local
wenzelm@47336
   184
wenzelm@47336
   185
fun run int tr st =
wenzelm@53189
   186
  if Goal.future_enabled 1 andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@53192
   187
    (Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   188
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   189
  else Toplevel.command_errors int tr st;
wenzelm@47336
   190
wenzelm@52510
   191
fun check_cmts span tr st' =
wenzelm@52510
   192
  Toplevel.setmp_thread_position tr
wenzelm@52510
   193
    (fn () =>
wenzelm@52510
   194
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   195
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52619
   196
          handle exn =>
wenzelm@52619
   197
            if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   198
            else ML_Compiler.exn_messages_ids exn)) ();
wenzelm@52510
   199
wenzelm@54678
   200
fun report tr m =
wenzelm@54678
   201
  Toplevel.setmp_thread_position tr (fn () => Output.report (Markup.markup_only m)) ();
wenzelm@54678
   202
wenzelm@54678
   203
fun status tr m =
wenzelm@54678
   204
  Toplevel.setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
wenzelm@54678
   205
wenzelm@47336
   206
fun proof_status tr st =
wenzelm@47336
   207
  (case try Toplevel.proof_of st of
wenzelm@54678
   208
    SOME prf => status tr (Proof.status_markup prf)
wenzelm@47336
   209
  | NONE => ());
wenzelm@47336
   210
wenzelm@52534
   211
fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   212
  if malformed then
wenzelm@52526
   213
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   214
  else
wenzelm@48772
   215
    let
wenzelm@48772
   216
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   217
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   218
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   219
wenzelm@48772
   220
      val _ = Multithreading.interrupted ();
wenzelm@54678
   221
      val _ = status tr Markup.running;
wenzelm@48918
   222
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   223
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   224
      val errs = errs1 @ errs2;
wenzelm@50914
   225
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   226
    in
wenzelm@48772
   227
      (case result of
wenzelm@48772
   228
        NONE =>
wenzelm@48772
   229
          let
wenzelm@54678
   230
            val _ = status tr Markup.failed;
wenzelm@54678
   231
            val _ = status tr Markup.finished;
wenzelm@54678
   232
            val _ = if null errs then (report tr Markup.bad; Exn.interrupt ()) else ();
wenzelm@52526
   233
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   234
      | SOME st' =>
wenzelm@48772
   235
          let
wenzelm@48772
   236
            val _ = proof_status tr st';
wenzelm@54678
   237
            val _ = status tr Markup.finished;
wenzelm@52526
   238
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   239
    end;
wenzelm@47336
   240
wenzelm@52534
   241
in
wenzelm@52534
   242
wenzelm@54526
   243
fun eval init master_dir blobs span eval0 =
wenzelm@52534
   244
  let
wenzelm@52534
   245
    val exec_id = Document_ID.make ();
wenzelm@52534
   246
    fun process () =
wenzelm@52534
   247
      let
wenzelm@52534
   248
        val tr =
wenzelm@52534
   249
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@54526
   250
            (fn () => read init master_dir blobs span |> Toplevel.exec_id exec_id) ();
wenzelm@52534
   251
      in eval_state span tr (eval_result eval0) end;
wenzelm@52600
   252
  in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
wenzelm@52534
   253
wenzelm@47336
   254
end;
wenzelm@47336
   255
wenzelm@52509
   256
wenzelm@52509
   257
(* print *)
wenzelm@52509
   258
wenzelm@52600
   259
datatype print = Print of
wenzelm@52850
   260
 {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
wenzelm@52600
   261
  exec_id: Document_ID.exec, print_process: unit memo};
wenzelm@52600
   262
wenzelm@52526
   263
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   264
wenzelm@52647
   265
type print_function =
wenzelm@52850
   266
  {command_name: string, args: string list} ->
wenzelm@52953
   267
    {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
wenzelm@52647
   268
wenzelm@52511
   269
local
wenzelm@52511
   270
wenzelm@52647
   271
val print_functions =
wenzelm@52647
   272
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   273
wenzelm@52570
   274
fun print_error tr e =
wenzelm@53709
   275
  (Toplevel.setmp_thread_position tr o Toplevel.controlled_execution) e ()
wenzelm@52619
   276
    handle exn =>
wenzelm@52619
   277
      if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   278
      else List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
wenzelm@52516
   279
wenzelm@52607
   280
fun print_eq (Print {exec_id, ...}, Print {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   281
wenzelm@52775
   282
fun print_finished (Print {print_process, ...}) = memo_finished print_process;
wenzelm@52656
   283
wenzelm@52600
   284
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   285
wenzelm@52853
   286
val overlay_ord = prod_ord string_ord (list_ord string_ord);
wenzelm@52853
   287
wenzelm@52511
   288
in
wenzelm@52509
   289
wenzelm@52850
   290
fun print command_visible command_overlays command_name eval old_prints =
wenzelm@52570
   291
  let
wenzelm@52850
   292
    val print_functions = Synchronized.value print_functions;
wenzelm@52850
   293
wenzelm@52999
   294
    fun make_print name args {delay, pri, persistent, strict, print_fn} =
wenzelm@52999
   295
      let
wenzelm@52999
   296
        val exec_id = Document_ID.make ();
wenzelm@52999
   297
        fun process () =
wenzelm@52999
   298
          let
wenzelm@52999
   299
            val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52999
   300
            val tr = Toplevel.exec_id exec_id command;
wenzelm@52999
   301
          in
wenzelm@52999
   302
            if failed andalso strict then ()
wenzelm@52999
   303
            else print_error tr (fn () => print_fn tr st')
wenzelm@52999
   304
          end;
wenzelm@52999
   305
      in
wenzelm@52999
   306
        Print {
wenzelm@52999
   307
          name = name, args = args, delay = delay, pri = pri, persistent = persistent,
wenzelm@52999
   308
          exec_id = exec_id, print_process = memo exec_id process}
wenzelm@52999
   309
      end;
wenzelm@52999
   310
wenzelm@52999
   311
    fun bad_print name args exn =
wenzelm@52999
   312
      make_print name args {delay = NONE, pri = 0, persistent = false,
wenzelm@52999
   313
        strict = false, print_fn = fn _ => fn _ => reraise exn};
wenzelm@52999
   314
wenzelm@52850
   315
    fun new_print name args get_pr =
wenzelm@52570
   316
      let
wenzelm@52850
   317
        val params = {command_name = command_name, args = args};
wenzelm@52570
   318
      in
wenzelm@53709
   319
        (case Exn.capture (Toplevel.controlled_execution get_pr) params of
wenzelm@52570
   320
          Exn.Res NONE => NONE
wenzelm@52999
   321
        | Exn.Res (SOME pr) => SOME (make_print name args pr)
wenzelm@52999
   322
        | Exn.Exn exn => SOME (bad_print name args exn))
wenzelm@52570
   323
      end;
wenzelm@52570
   324
wenzelm@52850
   325
    fun get_print (a, b) =
wenzelm@52850
   326
      (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of
wenzelm@52850
   327
        NONE =>
wenzelm@52850
   328
          (case AList.lookup (op =) print_functions a of
wenzelm@52999
   329
            NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a)))
wenzelm@52850
   330
          | SOME get_pr => new_print a b get_pr)
wenzelm@52850
   331
      | some => some);
wenzelm@52850
   332
wenzelm@52570
   333
    val new_prints =
wenzelm@52570
   334
      if command_visible then
wenzelm@52853
   335
        fold (fn (a, _) => cons (a, [])) print_functions command_overlays
wenzelm@52853
   336
        |> sort_distinct overlay_ord
wenzelm@52850
   337
        |> map_filter get_print
wenzelm@52656
   338
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   339
  in
wenzelm@52600
   340
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   341
  end;
wenzelm@52511
   342
wenzelm@52647
   343
fun print_function name f =
wenzelm@52511
   344
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   345
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   346
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   347
    AList.update (op =) (name, f) funs));
wenzelm@52511
   348
wenzelm@52571
   349
fun no_print_function name =
wenzelm@52571
   350
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   351
wenzelm@52511
   352
end;
wenzelm@52511
   353
wenzelm@52526
   354
val _ =
wenzelm@52647
   355
  print_function "print_state"
wenzelm@52850
   356
    (fn {command_name, ...} =>
wenzelm@53404
   357
      SOME {delay = NONE, pri = 1, persistent = false, strict = true,
wenzelm@52651
   358
        print_fn = fn tr => fn st' =>
wenzelm@52651
   359
          let
wenzelm@52651
   360
            val is_init = Keyword.is_theory_begin command_name;
wenzelm@52651
   361
            val is_proof = Keyword.is_proof command_name;
wenzelm@52651
   362
            val do_print =
wenzelm@52651
   363
              not is_init andalso
wenzelm@52651
   364
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52651
   365
          in if do_print then Toplevel.print_state false st' else () end});
wenzelm@52509
   366
wenzelm@52532
   367
wenzelm@52600
   368
(* combined execution *)
wenzelm@52600
   369
wenzelm@52600
   370
type exec = eval * print list;
wenzelm@52600
   371
val no_exec: exec =
wenzelm@52600
   372
  (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
wenzelm@52532
   373
wenzelm@52600
   374
fun exec_ids NONE = []
wenzelm@52600
   375
  | exec_ids (SOME (Eval {exec_id, ...}, prints)) =
wenzelm@52600
   376
      exec_id :: map (fn Print {exec_id, ...} => exec_id) prints;
wenzelm@52600
   377
wenzelm@52600
   378
local
wenzelm@52600
   379
wenzelm@52651
   380
fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
wenzelm@53976
   381
  if Multithreading.enabled () orelse pri <= 0 then
wenzelm@52651
   382
    let
wenzelm@52651
   383
      val group = Future.worker_subgroup ();
wenzelm@52651
   384
      fun fork () =
wenzelm@52651
   385
        memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@52651
   386
          execution_id print_process;
wenzelm@52651
   387
    in
wenzelm@52762
   388
      (case delay of
wenzelm@52762
   389
        NONE => fork ()
wenzelm@52762
   390
      | SOME d => ignore (Event_Timer.request (Time.+ (Time.now (), d)) fork))
wenzelm@52651
   391
    end
wenzelm@52651
   392
  else memo_exec execution_id print_process;
wenzelm@52559
   393
wenzelm@52600
   394
in
wenzelm@52600
   395
wenzelm@52607
   396
fun exec execution_id (Eval {eval_process, ...}, prints) =
wenzelm@52607
   397
  (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
wenzelm@52532
   398
wenzelm@47336
   399
end;
wenzelm@47336
   400
wenzelm@52600
   401
end;
wenzelm@52600
   402