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