src/Pure/PIDE/command.ML
author wenzelm
Sat Apr 02 23:29:05 2016 +0200 (2016-04-02 ago)
changeset 62826 eb94e570c1a4
parent 62505 9e2a65912111
child 62895 54c2abe7e9a4
permissions -rw-r--r--
prefer infix operations;
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@58928
    11
  val read_thy: Toplevel.state -> theory
wenzelm@58934
    12
  val read: Keyword.keywords -> theory -> Path.T-> (unit -> theory) ->
wenzelm@59689
    13
    blob list * int -> Token.T list -> Toplevel.transition
wenzelm@52600
    14
  type eval
wenzelm@52607
    15
  val eval_eq: eval * eval -> bool
wenzelm@52784
    16
  val eval_running: eval -> bool
wenzelm@52772
    17
  val eval_finished: eval -> bool
wenzelm@52536
    18
  val eval_result_state: eval -> Toplevel.state
wenzelm@58934
    19
  val eval: Keyword.keywords -> Path.T -> (unit -> theory) ->
wenzelm@59689
    20
    blob list * int -> Token.T list -> eval -> eval
wenzelm@52600
    21
  type print
wenzelm@58923
    22
  val print: bool -> (string * string list) list -> Keyword.keywords -> string ->
wenzelm@52850
    23
    eval -> print list -> print list option
wenzelm@52526
    24
  type print_fn = Toplevel.transition -> Toplevel.state -> unit
wenzelm@52953
    25
  type print_function =
