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