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