src/Pure/PIDE/protocol.ML
author wenzelm
Sun May 19 18:10:45 2019 +0200 (5 months ago)
changeset 70284 3e17c3a5fd39
parent 69849 09f200c658ed
child 70663 4a358f8c7cb7
permissions -rw-r--r--
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
     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.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 :: consolidate_yxml :: edits_yxml =>
    78         Document.change_state (fn state =>
    79           let
    80             val old_id = Document_ID.parse old_id_string;
    81             val new_id = Document_ID.parse new_id_string;
    82             val consolidate =
    83               YXML.parse_body consolidate_yxml |>
    84                 let open XML.Decode in list string end;
    85             val edits =
    86               edits_yxml |> map (YXML.parse_body #>
    87                 let open XML.Decode in
    88                   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 _ = Execution.discontinue ();
   108 
   109             val (edited, removed, assign_update, state') =
   110               Document.update old_id new_id edits consolidate state;
   111             val _ =
   112               (singleton o Future.forks)
   113                {name = "Document.update/remove", group = NONE,
   114                 deps = Execution.snapshot removed,
   115                 pri = Task_Queue.urgent_pri + 2, interrupts = false}
   116                (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
   117 
   118             val _ =
   119               Output.protocol_message Markup.assign_update
   120                 ((new_id, edited, assign_update) |>
   121                   let
   122                     open XML.Encode;
   123                     fun encode_upd (a, bs) =
   124                       string (space_implode "," (map Value.print_int (a :: bs)));
   125                   in triple int (list string) (list encode_upd) end
   126                   |> YXML.chunks_of_body);
   127           in Document.start_execution state' end)));
   128 
   129 val _ =
   130   Isabelle_Process.protocol_command "Document.remove_versions"
   131     (fn [versions_yxml] => Document.change_state (fn state =>
   132       let
   133         val versions =
   134           YXML.parse_body versions_yxml |>
   135             let open XML.Decode in list int end;
   136         val state1 = Document.remove_versions versions state;
   137         val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
   138       in state1 end));
   139 
   140 val _ =
   141   Isabelle_Process.protocol_command "Document.dialog_result"
   142     (fn [serial, result] =>
   143       Active.dialog_result (Value.parse_int serial) result
   144         handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn);
   145 
   146 val _ =
   147   Isabelle_Process.protocol_command "ML_Heap.share_common_data"
   148     (fn [] => ML_Heap.share_common_data ());
   149 
   150 end;
   151