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;
     1 (*  Title:      Pure/PIDE/protocol.ML
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 *)
     6 
     7 structure Protocol: sig end =
     8 struct
     9 
    10 val _ =
    11   Isabelle_Process.protocol_command "Prover.echo"
    12     (fn args => List.app writeln args);
    13 
    14 val _ =
    15   Isabelle_Process.protocol_command "Prover.options"
    16     (fn [options_yxml] =>
    17       (Options.set_default (Options.decode (YXML.parse_body options_yxml));
    18        Isabelle_Process.init_options_interactive ()));
    19 
    20 val _ =
    21   Isabelle_Process.protocol_command "Prover.session_base"
    22     (fn [global_theories_yxml, loaded_theories_yxml, known_theories_yxml] =>
    23       let
    24         val decode_table = YXML.parse_body #> let open XML.Decode in list (pair string string) end;
    25       in
    26         Resources.init_session_base
    27           {global_theories = decode_table global_theories_yxml,
    28            loaded_theories = decode_table loaded_theories_yxml,
    29            known_theories = decode_table known_theories_yxml}
    30       end);
    31 
    32 val _ =
    33   Isabelle_Process.protocol_command "Document.define_blob"
    34     (fn [digest, content] => Document.change_state (Document.define_blob digest content));
    35 
    36 val _ =
    37   Isabelle_Process.protocol_command "Document.define_command"
    38     (fn id :: name :: blobs_yxml :: toks_yxml :: sources =>
    39       let
    40         val (blobs, blobs_index) =
    41           YXML.parse_body blobs_yxml |>
    42             let
    43               val message =
    44                 YXML.string_of_body o Protocol_Message.command_positions id;
    45               open XML.Decode;
    46             in
    47               pair
    48                 (list (variant
    49                  [fn ([], a) => Exn.Res (pair string (option string) a),
    50                   fn ([], a) => Exn.Exn (ERROR (message a))]))
    51                 int
    52             end;
    53         val toks =
    54           (YXML.parse_body toks_yxml |> let open XML.Decode in list (pair int int) end) ~~ sources;
    55       in
    56         Document.change_state
    57           (Document.define_command (Document_ID.parse id) name blobs blobs_index toks)
    58       end);
    59 
    60 val _ =
    61   Isabelle_Process.protocol_command "Document.discontinue_execution"
    62     (fn [] => Execution.discontinue ());
    63 
    64 val _ =
    65   Isabelle_Process.protocol_command "Document.cancel_exec"
    66     (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
    67 
    68 val _ =
    69   Isabelle_Process.protocol_command "Document.update"
    70     (Future.task_context "Document.update" (Future.new_group NONE)
    71       (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    72         let
    73           val _ = Execution.discontinue ();
    74 
    75           val old_id = Document_ID.parse old_id_string;
    76           val new_id = Document_ID.parse new_id_string;
    77           val edits =
    78             YXML.parse_body edits_yxml |>
    79               let open XML.Decode in
    80                 list (pair string
    81                   (variant
    82                    [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    83                     fn ([], a) =>
    84                       let
    85                         val (master, (name, (imports, (keywords, errors)))) =
    86                           pair string (pair string (pair (list string)
    87                             (pair (list (pair string
    88                               (pair (pair string (list string)) (list string))))
    89                               (list YXML.string_of_body)))) a;
    90                         val imports' = map (rpair Position.none) imports;
    91                         val keywords' = map (fn (x, y) => ((x, Position.none), y)) keywords;
    92                         val header = Thy_Header.make (name, Position.none) imports' keywords';
    93                       in Document.Deps {master = master, header = header, errors = errors} end,
    94                     fn (a :: b, c) =>
    95                       Document.Perspective (bool_atom a, map int_atom b,
    96                         list (pair int (pair string (list string))) c)]))
    97               end;
    98 
    99           val (removed, assign_update, state') = Document.update old_id new_id edits state;
   100           val _ =
   101             (singleton o Future.forks)
   102              {name = "Document.update/remove", group = NONE,
   103               deps = maps Future.group_snapshot (maps Execution.peek removed),
   104               pri = Task_Queue.urgent_pri + 2, interrupts = false}
   105              (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
   106 
   107           val _ =
   108             Output.protocol_message Markup.assign_update
   109               [(new_id, assign_update) |>
   110                 let open XML.Encode
   111                 in pair int (list (pair int (list int))) end
   112                 |> YXML.string_of_body];
   113         in Document.start_execution state' end)));
   114 
   115 val _ =
   116   Isabelle_Process.protocol_command "Document.remove_versions"
   117     (fn [versions_yxml] => Document.change_state (fn state =>
   118       let
   119         val versions =
   120           YXML.parse_body versions_yxml |>
   121             let open XML.Decode in list int end;
   122         val state1 = Document.remove_versions versions state;
   123         val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
   124       in state1 end));
   125 
   126 val _ =
   127   Isabelle_Process.protocol_command "Document.dialog_result"
   128     (fn [serial, result] =>
   129       Active.dialog_result (Value.parse_int serial) result
   130         handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn);
   131 
   132 val _ =
   133   Isabelle_Process.protocol_command "ML_Heap.share_common_data"
   134     (fn [] => ML_Heap.share_common_data ());
   135 
   136 end;
   137