src/Pure/PIDE/protocol.ML
author wenzelm
Thu, 18 Oct 2012 19:58:30 +0200
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);

(*  Title:      Pure/PIDE/protocol.ML
    Author:     Makarius

Protocol message formats for interactive proof documents.
*)

structure Protocol: sig end =
struct

val _ =
  Isabelle_Process.protocol_command "Document.define_command"
    (fn [id, name, text] =>
      Document.change_state (Document.define_command (Document.parse_id id) name text));

val _ =
  Isabelle_Process.protocol_command "Document.discontinue_execution"
    (fn [] => Document.discontinue_execution (Document.state ()));

val _ =
  Isabelle_Process.protocol_command "Document.cancel_execution"
    (fn [] => Document.cancel_execution (Document.state ()));

val _ =
  Isabelle_Process.protocol_command "Document.update"
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
      let
        val _ = Document.cancel_execution state;

        val old_id = Document.parse_id old_id_string;
        val new_id = Document.parse_id new_id_string;
        val edits =
          YXML.parse_body edits_yxml |>
            let open XML.Decode in
              list (pair string
                (variant
                 [fn ([], []) => Document.Clear,  (* FIXME unused !? *)
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
                  fn ([], a) =>
                    let
                      val (master, (name, (imports, (keywords, (uses, errors))))) =
                        pair string (pair string (pair (list string)
                          (pair (list (pair string
                            (option (pair (pair string (list string)) (list string)))))
                            (pair (list (pair string bool)) (list string))))) a;
                      val imports' = map (rpair Position.none) imports;
                      val (uses', errors') =
                        (map (apfst Path.explode) uses, errors)
                          handle ERROR msg => ([], errors @ [msg]);
                      val header = Thy_Header.make (name, Position.none) imports' keywords uses';
                    in Document.Deps (master, header, errors') end,
                  fn (a, []) => Document.Perspective (map int_atom a)]))
            end;

        val (assignment, state') = Document.update old_id new_id edits state;
        val _ =
          Output.protocol_message Isabelle_Markup.assign_execs
            ((new_id, assignment) |>
              let open XML.Encode
              in pair int (list (pair int (option int))) end
              |> YXML.string_of_body);

        val _ = List.app Future.cancel_group (Goal.reset_futures ());
        val _ = Isabelle_Process.reset_tracing_limits ();
        val _ = Document.start_execution state';
      in state' end));

val _ =
  Isabelle_Process.protocol_command "Document.remove_versions"
    (fn [versions_yxml] => Document.change_state (fn state =>
      let
        val versions =
          YXML.parse_body versions_yxml |>
            let open XML.Decode in list int end;
        val state1 = Document.remove_versions versions state;
        val _ = Output.protocol_message Isabelle_Markup.removed_versions versions_yxml;
      in state1 end));

val _ =
  Isabelle_Process.protocol_command "Document.invoke_scala"
    (fn [id, tag, res] =>
      Invoke_Scala.fulfill_method id tag res
        handle exn => if Exn.is_interrupt exn then () else reraise exn);

end;