src/Pure/PIDE/protocol.ML
author wenzelm
Mon Mar 13 21:37:35 2017 +0100 (2017-03-13 ago)
changeset 65214 a2ec0db555c7
parent 63806 c54a53ef1873
child 65300 c262653a3b88
permissions -rw-r--r--
clarified modules;
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@62599
    17
      Isabelle_Process.init_protocol_options (Options.decode (YXML.parse_body options_yxml)));
wenzelm@52585
    18
wenzelm@52585
    19
val _ =
wenzelm@54519
    20
  Isabelle_Process.protocol_command "Document.define_blob"
wenzelm@54519
    21
    (fn [digest, content] => Document.change_state (Document.define_blob digest content));
wenzelm@54519
    22
wenzelm@54519
    23
val _ =
wenzelm@46119
    24
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@59085
    25
    (fn id :: name :: blobs_yxml :: toks_yxml :: sources =>
wenzelm@54519
    26
      let
wenzelm@59685
    27
        val (blobs, blobs_index) =
wenzelm@54519
    28
          YXML.parse_body blobs_yxml |>
wenzelm@59714
    29
            let
wenzelm@59714
    30
              val message =
wenzelm@59714
    31
                YXML.string_of_body o Protocol_Message.command_positions id;
wenzelm@59714
    32
              open XML.Decode;
wenzelm@59714
    33
            in
wenzelm@59685
    34
              pair
wenzelm@59685
    35
                (list (variant
wenzelm@59685
    36
                 [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@59714
    37
                  fn ([], a) => Exn.Exn (ERROR (message a))]))
wenzelm@59685
    38
                int
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@59685
    43
        Document.change_state
wenzelm@59685
    44
          (Document.define_command (Document_ID.parse id) name blobs blobs_index toks)
wenzelm@54519
    45
      end);
wenzelm@38412
    46
wenzelm@38418
    47
val _ =
wenzelm@47343
    48
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    49
    (fn [] => Execution.discontinue ());
wenzelm@47343
    50
wenzelm@47343
    51
val _ =
wenzelm@52931
    52
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    53
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    54
wenzelm@52931
    55
val _ =
wenzelm@46119
    56
  Isabelle_Process.protocol_command "Document.update"
wenzelm@59370
    57
    (Future.task_context "Document.update" (Future.new_group NONE)
wenzelm@59370
    58
      (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@59370
    59
        let
wenzelm@59370
    60
          val _ = Execution.discontinue ();
wenzelm@47404
    61
wenzelm@59370
    62
          val old_id = Document_ID.parse old_id_string;
wenzelm@59370
    63
          val new_id = Document_ID.parse new_id_string;
wenzelm@59370
    64
          val edits =
wenzelm@59370
    65
            YXML.parse_body edits_yxml |>
wenzelm@59370
    66
              let open XML.Decode in
wenzelm@59370
    67
                list (pair string
wenzelm@59370
    68
                  (variant
wenzelm@59370
    69
                   [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@59370
    70
                    fn ([], a) =>
wenzelm@59370
    71
                      let
wenzelm@59370
    72
                        val (master, (name, (imports, (keywords, errors)))) =
wenzelm@59370
    73
                          pair string (pair string (pair (list string)
wenzelm@59370
    74
                            (pair (list (pair string
wenzelm@63429
    75
                              (pair (pair string (list string)) (list string))))
wenzelm@59671
    76
                              (list YXML.string_of_body)))) a;
wenzelm@59370
    77
                        val imports' = map (rpair Position.none) imports;
wenzelm@59934
    78
                        val keywords' = map (fn (x, y) => ((x, Position.none), y)) keywords;
wenzelm@59934
    79
                        val header = Thy_Header.make (name, Position.none) imports' keywords';
wenzelm@59715
    80
                      in Document.Deps {master = master, header = header, errors = errors} end,
wenzelm@59370
    81
                    fn (a :: b, c) =>
wenzelm@59370
    82
                      Document.Perspective (bool_atom a, map int_atom b,
wenzelm@59370
    83
                        list (pair int (pair string (list string))) c)]))
wenzelm@59370
    84
              end;
wenzelm@38412
    85
wenzelm@59370
    86
          val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@59463
    87
          val _ =
wenzelm@59463
    88
            (singleton o Future.forks)
wenzelm@59463
    89
             {name = "Document.update/remove", group = NONE,
wenzelm@59463
    90
              deps = maps Future.group_snapshot (maps Execution.peek removed),
wenzelm@60610
    91
              pri = Task_Queue.urgent_pri + 2, interrupts = false}
wenzelm@59463
    92
             (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
wenzelm@52601
    93
wenzelm@59370
    94
          val _ =
wenzelm@59370
    95
            Output.protocol_message Markup.assign_update
wenzelm@59370
    96
              [(new_id, assign_update) |>
wenzelm@59370
    97
                let open XML.Encode
wenzelm@59370
    98
                in pair int (list (pair int (list int))) end
wenzelm@59370
    99
                |> YXML.string_of_body];
wenzelm@59370
   100
        in Document.start_execution state' end)));
wenzelm@38412
   101
wenzelm@43748
   102
val _ =
wenzelm@46119
   103
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   104
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   105
      let
wenzelm@44673
   106
        val versions =
wenzelm@44673
   107
          YXML.parse_body versions_yxml |>
wenzelm@44673
   108
            let open XML.Decode in list int end;
wenzelm@44676
   109
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   110
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   111
      in state1 end));
wenzelm@44673
   112
wenzelm@44673
   113
val _ =
wenzelm@50498
   114
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   115
    (fn [serial, result] =>
wenzelm@63806
   116
      Active.dialog_result (Value.parse_int serial) result
wenzelm@62505
   117
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn);
wenzelm@50498
   118
wenzelm@56616
   119
val _ =
wenzelm@62467
   120
  Isabelle_Process.protocol_command "ML_Heap.share_common_data"
wenzelm@62467
   121
    (fn [] => ML_Heap.share_common_data ());
wenzelm@57868
   122
wenzelm@38412
   123
end;
wenzelm@38412
   124