src/Pure/PIDE/protocol.ML
author wenzelm
Mon Aug 05 15:29:10 2013 +0200 (2013-08-05 ago)
changeset 52862 930ce8eacb87
parent 52849 199e9fa5a5c2
child 52931 ac6648c0c0fb
permissions -rw-r--r--
tuned signature -- more uniform treatment of overlays as command mapping;
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@52786
    11
  Isabelle_Process.protocol_command "Isabelle_Process.echo"
wenzelm@52786
    12
    (fn args => List.app writeln args);
wenzelm@52786
    13
wenzelm@52786
    14
val _ =
wenzelm@52786
    15
  Isabelle_Process.protocol_command "Isabelle_Process.options"
wenzelm@52786
    16
    (fn [options_yxml] =>
wenzelm@52786
    17
      let val options = Options.decode (YXML.parse_body options_yxml) in
wenzelm@52786
    18
        Options.set_default options;
wenzelm@52786
    19
        Future.ML_statistics := true;
wenzelm@52786
    20
        Multithreading.trace := Options.int options "threads_trace";
wenzelm@52786
    21
        Multithreading.max_threads := Options.int options "threads";
wenzelm@52786
    22
        Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)
wenzelm@52786
    23
      end);
wenzelm@52585
    24
wenzelm@52585
    25
val _ =
wenzelm@46119
    26
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@44644
    27
    (fn [id, name, text] =>
wenzelm@52530
    28
      Document.change_state (Document.define_command (Document_ID.parse id) name text));
wenzelm@38412
    29
wenzelm@38418
    30
val _ =
wenzelm@47343
    31
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    32
    (fn [] => Execution.discontinue ());
wenzelm@47343
    33
wenzelm@47343
    34
val _ =
wenzelm@46119
    35
  Isabelle_Process.protocol_command "Document.update"
wenzelm@44157
    36
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    37
      let
wenzelm@52606
    38
        val _ = Execution.discontinue ();
wenzelm@47404
    39
wenzelm@52530
    40
        val old_id = Document_ID.parse old_id_string;
wenzelm@52530
    41
        val new_id = Document_ID.parse new_id_string;
wenzelm@44157
    42
        val edits =
wenzelm@44157
    43
          YXML.parse_body edits_yxml |>
wenzelm@44157
    44
            let open XML.Decode in
wenzelm@44157
    45
              list (pair string
wenzelm@44157
    46
                (variant
wenzelm@48755
    47
                 [fn ([], []) => Document.Clear,  (* FIXME unused !? *)
wenzelm@44157
    48
                  fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    49
                  fn ([], a) =>
wenzelm@46938
    50
                    let
wenzelm@51294
    51
                      val (master, (name, (imports, (keywords, errors)))) =
wenzelm@48707
    52
                        pair string (pair string (pair (list string)
wenzelm@48864
    53
                          (pair (list (pair string
wenzelm@48864
    54
                            (option (pair (pair string (list string)) (list string)))))
wenzelm@51294
    55
                            (list string)))) a;
wenzelm@48927
    56
                      val imports' = map (rpair Position.none) imports;
wenzelm@51294
    57
                      val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@51294
    58
                    in Document.Deps (master, header, errors) end,
wenzelm@52849
    59
                  fn (a :: b, c) =>
wenzelm@52849
    60
                    Document.Perspective (bool_atom a, map int_atom b,
wenzelm@52862
    61
                      list (pair int (pair string (list string))) c)]))
wenzelm@44157
    62
            end;
wenzelm@38412
    63
wenzelm@52655
    64
        val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@52655
    65
        val _ = List.app Execution.terminate removed;
wenzelm@52765
    66
        val _ = Goal.purge_futures removed;
wenzelm@52655
    67
        val _ = List.app Isabelle_Process.reset_tracing removed;
wenzelm@52601
    68
wenzelm@52601
    69
        val _ =
wenzelm@52601
    70
          Output.protocol_message Markup.assign_update
wenzelm@52601
    71
            ((new_id, assign_update) |>
wenzelm@52601
    72
              let open XML.Encode
wenzelm@52601
    73
              in pair int (list (pair int (list int))) end
wenzelm@52601
    74
              |> YXML.string_of_body);
wenzelm@52774
    75
      in Document.start_execution state' end));
wenzelm@38412
    76
wenzelm@43748
    77
val _ =
wenzelm@46119
    78
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
    79
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
    80
      let
wenzelm@44673
    81
        val versions =
wenzelm@44673
    82
          YXML.parse_body versions_yxml |>
wenzelm@44673
    83
            let open XML.Decode in list int end;
wenzelm@44676
    84
        val state1 = Document.remove_versions versions state;
wenzelm@50201
    85
        val _ = Output.protocol_message Markup.removed_versions versions_yxml;
wenzelm@44676
    86
      in state1 end));
wenzelm@44673
    87
wenzelm@44673
    88
val _ =
wenzelm@50498
    89
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
    90
    (fn [serial, result] =>
wenzelm@50500
    91
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
    92
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
    93
wenzelm@38412
    94
end;
wenzelm@38412
    95