src/Pure/PIDE/protocol.ML
author wenzelm
Wed Jan 28 22:19:22 2015 +0100 (2015-01-28 ago)
changeset 59463 b91dc7ab3464
parent 59370 b13ff987c559
child 59671 9715eb8e9408
permissions -rw-r--r--
more robust protocol command: purge removed execs asynchronously, to remain reactive despite problems to cancel "Command.run_process" in a situation of overrunning non-terminating tasks (see also 59f1591a11cb);
wenzelm@45709
     1
(*  Title:      Pure/PIDE/protocol.ML
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*)
wenzelm@38412
     6
wenzelm@45709
     7
structure Protocol: sig end =
wenzelm@38412
     8
struct
wenzelm@38412
     9
wenzelm@38418
    10
val _ =
wenzelm@56387
    11
  Isabelle_Process.protocol_command "Prover.echo"
wenzelm@52786
    12
    (fn args => List.app writeln args);
wenzelm@52786
    13
wenzelm@52786
    14
val _ =
wenzelm@56387
    15
  Isabelle_Process.protocol_command "Prover.options"
wenzelm@52786
    16
    (fn [options_yxml] =>
wenzelm@52786
    17
      let val options = Options.decode (YXML.parse_body options_yxml) in
wenzelm@52786
    18
        Options.set_default options;
wenzelm@52786
    19
        Future.ML_statistics := true;
wenzelm@52786
    20
        Multithreading.trace := Options.int options "threads_trace";
wenzelm@54717
    21
        Multithreading.max_threads_update (Options.int options "threads");
wenzelm@52786
    22
        Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)
wenzelm@52786
    23
      end);
wenzelm@52585
    24
wenzelm@52585
    25
val _ =
wenzelm@54519
    26
  Isabelle_Process.protocol_command "Document.define_blob"
wenzelm@54519
    27
    (fn [digest, content] => Document.change_state (Document.define_blob digest content));
wenzelm@54519
    28
wenzelm@54519
    29
val _ =
wenzelm@46119
    30
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@59085
    31
    (fn id :: name :: blobs_yxml :: toks_yxml :: sources =>
wenzelm@54519
    32
      let
wenzelm@54519
    33
        val blobs =
wenzelm@54519
    34
          YXML.parse_body blobs_yxml |>
wenzelm@54519
    35
            let open XML.Decode in
wenzelm@54519
    36
              list (variant
wenzelm@56458
    37
               [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@54526
    38
                fn ([], a) => Exn.Exn (ERROR (string a))])
wenzelm@54519
    39
            end;
wenzelm@59085
    40
        val toks =
wenzelm@59085
    41
          (YXML.parse_body toks_yxml |> let open XML.Decode in list (pair int int) end) ~~ sources;
wenzelm@54519
    42
      in
wenzelm@59085
    43
        Document.change_state (Document.define_command (Document_ID.parse id) name blobs toks)
wenzelm@54519
    44
      end);
wenzelm@38412
    45
wenzelm@38418
    46
val _ =
wenzelm@47343
    47
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    48
    (fn [] => Execution.discontinue ());
wenzelm@47343
    49
wenzelm@47343
    50
val _ =
wenzelm@52931
    51
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    52
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    53
wenzelm@52931
    54
val _ =
wenzelm@46119
    55
  Isabelle_Process.protocol_command "Document.update"
wenzelm@59370
    56
    (Future.task_context "Document.update" (Future.new_group NONE)
wenzelm@59370
    57
      (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@59370
    58
        let
wenzelm@59370
    59
          val _ = Execution.discontinue ();
wenzelm@47404
    60
wenzelm@59370
    61
          val old_id = Document_ID.parse old_id_string;
wenzelm@59370
    62
          val new_id = Document_ID.parse new_id_string;
wenzelm@59370
    63
          val edits =
wenzelm@59370
    64
            YXML.parse_body edits_yxml |>
wenzelm@59370
    65
              let open XML.Decode in
wenzelm@59370
    66
                list (pair string
wenzelm@59370
    67
                  (variant
wenzelm@59370
    68
                   [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@59370
    69
                    fn ([], a) =>
wenzelm@59370
    70
                      let
wenzelm@59370
    71
                        val (master, (name, (imports, (keywords, errors)))) =
wenzelm@59370
    72
                          pair string (pair string (pair (list string)
wenzelm@59370
    73
                            (pair (list (pair string
wenzelm@59370
    74
                              (option (pair (pair string (list string)) (list string)))))
wenzelm@59370
    75
                              (list string)))) a;
wenzelm@59370
    76
                        val imports' = map (rpair Position.none) imports;
wenzelm@59370
    77
                        val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@59370
    78
                      in Document.Deps (master, header, errors) end,
wenzelm@59370
    79
                    fn (a :: b, c) =>
wenzelm@59370
    80
                      Document.Perspective (bool_atom a, map int_atom b,
wenzelm@59370
    81
                        list (pair int (pair string (list string))) c)]))
wenzelm@59370
    82
              end;
wenzelm@38412
    83
wenzelm@59370
    84
          val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@59463
    85
          val _ =
wenzelm@59463
    86
            (singleton o Future.forks)
wenzelm@59463
    87
             {name = "Document.update/remove", group = NONE,
wenzelm@59463
    88
              deps = maps Future.group_snapshot (maps Execution.peek removed),
wenzelm@59463
    89
              pri = 1, interrupts = false}
wenzelm@59463
    90
             (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
wenzelm@52601
    91
wenzelm@59370
    92
          val _ =
wenzelm@59370
    93
            Output.protocol_message Markup.assign_update
wenzelm@59370
    94
              [(new_id, assign_update) |>
wenzelm@59370
    95
                let open XML.Encode
wenzelm@59370
    96
                in pair int (list (pair int (list int))) end
wenzelm@59370
    97
                |> YXML.string_of_body];
wenzelm@59370
    98
        in Document.start_execution state' end)));
wenzelm@38412
    99
wenzelm@43748
   100
val _ =
wenzelm@46119
   101
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   102
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   103
      let
wenzelm@44673
   104
        val versions =
wenzelm@44673
   105
          YXML.parse_body versions_yxml |>
wenzelm@44673
   106
            let open XML.Decode in list int end;
wenzelm@44676
   107
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   108
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   109
      in state1 end));
wenzelm@44673
   110
wenzelm@44673
   111
val _ =
wenzelm@50498
   112
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   113
    (fn [serial, result] =>
wenzelm@50500
   114
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
   115
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
   116
wenzelm@56616
   117
val _ =
wenzelm@57868
   118
  Isabelle_Process.protocol_command "ML_System.share_common_data"
wenzelm@57868
   119
    (fn [] => ML_System.share_common_data ());
wenzelm@57868
   120
wenzelm@38412
   121
end;
wenzelm@38412
   122