src/Pure/PIDE/command.ML
author wenzelm
Wed Nov 20 12:04:06 2013 +0100 (2013-11-20 ago)
changeset 54527 bfeb0ea6c2c0
parent 54526 92961f196d9e
child 54671 d64a4ef26edb
permissions -rw-r--r--
proper static resolution of files via Thy_Load.load_thy, instead of TTY fall-back;
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@54526
     9
  type blob = (string * string option) Exn.result
wenzelm@54526
    10
  val read_file: Path.T -> Position.T -> Path.T -> Token.file
wenzelm@54526
    11
  val read: (unit -> theory) -> Path.T -> blob list -> Token.T list -> Toplevel.transition
wenzelm@52600
    12
  type eval
wenzelm@52607
    13
  val eval_eq: eval * eval -> bool
wenzelm@52784
    14
  val eval_running: eval -> bool
wenzelm@52772
    15
  val eval_finished: eval -> bool
wenzelm@52536
    16
  val eval_result_state: eval -> Toplevel.state
wenzelm@54526
    17
  val eval: (unit -> theory) -> Path.T -> blob list -> Token.T list -> eval -> eval
wenzelm@52600
    18
  type print
wenzelm@52850
    19
  val print: bool -> (string * string list) list -> string ->
wenzelm@52850
    20
    eval -> print list -> print list option
wenzelm@52526
    21
  type print_fn = Toplevel.transition -> Toplevel.state -> unit
wenzelm@52953
    22
  type print_function =
wenzelm@52953
    23
    {command_name: string, args: string list} ->
