src/Pure/PIDE/protocol.ML
author wenzelm
Thu Apr 05 14:14:51 2012 +0200 (2012-04-05 ago)
changeset 47343 b8aeab386414
parent 46938 cda018294515
child 47346 cd3ab7625519
permissions -rw-r--r--
less aggressive discontinue_execution before document update, to avoid unstable execs that need to be re-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 "Document.define_command"
    12     (fn [id, name, text] =>
    13       Document.change_state (Document.define_command (Document.parse_id id) name text));
    14 
    15 val _ =
    16   Isabelle_Process.protocol_command "Document.discontinue_execution"
    17     (fn [] => Document.discontinue_execution (Document.state ()));
    18 
    19 val _ =
    20   Isabelle_Process.protocol_command "Document.cancel_execution"
    21     (fn [] => ignore (Document.cancel_execution (Document.state ())));
    22 
    23 val _ =
    24   Isabelle_Process.protocol_command "Document.update_perspective"
    25     (fn [old_id_string, new_id_string, name, ids_yxml] => Document.change_state (fn state =>
    26       let
    27         val old_id = Document.parse_id old_id_string;
    28         val new_id = Document.parse_id new_id_string;
    29         val ids = YXML.parse_body ids_yxml
    30           |> let open XML.Decode in list int end;
    31 
    32         val _ = Future.join_tasks (Document.cancel_execution state);
    33       in
    34         state
    35         |> Document.update_perspective old_id new_id name ids
    36         |> Document.execute new_id
    37       end));
    38 
    39 val _ =
    40   Isabelle_Process.protocol_command "Document.update"
    41     (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    42       let
    43         val old_id = Document.parse_id old_id_string;
    44         val new_id = Document.parse_id new_id_string;
    45         val edits =
    46           YXML.parse_body edits_yxml |>
    47             let open XML.Decode in
    48               list (pair string
    49                 (variant
    50                  [fn ([], []) => Document.Clear,
    51                   fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    52                   fn ([], a) =>
    53                     let
    54                       val ((((master, name), imports), keywords), uses) =
    55                         pair (pair (pair (pair string string) (list string))
    56                           (list (pair string (option (pair string (list string))))))
    57                           (list (pair string bool)) a;
    58                       val res =
    59                         Exn.capture (fn () =>
    60                           (master, Thy_Header.make name imports keywords
    61                             (map (apfst Path.explode) uses))) ();
    62                     in Document.Header res end,
    63                   fn ([a], []) => Document.Header (Exn.Exn (ERROR a)),
    64                   fn (a, []) => Document.Perspective (map int_atom a)]))
    65             end;
    66 
    67         val running = Document.cancel_execution state;
    68         val (assignment, state1) = Document.update old_id new_id edits state;
    69         val _ = Future.join_tasks running;
    70         val _ =
    71           Output.protocol_message Isabelle_Markup.assign_execs
    72             ((new_id, assignment) |>
    73               let open XML.Encode
    74               in pair int (pair (list (pair int (option int))) (list (pair string (option int)))) end
    75               |> YXML.string_of_body);
    76         val state2 = Document.execute new_id state1;
    77       in state2 end));
    78 
    79 val _ =
    80   Isabelle_Process.protocol_command "Document.remove_versions"
    81     (fn [versions_yxml] => Document.change_state (fn state =>
    82       let
    83         val versions =
    84           YXML.parse_body versions_yxml |>
    85             let open XML.Decode in list int end;
    86         val state1 = Document.remove_versions versions state;
    87         val _ = Output.protocol_message Isabelle_Markup.removed_versions versions_yxml;
    88       in state1 end));
    89 
    90 val _ =
    91   Isabelle_Process.protocol_command "Document.invoke_scala"
    92     (fn [id, tag, res] => Invoke_Scala.fulfill_method id tag res);
    93 
    94 end;
    95