| author | Fabian Huch <huch@in.tum.de> | 
| Fri, 23 Aug 2024 15:30:09 +0200 | |
| changeset 80949 | 97924a26a5c3 | 
| parent 80861 | 9de19e3a7231 | 
| child 82316 | 83584916b6d7 | 
| permissions | -rw-r--r-- | 
| 30173 
eabece26b89b
moved isabelle_process.ML, isabelle_process.scala, isar.ML, session.ML to Pure/System/ (together with associated Isar commands);
 wenzelm parents: 
29522diff
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 | 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 | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 9 | val reset_tracing: Document_ID.exec -> unit | 
| 43684 | 10 | val crashes: exn list Synchronized.var | 
| 62563 
2e352f63d15f
separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
 wenzelm parents: 
62505diff
changeset | 11 | val init_options: unit -> unit | 
| 65300 | 12 | val init_options_interactive: unit -> unit | 
| 69572 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 13 | val init: unit -> unit | 
| 71644 | 14 | val init_build: unit -> unit | 
| 25528 
e67230c2b952
Isabelle process wrapper -- interaction via external program.
 wenzelm parents: diff
changeset | 15 | end; | 
| 
e67230c2b952
Isabelle process wrapper -- interaction via external program.
 wenzelm parents: diff
changeset | 16 | |
| 31797 | 17 | structure Isabelle_Process: ISABELLE_PROCESS = | 
| 25528 
e67230c2b952
Isabelle process wrapper -- interaction via external program.
 wenzelm parents: diff
changeset | 18 | struct | 
| 
e67230c2b952
Isabelle process wrapper -- interaction via external program.
 wenzelm parents: diff
changeset | 19 | |
| 80861 
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
 wenzelm parents: 
80855diff
changeset | 20 | (* print modes *) | 
| 25841 | 21 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 22 | val protocol_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN]; | 
| 80861 
9de19e3a7231
dismantle print_mode operations for Markup/Pretty: hardwired check of "print_mode_active Print_Mode.PIDE";
 wenzelm parents: 
80855diff
changeset | 23 | val protocol_modes2 = [Print_Mode.PIDE]; | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 24 | |
| 25841 | 25 | |
| 50505 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 26 | (* restricted tracing messages *) | 
| 49647 | 27 | |
| 52105 | 28 | val tracing_messages = | 
| 29 | Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table); | |
| 49647 | 30 | |
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 31 | fun reset_tracing exec_id = | 
| 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 32 | Synchronized.change tracing_messages (Inttab.delete_safe exec_id); | 
| 49647 | 33 | |
| 50505 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 34 | fun update_tracing () = | 
| 50911 
ee7fe4230642
more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
 wenzelm parents: 
