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