src/Pure/System/isabelle_process.ML
author wenzelm
Mon, 01 Mar 2021 17:44:44 +0100
changeset 73332 91703452523d
parent 73225 3ab0cedaccad
child 73559 22b5ecb53dd9
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30173
eabece26b89b moved isabelle_process.ML, isabelle_process.scala, isar.ML, session.ML to Pure/System/ (together with associated Isar commands);
wenzelm
parents: 29522
diff changeset
     1
(*  Title:      Pure/System/isabelle_process.ML
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     3
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57913
diff changeset
     4
Isabelle process wrapper.
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     5
*)
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     6
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     7
signature ISABELLE_PROCESS =
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     8
sig
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
     9
  val is_active: unit -> bool
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    10
  val reset_tracing: Document_ID.exec -> unit
43684
85388f5570c4 prefer Synchronized.var;
wenzelm
parents: 43673
diff changeset
    11
  val crashes: exn list Synchronized.var
62563
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
    12
  val init_options: unit -> unit
65300
c262653a3b88 clarified signature;
wenzelm
parents: 65212
diff changeset
    13
  val init_options_interactive: unit -> unit
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
    14
  val init: unit -> unit
71644
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
    15
  val init_build: unit -> unit
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    16
end;
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    17
31797
203d5e61e3bc renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 31384
diff changeset
    18
structure Isabelle_Process: ISABELLE_PROCESS =
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    19
struct
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    20
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    21
(* print mode *)
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    22
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    23
val isabelle_processN = "isabelle_process";
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    24
42897
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    25
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
6bc8a6dcb3e0 added Isabelle_Process.is_active;
wenzelm
parents: 40521
diff changeset
    26
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    27
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
28036
wenzelm
parents: 27986
diff changeset
    28
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    29
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    30
val protocol_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    31
val protocol_modes2 = [isabelle_processN, Pretty.symbolicN];
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    32
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    33
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    34
(* restricted tracing messages *)
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    35
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    36
val tracing_messages =
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    37
  Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    38
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    39
fun reset_tracing exec_id =
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    40
  Synchronized.change tracing_messages (Inttab.delete_safe exec_id);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    41
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    42
fun update_tracing () =
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50715
diff changeset
    43
  (case Position.parse_id (Position.thread_data ()) of
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    44
    NONE => ()
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    45
  | SOME exec_id =>
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    46
      let
52560
wenzelm
parents: 52559
diff changeset
    47
        val ok =
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    48
          Synchronized.change_result tracing_messages (fn tab =>
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    49
            let
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    50
              val n = the_default 0 (Inttab.lookup tab exec_id) + 1;
69103
814a1ab42d70 unbounded tracing for proper termination, e.g. relevant for theory Sequents.Hard_Quantifiers;
wenzelm
parents: 68025
diff changeset
    51
              val limit = Options.default_int "editor_tracing_messages";
814a1ab42d70 unbounded tracing for proper termination, e.g. relevant for theory Sequents.Hard_Quantifiers;
wenzelm
parents: 68025
diff changeset
    52
              val ok = limit <= 0 orelse n <= limit;
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    53
            in (ok, Inttab.update (exec_id, n) tab) end);
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    54
      in
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    55
        if ok then ()
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    56
        else
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    57
          let
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    58
            val (text, promise) = Active.dialog_text ();
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    59
            val _ =
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    60
              writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
51044
890f502f0e89 more generous tracing limit, which is relevant for applications where this occurs routinely (e.g. HO unification trace);
wenzelm
parents: 50911
diff changeset
    61
                text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
63806
c54a53ef1873 clarified modules;
wenzelm
parents: 62930
diff changeset
    62
            val m = Value.parse_int (Future.join promise)
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    63
              handle Fail _ => error "Stopped";
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    64
          in
52105
88b423034d4f proper options;
wenzelm
parents: 52104
diff changeset
    65
            Synchronized.change tracing_messages
52655
3b2b1ef13979 more careful termination of removed execs, leaving running execs undisturbed;
wenzelm
parents: 52584
diff changeset
    66
              (Inttab.map_default (exec_id, 0) (fn k => k - m))
50505
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    67
          end
33c92722cc3d smarter handling of tracing messages: prover process pauses and enters user dialog;
wenzelm
parents: 50503
diff changeset
    68
      end);
