src/Pure/PIDE/protocol.ML
author wenzelm
Thu Apr 05 14:14:51 2012 +0200 (2012-04-05)
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;
wenzelm@45709
     1
(*  Title:      Pure/PIDE/protocol.ML
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*)
wenzelm@38412
     6
wenzelm@45709
     7
structure Protocol: sig end =
wenzelm@38412
     8
struct
wenzelm@38412
     9
wenzelm@38418
    10
val _ =
wenzelm@46119
    11
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@44644
    12
    (fn [id, name, text] =>
wenzelm@44644
    13
      Document.change_state (Document.define_command (Document.parse_id id) name text));
wenzelm@38412
    14
wenzelm@38418
    15
val _ =
wenzelm@47343
    16
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@47343
    17
    (fn [] => Document.discontinue_execution (Document.state ()));
wenzelm@47343
    18
wenzelm@47343
    19
val _ =
wenzelm@46119
    20
  Isabelle_Process.protocol_command "Document.cancel_execution"
wenzelm@44612
    21
    (fn [] => ignore (Document.cancel_execution (Document.state ())));
wenzelm@44612
    22
wenzelm@44612
    23
val _ =
wenzelm@46119
    24
  Isabelle_Process.protocol_command "Document.update_perspective"
wenzelm@44436
    25
    (fn [old_id_string, new_id_string, name, ids_yxml] => Document.change_state (fn state =>
wenzelm@44436
    26
      let
wenzelm@44436
    27
        val old_id = Document.parse_id old_id_string;
wenzelm@44436
    28
        val new_id = Document.parse_id new_id_string;
wenzelm@44436
    29
        val ids = YXML.parse_body ids_yxml
wenzelm@44436
    30
          |> let open XML.Decode in list int end;
wenzelm@44436
    31
wenzelm@44436
    32
        val _ = Future.join_tasks (Document.cancel_execution state);
wenzelm@44436
    33
      in
wenzelm@44436
    34
        state
wenzelm@44436
    35
        |> Document.update_perspective old_id new_id name ids
wenzelm@44436
    36
        |> Document.execute new_id
wenzelm@44436
    37
      end));
wenzelm@44436
    38
wenzelm@44436
    39
val _ =
wenzelm@46119
    40
  Isabelle_Process.protocol_command "Document.update"
wenzelm@44157
    41
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    42
      let
wenzelm@38418
    43
        val old_id = Document.parse_id old_id_string;
wenzelm@38418
    44
        val new_id = Document.parse_id new_id_string;
wenzelm@44157
    45
        val edits =
wenzelm@44157
    46
          YXML.parse_body edits_yxml |>
wenzelm@44157
    47
            let open XML.Decode in
wenzelm@44157
    48
              list (pair string
wenzelm@44157
    49
                (variant
wenzelm@44185
    50
                 [fn ([], []) => Document.Clear,
wenzelm@44157
    51
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    52
                  fn ([], a) =>
wenzelm@46938
    53
                    let
wenzelm@46938
    54
                      val ((((master, name), imports), keywords), uses) =
wenzelm@46938
    55
                        pair (pair (pair (pair string string) (list string))
wenzelm@46938
    56
                          (list (pair string (option (pair string (list string))))))
wenzelm@46938
    57
                          (list (pair string bool)) a;
wenzelm@46938
    58
                      val res =
wenzelm@46938
    59
                        Exn.capture (fn () =>
wenzelm@46938
    60
                          (master, Thy_Header.make name imports keywords
wenzelm@46938
    61
                            (map (apfst Path.explode) uses))) ();
wenzelm@46938
    62
                    in Document.Header res end,
wenzelm@44384
    63
                  fn ([a], []) => Document.Header (Exn.Exn (ERROR a)),
wenzelm@44384
    64
                  fn (a, []) => Document.Perspective (map int_atom a)]))
wenzelm@44157
    65
            end;
wenzelm@38412
    66
wenzelm@44436
    67
        val running = Document.cancel_execution state;
wenzelm@44610
    68
        val (assignment, state1) = Document.update old_id new_id edits state;
wenzelm@44436
    69
        val _ = Future.join_tasks running;
wenzelm@44436
    70
        val _ =
wenzelm@46774
    71
          Output.protocol_message Isabelle_Markup.assign_execs
wenzelm@44661
    72
            ((new_id, assignment) |>
wenzelm@44476
    73
              let open XML.Encode
wenzelm@44661
    74
              in pair int (pair (list (pair int (option int))) (list (pair string (option int)))) end
wenzelm@44661
    75
              |> YXML.string_of_body);
wenzelm@44660
    76
        val state2 = Document.execute new_id state1;
wenzelm@44660
    77
      in state2 end));
wenzelm@38412
    78
wenzelm@43748
    79
val _ =
wenzelm@46119
    80
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
    81
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
    82
      let
wenzelm@44673
    83
        val versions =
wenzelm@44673
    84
          YXML.parse_body versions_yxml |>
wenzelm@44673
    85
            let open XML.Decode in list int end;
wenzelm@44676
    86
        val state1 = Document.remove_versions versions state;
wenzelm@46774
    87
        val _ = Output.protocol_message Isabelle_Markup.removed_versions versions_yxml;
wenzelm@44676
    88
      in state1 end));
wenzelm@44673
    89
wenzelm@44673
    90
val _ =
wenzelm@46119
    91
  Isabelle_Process.protocol_command "Document.invoke_scala"
wenzelm@43748
    92
    (fn [id, tag, res] => Invoke_Scala.fulfill_method id tag res);
wenzelm@43748
    93
wenzelm@38412
    94
end;
wenzelm@38412
    95