src/Pure/PIDE/protocol.ML
author wenzelm
Fri Jan 19 14:55:46 2018 +0100 (19 months ago ago)
changeset 67472 bddfa23a4ea9
parent 67219 81e9804b2014
child 67493 c4e9e0c50487
permissions -rw-r--r--
formal treatment of documentation names;
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@65300
    17
      (Options.set_default (Options.decode (YXML.parse_body options_yxml));
wenzelm@65300
    18
       Isabelle_Process.init_options_interactive ()));
wenzelm@52585
    19
wenzelm@52585
    20
val _ =
wenzelm@67219
    21
  Isabelle_Process.protocol_command "Prover.init_session_base"
wenzelm@67472
    22
    (fn [sessions_yxml, doc_names_yxml, global_theories_yxml, loaded_theories_yxml,
wenzelm@67472
    23
          known_theories_yxml] =>
wenzelm@65470
    24
      let
wenzelm@65470
    25
        val decode_table = YXML.parse_body #> let open XML.Decode in list (pair string string) end;
wenzelm@66712
    26
        val decode_list = YXML.parse_body #> let open XML.Decode in list string end;
wenzelm@65470
    27
      in
wenzelm@65478
    28
        Resources.init_session_base
wenzelm@67219
    29
          {sessions = decode_list sessions_yxml,
wenzelm@67472
    30
           doc_names = decode_list doc_names_yxml,
wenzelm@67219
    31
           global_theories = decode_table global_theories_yxml,
wenzelm@66712
    32
           loaded_theories = decode_list loaded_theories_yxml,
wenzelm@65470
    33
           known_theories = decode_table known_theories_yxml}
wenzelm@65470
    34
      end);
wenzelm@65470
    35
wenzelm@65470
    36
val _ =
wenzelm@54519
    37
  Isabelle_Process.protocol_command "Document.define_blob"
wenzelm@54519
    38
    (fn [digest, content] => Document.change_state (Document.define_blob digest content));
wenzelm@54519
    39
wenzelm@54519
    40
val _ =
wenzelm@46119
    41
  Isabelle_Process.protocol_command "Document.define_command"
wenzelm@59085
    42
    (fn id :: name :: blobs_yxml :: toks_yxml :: sources =>
wenzelm@54519
    43
      let
wenzelm@59685
    44
        val (blobs, blobs_index) =
wenzelm@54519
    45
          YXML.parse_body blobs_yxml |>
wenzelm@59714
    46
            let
wenzelm@59714
    47
              val message =
wenzelm@59714
    48
                YXML.string_of_body o Protocol_Message.command_positions id;
wenzelm@59714
    49
              open XML.Decode;
wenzelm@59714
    50
            in
wenzelm@59685
    51
              pair
wenzelm@59685
    52
                (list (variant
wenzelm@59685
    53
                 [fn ([], a) => Exn.Res (pair string (option string) a),
wenzelm@59714
    54
                  fn ([], a) => Exn.Exn (ERROR (message a))]))
wenzelm@59685
    55
                int
wenzelm@54519
    56
            end;
wenzelm@59085
    57
        val toks =
wenzelm@59085
    58
          (YXML.parse_body toks_yxml |> let open XML.Decode in list (pair int int) end) ~~ sources;
wenzelm@54519
    59
      in
wenzelm@59685
    60
        Document.change_state
wenzelm@59685
    61
          (Document.define_command (Document_ID.parse id) name blobs blobs_index toks)
wenzelm@54519
    62
      end);
wenzelm@38412
    63
wenzelm@38418
    64
val _ =
wenzelm@66382
    65
  Isabelle_Process.protocol_command "Document.consolidate_execution"
wenzelm@66382
    66
    (fn [] => Document.consolidate_execution (Document.state ()));
wenzelm@66382
    67
wenzelm@66382
    68
val _ =
wenzelm@47343
    69
  Isabelle_Process.protocol_command "Document.discontinue_execution"
wenzelm@52606
    70
    (fn [] => Execution.discontinue ());
wenzelm@47343
    71
wenzelm@47343
    72
val _ =
wenzelm@52931
    73
  Isabelle_Process.protocol_command "Document.cancel_exec"
