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