src/Pure/PIDE/protocol.ML
author wenzelm
Thu Oct 18 19:58:30 2012 +0200 (2012-10-18)
changeset 49931 85780e6f8fd2
parent 49647 21ae8500d261
child 50201 c26369c9eda6
permissions -rw-r--r--
more basic Goal.reset_futures as snapshot of implicit state;
more robust Session.finish_futures: do not cancel here, to allow later Future.map of persistent futures (notably proof terms);
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@47404
    21
    (fn [] => Document.cancel_execution (Document.state ()));
wenzelm@44612
    22
wenzelm@44612
    23
val _ =
wenzelm@46119
    24
  Isabelle_Process.protocol_command "Document.update"
wenzelm@44157
    25
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    26
      let
wenzelm@47404
    27
        val _ = Document.cancel_execution state;
wenzelm@47404
    28
wenzelm@38418
    29
        val old_id = Document.parse_id old_id_string;
wenzelm@38418
    30
        val new_id = Document.parse_id new_id_string;
wenzelm@44157
    31
        val edits =
wenzelm@44157
    32
          YXML.parse_body edits_yxml |>
wenzelm@44157
    33
            let open XML.Decode in
wenzelm@44157
    34
              list (pair string
wenzelm@44157
    35
                (variant
wenzelm@48755
    36
                 [fn ([], []) => Document.Clear,  (* FIXME unused !? *)
wenzelm@44157
    37
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    38
                  fn ([], a) =>
wenzelm@46938
    39
                    let
wenzelm@48707
    40
                      val (master, (name, (imports, (keywords, (uses, errors))))) =
wenzelm@48707
    41
                        pair string (pair string (pair (list string)
wenzelm@48864
    42
                          (pair (list (pair string
wenzelm@48864
    43
                            (option (pair (pair string (list string)) (list string)))))
wenzelm@48707
    44
                            (pair (list (pair string bool)) (list string))))) a;
wenzelm@48927
    45
                      val imports' = map (rpair Position.none) imports;
wenzelm@48707
    46
                      val (uses', errors') =
wenzelm@48707
    47
                        (map (apfst Path.explode) uses, errors)
wenzelm@48707
    48
                          handle ERROR msg => ([], errors @ [msg]);
wenzelm@48927
    49
                      val header = Thy_Header.make (name, Position.none) imports' keywords uses';
wenzelm@48707
    50
                    in Document.Deps (master, header, errors') end,
wenzelm@44384
    51
                  fn (a, []) => Document.Perspective (map int_atom a)]))
wenzelm@44157
    52
            end;
wenzelm@38412
    53
wenzelm@47420
    54
        val (assignment, state') = Document.update old_id new_id edits state;
wenzelm@44436
    55
        val _ =
wenzelm@46774
    56
          Output.protocol_message Isabelle_Markup.assign_execs
wenzelm@44661
    57
            ((new_id, assignment) |>
wenzelm@44476
    58
              let open XML.Encode
wenzelm@47388
    59
              in pair int (list (pair int (option int))) end
wenzelm@44661
    60
              |> YXML.string_of_body);
wenzelm@47420
    61
wenzelm@49931
    62
        val _ = List.app Future.cancel_group (Goal.reset_futures ());
wenzelm@49647
    63
        val _ = Isabelle_Process.reset_tracing_limits ();
wenzelm@47420
    64
        val _ = Document.start_execution state';
wenzelm@47420
    65
      in state' end));
wenzelm@38412
    66
wenzelm@43748
    67
val _ =
wenzelm@46119
    68
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
    69
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
    70
      let
wenzelm@44673
    71
        val versions =
wenzelm@44673
    72
          YXML.parse_body versions_yxml |>
wenzelm@44673
    73
            let open XML.Decode in list int end;
wenzelm@44676
    74
        val state1 = Document.remove_versions versions state;
wenzelm@46774
    75
        val _ = Output.protocol_message Isabelle_Markup.removed_versions versions_yxml;
wenzelm@44676
    76
      in state1 end));
wenzelm@44673
    77
wenzelm@44673
    78
val _ =
wenzelm@46119
    79
  Isabelle_Process.protocol_command "Document.invoke_scala"
wenzelm@49470
    80
    (fn [id, tag, res] =>
wenzelm@49470
    81
      Invoke_Scala.fulfill_method id tag res
wenzelm@49470
    82
        handle exn => if Exn.is_interrupt exn then () else reraise exn);
wenzelm@43748
    83
wenzelm@38412
    84
end;
wenzelm@38412
    85