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