49647
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    69
21ae8500d261 smarter handling of tracing messages;
wenzelm
parents: 49566
diff changeset
    70
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    71
(* init protocol -- uninterruptible *)
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    72
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    73
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    74
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    75
local
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    76
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    77
fun recover crash =
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    78
  (Synchronized.change crashes (cons crash);
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    79
    Output.physical_stderr
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    80
      "Recovered from Isabelle process crash -- see also Isabelle_Process.crashes\n");
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    81
72119
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72116
diff changeset
    82
fun ml_statistics () =
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72116
diff changeset
    83
  Output.protocol_message
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72116
diff changeset
    84
    (Markup.ML_statistics {pid = ML_Pid.get (), stats_dir = getenv "POLYSTATSDIR"}) [];
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72116
diff changeset
    85
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    86
in
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    87
71644
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
    88
fun init_protocol modes = Thread_Attributes.uninterruptible (fn _ => fn (address, password) =>
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    89
  let
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    90
    val _ = SHA1.test_samples ()
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    91
      handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); Exn.reraise exn);
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    92
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    93
    val _ = Output.physical_stderr Symbol.STX;
52854
92932931bd82 more general Output.result: allow to update arbitrary properties;
wenzelm
parents: 52852
diff changeset
    94
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    95
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    96
    (* streams *)
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    97
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    98
    val (in_stream, out_stream) = Socket_IO.open_streams address;
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
    99
    val _ = Byte_Message.write_line out_stream password;
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   100
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   101
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   102
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
69449
b516fdf8005c more uniform multi-language operations;
wenzelm
parents: 69103
diff changeset
   103
    val _ = BinIO.StreamIO.setBufferMode (BinIO.getOutstream out_stream, IO.BLOCK_BUF);
40134
8baded087d34 explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
wenzelm
parents: 40133
diff changeset
   104
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   105
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   106
    (* messages *)
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   107
69449
b516fdf8005c more uniform multi-language operations;
wenzelm
parents: 69103
diff changeset
   108
    val msg_channel = Message_Channel.make out_stream;
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   109
52800
1baa5d19ac44 less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
wenzelm
parents: 52799
diff changeset
   110
    fun message name props body =
1baa5d19ac44 less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
wenzelm
parents: 52799
diff changeset
   111
      Message_Channel.send msg_channel (Message_Channel.message name props body);
1baa5d19ac44 less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
wenzelm
parents: 52799
diff changeset
   112
70995
2c17fa0f5187 more direct output of XML material -- bypass Buffer.T;
wenzelm
parents: 70991
diff changeset
   113
    fun standard_message props name ss =
2c17fa0f5187 more direct output of XML material -- bypass Buffer.T;
wenzelm
parents: 70991
diff changeset
   114
      if forall (fn s => s = "") ss then ()
52580
36aa39694ab4 fall back on synchronous message output for single-threaded SML/NJ;
wenzelm
parents: 52578
diff changeset
   115
      else
57913
8544ef75d1d8 more frugal standard message properties;
wenzelm
parents: 56895
diff changeset
   116
        let
72709
cb9d5af781b4 more complete report positions, notably for command 'back' (amending eca176f773e0);
wenzelm
parents: 72158
diff changeset
   117
          val pos_props =
cb9d5af781b4 more complete report positions, notably for command 'back' (amending eca176f773e0);
wenzelm
parents: 72158
diff changeset
   118
            if exists Markup.position_property props then props
cb9d5af781b4 more complete report positions, notably for command 'back' (amending eca176f773e0);
wenzelm
parents: 72158
diff changeset
   119
            else props @ Position.properties_of (Position.thread_data ());
cb9d5af781b4 more complete report positions, notably for command 'back' (amending eca176f773e0);
wenzelm
parents: 72158
diff changeset
   120
        in message name pos_props (XML.blob ss) end;
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   121
71675
55cb4271858b less redundant markup reports;
wenzelm
parents: 71667
diff changeset
   122
    fun report_message ss =
55cb4271858b less redundant markup reports;
wenzelm
parents: 71667
diff changeset
   123
      if Context_Position.pide_reports ()
