src/Pure/PIDE/protocol.ML
author wenzelm
Thu Mar 15 00:10:45 2012 +0100 (2012-03-15 ago)
changeset 46938 cda018294515
parent 46774 38f113b052b1
child 47343 b8aeab386414
permissions -rw-r--r--
some support for outer syntax keyword declarations within theory header;
more uniform Thy_Header.header as argument for begin_theory etc.;
     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.cancel_execution"
    17     (fn [] => ignore (Document.cancel_execution (Document.state ())));
    18 
    19 val _ =
    20   Isabelle_Process.protocol_command "Document.update_perspective"
    21     (fn [old_id_string, new_id_string, name, ids_yxml] => Document.change_state (fn state =>
    22       let
    23         val old_id = Document.parse_id old_id_string;
    24         val new_id = Document.parse_id new_id_string;
    25         val ids = YXML.parse_body ids_yxml
    26           |> let open XML.Decode in list int end;
    27 
    28         val _ = Future.join_tasks (Document.cancel_execution state);
    29       in
    30         state
    31         |> Document.update_perspective old_id new_id name ids
    32         |> Document.execute new_id
    33       end));
    34 
    35 val _ =
    36   Isabelle_Process.protocol_command "Document.update"
    37     (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    38       let
    39         val old_id = Document.parse_id old_id_string;
    40         val new_id = Document.parse_id new_id_string;
    41         val edits =
    42           YXML.parse_body edits_yxml |>
    43             let open XML.Decode in
    44               list (pair string
    45                 (variant
    46                  [fn ([], []) => Document.Clear,
    47                   fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    48                   fn ([], a) =>
    49                     let
    50                       val ((((master, name), imports), keywords), uses) =
    51                         pair (pair (pair (pair string string) (list string))
    52                           (list (pair string (option (pair string (list string))))))
    53                           (list (pair string bool)) a;
    54                       val res =
    55                         Exn.capture (fn () =>
    56                           (master, Thy_Header.make name imports keywords
    57                             (map (apfst Path.explode) uses))) ();
    58                     in Document.Header res end,
    59                   fn ([a], []) => Document.Header (Exn.Exn (ERROR a)),
    60                   fn (a, []) => Document.Perspective (map int_atom a)]))
    61             end;
    62 
    63         val running = Document.cancel_execution state;
    64         val (assignment, state1) = Document.update old_id new_id edits state;
    65         val _ = Future.join_tasks running;
    66         val _ =
    67           Output.protocol_message Isabelle_Markup.assign_execs
    68             ((new_id, assignment) |>
    69               let open XML.Encode
    70               in pair int (pair (list (pair int (option int))) (list (pair string (option int)))) end
    71               |> YXML.string_of_body);
    72         val state2 = Document.execute new_id state1;
    73       in state2 end));
    74 
    75 val _ =
    76   Isabelle_Process.protocol_command "Document.remove_versions"
    77     (fn [versions_yxml] => Document.change_state (fn state =>
    78       let
    79         val versions =
    80           YXML.parse_body versions_yxml |>
    81             let open XML.Decode in list int end;
    82         val state1 = Document.remove_versions versions state;
    83         val _ = Output.protocol_message Isabelle_Markup.removed_versions versions_yxml;
    84       in state1 end));
    85 
    86 val _ =
    87   Isabelle_Process.protocol_command "Document.invoke_scala"
    88     (fn [id, tag, res] => Invoke_Scala.fulfill_method id tag res);
    89 
    90 end;
    91