wenzelm@52953
    24
      {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option
wenzelm@52953
    25
  val print_function: string -> print_function -> unit
wenzelm@52571
    26
  val no_print_function: string -> unit
wenzelm@52600
    27
  type exec = eval * print list
wenzelm@52600
    28
  val no_exec: exec
wenzelm@52600
    29
  val exec_ids: exec option -> Document_ID.exec list
wenzelm@52606
    30
  val exec: Document_ID.execution -> exec -> unit
wenzelm@47336
    31
end;
wenzelm@47336
    32
wenzelm@47336
    33
structure Command: COMMAND =
wenzelm@47336
    34
struct
wenzelm@47336
    35
wenzelm@52775
    36
(** memo results **)
wenzelm@47341
    37
wenzelm@47341
    38
datatype 'a expr =
wenzelm@52596
    39
  Expr of Document_ID.exec * (unit -> 'a) |
wenzelm@47341
    40
  Result of 'a Exn.result;
wenzelm@47341
    41
wenzelm@47341
    42
abstype 'a memo = Memo of 'a expr Synchronized.var
wenzelm@47341
    43
with
wenzelm@47341
    44
wenzelm@52596
    45
fun memo exec_id e = Memo (Synchronized.var "Command.memo" (Expr (exec_id, e)));
wenzelm@47341
    46
fun memo_value a = Memo (Synchronized.var "Command.memo" (Result (Exn.Res a)));
wenzelm@47341
    47
wenzelm@47342
    48
fun memo_result (Memo v) =
wenzelm@47342
    49
  (case Synchronized.value v of
wenzelm@52607
    50
    Expr (exec_id, _) => error ("Unfinished execution result: " ^ Document_ID.print exec_id)
wenzelm@52607
    51
  | Result res => Exn.release res);
wenzelm@47341
    52
wenzelm@52656
    53
fun memo_finished (Memo v) =
wenzelm@52785
    54
  (case Synchronized.value v of Expr _ => false | Result _ => true);
wenzelm@52656
    55
wenzelm@52607
    56
fun memo_exec execution_id (Memo v) =
wenzelm@52774
    57
  Synchronized.timed_access v (K (SOME Time.zeroTime))
wenzelm@52609
    58
    (fn expr =>
wenzelm@52609
    59
      (case expr of
wenzelm@52775
    60
        Expr (exec_id, body) =>
wenzelm@52609
    61
          uninterruptible (fn restore_attributes => fn () =>
wenzelm@53375
    62
            if Execution.running execution_id exec_id [Future.the_worker_group ()] then
wenzelm@52609
    63
              let
wenzelm@52775
    64
                val res =
wenzelm@52775
    65
                  (body
wenzelm@52775
    66
                    |> restore_attributes
wenzelm@52775
    67
                    |> Future.worker_nest "Command.memo_exec"
wenzelm@52775
    68
                    |> Exn.interruptible_capture) ();
wenzelm@52775
    69
              in SOME ((), Result res) end
wenzelm@52775
    70
            else SOME ((), expr)) ()
wenzelm@52775
    71
      | Result _ => SOME ((), expr)))
wenzelm@52775
    72
  |> (fn NONE => error "Conflicting command execution" | _ => ());
wenzelm@52596
    73
wenzelm@52607
    74
fun memo_fork params execution_id (Memo v) =
wenzelm@52596
    75
  (case Synchronized.value v of
wenzelm@52596
    76
    Result _ => ()
wenzelm@52607
    77
  | _ => ignore ((singleton o Future.forks) params (fn () => memo_exec execution_id (Memo v))));
wenzelm@52526
    78
wenzelm@47341
    79
end;
wenzelm@47341
    80
wenzelm@47341
    81
wenzelm@52607
    82
wenzelm@52536
    83
(** main phases of execution **)
wenzelm@52536
    84
wenzelm@52510
    85
(* read *)
wenzelm@52509
    86
wenzelm@54526
    87
type blob = (string * string option) Exn.result;  (*file node name, digest or text*)
wenzelm@54519
    88
wenzelm@54526
    89
fun read_file master_dir pos src_path =
wenzelm@54526
    90
  let
wenzelm@54526
    91
    val full_path = File.check_file (File.full_path master_dir src_path);
wenzelm@54526
    92
    val _ = Position.report pos (Markup.path (Path.implode full_path));
wenzelm@54526
    93
  in {src_path = src_path, text = File.read full_path, pos = Path.position full_path} end;
wenzelm@54526
    94
wenzelm@54526
    95
fun resolve_files master_dir blobs toks =
wenzelm@54519
    96
  (case Thy_Syntax.parse_spans toks of
wenzelm@54519
    97
    [span] => span
wenzelm@54523
    98
      |> Thy_Syntax.resolve_files (fn cmd => fn (path, pos) =>
wenzelm@54523
    99
        let
wenzelm@54526
   100
          fun make_file src_path (Exn.Res (_, NONE)) =
wenzelm@54526
   101
                Exn.interruptible_capture (fn () => read_file master_dir pos src_path) ()
wenzelm@54526
   102
            | make_file src_path (Exn.Res (file, SOME text)) =
wenzelm@54526
   103
                let val _ = Position.report pos (Markup.path file)
wenzelm@54523
   104
                in Exn.Res {src_path = src_path, text = text, pos = Position.file file} end
wenzelm@54523
   105
            | make_file _ (Exn.Exn e) = Exn.Exn e;
wenzelm@54523
   106
wenzelm@54523
   107
          val src_paths = Keyword.command_files cmd path;
wenzelm@54523
   108
        in
wenzelm@54527
   109
          if null blobs then
wenzelm@54527
   110
            map2 make_file src_paths (map (K (Exn.Res ("", NONE))) src_paths)
wenzelm@54527
   111
          else if length src_paths = length blobs then
wenzelm@54527
   112
            map2 make_file src_paths blobs
wenzelm@54527
   113
          else error ("Misalignment of inlined files" ^ Position.here pos)
wenzelm@54523
   114
        end)
wenzelm@54519
   115
      |> Thy_Syntax.span_content
wenzelm@54519
   116
  | _ => toks);
wenzelm@54519
   117
wenzelm@54526
   118
fun read init master_dir blobs span =
wenzelm@52510
   119
  let
wenzelm@52510
   120
    val outer_syntax = #2 (Outer_Syntax.get_syntax ());
wenzelm@52510
   121
    val command_reports = Outer_Syntax.command_reports outer_syntax;
wenzelm@52509
   122
wenzelm@52534
   123
    val proper_range =
wenzelm@52534
   124
      Position.set_range (Token.position_range_of (#1 (take_suffix Token.is_improper span)));
wenzelm@52510
   125
    val pos =
wenzelm@52510
   126
      (case find_first Token.is_command span of
wenzelm@52510
   127
        SOME tok => Token.position_of tok
wenzelm@52510
   128
      | NONE => proper_range);
wenzelm@52509
   129
wenzelm@52510
   130
    val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens span;
wenzelm@52510
   131
    val _ = Position.reports_text (token_reports @ maps command_reports span);
wenzelm@52510
   132
  in
wenzelm@52510
   133
    if is_malformed then Toplevel.malformed pos "Malformed command syntax"
wenzelm@52510
   134
    else
wenzelm@54526
   135
      (case Outer_Syntax.read_spans outer_syntax (resolve_files master_dir blobs span) of
wenzelm@52510
   136
        [tr] =>
wenzelm@52510
   137
          if Keyword.is_control (Toplevel.name_of tr) then
wenzelm@52510
   138
            Toplevel.malformed pos "Illegal control command"
wenzelm@52534
   139
          else Toplevel.modify_init init tr
wenzelm@52534
   140
      | [] => Toplevel.ignored (Position.set_range (Token.position_range_of span))
wenzelm@52510
   141
      | _ => Toplevel.malformed proper_range "Exactly one command expected")
wenzelm@52510
   142
      handle ERROR msg => Toplevel.malformed proper_range msg
wenzelm@52510
   143
  end;
wenzelm@52509
   144
wenzelm@52509
   145
wenzelm@52509
   146
(* eval *)
wenzelm@47336
   147
wenzelm@52600
   148
type eval_state =
wenzelm@52600
   149
  {failed: bool, malformed: bool, command: Toplevel.transition, state: Toplevel.state};
wenzelm@52600
   150
val init_eval_state =
wenzelm@52600
   151
  {failed = false, malformed = false, command = Toplevel.empty, state = Toplevel.toplevel};
wenzelm@52600
   152
wenzelm@52600
   153
datatype eval = Eval of {exec_id: Document_ID.exec, eval_process: eval_state memo};
wenzelm@52600
   154
wenzelm@52607
   155
fun eval_eq (Eval {exec_id, ...}, Eval {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   156
wenzelm@52784
   157
fun eval_running (Eval {exec_id, ...}) = Execution.is_running_exec exec_id;
wenzelm@52772
   158
fun eval_finished (Eval {eval_process, ...}) = memo_finished eval_process;
wenzelm@52772
   159
wenzelm@52600
   160
fun eval_result (Eval {eval_process, ...}) = memo_result eval_process;
wenzelm@52600
   161
val eval_result_state = #state o eval_result;
wenzelm@52600
   162
wenzelm@47336
   163
local
wenzelm@47336
   164
wenzelm@47336
   165
fun run int tr st =
wenzelm@53189
   166
  if Goal.future_enabled 1 andalso Keyword.is_diag (Toplevel.name_of tr) then
wenzelm@53192
   167
    (Execution.fork {name = "Toplevel.diag", pos = Toplevel.pos_of tr, pri = ~1}
wenzelm@51605
   168
      (fn () => Toplevel.command_exception int tr st); ([], SOME st))
wenzelm@51284
   169
  else Toplevel.command_errors int tr st;
wenzelm@47336
   170
wenzelm@52510
   171
fun check_cmts span tr st' =
wenzelm@52510
   172
  Toplevel.setmp_thread_position tr
wenzelm@52510
   173
    (fn () =>
wenzelm@52510
   174
      Outer_Syntax.side_comments span |> maps (fn cmt =>
wenzelm@52510
   175
        (Thy_Output.check_text (Token.source_position_of cmt) st'; [])
wenzelm@52619
   176
          handle exn =>
wenzelm@52619
   177
            if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   178
            else ML_Compiler.exn_messages_ids exn)) ();
wenzelm@52510
   179
wenzelm@47336
   180
fun proof_status tr st =
wenzelm@47336
   181
  (case try Toplevel.proof_of st of
wenzelm@47336
   182
    SOME prf => Toplevel.status tr (Proof.status_markup prf)
wenzelm@47336
   183
  | NONE => ());
wenzelm@47336
   184
wenzelm@52534
   185
fun eval_state span tr ({malformed, state = st, ...}: eval_state) =
wenzelm@52509
   186
  if malformed then
wenzelm@52526
   187
    {failed = true, malformed = malformed, command = tr, state = Toplevel.toplevel}
wenzelm@48772
   188
  else
wenzelm@48772
   189
    let
wenzelm@48772
   190
      val malformed' = Toplevel.is_malformed tr;
wenzelm@48772
   191
      val is_init = Toplevel.is_init tr;
wenzelm@48772
   192
      val is_proof = Keyword.is_proof (Toplevel.name_of tr);
wenzelm@47336
   193
wenzelm@48772
   194
      val _ = Multithreading.interrupted ();
wenzelm@50201
   195
      val _ = Toplevel.status tr Markup.running;
wenzelm@48918
   196
      val (errs1, result) = run (is_init orelse is_proof) (Toplevel.set_print false tr) st;
wenzelm@52509
   197
      val errs2 = (case result of NONE => [] | SOME st' => check_cmts span tr st');
wenzelm@48918
   198
      val errs = errs1 @ errs2;
wenzelm@50201
   199
      val _ = Toplevel.status tr Markup.finished;
wenzelm@50914
   200
      val _ = List.app (Future.error_msg (Toplevel.pos_of tr)) errs;
wenzelm@48772
   201
    in
wenzelm@48772
   202
      (case result of
wenzelm@48772
   203
        NONE =>
wenzelm@48772
   204
          let
wenzelm@48772
   205
            val _ = if null errs then Exn.interrupt () else ();
wenzelm@50201
   206
            val _ = Toplevel.status tr Markup.failed;
wenzelm@52526
   207
          in {failed = true, malformed = malformed', command = tr, state = st} end
wenzelm@48772
   208
      | SOME st' =>
wenzelm@48772
   209
          let
wenzelm@48772
   210
            val _ = proof_status tr st';
wenzelm@52526
   211
          in {failed = false, malformed = malformed', command = tr, state = st'} end)
wenzelm@48772
   212
    end;
wenzelm@47336
   213
wenzelm@52534
   214
in
wenzelm@52534
   215
wenzelm@54526
   216
fun eval init master_dir blobs span eval0 =
wenzelm@52534
   217
  let
wenzelm@52534
   218
    val exec_id = Document_ID.make ();
wenzelm@52534
   219
    fun process () =
wenzelm@52534
   220
      let
wenzelm@52534
   221
        val tr =
wenzelm@52534
   222
          Position.setmp_thread_data (Position.id_only (Document_ID.print exec_id))
wenzelm@54526
   223
            (fn () => read init master_dir blobs span |> Toplevel.exec_id exec_id) ();
wenzelm@52534
   224
      in eval_state span tr (eval_result eval0) end;
wenzelm@52600
   225
  in Eval {exec_id = exec_id, eval_process = memo exec_id process} end;
wenzelm@52534
   226
wenzelm@47336
   227
end;
wenzelm@47336
   228
wenzelm@52509
   229
wenzelm@52509
   230
(* print *)
wenzelm@52509
   231
wenzelm@52600
   232
datatype print = Print of
wenzelm@52850
   233
 {name: string, args: string list, delay: Time.time option, pri: int, persistent: bool,
wenzelm@52600
   234
  exec_id: Document_ID.exec, print_process: unit memo};
wenzelm@52600
   235
wenzelm@52526
   236
type print_fn = Toplevel.transition -> Toplevel.state -> unit;
wenzelm@52515
   237
wenzelm@52647
   238
type print_function =
wenzelm@52850
   239
  {command_name: string, args: string list} ->
wenzelm@52953
   240
    {delay: Time.time option, pri: int, persistent: bool, strict: bool, print_fn: print_fn} option;
wenzelm@52647
   241
wenzelm@52511
   242
local
wenzelm@52511
   243
wenzelm@52647
   244
val print_functions =
wenzelm@52647
   245
  Synchronized.var "Command.print_functions" ([]: (string * print_function) list);
wenzelm@52511
   246
wenzelm@52570
   247
fun print_error tr e =
wenzelm@53709
   248
  (Toplevel.setmp_thread_position tr o Toplevel.controlled_execution) e ()
wenzelm@52619
   249
    handle exn =>
wenzelm@52619
   250
      if Exn.is_interrupt exn then reraise exn
wenzelm@52619
   251
      else List.app (Future.error_msg (Toplevel.pos_of tr)) (ML_Compiler.exn_messages_ids exn);
wenzelm@52516
   252
wenzelm@52607
   253
fun print_eq (Print {exec_id, ...}, Print {exec_id = exec_id', ...}) = exec_id = exec_id';
wenzelm@52607
   254
wenzelm@52775
   255
fun print_finished (Print {print_process, ...}) = memo_finished print_process;
wenzelm@52656
   256
wenzelm@52600
   257
fun print_persistent (Print {persistent, ...}) = persistent;
wenzelm@52596
   258
wenzelm@52853
   259
val overlay_ord = prod_ord string_ord (list_ord string_ord);
wenzelm@52853
   260
wenzelm@52511
   261
in
wenzelm@52509
   262
wenzelm@52850
   263
fun print command_visible command_overlays command_name eval old_prints =
wenzelm@52570
   264
  let
wenzelm@52850
   265
    val print_functions = Synchronized.value print_functions;
wenzelm@52850
   266
wenzelm@52999
   267
    fun make_print name args {delay, pri, persistent, strict, print_fn} =
wenzelm@52999
   268
      let
wenzelm@52999
   269
        val exec_id = Document_ID.make ();
wenzelm@52999
   270
        fun process () =
wenzelm@52999
   271
          let
wenzelm@52999
   272
            val {failed, command, state = st', ...} = eval_result eval;
wenzelm@52999
   273
            val tr = Toplevel.exec_id exec_id command;
wenzelm@52999
   274
          in
wenzelm@52999
   275
            if failed andalso strict then ()
wenzelm@52999
   276
            else print_error tr (fn () => print_fn tr st')
wenzelm@52999
   277
          end;
wenzelm@52999
   278
      in
wenzelm@52999
   279
        Print {
wenzelm@52999
   280
          name = name, args = args, delay = delay, pri = pri, persistent = persistent,
wenzelm@52999
   281
          exec_id = exec_id, print_process = memo exec_id process}
wenzelm@52999
   282
      end;
wenzelm@52999
   283
wenzelm@52999
   284
    fun bad_print name args exn =
wenzelm@52999
   285
      make_print name args {delay = NONE, pri = 0, persistent = false,
wenzelm@52999
   286
        strict = false, print_fn = fn _ => fn _ => reraise exn};
wenzelm@52999
   287
wenzelm@52850
   288
    fun new_print name args get_pr =
wenzelm@52570
   289
      let
wenzelm@52850
   290
        val params = {command_name = command_name, args = args};
wenzelm@52570
   291
      in
wenzelm@53709
   292
        (case Exn.capture (Toplevel.controlled_execution get_pr) params of
wenzelm@52570
   293
          Exn.Res NONE => NONE
wenzelm@52999
   294
        | Exn.Res (SOME pr) => SOME (make_print name args pr)
wenzelm@52999
   295
        | Exn.Exn exn => SOME (bad_print name args exn))
wenzelm@52570
   296
      end;
wenzelm@52570
   297
wenzelm@52850
   298
    fun get_print (a, b) =
wenzelm@52850
   299
      (case find_first (fn Print {name, args, ...} => name = a andalso args = b) old_prints of
wenzelm@52850
   300
        NONE =>
wenzelm@52850
   301
          (case AList.lookup (op =) print_functions a of
wenzelm@52999
   302
            NONE => SOME (bad_print a b (ERROR ("Missing print function " ^ quote a)))
wenzelm@52850
   303
          | SOME get_pr => new_print a b get_pr)
wenzelm@52850
   304
      | some => some);
wenzelm@52850
   305
wenzelm@52570
   306
    val new_prints =
wenzelm@52570
   307
      if command_visible then
wenzelm@52853
   308
        fold (fn (a, _) => cons (a, [])) print_functions command_overlays
wenzelm@52853
   309
        |> sort_distinct overlay_ord
wenzelm@52850
   310
        |> map_filter get_print
wenzelm@52656
   311
      else filter (fn print => print_finished print andalso print_persistent print) old_prints;
wenzelm@52570
   312
  in
wenzelm@52600
   313
    if eq_list print_eq (old_prints, new_prints) then NONE else SOME new_prints
wenzelm@52570
   314
  end;
wenzelm@52511
   315
wenzelm@52647
   316
fun print_function name f =
wenzelm@52511
   317
  Synchronized.change print_functions (fn funs =>
wenzelm@52511
   318
   (if not (AList.defined (op =) funs name) then ()
wenzelm@52511
   319
    else warning ("Redefining command print function: " ^ quote name);
wenzelm@52647
   320
    AList.update (op =) (name, f) funs));
wenzelm@52511
   321
wenzelm@52571
   322
fun no_print_function name =
wenzelm@52571
   323
  Synchronized.change print_functions (filter_out (equal name o #1));
wenzelm@52571
   324
wenzelm@52511
   325
end;
wenzelm@52511
   326
wenzelm@52526
   327
val _ =
wenzelm@52647
   328
  print_function "print_state"
wenzelm@52850
   329
    (fn {command_name, ...} =>
wenzelm@53404
   330
      SOME {delay = NONE, pri = 1, persistent = false, strict = true,
wenzelm@52651
   331
        print_fn = fn tr => fn st' =>
wenzelm@52651
   332
          let
wenzelm@52651
   333
            val is_init = Keyword.is_theory_begin command_name;
wenzelm@52651
   334
            val is_proof = Keyword.is_proof command_name;
wenzelm@52651
   335
            val do_print =
wenzelm@52651
   336
              not is_init andalso
wenzelm@52651
   337
                (Toplevel.print_of tr orelse (is_proof andalso Toplevel.is_proof st'));
wenzelm@52651
   338
          in if do_print then Toplevel.print_state false st' else () end});
wenzelm@52509
   339
wenzelm@52532
   340
wenzelm@52600
   341
(* combined execution *)
wenzelm@52600
   342
wenzelm@52600
   343
type exec = eval * print list;
wenzelm@52600
   344
val no_exec: exec =
wenzelm@52600
   345
  (Eval {exec_id = Document_ID.none, eval_process = memo_value init_eval_state}, []);
wenzelm@52532
   346
wenzelm@52600
   347
fun exec_ids NONE = []
wenzelm@52600
   348
  | exec_ids (SOME (Eval {exec_id, ...}, prints)) =
wenzelm@52600
   349
      exec_id :: map (fn Print {exec_id, ...} => exec_id) prints;
wenzelm@52600
   350
wenzelm@52600
   351
local
wenzelm@52600
   352
wenzelm@52651
   353
fun run_print execution_id (Print {name, delay, pri, print_process, ...}) =
wenzelm@53976
   354
  if Multithreading.enabled () orelse pri <= 0 then
wenzelm@52651
   355
    let
wenzelm@52651
   356
      val group = Future.worker_subgroup ();
wenzelm@52651
   357
      fun fork () =
wenzelm@52651
   358
        memo_fork {name = name, group = SOME group, deps = [], pri = pri, interrupts = true}
wenzelm@52651
   359
          execution_id print_process;
wenzelm@52651
   360
    in
wenzelm@52762
   361
      (case delay of
wenzelm@52762
   362
        NONE => fork ()
wenzelm@52762
   363
      | SOME d => ignore (Event_Timer.request (Time.+ (Time.now (), d)) fork))
wenzelm@52651
   364
    end
wenzelm@52651
   365
  else memo_exec execution_id print_process;
wenzelm@52559
   366
wenzelm@52600
   367
in
wenzelm@52600
   368
wenzelm@52607
   369
fun exec execution_id (Eval {eval_process, ...}, prints) =
wenzelm@52607
   370
  (memo_exec execution_id eval_process; List.app (run_print execution_id) prints);
wenzelm@52532
   371
wenzelm@47336
   372
end;
wenzelm@47336
   373
wenzelm@52600
   374
end;
wenzelm@52600
   375