src/Pure/PIDE/protocol.ML
author wenzelm
Mon Jun 29 20:55:46 2015 +0200 (2015-06-29 ago)
changeset 60610 f52b4b0c10c4
parent 60074 38a64cc17403
child 61376 93224745477f
permissions -rw-r--r--
improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
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@60074
    19
        Future.ML_statistics := Options.bool options "ML_statistics";
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@59685
    33
        val (blobs, blobs_index) =
wenzelm@54519
    34
          YXML.parse_body blobs_yxml |>
wenzelm@59714
    35
            let
wenzelm@59714
    36
              val message =
wenzelm@59714
    37
                YXML.string_of_body o Protocol_Message.command_positions id;
wenzelm@59714
    38
              open XML.Decode;
wenzelm@59714
    39
            in
wenzelm@59685
    40
              pair
wenzelm@59685
    41
                (list (variant
wenzelm@59685
    42
                 [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@59714
    43
                  fn ([], a) => Exn.Exn (ERROR (message a))]))
wenzelm@59685
    44
                int
wenzelm@54519
    45
            end;
wenzelm@59085
    46
        val toks =
wenzelm@59085
    47
          (YXML.parse_body toks_yxml |> let open XML.Decode in list (pair int int) end) ~~ sources;
wenzelm@54519
    48
      in
wenzelm@59685
    49
        Document.change_state
wenzelm@59685
    50
          (Document.define_command (Document_ID.parse id) name blobs blobs_index toks)
wenzelm@54519
    51
      end);
wenzelm@38412
    52
wenzelm@38418
    53
val _ =
wenzelm@47343
    54
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    55
    (fn [] => Execution.discontinue ());
wenzelm@47343
    56
wenzelm@47343
    57
val _ =
wenzelm@52931
    58
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    59
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    60
wenzelm@52931
    61
val _ =
wenzelm@46119
    62
  Isabelle_Process.protocol_command "Document.update"
wenzelm@59370
    63
    (Future.task_context "Document.update" (Future.new_group NONE)
wenzelm@59370
    64
      (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@59370
    65
        let
wenzelm@59370
    66
          val _ = Execution.discontinue ();
wenzelm@47404
    67
wenzelm@59370
    68
          val old_id = Document_ID.parse old_id_string;
wenzelm@59370
    69
          val new_id = Document_ID.parse new_id_string;
wenzelm@59370
    70
          val edits =
wenzelm@59370
    71
            YXML.parse_body edits_yxml |>
wenzelm@59370
    72
              let open XML.Decode in
wenzelm@59370
    73
                list (pair string
wenzelm@59370
    74
                  (variant
wenzelm@59370
    75
                   [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@59370
    76
                    fn ([], a) =>
wenzelm@59370
    77
                      let
wenzelm@59370
    78
                        val (master, (name, (imports, (keywords, errors)))) =
wenzelm@59370
    79
                          pair string (pair string (pair (list string)
wenzelm@59370
    80
                            (pair (list (pair string
wenzelm@59370
    81
                              (option (pair (pair string (list string)) (list string)))))
wenzelm@59671
    82
                              (list YXML.string_of_body)))) a;
wenzelm@59370
    83
                        val imports' = map (rpair Position.none) imports;
wenzelm@59934
    84
                        val keywords' = map (fn (x, y) => ((x, Position.none), y)) keywords;
wenzelm@59934
    85
                        val header = Thy_Header.make (name, Position.none) imports' keywords';
wenzelm@59715
    86
                      in Document.Deps {master = master, header = header, errors = errors} end,
wenzelm@59370
    87
                    fn (a :: b, c) =>
wenzelm@59370
    88
                      Document.Perspective (bool_atom a, map int_atom b,
wenzelm@59370
    89
                        list (pair int (pair string (list string))) c)]))
wenzelm@59370
    90
              end;
wenzelm@38412
    91
wenzelm@59370
    92
          val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@59463
    93
          val _ =
wenzelm@59463
    94
            (singleton o Future.forks)
wenzelm@59463
    95
             {name = "Document.update/remove", group = NONE,
wenzelm@59463
    96
              deps = maps Future.group_snapshot (maps Execution.peek removed),
wenzelm@60610
    97
              pri = Task_Queue.urgent_pri + 2, interrupts = false}
wenzelm@59463
    98
             (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
wenzelm@52601
    99
wenzelm@59370
   100
          val _ =
wenzelm@59370
   101
            Output.protocol_message Markup.assign_update
wenzelm@59370
   102
              [(new_id, assign_update) |>
wenzelm@59370
   103
                let open XML.Encode
wenzelm@59370
   104
                in pair int (list (pair int (list int))) end
wenzelm@59370
   105
                |> YXML.string_of_body];
wenzelm@59370
   106
        in Document.start_execution state' end)));
wenzelm@38412
   107
wenzelm@43748
   108
val _ =
wenzelm@46119
   109
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   110
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   111
      let
wenzelm@44673
   112
        val versions =
wenzelm@44673
   113
          YXML.parse_body versions_yxml |>
wenzelm@44673
   114
            let open XML.Decode in list int end;
wenzelm@44676
   115
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   116
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   117
      in state1 end));
wenzelm@44673
   118
wenzelm@44673
   119
val _ =
wenzelm@50498
   120
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   121
    (fn [serial, result] =>
wenzelm@50500
   122
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
   123
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
   124
wenzelm@56616
   125
val _ =
wenzelm@57868
   126
  Isabelle_Process.protocol_command "ML_System.share_common_data"
wenzelm@57868
   127
    (fn [] => ML_System.share_common_data ());
wenzelm@57868
   128
wenzelm@38412
   129
end;
wenzelm@38412
   130