50715diff
changeset | 35 | (case Position.parse_id (Position.thread_data ()) of | 
| 49647 | 36 | NONE => () | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 37 | | SOME exec_id => | 
| 50505 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 38 | let | 
| 52560 | 39 | val ok = | 
| 52105 | 40 | Synchronized.change_result tracing_messages (fn tab => | 
| 50505 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 41 | let | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 42 | 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: 
68025diff
changeset | 43 | val limit = Options.default_int "editor_tracing_messages"; | 
| 
814a1ab42d70
unbounded tracing for proper termination, e.g. relevant for theory Sequents.Hard_Quantifiers;
 wenzelm parents: 
68025diff
changeset | 44 | val ok = limit <= 0 orelse n <= limit; | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 45 | 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: 
50503diff
changeset | 46 | in | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 47 | if ok then () | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 48 | else | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 49 | let | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 50 | val (text, promise) = Active.dialog_text (); | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 51 | val _ = | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 52 |               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: 
50911diff
changeset | 53 | text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?") | 
| 63806 | 54 | val m = Value.parse_int (Future.join promise) | 
| 50505 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 55 | handle Fail _ => error "Stopped"; | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 56 | in | 
| 52105 | 57 | Synchronized.change tracing_messages | 
| 52655 
3b2b1ef13979
more careful termination of removed execs, leaving running execs undisturbed;
 wenzelm parents: 
52584diff
changeset | 58 | (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: 
50503diff
changeset | 59 | end | 
| 
33c92722cc3d
smarter handling of tracing messages: prover process pauses and enters user dialog;
 wenzelm parents: 
50503diff
changeset | 60 | end); | 
| 49647 | 61 | |
| 62 | ||
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 63 | (* init protocol -- uninterruptible *) | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 64 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 65 | 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: 
71631diff
changeset | 66 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 67 | local | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 68 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 69 | fun recover crash = | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 70 | (Synchronized.change crashes (cons crash); | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 71 | Output.physical_stderr | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 72 | "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: 
71631diff
changeset | 73 | |
| 72119 
d115d50a19c0
provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
 wenzelm parents: 
72116diff
changeset | 74 | fun ml_statistics () = | 
| 
d115d50a19c0
provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
 wenzelm parents: 
72116diff
changeset | 75 | Output.protocol_message | 
| 
d115d50a19c0
provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
 wenzelm parents: 
72116diff
changeset | 76 |     (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: 
72116diff
changeset | 77 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 78 | in | 
| 28188 | 79 | |
| 71644 | 80 | 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: 
71631diff
changeset | 81 | let | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 82 | val _ = SHA1.test_samples () | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 83 | 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: 
71631diff
changeset | 84 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 85 | val _ = Output.physical_stderr Symbol.STX; | 
| 52854 
92932931bd82
more general Output.result: allow to update arbitrary properties;
 wenzelm parents: 
52852diff
changeset | 86 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 87 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 88 | (* streams *) | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 89 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 90 | val (in_stream, out_stream) = Socket_IO.open_streams address; | 
| 75577 
c51e1cef1eae
more scalable byte messages, notably for Scala functions in ML;
 wenzelm parents: 
73578diff
changeset | 91 | val _ = Byte_Message.write_line out_stream (Bytes.string password); | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 92 | |
| 40134 
8baded087d34
explicit message_output thread, with flushing after timeout, ensure atomic user-operations without the danger of IO Interrupt;
 wenzelm parents: 
40133diff
changeset | 93 | 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: 
40133diff
changeset | 94 | val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF); | 
| 69449 | 95 | 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: 
40133diff
changeset | 96 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 97 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 98 | (* messages *) | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 99 | |
| 73577 | 100 | val message_channel = Message_Channel.make out_stream; | 
| 73578 | 101 | val message = Message_Channel.message message_channel; | 
| 52800 
1baa5d19ac44
less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
 wenzelm parents: 
52799diff
changeset | 102 | |
| 70995 
2c17fa0f5187
more direct output of XML material -- bypass Buffer.T;
 wenzelm parents: 
70991diff
changeset | 103 | fun standard_message props name ss = | 
| 
2c17fa0f5187
more direct output of XML material -- bypass Buffer.T;
 wenzelm parents: 
70991diff
changeset | 104 | if forall (fn s => s = "") ss then () | 
| 52580 
36aa39694ab4
fall back on synchronous message output for single-threaded SML/NJ;
 wenzelm parents: 
52578diff
changeset | 105 | else | 
| 57913 | 106 | let | 
| 72709 
cb9d5af781b4
more complete report positions, notably for command 'back' (amending eca176f773e0);
 wenzelm parents: 
72158diff
changeset | 107 | val pos_props = | 
| 
cb9d5af781b4
more complete report positions, notably for command 'back' (amending eca176f773e0);
 wenzelm parents: 
72158diff
changeset | 108 | if exists Markup.position_property props then props | 
| 
cb9d5af781b4
more complete report positions, notably for command 'back' (amending eca176f773e0);
 wenzelm parents: 
72158diff
changeset | 109 | else props @ Position.properties_of (Position.thread_data ()); | 
| 73559 | 110 | 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: 
71631diff
changeset | 111 | |
| 71675 | 112 | fun report_message ss = | 
| 76804 | 113 | if Context_Position.reports_enabled0 () | 
| 71675 | 114 | then standard_message [] Markup.reportN ss else (); | 
| 115 | ||
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 116 | val serial_props = Markup.serial_properties o serial; | 
| 65212 
fd6bc719c98b
more robust startup/init: let Session.stop wait for protocol handler initialization;
 wenzelm parents: 
63806diff
changeset | 117 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 118 | val message_context = | 
| 71675 | 119 | Unsynchronized.setmp Private_Output.status_fn (standard_message [] Markup.statusN) #> | 
| 120 | 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: 
71631diff
changeset | 121 | 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: 
71631diff
changeset | 122 | (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: 
71631diff
changeset | 123 | 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: 
71631diff
changeset | 124 | (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: 
71631diff
changeset | 125 | 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: 
71631diff
changeset | 126 | (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: 
71631diff
changeset | 127 | 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: 
71631diff
changeset | 128 | (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: 
71631diff
changeset | 129 | 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: 
71631diff
changeset | 130 | (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: 
71631diff
changeset | 131 | 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: 
71631diff
changeset | 132 | (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: 
71631diff
changeset | 133 | 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: 
71631diff
changeset | 134 | (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: 
71631diff
changeset | 135 | 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: 
71631diff
changeset | 136 | (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: 
71631diff
changeset | 137 | Unsynchronized.setmp Private_Output.system_message_fn | 
| 73559 | 138 | (fn ss => message Markup.systemN [] [XML.blob ss]) #> | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 139 | Unsynchronized.setmp Private_Output.protocol_message_fn | 
| 73559 | 140 | (fn props => fn chunks => message Markup.protocolN props chunks) #> | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 141 | Unsynchronized.setmp print_mode | 
| 71644 | 142 | ((! print_mode @ #1 modes) |> fold (update op =) (#2 modes)); | 
| 25841 | 143 | |
| 144 | ||
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 145 | (* protocol *) | 
| 38270 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 146 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 147 | fun protocol_loop () = | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 148 | let | 
| 78757 | 149 | fun main () = | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 150 | (case Byte_Message.read_message in_stream of | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72709diff
changeset | 151 | NONE => raise Protocol_Command.STOP 0 | 
| 71667 
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
 wenzelm parents: 
71656diff
changeset | 152 | | SOME [] => Output.system_message "Isabelle process: no input" | 
| 78725 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 153 | | SOME (name :: args) => Protocol_Command.run (Bytes.content name) args); | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 154 | val _ = | 
| 78757 | 155 | (case Exn.capture_body main of | 
| 78725 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 156 | Exn.Res () => () | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 157 | | Exn.Exn exn => | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 158 | if Protocol_Command.is_protocol_exn exn then Exn.reraise exn | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 159 | else | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 160 | (case Exn.capture Runtime.exn_system_message exn of | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 161 | Exn.Res () => () | 
| 
3c02ad5a1586
clarified treatment of exceptions: avoid catch-all handlers;
 wenzelm parents: 
78711diff
changeset | 162 | | Exn.Exn crash => recover crash)); | 
| 71667 
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
 wenzelm parents: 
71656diff
changeset | 163 | in protocol_loop () end; | 
| 38270 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 164 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 165 | fun protocol () = | 
| 73559 | 166 | (message Markup.initN [] [[XML.Text (Session.welcome ())]]; | 
| 72158 | 167 | ml_statistics (); | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 168 | protocol_loop ()); | 
| 71631 | 169 | |
| 78758 | 170 | val result = Exn.capture_body (message_context protocol); | 
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 171 | |
| 38270 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 172 | |
| 71637 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 173 | (* shutdown *) | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 174 | |
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 175 | val _ = Future.shutdown (); | 
| 
45c2b8cf1b26
more robust message_context and shutdown, e.g. after errors in protocol loop (see also 3f02bc5a5a03);
 wenzelm parents: 
71631diff
changeset | 176 | val _ = Execution.reset (); | 
| 73577 | 177 | val _ = Message_Channel.shutdown message_channel; | 
| 71638 | 178 | val _ = BinIO.closeIn in_stream; | 
| 179 | val _ = BinIO.closeOut out_stream; | |
| 71667 
4d2de35214c5
proper treatment of protocol exceptions and prover termination: avoid session.stop while saving image;
 wenzelm parents: 
71656diff
changeset | 180 | |
| 72104 | 181 | val _ = Options.reset_default (); | 
| 71656 
3e121f999120
clarified signature: more direct Isabelle_Process.EXIT;
 wenzelm parents: 
71644diff
changeset | 182 | in | 
| 
3e121f999120
clarified signature: more direct Isabelle_Process.EXIT;
 wenzelm parents: 
71644diff
changeset | 183 | (case result of | 
| 73225 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 wenzelm parents: 
72709diff
changeset | 184 | Exn.Exn (Protocol_Command.STOP rc) => if rc = 0 then () else exit rc | 
| 71656 
3e121f999120
clarified signature: more direct Isabelle_Process.EXIT;
 wenzelm parents: 
71644diff
changeset | 185 | | _ => Exn.release result) | 
| 
3e121f999120
clarified signature: more direct Isabelle_Process.EXIT;
 wenzelm parents: 
71644diff
changeset | 186 | end); | 
| 38270 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 187 | |
| 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 188 | end; | 
| 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 189 | |
| 
71bb3c273dd1
native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
 wenzelm parents: 
38265diff
changeset | 190 | |
| 62563 
2e352f63d15f
separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
 wenzelm parents: 
62505diff
changeset | 191 | (* init options *) | 
| 
2e352f63d15f
separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
 wenzelm parents: 
62505diff
changeset | 192 | |
| 
2e352f63d15f
separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
 wenzelm parents: 
62505diff
changeset | 193 | fun init_options () = | 
| 62878 | 194 | (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: 
62505diff
changeset | 195 | Multithreading.trace := Options.default_int "threads_trace"; | 
| 
2e352f63d15f
separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
 wenzelm parents: 
62505diff
changeset | 196 | Multithreading.max_threads_update (Options.default_int "threads"); | 
| 68025 | 197 | Multithreading.parallel_proofs := Options.default_int "parallel_proofs"; | 
| 78711 
3a3a70d4d422
clarified order of modules: early access to interrupt management of Isabelle_Threads;
 wenzelm parents: 
78018diff
changeset | 198 | Isabelle_Thread.threads_stack_limit := Options.default_real "threads_stack_limit"; | 
| 70914 
05c4c6a99b3f
option to export standardized proof terms (not scalable);
 wenzelm parents: 
69572diff
changeset | 199 | 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: 
65303diff
changeset | 200 | 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: 
65303diff
changeset | 201 | in if proofs < 0 then () else Proofterm.proofs := proofs end; | 
| 78018 
dfa44d85d751
proper system options to control context tracing/timing;
 wenzelm parents: 
76804diff
changeset | 202 | Printer.show_markup_default := false; | 
| 
dfa44d85d751
proper system options to control context tracing/timing;
 wenzelm parents: 
76804diff
changeset | 203 | Context.theory_tracing := Options.default_bool "context_theory_tracing"; | 
| 
dfa44d85d751
proper system options to control context tracing/timing;
 wenzelm parents: 
76804diff
changeset | 204 | Context.proof_tracing := Options.default_bool "context_proof_tracing"; | 
| 80073 
40f5ddeda2b4
further performance tuning (after f906f7f83dae): interactive mode is closer to earlier approach with Lazy.value, which could be relevant with rather complex grammars under tight memory situations;
 wenzelm parents: 
78758diff
changeset | 205 | Context.data_timing := Options.default_bool "context_data_timing"; | 
| 
40f5ddeda2b4
further performance tuning (after f906f7f83dae): interactive mode is closer to earlier approach with Lazy.value, which could be relevant with rather complex grammars under tight memory situations;
 wenzelm parents: 
78758diff
changeset | 206 | Syntax.cache_persistent := false); | 
| 62563 
2e352f63d15f
separate Isabelle_Process.init_options after Options.load_defaults, notably for "isabelle console";
 wenzelm parents: 
62505diff
changeset | 207 | |
| 65300 | 208 | fun init_options_interactive () = | 
| 209 | (init_options (); | |
| 68025 | 210 | Multithreading.parallel_proofs := (if Options.default_int "parallel_proofs" > 0 then 3 else 0); | 
| 80073 
40f5ddeda2b4
further performance tuning (after f906f7f83dae): interactive mode is closer to earlier approach with Lazy.value, which could be relevant with rather complex grammars under tight memory situations;
 wenzelm parents: 
78758diff
changeset | 211 | Printer.show_markup_default := true; | 
| 
40f5ddeda2b4
further performance tuning (after f906f7f83dae): interactive mode is closer to earlier approach with Lazy.value, which could be relevant with rather complex grammars under tight memory situations;
 wenzelm parents: 
78758diff
changeset | 212 | Syntax.cache_persistent := true); | 
| 62599 
f35858c831e5
clarified session build options: already provided by ML_Process;
 wenzelm parents: 
62564diff
changeset | 213 | |
| 69572 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 214 | |
| 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 215 | (* generic init *) | 
| 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 216 | |
| 71644 | 217 | fun init_modes modes = | 
| 69572 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 218 | let | 
| 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 219 | 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: 
69449diff
changeset | 220 | 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: 
69449diff
changeset | 221 | in | 
| 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 222 | if address <> "" andalso password <> "" | 
| 71644 | 223 | then init_protocol modes (address, password) | 
| 69572 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 224 | else init_options () | 
| 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 225 | end; | 
| 
09a6a7c04b45
more robust system channel via options that are private to the user;
 wenzelm parents: 
69449diff
changeset | 226 | |
| 71644 | 227 | fun init () = init_modes (protocol_modes1, protocol_modes2); | 
| 228 | fun init_build () = init_modes ([], protocol_modes2); | |
| 229 | ||
| 25528 
e67230c2b952
Isabelle process wrapper -- interaction via external program.
 wenzelm parents: diff
changeset | 230 | end; |