wenzelm@58923
    26
    {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
wenzelm@52953
    27
      {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option
wenzelm@52953
    28
  val print_function: string -> print_function -> unit
wenzelm@52571
    29
  val no_print_function: string -> unit
wenzelm@52600
    30
  type exec = eval * print list
wenzelm@52600
    31
  val no_exec: exec
wenzelm@52600
    32
  val exec_ids: exec option -> Document_ID.exec list
wenzelm@52606
    33
  val exec: Document_ID.execution -> exec -> unit
wenzelm@47336
    34
end;
wenzelm@47336
    35
wenzelm@47336
    36
structure Command: COMMAND =
wenzelm@47336
    37
struct
wenzelm@47336
    38
wenzelm@52536
    39
(** main phases of execution **)
wenzelm@52536
    40
wenzelm@59466
    41
fun task_context group f =
wenzelm@59466
    42
  f
wenzelm@59466
    43
  |> Future.interruptible_task
wenzelm@59466
    44
  |> Future.task_context "Command.run_process" group;
wenzelm@59466
    45
wenzelm@59466
    46
wenzelm@52510
    47
(* read *)
wenzelm@52509
    48
wenzelm@55798
    49
type blob =
wenzelm@56458
    50
  (string * (SHA1.digest * string list) option) Exn.result;  (*file node name, digest, lines*)
wenzelm@54519
    51
wenzelm@56504
    52
fun read_file_node file_node master_dir pos src_path =
wenzelm@54526
    53
  let
wenzelm@59944
    54
    val _ = Position.report pos Markup.language_path;
wenzelm@56504
    55
    val _ =
wenzelm@56504
    56
      (case try Url.explode file_node of
wenzelm@56504
    57
        NONE => ()
wenzelm@56504
    58
      | SOME (Url.File _) => ()
wenzelm@56504
    59
      | _ =>
wenzelm@56504
    60
          error ("Prover cannot load remote file " ^
wenzelm@59685
    61
            Markup.markup (Markup.path file_node) (quote file_node)));
wenzelm@54526
    62
    val full_path = File.check_file (File.full_path master_dir src_path);
wenzelm@55788
    63
    val text = File.read full_path;
wenzelm@55788
    64
    val lines = split_lines text;
wenzelm@55788
    65
    val digest = SHA1.digest text;
wenzelm@59685
    66
  in {src_path = src_path, lines = lines, digest = digest, pos = Path.position full_path} end
wenzelm@59685
    67
  handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@55788
    68
wenzelm@56504
    69
val read_file = read_file_node "";
wenzelm@56504
    70
wenzelm@55788
    71
local
wenzelm@55788
    72
wenzelm@56447
    73
fun blob_file src_path lines digest file_node =
wenzelm@55788
    74
  let
wenzelm@55788
    75
    val file_pos =
wenzelm@56447
    76
      Position.file file_node |>
wenzelm@55788
    77
      (case Position.get_id (Position.thread_data ()) of
wenzelm@55788
    78
        NONE => I
wenzelm@55788
    79
      | SOME exec_id => Position.put_id exec_id);
wenzelm@55788
    80
  in {src_path = src_path, lines = lines, digest = digest, pos = file_pos} end
wenzelm@54526
    81
wenzelm@59689
    82
fun resolve_files keywords master_dir (blobs, blobs_index) toks =
wenzelm@57905
    83
  (case Outer_Syntax.parse_spans toks of
wenzelm@59689
    84
    [Command_Span.Span (Command_Span.Command_Span (cmd, _), _)] =>
wenzelm@59689
    85
      (case try (nth toks) blobs_index of
wenzelm@59689
    86
        SOME tok =>
wenzelm@59689
    87
          let
wenzelm@59689
    88
            val pos = Token.pos_of tok;
wenzelm@59689
    89
            val path = Path.explode (Token.content_of tok)
wenzelm@59689
    90
              handle ERROR msg => error (msg ^ Position.here pos);
wenzelm@59689
    91
            fun make_file src_path (Exn.Res (file_node, NONE)) =
wenzelm@59689
    92
                  Exn.interruptible_capture (fn () =>
wenzelm@59689
    93
                    read_file_node file_node master_dir pos src_path) ()
wenzelm@59689
    94
              | make_file src_path (Exn.Res (file_node, SOME (digest, lines))) =
wenzelm@60034
    95
                  (Position.report pos Markup.language_path;
wenzelm@60034
    96
                    Exn.Res (blob_file src_path lines digest file_node))
wenzelm@59689
    97
              | make_file _ (Exn.Exn e) = Exn.Exn e;
wenzelm@59689
    98
            val src_paths = Keyword.command_files keywords cmd path;
wenzelm@59689
    99
            val files =
wenzelm@59689
   100
              if null blobs then
wenzelm@59689
   101
                map2 make_file src_paths (map (K (Exn.Res ("", NONE))) src_paths)
wenzelm@59689
   102
              else if length src_paths = length blobs then
wenzelm@59689
   103
                map2 make_file src_paths blobs
wenzelm@59689
   104
              else error ("Misalignment of inlined files" ^ Position.here pos);
wenzelm@59689
   105
          in
wenzelm@59689
   106
            toks |> map_index (fn (i, tok) =>
wenzelm@59689
   107
              if i = blobs_index then Token.put_files files tok else tok)
wenzelm@59689
   108
          end
wenzelm@59689
   109
      | NONE => toks)
wenzelm@54519
   110
  | _ => toks);
wenzelm@54519
   111
wenzelm@61379
   112
fun reports_of_token keywords tok =
wenzelm@61379
   113
  let
wenzelm@61379
   114
    val malformed_symbols =
wenzelm@61379
   115
      Input.source_explode (Token.input_of tok)
wenzelm@61379
   116
      |> map_filter (fn (sym, pos) =>
wenzelm@61379
   117
          if Symbol.is_malformed sym
wenzelm@61379
   118
          then SOME ((pos, Markup.bad), "Malformed symbolic character") else NONE);
wenzelm@61379
   119
    val is_malformed = Token.is_error tok orelse not (null malformed_symbols);
wenzelm@61379
   120
    val reports = Token.reports keywords tok @ Token.completion_report tok @ malformed_symbols;
wenzelm@61379
   121
  in (is_malformed, reports) end;
wenzelm@61379
   122
wenzelm@55788
   123
in
wenzelm@55788
   124
wenzelm@60097
   125
fun read_thy st = Toplevel.theory_of st
wenzelm@60097
   126
  handle Toplevel.UNDEF => Pure_Syn.bootstrap_thy;
wenzelm@58928
   127
wenzelm@59689
   128
fun read keywords thy master_dir init blobs_info span =
wenzelm@52510
   129
  let
wenzelm@58928
   130
    val command_reports = Outer_Syntax.command_reports thy;
wenzelm@52509
   131
wenzelm@55709
   132
    val proper_range = Token.range_of (#1 (take_suffix Token.is_improper span));
wenzelm@52510
   133
    val pos =
wenzelm@52510
   134
      (case find_first Token.is_command span of
wenzelm@55708
   135
        SOME tok => Token.pos_of tok
wenzelm@55709
   136
      | NONE => #1 proper_range);
wenzelm@52509
   137
wenzelm@61379
   138
    val token_reports = map (reports_of_token keywords) span;
wenzelm@61379
   139
    val _ = Position.reports_text (maps #2 token_reports @ maps command_reports span);
wenzelm@52510
   140
  in
wenzelm@61379
   141
    if exists #1 token_reports then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   142
    else
wenzelm@59689
   143
      (case Outer_Syntax.parse_tokens thy (resolve_files keywords master_dir blobs_info span) of
wenzelm@58853
   144
        [tr] => Toplevel.modify_init init tr
wenzelm@55709
   145
      | [] => Toplevel.ignored (#1 (Token.range_of span))
wenzelm@55709
   146
      | _ => Toplevel.malformed (#1 proper_range) "Exactly one command expected")
wenzelm@55709
   147
      handle ERROR msg => Toplevel.malformed (#1 proper_range) msg
wenzelm@52510
   148
  end;
wenzelm@52509
   149
wenzelm@55788
   150
end;
wenzelm@55788
   151
wenzelm@52509
   152
wenzelm@52509
   153
(* eval *)
wenzelm@47336
   154
wenzelm@59472
   155
type eval_state = {failed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@59472
   156
val init_eval_state = {failed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52600
   157
wenzelm@59193
   158
datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state lazy};
wenzelm@52600
   159
wenzelm@56291
   160
fun eval_exec_id (Eval {exec_id, ...}) = exec_id;
wenzelm@59058
   161
val eval_eq = op = o apply2 eval_exec_id;
wenzelm@52607
   162
wenzelm@56291
   163
val eval_running = Execution.is_running_exec o eval_exec_id;
wenzelm@59193
   164
fun eval_finished (Eval {eval_process, ...}) = Lazy.is_finished eval_process;
wenzelm@52772
   165
wenzelm@59466
   166
fun eval_result (Eval {eval_process, ...}) =
wenzelm@59466
   167
  task_context (Future.worker_subgroup ()) Lazy.force eval_process;
wenzelm@59466
   168
wenzelm@52600
   169
val eval_result_state = #state o eval_result;
wenzelm@52600
   170
wenzelm@47336
   171
local
wenzelm@47336
   172
wenzelm@58923
   173
fun reset_state keywords tr st0 = Toplevel.setmp_thread_position tr (fn () =>
wenzelm@56937
   174
  let
wenzelm@56937
   175
    val name = Toplevel.name_of tr;
wenzelm@56937
   176
    val res =
wenzelm@58923
   177
      if Keyword.is_theory_body keywords name then Toplevel.reset_theory st0
wenzelm@58923
   178
      else if Keyword.is_proof keywords name then Toplevel.reset_proof st0
wenzelm@56937
   179
      else NONE;
wenzelm@56937
   180
  in
wenzelm@56937
   181
    (case res of
wenzelm@56937
   182
      NONE => st0
wenzelm@60076
   183
    | SOME st => (Output.error_message (Toplevel.type_error tr ^ " -- using reset state"); st))
wenzelm@56937
   184
  end) ();
wenzelm@56937
   185
wenzelm@58923
   186
fun run keywords int tr st =
wenzelm@58923
   187
  if Goal.future_enabled 1 andalso Keyword.is_diag keywords (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@61457
   196
        (Thy_Output.output_text st' {markdown = false} (Token.input_of cmt); [])
wenzelm@52619
   197
          handle exn =>
wenzelm@62505
   198
            if Exn.is_interrupt exn then Exn.reraise exn
wenzelm@56303
   199
            else Runtime.exn_messages_ids exn)) ();
wenzelm@52510
   200
wenzelm@54678
   201
fun report tr m =
wenzelm@56333
   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@59472
   212
fun eval_state keywords span tr ({state, ...}: eval_state) =
wenzelm@59472
   213
  let
wenzelm@59472
   214
    val _ = Multithreading.interrupted ();
wenzelm@47336
   215
wenzelm@59472
   216
    val st = reset_state keywords tr state;
wenzelm@56937
   217
wenzelm@59472
   218
    val _ = status tr Markup.running;
wenzelm@59472
   219
    val (errs1, result) = run keywords true tr st;
wenzelm@59472
   220
    val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@59472
   221
    val errs = errs1 @ errs2;
wenzelm@59472
   222
    val _ = List.app (Future.error_message (Toplevel.pos_of tr)) errs;
wenzelm@59472
   223
  in
wenzelm@59472
   224
    (case result of
wenzelm@59472
   225
      NONE =>
wenzelm@59472
   226
        let
wenzelm@59472
   227
          val _ = status tr Markup.failed;
wenzelm@59472
   228
          val _ = status tr Markup.finished;
wenzelm@59472
   229
          val _ = if null errs then (report tr Markup.bad; Exn.interrupt ()) else ();
wenzelm@59472
   230
        in {failed = true, command = tr, state = st} end
wenzelm@59472
   231
    | SOME st' =>
wenzelm@59472
   232
        let
wenzelm@59472
   233
          val _ = proof_status tr st';
wenzelm@59472
   234
          val _ = status tr Markup.finished;
wenzelm@59472
   235
        in {failed = false, command = tr, state = st'} end)
wenzelm@59472
   236
  end;
wenzelm@47336
   237
wenzelm@52534
   238
in
wenzelm@52534
   239
wenzelm@59689
   240
fun eval keywords master_dir init blobs_info span eval0 =
wenzelm@52534
   241
  let
wenzelm@52534
   242
    val exec_id = Document_ID.make ();
wenzelm@52534
   243
    fun process () =
wenzelm@52534
   244
      let
wenzelm@58928
   245
        val eval_state0 = eval_result eval0;
wenzelm@58928
   246
        val thy = read_thy (#state eval_state0);
wenzelm@52534
   247
        val tr =
wenzelm@52534
   248
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@59689
   249
            (fn () =>
wenzelm@59689
   250
              read keywords thy master_dir init blobs_info span |> Toplevel.exec_id exec_id) ();
wenzelm@58934
   251
      in eval_state keywords span tr eval_state0 end;
wenzelm@59193
   252
  in Eval {exec_id = exec_id, eval_process = Lazy.lazy 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@59193
   261
  exec_id: Document_ID.exec, print_process: unit lazy};
wenzelm@52600
   262
wenzelm@56291
   263
fun print_exec_id (Print {exec_id, ...}) = exec_id;
wenzelm@59058
   264
val print_eq = op = o apply2 print_exec_id;
wenzelm@56291
   265
wenzelm@52526
   266
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   267
wenzelm@52647
   268
type print_function =
wenzelm@58923
   269
  {keywords: Keyword.keywords, command_name: string, args: string list, exec_id: Document_ID.exec} ->
wenzelm@52953
   270
    {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
wenzelm@52647
   271
wenzelm@52511
   272
local
wenzelm@52511
   273
wenzelm@52647
   274
val print_functions =
wenzelm@52647
   275
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   276
wenzelm@56265
   277
fun print_error tr opt_context e =
wenzelm@56303
   278
  (Toplevel.setmp_thread_position tr o Runtime.controlled_execution opt_context) e ()
wenzelm@52619
   279
    handle exn =>
wenzelm@62505
   280
      if Exn.is_interrupt exn then Exn.reraise exn
wenzelm@56333
   281
      else List.app (Future.error_message (Toplevel.pos_of tr)) (Runtime.exn_messages_ids exn);
wenzelm@52516
   282
wenzelm@59193
   283
fun print_finished (Print {print_process, ...}) = Lazy.is_finished print_process;
wenzelm@52656
   284
wenzelm@52600
   285
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   286
wenzelm@52853
   287
val overlay_ord = prod_ord string_ord (list_ord string_ord);
wenzelm@52853
   288
wenzelm@52511
   289
in
wenzelm@52509
   290
wenzelm@58923
   291
fun print command_visible command_overlays keywords command_name eval old_prints =
wenzelm@52570
   292
  let
wenzelm@52850
   293
    val print_functions = Synchronized.value print_functions;
wenzelm@52850
   294
wenzelm@52999
   295
    fun make_print name args {delay, pri, persistent, strict, print_fn} =
wenzelm@52999
   296
      let
wenzelm@52999
   297
        val exec_id = Document_ID.make ();
wenzelm@52999
   298
        fun process () =
wenzelm@52999
   299
          let
wenzelm@52999
   300
            val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52999
   301
            val tr = Toplevel.exec_id exec_id command;
wenzelm@56265
   302
            val opt_context = try Toplevel.generic_theory_of st';
wenzelm@52999
   303
          in
wenzelm@52999
   304
            if failed andalso strict then ()
wenzelm@56265
   305
            else print_error tr opt_context (fn () => print_fn tr st')
wenzelm@52999
   306
          end;
wenzelm@52999
   307
      in
wenzelm@52999
   308
        Print {
wenzelm@52999
   309
          name = name, args = args, delay = delay, pri = pri, persistent = persistent,
wenzelm@59193
   310
          exec_id = exec_id, print_process = Lazy.lazy process}
wenzelm@52999
   311
      end;
wenzelm@52999
   312
wenzelm@52999
   313
    fun bad_print name args exn =
wenzelm@52999
   314
      make_print name args {delay = NONE, pri = 0, persistent = false,
wenzelm@62505
   315
        strict = false, print_fn = fn _ => fn _ => Exn.reraise exn};
wenzelm@52999
   316
wenzelm@52850
   317
    fun new_print name args get_pr =
wenzelm@52570
   318
      let
wenzelm@58923
   319
        val params =
wenzelm@58923
   320
         {keywords = keywords,
wenzelm@58923
   321
          command_name = command_name,
wenzelm@58923
   322
          args = args,
wenzelm@58923
   323
          exec_id = eval_exec_id eval};
wenzelm@52570
   324
      in
wenzelm@56303
   325
        (case Exn.capture (Runtime.controlled_execution NONE get_pr) params of
wenzelm@52570
   326
          Exn.Res NONE => NONE
wenzelm@52999
   327
        | Exn.Res (SOME pr) => SOME (make_print name args pr)
wenzelm@52999
   328
        | Exn.Exn exn => SOME (bad_print name args exn))
wenzelm@52570
   329
      end;
wenzelm@52570
   330
wenzelm@52850
   331
    fun get_print (a, b) =
wenzelm@52850
   332
      (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of
wenzelm@52850
   333
        NONE =>
wenzelm@52850
   334
          (case AList.lookup (op =) print_functions a of
wenzelm@52999
   335
            NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a)))
wenzelm@52850
   336
          | SOME get_pr => new_print a b get_pr)
wenzelm@52850
   337
      | some => some);
wenzelm@52850
   338
wenzelm@52570
   339
    val new_prints =
wenzelm@52570
   340
      if command_visible then
wenzelm@52853
   341
        fold (fn (a, _) => cons (a, [])) print_functions command_overlays
wenzelm@52853
   342
        |> sort_distinct overlay_ord
wenzelm@52850
   343
        |> map_filter get_print
wenzelm@52656
   344
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   345
  in
wenzelm@52600
   346
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   347
  end;
wenzelm@52511
   348
wenzelm@52647
   349
fun print_function name f =
wenzelm@52511
   350
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   351
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   352
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   353
    AList.update (op =) (name, f) funs));
wenzelm@52511
   354
wenzelm@52571
   355
fun no_print_function name =
wenzelm@52571
   356
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   357
wenzelm@52511
   358
end;
wenzelm@52511
   359
wenzelm@52526
   360
val _ =
wenzelm@56291
   361
  print_function "Execution.print"
wenzelm@56291
   362
    (fn {args, exec_id, ...} =>
wenzelm@56291
   363
      if null args then
wenzelm@60610
   364
        SOME {delay = NONE, pri = Task_Queue.urgent_pri + 2, persistent = false, strict = false,
wenzelm@56292
   365
          print_fn = fn _ => fn _ => Execution.fork_prints exec_id}
wenzelm@56291
   366
      else NONE);
wenzelm@56291
   367
wenzelm@56291
   368
val _ =
wenzelm@52647
   369
  print_function "print_state"
wenzelm@58923
   370
    (fn {keywords, command_name, ...} =>
wenzelm@61213
   371
      if Options.default_bool "editor_output_state" andalso Keyword.is_printed keywords command_name
wenzelm@61213
   372
      then
wenzelm@60610
   373
        SOME {delay = NONE, pri = Task_Queue.urgent_pri + 1, persistent = false, strict = false,
wenzelm@61208
   374
          print_fn = fn _ => fn st =>
wenzelm@61208
   375
            if Toplevel.is_proof st then Output.state (Toplevel.string_of_state st)
wenzelm@61208
   376
            else ()}
wenzelm@56895
   377
      else NONE);
wenzelm@52509
   378
wenzelm@52532
   379
wenzelm@52600
   380
(* combined execution *)
wenzelm@52600
   381
wenzelm@52600
   382
type exec = eval * print list;
wenzelm@52600
   383
val no_exec: exec =
wenzelm@59193
   384
  (Eval {exec_id = Document_ID.none, eval_process = Lazy.value init_eval_state}, []);
wenzelm@52532
   385
wenzelm@52600
   386
fun exec_ids NONE = []
wenzelm@56291
   387
  | exec_ids (SOME (eval, prints)) = eval_exec_id eval :: map print_exec_id prints;
wenzelm@52600
   388
wenzelm@52600
   389
local
wenzelm@52600
   390
wenzelm@59193
   391
fun run_process execution_id exec_id process =
wenzelm@59193
   392
  let val group = Future.worker_subgroup () in
wenzelm@59193
   393
    if Execution.running execution_id exec_id [group] then
wenzelm@59466
   394
      ignore (task_context group Lazy.force_result process)
wenzelm@59193
   395
    else ()
wenzelm@59193
   396
  end;
wenzelm@59193
   397
wenzelm@59328
   398
fun ignore_process process =
wenzelm@59328
   399
  Lazy.is_running process orelse Lazy.is_finished process;
wenzelm@59328
   400
wenzelm@59193
   401
fun run_eval execution_id (Eval {exec_id, eval_process}) =
wenzelm@59193
   402
  if Lazy.is_finished eval_process then ()
wenzelm@59193
   403
  else run_process execution_id exec_id eval_process;
wenzelm@59193
   404
wenzelm@59193
   405
fun run_print execution_id (Print {name, delay, pri, exec_id, print_process, ...}) =
wenzelm@59328
   406
  if ignore_process print_process then ()
wenzelm@59193
   407
  else if pri <= 0 orelse (Multithreading.enabled () andalso Options.default_bool "parallel_print")
wenzelm@56875
   408
  then
wenzelm@52651
   409
    let
wenzelm@52651
   410
      val group = Future.worker_subgroup ();
wenzelm@52651
   411
      fun fork () =
wenzelm@59193
   412
        ignore ((singleton o Future.forks)
wenzelm@59193
   413
          {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@59328
   414
          (fn () =>
wenzelm@59328
   415
            if ignore_process print_process then ()
wenzelm@59328
   416
            else run_process execution_id exec_id print_process));
wenzelm@52651
   417
    in
wenzelm@52762
   418
      (case delay of
wenzelm@52762
   419
        NONE => fork ()
wenzelm@62826
   420
      | SOME d => ignore (Event_Timer.request (Time.now () + d) fork))
wenzelm@52651
   421
    end
wenzelm@59193
   422
  else run_process execution_id exec_id print_process;
wenzelm@52559
   423
wenzelm@52600
   424
in
wenzelm@52600
   425
wenzelm@59193
   426
fun exec execution_id (eval, prints) =
wenzelm@59193
   427
  (run_eval execution_id eval; List.app (run_print execution_id) prints);
wenzelm@52532
   428
wenzelm@47336
   429
end;
wenzelm@47336
   430
wenzelm@52600
   431
end;
wenzelm@52600
   432