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