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