src/Pure/PIDE/protocol.ML
author wenzelm
Wed Dec 03 14:04:38 2014 +0100 (2014-12-03 ago)
changeset 59083 88b0b1f28adc
parent 58928 23d0ffd48006
child 59085 08a6901eb035
permissions -rw-r--r--
tuned signature;
     1 (*  Title:      Pure/PIDE/protocol.ML
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 *)
     6 
     7 structure Protocol: sig end =
     8 struct
     9 
    10 val _ =
    11   Isabelle_Process.protocol_command "Prover.echo"
    12     (fn args => List.app writeln args);
    13 
    14 val _ =
    15   Isabelle_Process.protocol_command "Prover.options"
    16     (fn [options_yxml] =>
    17       let val options = Options.decode (YXML.parse_body options_yxml) in
    18         Options.set_default options;
    19         Future.ML_statistics := true;
    20         Multithreading.trace := Options.int options "threads_trace";
    21         Multithreading.max_threads_update (Options.int options "threads");
    22         Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0)
    23       end);
    24 
    25 val _ =
    26   Isabelle_Process.protocol_command "Document.init_keywords"
    27     (fn [] => Document.init_keywords ());
    28 
    29 val _ =
    30   Isabelle_Process.protocol_command "Document.define_blob"
    31     (fn [digest, content] => Document.change_state (Document.define_blob digest content));
    32 
    33 val _ =
    34   Isabelle_Process.protocol_command "Document.define_command"
    35     (fn [id, name, blobs_yxml, text] =>
    36       let
    37         val blobs =
    38           YXML.parse_body blobs_yxml |>
    39             let open XML.Decode in
    40               list (variant
    41                [fn ([], a) => Exn.Res (pair string (option string) a),
    42                 fn ([], a) => Exn.Exn (ERROR (string a))])
    43             end;
    44       in
    45         Document.change_state (Document.define_command (Document_ID.parse id) name blobs text)
    46       end);
    47 
    48 val _ =
    49   Isabelle_Process.protocol_command "Document.discontinue_execution"
    50     (fn [] => Execution.discontinue ());
    51 
    52 val _ =
    53   Isabelle_Process.protocol_command "Document.cancel_exec"
    54     (fn [exec_id] => Execution.cancel (Document_ID.parse exec_id));
    55 
    56 val _ =
    57   Isabelle_Process.protocol_command "Document.update"
    58     (fn [old_id_string, new_id_string, edits_yxml] => Document.change_state (fn state =>
    59       let
    60         val _ = Execution.discontinue ();
    61 
    62         val old_id = Document_ID.parse old_id_string;
    63         val new_id = Document_ID.parse new_id_string;
    64         val edits =
    65           YXML.parse_body edits_yxml |>
    66             let open XML.Decode in
    67               list (pair string
    68                 (variant
    69                  [fn ([], a) => Document.Edits (list (pair (option int) (option int)) a),
    70                   fn ([], a) =>
    71                     let
    72                       val (master, (name, (imports, (keywords, errors)))) =
    73                         pair string (pair string (pair (list string)
    74                           (pair (list (pair string
    75                             (option (pair (pair string (list string)) (list string)))))
    76                             (list string)))) a;
    77                       val imports' = map (rpair Position.none) imports;
    78                       val header = Thy_Header.make (name, Position.none) imports' keywords;
    79                     in Document.Deps (master, header, errors) end,
    80                   fn (a :: b, c) =>
    81                     Document.Perspective (bool_atom a, map int_atom b,
    82                       list (pair int (pair string (list string))) c)]))
    83             end;
    84 
    85         val (removed, assign_update, state') = Document.update old_id new_id edits state;
    86         val _ = List.app Execution.terminate removed;
    87         val _ = Execution.purge removed;
    88         val _ = List.app Isabelle_Process.reset_tracing removed;
    89 
    90         val _ =
    91           Output.protocol_message Markup.assign_update
    92             [(new_id, assign_update) |>
    93               let open XML.Encode
    94               in pair int (list (pair int (list int))) end
    95               |> YXML.string_of_body];
    96       in Document.start_execution state' end));
    97 
    98 val _ =
    99   Isabelle_Process.protocol_command "Document.remove_versions"
   100     (fn [versions_yxml] => Document.change_state (fn state =>
   101       let
   102         val versions =
   103           YXML.parse_body versions_yxml |>
   104             let open XML.Decode in list int end;
   105         val state1 = Document.remove_versions versions state;
   106         val _ = Output.protocol_message Markup.removed_versions [versions_yxml];
   107       in state1 end));
   108 
   109 val _ =
   110   Isabelle_Process.protocol_command "Document.dialog_result"
   111     (fn [serial, result] =>
   112       Active.dialog_result (Markup.parse_int serial) result
   113         handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn);
   114 
   115 val _ =
   116   Isabelle_Process.protocol_command "use_theories"
   117     (fn id :: master_dir :: thys =>
   118       let
   119         val result =
   120           Exn.capture (fn () =>
   121             Thy_Info.use_theories
   122               {document = false, last_timing = K NONE, master_dir = Path.explode master_dir}
   123               (map (rpair Position.none) thys)) ();
   124         val ok =
   125           (case result of
   126             Exn.Res _ => true
   127           | Exn.Exn exn => (Runtime.exn_error_message exn; false));
   128     in Output.protocol_message (Markup.use_theories_result id ok) [] end);
   129 
   130 val _ =
   131   Isabelle_Process.protocol_command "ML_System.share_common_data"
   132     (fn [] => ML_System.share_common_data ());
   133 
   134 end;
   135