55cb4271858b less redundant markup reports;
wenzelm
parents: 71667
diff changeset
   124
      then standard_message [] Markup.reportN ss else ();
55cb4271858b less redundant markup reports;
wenzelm
parents: 71667
diff changeset
   125
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   126
    val serial_props = Markup.serial_properties o serial;
65212
fd6bc719c98b more robust startup/init: let Session.stop wait for protocol handler initialization;
wenzelm
parents: 63806
diff changeset
   127
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   128
    val message_context =
71675
55cb4271858b less redundant markup reports;
wenzelm
parents: 71667
diff changeset
   129
      Unsynchronized.setmp Private_Output.status_fn (standard_message [] Markup.statusN) #>
55cb4271858b less redundant markup reports;
wenzelm
parents: 71667
diff changeset
   130
      Unsynchronized.setmp Private_Output.report_fn report_message #>
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   131
      Unsynchronized.setmp Private_Output.result_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   132
        (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   133
      Unsynchronized.setmp Private_Output.writeln_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   134
        (fn s => standard_message (serial_props ()) Markup.writelnN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   135
      Unsynchronized.setmp Private_Output.state_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   136
        (fn s => standard_message (serial_props ()) Markup.stateN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   137
      Unsynchronized.setmp Private_Output.information_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   138
        (fn s => standard_message (serial_props ()) Markup.informationN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   139
      Unsynchronized.setmp Private_Output.tracing_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   140
        (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s)) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   141
      Unsynchronized.setmp Private_Output.warning_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   142
        (fn s => standard_message (serial_props ()) Markup.warningN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   143
      Unsynchronized.setmp Private_Output.legacy_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   144
        (fn s => standard_message (serial_props ()) Markup.legacyN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   145
      Unsynchronized.setmp Private_Output.error_message_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   146
        (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   147
      Unsynchronized.setmp Private_Output.system_message_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   148
        (fn ss => message Markup.systemN [] (XML.blob ss)) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   149
      Unsynchronized.setmp Private_Output.protocol_message_fn
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   150
        (fn props => fn body => message Markup.protocolN props body) #>
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   151
      Unsynchronized.setmp print_mode
71644
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
   152
        ((! print_mode @ #1 modes) |> fold (update op =) (#2 modes));
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   153
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   154
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   155
    (* protocol *)
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   156
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   157
    fun protocol_loop () =
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   158
      let
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   159
        val _ =
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   160
          (case Byte_Message.read_message in_stream of
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents: 72709
diff changeset
   161
            NONE => raise Protocol_Command.STOP 0
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   162
          | SOME [] => Output.system_message "Isabelle process: no input"
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents: 72709
diff changeset
   163
          | SOME (name :: args) => Protocol_Command.run name args)
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   164
          handle exn =>
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents: 72709
diff changeset
   165
            if Protocol_Command.is_protocol_exn exn then Exn.reraise exn
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   166
            else (Runtime.exn_system_message exn handle crash => recover crash);
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   167
      in protocol_loop () end;
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   168
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   169
    fun protocol () =
72158
cafe00f2161e proper protocol init (amending 065dcd80293e);
wenzelm
parents: 72156
diff changeset
   170
     (message Markup.initN [] [XML.Text (Session.welcome ())];
cafe00f2161e proper protocol init (amending 065dcd80293e);
wenzelm
parents: 72156
diff changeset
   171
      ml_statistics ();
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   172
      protocol_loop ());
71631
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 70995
diff changeset
   173
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   174
    val result = Exn.capture (message_context protocol) ();
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   175
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   176
71637
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   177
    (* shutdown *)
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   178
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   179
    val _ = Future.shutdown ();
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   180
    val _ = Execution.reset ();
45c2b8cf1b26 more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
wenzelm
parents: 71631
diff changeset
   181
    val _ = Message_Channel.shutdown msg_channel;
71638
ec14ef6dd09b close socket explicitly (idempotent);
wenzelm
parents: 71637
diff changeset
   182
    val _ = BinIO.closeIn in_stream;
ec14ef6dd09b close socket explicitly (idempotent);
wenzelm
parents: 71637
diff changeset
   183
    val _ = BinIO.closeOut out_stream;
71667
4d2de35214c5 proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
wenzelm
parents: 71656
diff changeset
   184
72104
d9a42786fbc9 more thorough cleanup, e.g. before ML_Heap.save;
wenzelm
parents: 71878
diff changeset
   185
    val _ = Options.reset_default ();
71656
3e121f999120 clarified signature: more direct Isabelle_Process.EXIT;
wenzelm
parents: 71644
diff changeset
   186
  in
3e121f999120 clarified signature: more direct Isabelle_Process.EXIT;
wenzelm
parents: 71644
diff changeset
   187
    (case result of
73225
3ab0cedaccad clarified modules: allow early definition of protocol commands;
wenzelm
parents: 72709
diff changeset
   188
      Exn.Exn (Protocol_Command.STOP rc) => if rc = 0 then () else exit rc
71656
3e121f999120 clarified signature: more direct Isabelle_Process.EXIT;
wenzelm
parents: 71644
diff changeset
   189
    | _ => Exn.release result)
3e121f999120 clarified signature: more direct Isabelle_Process.EXIT;
wenzelm
parents: 71644
diff changeset
   190
  end);
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   191
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   192
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   193
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   194
62563
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
   195
(* init options *)
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
   196
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
   197
fun init_options () =
62878
1cec457e0a03 clarified modules -- simplified bootstrap;
wenzelm
parents: 62713
diff changeset
   198
 (ML_Print_Depth.set_print_depth (Options.default_int "ML_print_depth");
62563
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
   199
  Multithreading.trace := Options.default_int "threads_trace";
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
   200
  Multithreading.max_threads_update (Options.default_int "threads");
68025
7fb7a6366a40 clarified modules;
wenzelm
parents: 66020
diff changeset
   201
  Multithreading.parallel_proofs := Options.default_int "parallel_proofs";
70914
05c4c6a99b3f option to export standardized proof terms (not scalable);
wenzelm
parents: 69572
diff changeset
   202
  if Options.default_bool "export_standard_proofs" then Proofterm.proofs := 2 else ();
65448
9bc3b57c1fa7 added system option record_proofs, which allows to build HOL-Proofs without special Proofs.thy;
wenzelm
parents: 65303
diff changeset
   203
  let val proofs = Options.default_int "record_proofs"
9bc3b57c1fa7 added system option record_proofs, which allows to build HOL-Proofs without special Proofs.thy;
wenzelm
parents: 65303
diff changeset
   204
  in if proofs < 0 then () else Proofterm.proofs := proofs end;
65302
3f92d63dad12 clarified init (see also 32755e357a51, ac48def96b69);
wenzelm
parents: 65301
diff changeset
   205
  Printer.show_markup_default := false);
62563
2e352f63d15f separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
wenzelm
parents: 62505
diff changeset
   206
65300
c262653a3b88 clarified signature;
wenzelm
parents: 65212
diff changeset
   207
fun init_options_interactive () =
c262653a3b88 clarified signature;
wenzelm
parents: 65212
diff changeset
   208
 (init_options ();
68025
7fb7a6366a40 clarified modules;
wenzelm
parents: 66020
diff changeset
   209
  Multithreading.parallel_proofs := (if Options.default_int "parallel_proofs" > 0 then 3 else 0);
65302
3f92d63dad12 clarified init (see also 32755e357a51, ac48def96b69);
wenzelm
parents: 65301
diff changeset
   210
  Printer.show_markup_default := true);
62599
f35858c831e5 clarified session build options: already provided by ML_Process;
wenzelm
parents: 62564
diff changeset
   211
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   212
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   213
(* generic init *)
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   214
71644
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
   215
fun init_modes modes =
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   216
  let
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   217
    val address = Options.default_string \<^system_option>\<open>system_channel_address\<close>;
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   218
    val password = Options.default_string \<^system_option>\<open>system_channel_password\<close>;
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   219
  in
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   220
    if address <> "" andalso password <> ""
71644
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
   221
    then init_protocol modes (address, password)
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   222
    else init_options ()
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   223
  end;
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 69449
diff changeset
   224
71644
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
   225
fun init () = init_modes (protocol_modes1, protocol_modes2);
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
   226
fun init_build () = init_modes ([], protocol_modes2);
60659474ed36 traditional print_mode for batch build;
wenzelm
parents: 71640
diff changeset
   227
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   228
end;