wenzelm@52931
    74
    (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
wenzelm@52931
    75
wenzelm@52931
    76
val _ =
wenzelm@46119
    77
  Isabelle_Process.protocol_command "Document.update"
wenzelm@59370
    78
    (Future.task_context "Document.update" (Future.new_group NONE)
wenzelm@59370
    79
      (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
wenzelm@59370
    80
        let
wenzelm@59370
    81
          val _ = Execution.discontinue ();
wenzelm@47404
    82
wenzelm@59370
    83
          val old_id = Document_ID.parse old_id_string;
wenzelm@59370
    84
          val new_id = Document_ID.parse new_id_string;
wenzelm@59370
    85
          val edits =
wenzelm@59370
    86
            YXML.parse_body edits_yxml |>
wenzelm@59370
    87
              let open XML.Decode in
wenzelm@59370
    88
                list (pair string
wenzelm@59370
    89
                  (variant
wenzelm@59370
    90
                   [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
wenzelm@59370
    91
                    fn ([], a) =>
wenzelm@59370
    92
                      let
wenzelm@59370
    93
                        val (master, (name, (imports, (keywords, errors)))) =
wenzelm@59370
    94
                          pair string (pair string (pair (list string)
wenzelm@59370
    95
                            (pair (list (pair string
wenzelm@63429
    96
                              (pair (pair string (list string)) (list string))))
wenzelm@59671
    97
                              (list YXML.string_of_body)))) a;
wenzelm@59370
    98
                        val imports' = map (rpair Position.none) imports;
wenzelm@59934
    99
                        val keywords' = map (fn (x, y) => ((x, Position.none), y)) keywords;
wenzelm@59934
   100
                        val header = Thy_Header.make (name, Position.none) imports' keywords';
wenzelm@59715
   101
                      in Document.Deps {master = master, header = header, errors = errors} end,
wenzelm@59370
   102
                    fn (a :: b, c) =>
wenzelm@59370
   103
                      Document.Perspective (bool_atom a, map int_atom b,
wenzelm@59370
   104
                        list (pair int (pair string (list string))) c)]))
wenzelm@59370
   105
              end;
wenzelm@38412
   106
wenzelm@59370
   107
          val (removed, assign_update, state') = Document.update old_id new_id edits state;
wenzelm@59463
   108
          val _ =
wenzelm@59463
   109
            (singleton o Future.forks)
wenzelm@59463
   110
             {name = "Document.update/remove", group = NONE,
wenzelm@66373
   111
              deps = Execution.snapshot removed,
wenzelm@60610
   112
              pri = Task_Queue.urgent_pri + 2, interrupts = false}
wenzelm@59463
   113
             (fn () => (Execution.purge removed; List.app Isabelle_Process.reset_tracing removed));
wenzelm@52601
   114
wenzelm@59370
   115
          val _ =
wenzelm@59370
   116
            Output.protocol_message Markup.assign_update
wenzelm@59370
   117
              [(new_id, assign_update) |>
wenzelm@59370
   118
                let open XML.Encode
wenzelm@59370
   119
                in pair int (list (pair int (list int))) end
wenzelm@59370
   120
                |> YXML.string_of_body];
wenzelm@59370
   121
        in Document.start_execution state' end)));
wenzelm@38412
   122
wenzelm@43748
   123
val _ =
wenzelm@46119
   124
  Isabelle_Process.protocol_command "Document.remove_versions"
wenzelm@44673
   125
    (fn [versions_yxml] => Document.change_state (fn state =>
wenzelm@44673
   126
      let
wenzelm@44673
   127
        val versions =
wenzelm@44673
   128
          YXML.parse_body versions_yxml |>
wenzelm@44673
   129
            let open XML.Decode in list int end;
wenzelm@44676
   130
        val state1 = Document.remove_versions versions state;
wenzelm@56333
   131
        val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
wenzelm@44676
   132
      in state1 end));
wenzelm@44673
   133
wenzelm@44673
   134
val _ =
wenzelm@50498
   135
  Isabelle_Process.protocol_command "Document.dialog_result"
wenzelm@50500
   136
    (fn [serial, result] =>
wenzelm@63806
   137
      Active.dialog_result (Value.parse_int serial) result
wenzelm@62505
   138
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else Exn.reraise exn);
wenzelm@50498
   139
wenzelm@56616
   140
val _ =
wenzelm@62467
   141
  Isabelle_Process.protocol_command "ML_Heap.share_common_data"
wenzelm@62467
   142
    (fn [] => ML_Heap.share_common_data ());
wenzelm@57868
   143
wenzelm@38412
   144
end;
wenzelm@38412
   145