src/Pure/PIDE/protocol.ML
author wenzelm
Tue Jan 23 19:25:39 2018 +0100 (21 months ago ago)
changeset 67493 c4e9e0c50487
parent 67472 bddfa23a4ea9
child 68337 09ac56914b29
permissions -rw-r--r--
treat sessions as entities with defining position;
tuned signature;
     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.init_session_base"
    22     (fn [sessions_yxml, doc_names_yxml, global_theories_yxml, loaded_theories_yxml,
    23           known_theories_yxml] =>
    24       let
    25         val decode_table = YXML.parse_body #> let open XML.Decode in list (pair string string) end;
    26         val decode_list = YXML.parse_body #> let open XML.Decode in list string end;
    27         val decode_sessions =
    28           YXML.parse_body #> let open XML.Decode in list (pair string properties) end;
    29       in
    30         Resources.init_session_base
    31           {sessions = decode_sessions sessions_yxml,
    32            docs = decode_list doc_names_yxml,
    33            global_theories = decode_table global_theories_yxml,
    34            loaded_theories = decode_list loaded_theories_yxml,
    35            known_theories = decode_table known_theories_yxml}
    36       end);
    37 
    38 val _ =
    39   Isabelle_Process.protocol_command "Document.define_blob"
    40     (fn [digest, content] => Document.change_state (Document.define_blob digest content));
    41 
    42 val _ =
    43   Isabelle_Process.protocol_command "Document.define_command"
    44     (fn id :: name :: blobs_yxml :: toks_yxml :: sources =>
    45       let
    46         val (blobs, blobs_index) =
    47           YXML.parse_body blobs_yxml |>
    48             let
    49               val message =
    50                 YXML.string_of_body o Protocol_Message.command_positions id;
    51               open XML.Decode;
    52             in
    53               pair
    54                 (list (variant
    55                  [fn ([], a) => Exn.Res (pair string (option string) a),
    56                   fn ([], a) => Exn.Exn (ERROR (message a))]))
    57                 int
    58             end;
    59         val toks =
    60           (YXML.parse_body toks_yxml |> let open XML.Decode in list (pair int int) end) ~~ sources;
    61       in
    62         Document.change_state
    63           (Document.define_command (Document_ID.parse id) name blobs blobs_index toks)
    64       end);
    65 
    66 val _ =
    67   Isabelle_Process.protocol_command "Document.consolidate_execution"
    68     (fn [] => Document.consolidate_execution (Document.state ()));
    69 
    70 val _ =
    71   Isabelle_Process.protocol_command "Document.discontinue_execution"
    72     (fn [] => Execution.discontinue ());
    73 
    74 val _ =
    75   Isabelle_Process.protocol_command "Document.cancel_exec"
    76     (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
    77 
    78 val _ =
    79   Isabelle_Process.protocol_command "Document.update"
    80     (Future.task_context "Document.update" (Future.new_group NONE)
    81       (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    82         let
    83           val _ = Execution.discontinue ();
    84 
    85           val old_id = Document_ID.parse old_id_string;
    86           val new_id = Document_ID.parse new_id_string;
    87           val edits =
    88             YXML.parse_body edits_yxml |>
    89               let open XML.Decode in
    90                 list (pair string
    91                   (variant
    92                    [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    93                     fn ([], a) =>
    94                       let
    95                         val (master, (name, (imports, (keywords, errors)))) =
    96                           pair string (pair string (pair (list string)
    97                             (pair (list (pair string
    98                               (pair (pair string (list string)) (list string))))
    99                               (list YXML.string_of_body)))) a;
   100                         val imports' = map (rpair Position.none) imports;
   101                         val keywords' = map (fn (x, y) => ((x, Position.none), y)) keywords;
   102                         val header = Thy_Header.make (name, Position.none) imports' keywords';
   103                       in Document.Deps {master = master, header = header, errors = errors} end,
   104                     fn (a :: b, c) =>
   105                       Document.Perspective (bool_atom a, map int_atom b,
   106                         list (pair int (pair string (list string))) c)]))
   107               end;
   108 
   109           val (removed, assign_update, state') = Document.update old_id new_id edits state;
   110           val _ =
   111             (singleton o Future.forks)
   112              {name = "Document.update/remove", group = NONE,
   113               deps = Execution.snapshot removed,
   114               pri = Task_Queue.urgent_pri + 2, interrupts = false}
   115              (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
   116 
   117           val _ =
   118             Output.protocol_message Markup.assign_update
   119               [(new_id, assign_update) |>
   120                 let open XML.Encode
   121                 in pair int (list (pair int (list int))) end
   122                 |> YXML.string_of_body];
   123         in Document.start_execution state' end)));
   124 
   125 val _ =
   126   Isabelle_Process.protocol_command "Document.remove_versions"
   127     (fn [versions_yxml] => Document.change_state (fn state =>
   128       let
   129         val versions =
   130           YXML.parse_body versions_yxml |>
   131             let open XML.Decode in list int end;
   132         val state1 = Document.remove_versions versions state;
   133         val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
   134       in state1 end));
   135 
   136 val _ =
   137   Isabelle_Process.protocol_command "Document.dialog_result"
   138     (fn [serial, result] =>
   139       Active.dialog_result (Value.parse_int serial) result
   140         handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn);
   141 
   142 val _ =
   143   Isabelle_Process.protocol_command "ML_Heap.share_common_data"
   144     (fn [] => ML_Heap.share_common_data ());
   145 
   146 end;
   147