src/Pure/PIDE/command.ML
author wenzelm
Wed Mar 26 12:32:51 2014 +0100 (2014-03-26 ago)
changeset 56292 1a91a0da65ab
parent 56291 e79f76a48449
child 56303 4cc3f4db3447
permissions -rw-r--r--
more uniform Execution.fork vs. Execution.print;
asynchronous Execution.fork_prints, entrusted to Execution.fork management (NB: Command.print_finished is only relevant for persistent print functions);
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@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@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@56291
   175
fun eval_exec_id (Eval {exec_id, ...}) = exec_id;
wenzelm@56291
   176
val eval_eq = op = o pairself eval_exec_id;
wenzelm@52607
   177
wenzelm@56291
   178
val eval_running = Execution.is_running_exec o eval_exec_id;
wenzelm@52772
   179
fun eval_finished (Eval {eval_process, ...}) = memo_finished eval_process;
wenzelm@52772
   180
wenzelm@52600
   181
fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
wenzelm@52600
   182
val eval_result_state = #state o eval_result;
wenzelm@52600
   183
wenzelm@47336
   184
local
wenzelm@47336
   185
wenzelm@47336
   186
fun run int tr st =
wenzelm@53189
   187
  if Goal.future_enabled 1 andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@53192
   188
    (Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   189
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   190
  else Toplevel.command_errors int tr st;
wenzelm@47336
   191
wenzelm@52510
   192
fun check_cmts span tr st' =
wenzelm@52510
   193
  Toplevel.setmp_thread_position tr
wenzelm@52510
   194
    (fn () =>
wenzelm@52510
   195
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   196
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52619
   197
          handle exn =>
wenzelm@52619
   198
            if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   199
            else ML_Compiler.exn_messages_ids exn)) ();
wenzelm@52510
   200
wenzelm@54678
   201
fun report tr m =
wenzelm@54678
   202
  Toplevel.setmp_thread_position tr (fn () => Output.report (Markup.markup_only m)) ();
wenzelm@54678
   203
wenzelm@54678
   204
fun status tr m =
wenzelm@54678
   205
  Toplevel.setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
wenzelm@54678
   206
wenzelm@47336
   207
fun proof_status tr st =
wenzelm@47336
   208
  (case try Toplevel.proof_of st of
wenzelm@54678
   209
    SOME prf => status tr (Proof.status_markup prf)
wenzelm@47336
   210
  | NONE => ());
wenzelm@47336
   211
wenzelm@52534
   212
fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   213
  if malformed then
wenzelm@52526
   214
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   215
  else
wenzelm@48772
   216
    let
wenzelm@48772
   217
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   218
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   219
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   220
wenzelm@48772
   221
      val _ = Multithreading.interrupted ();
wenzelm@54678
   222
      val _ = status tr Markup.running;
wenzelm@48918
   223
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   224
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   225
      val errs = errs1 @ errs2;
wenzelm@50914
   226
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   227
    in
