src/Pure/PIDE/protocol.ML
author wenzelm
Thu Mar 12 22:00:51 2015 +0100 (2015-03-12 ago)
changeset 59685 c043306d2598
parent 59671 9715eb8e9408
child 59714 ae322325adbb
permissions -rw-r--r--
clarified markup for embedded files, early before execution;
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@59085
    31
    (fn id :: name :: blobs_yxml :: toks_yxml :: sources =>
wenzelm@54519
    32
      let
wenzelm@59685
    33
        val (blobs, blobs_index) =
wenzelm@54519
    34
          YXML.parse_body blobs_yxml |>
wenzelm@54519
    35
            let open XML.Decode in
wenzelm@59685
    36
              pair
wenzelm@59685
    37
                (list (variant
wenzelm@59685
    38
                 [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@59685
    39
                  fn ([], a) => Exn.Exn (ERROR (YXML.string_of_body a))]))
wenzelm@59685
    40
                int
wenzelm@54519
    41
            end;
wenzelm@59085
    42
        val toks =
wenzelm@59085
    43
          (YXML.parse_body toks_yxml |> let open XML.Decode in list (pair int int) end) ~~ sources;
wenzelm@54519
    44
      in
wenzelm@59685
    45
        Document.change_state
wenzelm@59685
    46
          (Document.define_command (Document_ID.parse id) name blobs blobs_index toks)
wenzelm@54519
    47
      end);
wenzelm@38412
    48
wenzelm@38418
    49
val _ =
wenzelm@47343
    50
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    51
    (fn [] => Execution.discontinue ());
wenzelm@47343
    52
wenzelm@47343
    53
val _ =
wenzelm@52931
    54
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    55
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    56
wenzelm@52931
    57
val _ =
wenzelm@46119
    58
  Isabelle_Process.protocol_command "Document.update"
wenzelm@59370
    59
    (Future.task_context "Document.update" (Future.new_group NONE)
wenzelm@59370
    60
      (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@59370
    61
        let
wenzelm@59370
    62
          val _ = Execution.discontinue ();
wenzelm@47404
    63
wenzelm@59370
    64
          val old_id = Document_ID.parse old_id_string;
wenzelm@59370
    65
          val new_id = Document_ID.parse new_id_string;
wenzelm@59370
    66
          val edits =
wenzelm@59370
    67
            YXML.parse_body edits_yxml |>
wenzelm@59370
    68
              let open XML.Decode in
wenzelm@59370
    69
                list (pair string
wenzelm@59370
    70
                  (variant
wenzelm@59370
    71
                   [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@59370
    72
                    fn ([], a) =>
wenzelm@59370
    73
                      let
wenzelm@59370
    74
                        val (master, (name, (imports, (keywords, errors)))) =
wenzelm@59370
    75
                          pair string (pair string (pair (list string)
wenzelm@59370
    76
                            (pair (list (pair string
wenzelm@59370
    77
                              (option (pair (pair string (list string)) (list string)))))
wenzelm@59671
    78
                              (list YXML.string_of_body)))) a;
wenzelm@59370
    79
                        val imports' = map (rpair Position.none) imports;
wenzelm@59370
    80
                        val header = Thy_Header.make (name, Position.none) imports' keywords;
wenzelm@59370
    81
                      in Document.Deps (master, header, errors) end,
wenzelm@59370
    82
                    fn (a :: b, c) =>
wenzelm@59370
    83
                      Document.Perspective (bool_atom a, map int_atom b,
wenzelm@59370
    84
                        list (pair int (pair string (list string))) c)]))
wenzelm@59370
    85
              end;
wenzelm@38412
    86
wenzelm@59370
    87
          val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@59463
    88
          val _ =
wenzelm@59463
    89
            (singleton o Future.forks)
wenzelm@59463
    90
             {name = "Document.update/remove", group = NONE,
wenzelm@59463
    91
              deps = maps Future.group_snapshot (maps Execution.peek removed),
wenzelm@59463
    92
              pri = 1, interrupts = false}
wenzelm@59463
    93
             (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
wenzelm@52601
    94
wenzelm@59370
    95
          val _ =
wenzelm@59370
    96
            Output.protocol_message Markup.assign_update
wenzelm@59370
    97
              [(new_id, assign_update) |>
wenzelm@59370
    98
                let open XML.Encode
wenzelm@59370
    99
                in pair int (list (pair int (list int))) end
wenzelm@59370
   100
                |> YXML.string_of_body];
wenzelm@59370
   101
        in Document.start_execution state' end)));
wenzelm@38412
   102
wenzelm@43748
   103
val _ =
wenzelm@46119
   104
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   105
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   106
      let
wenzelm@44673
   107
        val versions =
wenzelm@44673
   108
          YXML.parse_body versions_yxml |>
wenzelm@44673
   109
            let open XML.Decode in list int end;
wenzelm@44676
   110
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   111
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   112
      in state1 end));
wenzelm@44673
   113
wenzelm@44673
   114
val _ =
wenzelm@50498
   115
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   116
    (fn [serial, result] =>
wenzelm@50500
   117
      Active.dialog_result (Markup.parse_int serial) result
wenzelm@52775
   118
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
wenzelm@50498
   119
wenzelm@56616
   120
val _ =
wenzelm@57868
   121
  Isabelle_Process.protocol_command "ML_System.share_common_data"
wenzelm@57868
   122
    (fn [] => ML_System.share_common_data ());
wenzelm@57868
   123
wenzelm@38412
   124
end;
wenzelm@38412
   125