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