src/Pure/PIDE/protocol.ML
author wenzelm
Thu Apr 03 14:54:17 2014 +0200 (2014-04-03 ago)
changeset 56387 d92eb5c3960d
parent 56333 38f1422ef473
child 56447 1e77ed11f2f7
permissions -rw-r--r--
more general prover operations;
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@54519
    26
  Isabelle_Process.protocol_command "Document.define_blob"
wenzelm@54519
    27
    (fn [digest, content] => Document.change_state (Document.define_blob digest content));
wenzelm@54519
    28
wenzelm@54519
    29
val _ =
wenzelm@46119
    30
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@54519
    31
    (fn [id, name, blobs_yxml, text] =>
wenzelm@54519
    32
      let
wenzelm@54519
    33
        val blobs =
wenzelm@54519
    34
          YXML.parse_body blobs_yxml |>
wenzelm@54519
    35
            let open XML.Decode in
wenzelm@54519
    36
              list (variant
wenzelm@54526
    37
               [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@54526
    38
                fn ([], a) => Exn.Exn (ERROR (string a))])
wenzelm@54519
    39
            end;
wenzelm@54519
    40
      in
wenzelm@54519
    41
        Document.change_state (Document.define_command (Document_ID.parse id) name blobs text)
wenzelm@54519
    42
      end);
wenzelm@38412
    43
wenzelm@38418
    44
val _ =
wenzelm@47343
    45
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    46
    (fn [] => Execution.discontinue ());
wenzelm@47343
    47
wenzelm@47343
    48
val _ =
wenzelm@52931
    49
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    50
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    51
wenzelm@52931
    52
val _ =
wenzelm@46119
    53
  Isabelle_Process.protocol_command "Document.update"
wenzelm@44157
    54
    (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@38418
    55
      let
wenzelm@52606
    56
        val _ = Execution.discontinue ();
wenzelm@47404
    57
wenzelm@52530
    58
        val old_id = Document_ID.parse old_id_string;
wenzelm@52530
    59
        val new_id = Document_ID.parse new_id_string;
wenzelm@44157
    60
        val edits =
wenzelm@44157
    61
          YXML.parse_body edits_yxml |>
wenzelm@44157
    62
            let open XML.Decode in
wenzelm@44157
    63
              list (pair string
wenzelm@44157
    64
                (variant
wenzelm@54562
    65
                 [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@44157
    66
                  fn ([], a) =>
wenzelm@46938
    67
                    let
wenzelm@51294
    68
                      val (master, (name, (imports, (keywords, errors)))) =
wenzelm@48707
    69
                        pair string (pair string (pair (list string)
wenzelm@48864
    70
                          (pair (list (pair string
wenzelm@48864
    71
                            (option (pair (pair string (list string)) (list string)))))
wenzelm@51294
    72
                            (list string)))) a;
wenzelm@48927
    73
                      val imports' = map (rpair Position.none) imports;
wenzelm@51294
    74
                      val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@51294
    75
                    in Document.Deps (master, header, errors) end,
wenzelm@52849
    76
                  fn (a :: b, c) =>
wenzelm@52849
    77
                    Document.Perspective (bool_atom a, map int_atom b,
wenzelm@52862
    78
                      list (pair int (pair string (list string))) c)]))
wenzelm@44157
    79
            end;
wenzelm@38412
    80
wenzelm@52655
    81
        val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@52655
    82
        val _ = List.app Execution.terminate removed;
wenzelm@53192
    83
        val _ = Execution.purge removed;
wenzelm@52655
    84
        val _ = List.app Isabelle_Process.reset_tracing removed;
wenzelm@52601
    85
wenzelm@52601
    86
        val _ =
wenzelm@52601
    87
          Output.protocol_message Markup.assign_update
wenzelm@56333
    88
            [(new_id, assign_update) |>
wenzelm@52601
    89
              let open XML.Encode
wenzelm@52601
    90
              in pair int (list (pair int (list int))) end
wenzelm@56333
    91
              |> YXML.string_of_body];
wenzelm@52774
    92
      in Document.start_execution state' end));
wenzelm@38412
    93
wenzelm@43748
    94
val _ =
wenzelm@46119
    95
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
    96
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
    97
      let
wenzelm@44673
    98
        val versions =
wenzelm@44673
    99
          YXML.parse_body versions_yxml |>
wenzelm@44673
   100
            let open XML.Decode in list int end;
wenzelm@44676
   101
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   102
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   103
      in state1 end));
wenzelm@44673
   104
wenzelm@44673
   105
val _ =
wenzelm@50498
   106
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   107
    (fn [serial, result] =>
wenzelm@50500
   108
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
   109
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
   110
wenzelm@38412
   111
end;
wenzelm@38412
   112