wenzelm@48772
   228
      (case result of
wenzelm@48772
   229
        NONE =>
wenzelm@48772
   230
          let
wenzelm@54678
   231
            val _ = status tr Markup.failed;
wenzelm@54678
   232
            val _ = status tr Markup.finished;
wenzelm@54678
   233
            val _ = if null errs then (report tr Markup.bad; Exn.interrupt ()) else ();
wenzelm@52526
   234
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   235
      | SOME st' =>
wenzelm@48772
   236
          let
wenzelm@48772
   237
            val _ = proof_status tr st';
wenzelm@54678
   238
            val _ = status tr Markup.finished;
wenzelm@52526
   239
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   240
    end;
wenzelm@47336
   241
wenzelm@52534
   242
in
wenzelm@52534
   243
wenzelm@54526
   244
fun eval init master_dir blobs span eval0 =
wenzelm@52534
   245
  let
wenzelm@52534
   246
    val exec_id = Document_ID.make ();
wenzelm@52534
   247
    fun process () =
wenzelm@52534
   248
      let
wenzelm@52534
   249
        val tr =
wenzelm@52534
   250
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@54526
   251
            (fn () => read init master_dir blobs span |> Toplevel.exec_id exec_id) ();
wenzelm@52534
   252
      in eval_state span tr (eval_result eval0) end;
wenzelm@52600
   253
  in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
wenzelm@52534
   254
wenzelm@47336
   255
end;
wenzelm@47336
   256
wenzelm@52509
   257
wenzelm@52509
   258
(* print *)
wenzelm@52509
   259
wenzelm@52600
   260
datatype print = Print of
wenzelm@52850
   261
 {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
wenzelm@52600
   262
  exec_id: Document_ID.exec, print_process: unit memo};
wenzelm@52600
   263
wenzelm@56291
   264
fun print_exec_id (Print {exec_id, ...}) = exec_id;
wenzelm@56291
   265
val print_eq = op = o pairself print_exec_id;
wenzelm@56291
   266
wenzelm@52526
   267
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   268
wenzelm@52647
   269
type print_function =
wenzelm@56291
   270
  {command_name: string, args: string list, exec_id: Document_ID.exec} ->
wenzelm@52953
   271
    {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
wenzelm@52647
   272
wenzelm@52511
   273
local
wenzelm@52511
   274
wenzelm@52647
   275
val print_functions =
wenzelm@52647
   276
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   277
wenzelm@56265
   278
fun print_error tr opt_context e =
wenzelm@56265
   279
  (Toplevel.setmp_thread_position tr o Toplevel.controlled_execution opt_context) e ()
wenzelm@52619
   280
    handle exn =>
wenzelm@52619
   281
      if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   282
      else List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
wenzelm@52516
   283
wenzelm@52775
   284
fun print_finished (Print {print_process, ...}) = memo_finished print_process;
wenzelm@52656
   285
wenzelm@52600
   286
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   287
wenzelm@52853
   288
val overlay_ord = prod_ord string_ord (list_ord string_ord);
wenzelm@52853
   289
wenzelm@52511
   290
in
wenzelm@52509
   291
wenzelm@52850
   292
fun print command_visible command_overlays command_name eval old_prints =
wenzelm@52570
   293
  let
wenzelm@52850
   294
    val print_functions = Synchronized.value print_functions;
wenzelm@52850
   295
wenzelm@52999
   296
    fun make_print name args {delay, pri, persistent, strict, print_fn} =
wenzelm@52999
   297
      let
wenzelm@52999
   298
        val exec_id = Document_ID.make ();
wenzelm@52999
   299
        fun process () =
wenzelm@52999
   300
          let
wenzelm@52999
   301
            val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52999
   302
            val tr = Toplevel.exec_id exec_id command;
wenzelm@56265
   303
            val opt_context = try Toplevel.generic_theory_of st';
wenzelm@52999
   304
          in
wenzelm@52999
   305
            if failed andalso strict then ()
wenzelm@56265
   306
            else print_error tr opt_context (fn () => print_fn tr st')
wenzelm@52999
   307
          end;
wenzelm@52999
   308
      in
wenzelm@52999
   309
        Print {
wenzelm@52999
   310
          name = name, args = args, delay = delay, pri = pri, persistent = persistent,
wenzelm@52999
   311
          exec_id = exec_id, print_process = memo exec_id process}
wenzelm@52999
   312
      end;
wenzelm@52999
   313
wenzelm@52999
   314
    fun bad_print name args exn =
wenzelm@52999
   315
      make_print name args {delay = NONE, pri = 0, persistent = false,
wenzelm@52999
   316
        strict = false, print_fn = fn _ => fn _ => reraise exn};
wenzelm@52999
   317
wenzelm@52850
   318
    fun new_print name args get_pr =
wenzelm@52570
   319
      let
wenzelm@56291
   320
        val params = {command_name = command_name, args = args, exec_id = eval_exec_id eval};
wenzelm@52570
   321
      in
wenzelm@56265
   322
        (case Exn.capture (Toplevel.controlled_execution NONE get_pr) params of
wenzelm@52570
   323
          Exn.Res NONE => NONE
wenzelm@52999
   324
        | Exn.Res (SOME pr) => SOME (make_print name args pr)
wenzelm@52999
   325
        | Exn.Exn exn => SOME (bad_print name args exn))
wenzelm@52570
   326
      end;
wenzelm@52570
   327
wenzelm@52850
   328
    fun get_print (a, b) =
wenzelm@52850
   329
      (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of
wenzelm@52850
   330
        NONE =>
wenzelm@52850
   331
          (case AList.lookup (op =) print_functions a of
wenzelm@52999
   332
            NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a)))
wenzelm@52850
   333
          | SOME get_pr => new_print a b get_pr)
wenzelm@52850
   334
      | some => some);
