src/Pure/PIDE/protocol.ML
author wenzelm
Mon Mar 16 11:30:54 2015 +0100 (2015-03-16 ago)
changeset 59714 ae322325adbb
parent 59685 c043306d2598
child 59715 4f0d0e4ad68d
permissions -rw-r--r--
tuned protocol -- resolve command positions in ML;
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@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@59370
    84
                        val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@59370
    85
                      in Document.Deps (master, header, errors) end,
wenzelm@59370
    86
                    fn (a :: b, c) =>
wenzelm@59370
    87
                      Document.Perspective (bool_atom a, map int_atom b,
wenzelm@59370
    88
                        list (pair int (pair string (list string))) c)]))
wenzelm@59370
    89
              end;
wenzelm@38412
    90
wenzelm@59370
    91
          val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@59463
    92
          val _ =
wenzelm@59463
    93
            (singleton o Future.forks)
wenzelm@59463
    94
             {name = "Document.update/remove", group = NONE,
wenzelm@59463
    95
              deps = maps Future.group_snapshot (maps Execution.peek removed),
wenzelm@59463
    96
              pri = 1, interrupts = false}
wenzelm@59463
    97
             (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
wenzelm@52601
    98
wenzelm@59370
    99
          val _ =
wenzelm@59370
   100
            Output.protocol_message Markup.assign_update
wenzelm@59370
   101
              [(new_id, assign_update) |>
wenzelm@59370
   102
                let open XML.Encode
wenzelm@59370
   103
                in pair int (list (pair int (list int))) end
wenzelm@59370
   104
                |> YXML.string_of_body];
wenzelm@59370
   105
        in Document.start_execution state' end)));
wenzelm@38412
   106
wenzelm@43748
   107
val _ =
wenzelm@46119
   108
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   109
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   110
      let
wenzelm@44673
   111
        val versions =
wenzelm@44673
   112
          YXML.parse_body versions_yxml |>
wenzelm@44673
   113
            let open XML.Decode in list int end;
wenzelm@44676
   114
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   115
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   116
      in state1 end));
wenzelm@44673
   117
wenzelm@44673
   118
val _ =
wenzelm@50498
   119
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   120
    (fn [serial, result] =>
wenzelm@50500
   121
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
   122
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
   123
wenzelm@56616
   124
val _ =
wenzelm@57868
   125
  Isabelle_Process.protocol_command "ML_System.share_common_data"
wenzelm@57868
   126
    (fn [] => ML_System.share_common_data ());
wenzelm@57868
   127
wenzelm@38412
   128
end;
wenzelm@38412
   129