src/Pure/PIDE/protocol.ML
author wenzelm
Wed Dec 03 14:04:38 2014 +0100 (2014-12-03 ago)
changeset 59083 88b0b1f28adc
parent 58928 23d0ffd48006
child 59085 08a6901eb035
permissions -rw-r--r--
tuned signature;
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@56387
    11
  Isabelle_Process.protocol_command "Prover.echo"
wenzelm@52786
    12
    (fn args => List.app writeln args);
wenzelm@52786
    13
wenzelm@52786
    14
val _ =
wenzelm@56387
    15
  Isabelle_Process.protocol_command "Prover.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@54717
    21
        Multithreading.max_threads_update (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@58928
    26
  Isabelle_Process.protocol_command "Document.init_keywords"
wenzelm@58928
    27
    (fn [] => Document.init_keywords ());
wenzelm@58928
    28
wenzelm@58928
    29
val _ =
wenzelm@54519
    30
  Isabelle_Process.protocol_command "Document.define_blob"
wenzelm@54519
    31
    (fn [digest, content] => Document.change_state (Document.define_blob digest content));
wenzelm@54519
    32
wenzelm@54519
    33
val _ =
wenzelm@46119
    34
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@54519
    35
    (fn [id, name, blobs_yxml, text] =>
wenzelm@54519
    36
      let
wenzelm@54519
    37
        val blobs =
wenzelm@54519
    38
          YXML.parse_body blobs_yxml |>
wenzelm@54519
    39
            let open XML.Decode in
wenzelm@54519
    40
              list (variant
wenzelm@56458
    41
               [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@54526
    42
                fn ([], a) => Exn.Exn (ERROR (string a))])
wenzelm@54519
    43
            end;
wenzelm@54519
    44
      in
wenzelm@54519
    45
        Document.change_state (Document.define_command (Document_ID.parse id) name blobs text)
wenzelm@54519
    46
      end);
wenzelm@38412
    47
wenzelm@38418
    48
val _ =
wenzelm@47343
    49
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    50
    (fn [] => Execution.discontinue ());
wenzelm@47343
    51
wenzelm@47343
    52
val _ =
wenzelm@52931
    53
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    54
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    55
wenzelm@52931
    56
val _ =
wenzelm@46119
    57
  Isabelle_Process.protocol_command "Document.update"
wenzelm@44157
    58
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    59
      let
wenzelm@52606
    60
        val _ = Execution.discontinue ();
wenzelm@47404
    61
wenzelm@52530
    62
        val old_id = Document_ID.parse old_id_string;
wenzelm@52530
    63
        val new_id = Document_ID.parse new_id_string;
wenzelm@44157
    64
        val edits =
wenzelm@44157
    65
          YXML.parse_body edits_yxml |>
wenzelm@44157
    66
            let open XML.Decode in
wenzelm@44157
    67
              list (pair string
wenzelm@44157
    68
                (variant
wenzelm@54562
    69
                 [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    70
                  fn ([], a) =>
wenzelm@46938
    71
                    let
wenzelm@51294
    72
                      val (master, (name, (imports, (keywords, errors)))) =
wenzelm@48707
    73
                        pair string (pair string (pair (list string)
wenzelm@48864
    74
                          (pair (list (pair string
wenzelm@48864
    75
                            (option (pair (pair string (list string)) (list string)))))
wenzelm@51294
    76
                            (list string)))) a;
wenzelm@48927
    77
                      val imports' = map (rpair Position.none) imports;
wenzelm@51294
    78
                      val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@51294
    79
                    in Document.Deps (master, header, errors) end,
wenzelm@52849
    80
                  fn (a :: b, c) =>
wenzelm@52849
    81
                    Document.Perspective (bool_atom a, map int_atom b,
wenzelm@52862
    82
                      list (pair int (pair string (list string))) c)]))
wenzelm@44157
    83
            end;
wenzelm@38412
    84
wenzelm@52655
    85
        val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@52655
    86
        val _ = List.app Execution.terminate removed;
wenzelm@53192
    87
        val _ = Execution.purge removed;
wenzelm@52655
    88
        val _ = List.app Isabelle_Process.reset_tracing removed;
wenzelm@52601
    89
wenzelm@52601
    90
        val _ =
wenzelm@52601
    91
          Output.protocol_message Markup.assign_update
wenzelm@56333
    92
            [(new_id, assign_update) |>
wenzelm@52601
    93
              let open XML.Encode
wenzelm@52601
    94
              in pair int (list (pair int (list int))) end
wenzelm@56333
    95
              |> YXML.string_of_body];
wenzelm@52774
    96
      in Document.start_execution state' end));
wenzelm@38412
    97
wenzelm@43748
    98
val _ =
wenzelm@46119
    99
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   100
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   101
      let
wenzelm@44673
   102
        val versions =
wenzelm@44673
   103
          YXML.parse_body versions_yxml |>
wenzelm@44673
   104
            let open XML.Decode in list int end;
wenzelm@44676
   105
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   106
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   107
      in state1 end));
wenzelm@44673
   108
wenzelm@44673
   109
val _ =
wenzelm@50498
   110
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   111
    (fn [serial, result] =>
wenzelm@50500
   112
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
   113
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
   114
wenzelm@56616
   115
val _ =
wenzelm@56616
   116
  Isabelle_Process.protocol_command "use_theories"
wenzelm@56616
   117
    (fn id :: master_dir :: thys =>
wenzelm@56616
   118
      let
wenzelm@56616
   119
        val result =
wenzelm@56616
   120
          Exn.capture (fn () =>
wenzelm@56616
   121
            Thy_Info.use_theories
wenzelm@56616
   122
              {document = false, last_timing = K NONE, master_dir = Path.explode master_dir}
wenzelm@56616
   123
              (map (rpair Position.none) thys)) ();
wenzelm@56616
   124
        val ok =
wenzelm@56616
   125
          (case result of
wenzelm@56616
   126
            Exn.Res _ => true
wenzelm@56616
   127
          | Exn.Exn exn => (Runtime.exn_error_message exn; false));
wenzelm@56616
   128
    in Output.protocol_message (Markup.use_theories_result id ok) [] end);
wenzelm@56616
   129
wenzelm@57868
   130
val _ =
wenzelm@57868
   131
  Isabelle_Process.protocol_command "ML_System.share_common_data"
wenzelm@57868
   132
    (fn [] => ML_System.share_common_data ());
wenzelm@57868
   133
wenzelm@38412
   134
end;
wenzelm@38412
   135