wenzelm@52850
   335
wenzelm@52570
   336
    val new_prints =
wenzelm@52570
   337
      if command_visible then
wenzelm@52853
   338
        fold (fn (a, _) => cons (a, [])) print_functions command_overlays
wenzelm@52853
   339
        |> sort_distinct overlay_ord
wenzelm@52850
   340
        |> map_filter get_print
wenzelm@52656
   341
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   342
  in
wenzelm@52600
   343
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   344
  end;
wenzelm@52511
   345
wenzelm@52647
   346
fun print_function name f =
wenzelm@52511
   347
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   348
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   349
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   350
    AList.update (op =) (name, f) funs));
wenzelm@52511
   351
wenzelm@52571
   352
fun no_print_function name =
wenzelm@52571
   353
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   354
wenzelm@52511
   355
end;
wenzelm@52511
   356
wenzelm@52526
   357
val _ =
wenzelm@56291
   358
  print_function "Execution.print"
wenzelm@56291
   359
    (fn {args, exec_id, ...} =>
wenzelm@56291
   360
      if null args then
wenzelm@56291
   361
        SOME {delay = NONE, pri = 1, persistent = false, strict = true,
wenzelm@56292
   362
          print_fn = fn _ => fn _ => Execution.fork_prints exec_id}
wenzelm@56291
   363
      else NONE);
wenzelm@56291
   364
wenzelm@56291
   365
val _ =
wenzelm@52647
   366
  print_function "print_state"
wenzelm@52850
   367
    (fn {command_name, ...} =>
wenzelm@53404
   368
      SOME {delay = NONE, pri = 1, persistent = false, strict = true,
wenzelm@52651
   369
        print_fn = fn tr => fn st' =>
wenzelm@52651
   370
          let
wenzelm@52651
   371
            val is_init = Keyword.is_theory_begin command_name;
wenzelm@52651
   372
            val is_proof = Keyword.is_proof command_name;
wenzelm@52651
   373
            val do_print =
wenzelm@52651
   374
              not is_init andalso
wenzelm@52651
   375
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52651
   376
          in if do_print then Toplevel.print_state false st' else () end});
wenzelm@52509
   377
wenzelm@52532
   378
wenzelm@52600
   379
(* combined execution *)
wenzelm@52600
   380
wenzelm@52600
   381
type exec = eval * print list;
wenzelm@52600
   382
val no_exec: exec =
wenzelm@52600
   383
  (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
wenzelm@52532
   384
wenzelm@52600
   385
fun exec_ids NONE = []
wenzelm@56291
   386
  | exec_ids (SOME (eval, prints)) = eval_exec_id eval :: map print_exec_id prints;
wenzelm@52600
   387
wenzelm@52600
   388
local
wenzelm@52600
   389
wenzelm@52651
   390
fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
wenzelm@53976
   391
  if Multithreading.enabled () orelse pri <= 0 then
wenzelm@52651
   392
    let
wenzelm@52651
   393
      val group = Future.worker_subgroup ();
wenzelm@52651
   394
      fun fork () =
wenzelm@52651
   395
        memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@52651
   396
          execution_id print_process;
wenzelm@52651
   397
    in
wenzelm@52762
   398
      (case delay of
wenzelm@52762
   399
        NONE => fork ()
wenzelm@52762
   400
      | SOME d => ignore (Event_Timer.request (Time.+ (Time.now (), d)) fork))
wenzelm@52651
   401
    end
wenzelm@52651
   402
  else memo_exec execution_id print_process;
wenzelm@52559
   403
wenzelm@52600
   404
in
wenzelm@52600
   405
wenzelm@52607
   406
fun exec execution_id (Eval {eval_process, ...}, prints) =
wenzelm@52607
   407
  (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
wenzelm@52532
   408
wenzelm@47336
   409
end;
wenzelm@47336
   410
wenzelm@52600
   411
end;
wenzelm@52600
   412