| author | wenzelm |
| Mon, 27 Oct 2025 15:16:32 +0100 | |
| changeset 83417 | b51e4a526897 |
| parent 83203 | 61277d1550d6 |
| permissions | -rw-r--r-- |
| 56385 | 1 |
/* Title: Pure/PIDE/prover.scala |
2 |
Author: Makarius |
|
| 57923 | 3 |
Options: :folding=explicit: |
| 56385 | 4 |
|
| 57923 | 5 |
Prover process wrapping. |
| 56385 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
| 67835 | 11 |
import java.io.{InputStream, OutputStream, BufferedOutputStream, IOException}
|
|
57915
448325de6e4f
more abstract Prover.System_Process, which allows to bypass Isabelle_System.Managed_Process;
wenzelm
parents:
57906
diff
changeset
|
12 |
|
|
448325de6e4f
more abstract Prover.System_Process, which allows to bypass Isabelle_System.Managed_Process;
wenzelm
parents:
57906
diff
changeset
|
13 |
|
| 75393 | 14 |
object Prover {
|
| 56385 | 15 |
/* messages */ |
16 |
||
17 |
sealed abstract class Message |
|
|
62556
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62310
diff
changeset
|
18 |
type Receiver = Message => Unit |
| 56385 | 19 |
|
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
20 |
class Input(val name: String, val args: List[XML.Body]) extends Message {
|
| 56385 | 21 |
override def toString: String = |
22 |
XML.Elem(Markup(Markup.PROVER_COMMAND, List((Markup.NAME, name))), |
|
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
23 |
args.flatMap(arg => List(XML.newline, XML.elem(Markup.PROVER_ARG, arg)))).toString |
| 56385 | 24 |
} |
25 |
||
| 75393 | 26 |
class Output(val message: XML.Elem) extends Message {
|
| 56385 | 27 |
def kind: String = message.markup.name |
28 |
def properties: Properties.T = message.markup.properties |
|
29 |
def body: XML.Body = message.body |
|
30 |
||
| 71601 | 31 |
def is_init: Boolean = kind == Markup.INIT |
32 |
def is_exit: Boolean = kind == Markup.EXIT |
|
33 |
def is_stdout: Boolean = kind == Markup.STDOUT |
|
34 |
def is_stderr: Boolean = kind == Markup.STDERR |
|
35 |
def is_system: Boolean = kind == Markup.SYSTEM |
|
36 |
def is_status: Boolean = kind == Markup.STATUS |
|
37 |
def is_report: Boolean = kind == Markup.REPORT |
|
38 |
def is_syslog: Boolean = is_init || is_exit || is_system || is_stderr |
|
| 56385 | 39 |
|
| 75393 | 40 |
override def toString: String = {
|
| 56385 | 41 |
val res = |
42 |
if (is_status || is_report) message.body.map(_.toString).mkString |
|
| 71649 | 43 |
else Pretty.string_of(message.body, metric = Symbol.Metric) |
| 56385 | 44 |
if (properties.isEmpty) |
| 73559 | 45 |
kind + " [[" + res + "]]" |
| 56385 | 46 |
else |
| 73559 | 47 |
kind + " " + |
| 73712 | 48 |
(properties.map(Properties.Eq.apply)).mkString("{", ",", "}") + " [[" + res + "]]"
|
| 56385 | 49 |
} |
50 |
} |
|
51 |
||
| 73560 | 52 |
class Malformed(msg: String) extends Exn.User_Error("Malformed prover message: " + msg)
|
53 |
def bad_header(print: String): Nothing = throw new Malformed("bad message header\n" + print)
|
|
54 |
def bad_chunks(): Nothing = throw new Malformed("bad message chunks")
|
|
| 73559 | 55 |
|
56 |
def the_chunk(chunks: List[Bytes], print: => String): Bytes = |
|
57 |
chunks match {
|
|
58 |
case List(chunk) => chunk |
|
| 73560 | 59 |
case _ => throw new Malformed("single chunk expected: " + print)
|
| 73559 | 60 |
} |
61 |
||
| 82794 | 62 |
class System_Output(text: String) extends |
63 |
Output(XML.Elem(Markup(Markup.SYSTEM, Nil), List(XML.Text(text)))) |
|
64 |
||
| 73559 | 65 |
class Protocol_Output(props: Properties.T, val chunks: List[Bytes]) |
| 83203 | 66 |
extends Output(XML.elem(Markup(Markup.PROTOCOL, props))) {
|
| 73559 | 67 |
def chunk: Bytes = the_chunk(chunks, toString) |
68 |
lazy val text: String = chunk.text |
|
| 56385 | 69 |
} |
70 |
} |
|
71 |
||
|
56393
22f533e6a049
more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents:
56387
diff
changeset
|
72 |
|
|
65345
2fdd4431b30e
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65317
diff
changeset
|
73 |
class Prover( |
|
62556
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62310
diff
changeset
|
74 |
receiver: Prover.Receiver, |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
71969
diff
changeset
|
75 |
cache: XML.Cache, |
| 65316 | 76 |
channel: System_Channel, |
| 75393 | 77 |
process: Bash.Process |
78 |
) extends Protocol {
|
|
| 57923 | 79 |
/** receiver output **/ |
| 57916 | 80 |
|
| 82794 | 81 |
private def system_output(text: String): Unit = |
82 |
receiver(new Prover.System_Output(text)) |
|
| 57916 | 83 |
|
| 82794 | 84 |
private def protocol_output(props: Properties.T, chunks: List[Bytes]): Unit = |
| 73562 | 85 |
receiver(new Prover.Protocol_Output(props, chunks)) |
| 57916 | 86 |
|
| 75393 | 87 |
private def output(kind: String, props: Properties.T, body: XML.Body): Unit = {
|
| 59713 | 88 |
val main = XML.Elem(Markup(kind, props), Protocol_Message.clean_reports(body)) |
89 |
val reports = Protocol_Message.reports(props, body) |
|
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
71969
diff
changeset
|
90 |
for (msg <- main :: reports) receiver(new Prover.Output(cache.elem(msg))) |
| 57916 | 91 |
} |
92 |
||
| 75393 | 93 |
private def exit_message(result: Process_Result): Unit = {
|
| 65317 | 94 |
output(Markup.EXIT, Markup.Process_Result(result), |
| 71747 | 95 |
List(XML.Text(result.print_return_code))) |
| 57916 | 96 |
} |
97 |
||
98 |
||
| 56387 | 99 |
|
| 57916 | 100 |
/** process manager **/ |
101 |
||
| 65317 | 102 |
private val process_result: Future[Process_Result] = |
103 |
Future.thread("process_result") {
|
|
| 74141 | 104 |
val rc = process.join() |
| 65317 | 105 |
val timing = process.get_timing |
106 |
Process_Result(rc, timing = timing) |
|
107 |
} |
|
| 57916 | 108 |
|
| 75393 | 109 |
private def terminate_process(): Unit = {
|
| 73367 | 110 |
try { process.terminate() }
|
| 57916 | 111 |
catch {
|
112 |
case exn @ ERROR(_) => system_output("Failed to terminate prover process: " + exn.getMessage)
|
|
113 |
} |
|
| 56387 | 114 |
} |
115 |
||
| 75393 | 116 |
private val process_manager = Isabelle_Thread.fork(name = "process_manager") {
|
|
71641
c1409b9c2b22
proper startup for Pure: its use_prelude produces stdout before stderr protocol init;
wenzelm
parents:
71601
diff
changeset
|
117 |
val stdout = physical_output(false) |
|
c1409b9c2b22
proper startup for Pure: its use_prelude produces stdout before stderr protocol init;
wenzelm
parents:
71601
diff
changeset
|
118 |
|
| 75393 | 119 |
val (startup_failed, startup_errors) = {
|
| 57916 | 120 |
var finished: Option[Boolean] = None |
121 |
val result = new StringBuilder(100) |
|
| 65316 | 122 |
while (finished.isEmpty && (process.stderr.ready || !process_result.is_finished)) {
|
123 |
while (finished.isEmpty && process.stderr.ready) {
|
|
| 57916 | 124 |
try {
|
| 65316 | 125 |
val c = process.stderr.read |
| 57916 | 126 |
if (c == 2) finished = Some(true) |
127 |
else result += c.toChar |
|
128 |
} |
|
129 |
catch { case _: IOException => finished = Some(false) }
|
|
130 |
} |
|
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73562
diff
changeset
|
131 |
Time.seconds(0.05).sleep() |
| 57916 | 132 |
} |
133 |
(finished.isEmpty || !finished.get, result.toString.trim) |
|
134 |
} |
|
135 |
if (startup_errors != "") system_output(startup_errors) |
|
136 |
||
137 |
if (startup_failed) {
|
|
138 |
terminate_process() |
|
139 |
process_result.join |
|
|
71641
c1409b9c2b22
proper startup for Pure: its use_prelude produces stdout before stderr protocol init;
wenzelm
parents:
71601
diff
changeset
|
140 |
stdout.join |
| 77243 | 141 |
exit_message(Process_Result.startup_failure) |
| 57916 | 142 |
} |
143 |
else {
|
|
| 65316 | 144 |
val (command_stream, message_stream) = channel.rendezvous() |
| 57916 | 145 |
|
146 |
command_input_init(command_stream) |
|
147 |
val stderr = physical_output(true) |
|
148 |
val message = message_output(message_stream) |
|
149 |
||
| 65317 | 150 |
val result = process_result.join |
| 57916 | 151 |
system_output("process terminated")
|
152 |
command_input_close() |
|
| 74140 | 153 |
for (thread <- List(stdout, stderr, message)) thread.join() |
| 57916 | 154 |
system_output("process_manager terminated")
|
| 65317 | 155 |
exit_message(result) |
| 57916 | 156 |
} |
|
69572
09a6a7c04b45
more robust system channel via options that are private to the user;
wenzelm
parents:
68805
diff
changeset
|
157 |
channel.shutdown() |
| 57916 | 158 |
} |
159 |
||
160 |
||
161 |
/* management methods */ |
|
162 |
||
| 73340 | 163 |
def join(): Unit = process_manager.join() |
| 57916 | 164 |
|
| 75393 | 165 |
def terminate(): Unit = {
|
|
62310
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
166 |
system_output("Terminating prover process")
|
| 57916 | 167 |
command_input_close() |
|
62310
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
168 |
|
|
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
169 |
var count = 10 |
|
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
170 |
while (!process_result.is_finished && count > 0) {
|
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73562
diff
changeset
|
171 |
Time.seconds(0.1).sleep() |
|
62310
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
172 |
count -= 1 |
|
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
173 |
} |
|
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
174 |
if (!process_result.is_finished) terminate_process() |
| 57916 | 175 |
} |
176 |
||
177 |
||
178 |
||
179 |
/** process streams **/ |
|
180 |
||
181 |
/* command input */ |
|
182 |
||
183 |
private var command_input: Option[Consumer_Thread[List[Bytes]]] = None |
|
184 |
||
| 73367 | 185 |
private def command_input_close(): Unit = command_input.foreach(_.shutdown()) |
| 57916 | 186 |
|
| 75393 | 187 |
private def command_input_init(raw_stream: OutputStream): Unit = {
|
| 57916 | 188 |
val name = "command_input" |
189 |
val stream = new BufferedOutputStream(raw_stream) |
|
190 |
command_input = |
|
191 |
Some( |
|
192 |
Consumer_Thread.fork(name)( |
|
193 |
consume = |
|
194 |
{
|
|
195 |
case chunks => |
|
196 |
try {
|
|
|
80357
fe123d033e76
clarified signature: pro-forma support for Bytes with size: Long;
wenzelm
parents:
77243
diff
changeset
|
197 |
Bytes(chunks.map(_.size).mkString("", ",", "\n")).write_stream(stream)
|
| 64004 | 198 |
chunks.foreach(_.write_stream(stream)) |
| 57916 | 199 |
stream.flush |
200 |
true |
|
201 |
} |
|
202 |
catch { case e: IOException => system_output(name + ": " + e.getMessage); false }
|
|
203 |
}, |
|
| 73367 | 204 |
finish = { case () => stream.close(); system_output(name + " terminated") }
|
| 57916 | 205 |
) |
206 |
) |
|
207 |
} |
|
| 56387 | 208 |
|
209 |
||
| 57916 | 210 |
/* physical output */ |
211 |
||
| 75393 | 212 |
private def physical_output(err: Boolean): Thread = {
|
| 57916 | 213 |
val (name, reader, markup) = |
| 65316 | 214 |
if (err) ("standard_error", process.stderr, Markup.STDERR)
|
215 |
else ("standard_output", process.stdout, Markup.STDOUT)
|
|
| 56387 | 216 |
|
| 71692 | 217 |
Isabelle_Thread.fork(name = name) {
|
| 57916 | 218 |
try {
|
| 82144 | 219 |
val result = new StringBuilder(100) |
| 57916 | 220 |
var finished = false |
221 |
while (!finished) {
|
|
222 |
//{{{
|
|
223 |
var c = -1 |
|
224 |
var done = false |
|
| 71383 | 225 |
while (!done && (result.isEmpty || reader.ready)) {
|
| 57916 | 226 |
c = reader.read |
227 |
if (c >= 0) result.append(c.asInstanceOf[Char]) |
|
228 |
else done = true |
|
229 |
} |
|
| 71383 | 230 |
if (result.nonEmpty) {
|
|
65345
2fdd4431b30e
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65317
diff
changeset
|
231 |
output(markup, Nil, List(XML.Text(Symbol.decode(result.toString)))) |
| 73367 | 232 |
result.clear() |
| 57916 | 233 |
} |
234 |
else {
|
|
| 73367 | 235 |
reader.close() |
| 57916 | 236 |
finished = true |
237 |
} |
|
238 |
//}}} |
|
239 |
} |
|
240 |
} |
|
241 |
catch { case e: IOException => system_output(name + ": " + e.getMessage) }
|
|
242 |
system_output(name + " terminated") |
|
243 |
} |
|
244 |
} |
|
| 56387 | 245 |
|
246 |
||
| 57916 | 247 |
/* message output */ |
248 |
||
| 75393 | 249 |
private def message_output(stream: InputStream): Thread = {
|
|
80505
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
250 |
def decode_prop(bytes: Bytes): Properties.Entry = {
|
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
251 |
val (a, b) = Properties.Eq.parse(bytes.text) |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
252 |
(Symbol.decode(a), Symbol.decode(b)) |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
253 |
} |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
254 |
|
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
255 |
def decode_xml(bytes: Bytes): XML.Body = |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
256 |
Symbol.decode_yxml_failsafe(bytes.text, cache = cache) |
| 56387 | 257 |
|
| 73559 | 258 |
val thread_name = "message_output" |
259 |
Isabelle_Thread.fork(name = thread_name) {
|
|
260 |
try {
|
|
261 |
var finished = false |
|
262 |
while (!finished) {
|
|
263 |
Byte_Message.read_message(stream) match {
|
|
264 |
case None => finished = true |
|
|
80505
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
265 |
case Some(k :: Value.Nat(props_length) :: rest) => |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
266 |
val kind = k.text |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
267 |
val props = rest.take(props_length).map(decode_prop) |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
268 |
val chunks = rest.drop(props_length) |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
269 |
if (kind == Markup.PROTOCOL) protocol_output(props, chunks) |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
270 |
else output(kind, props, chunks.flatMap(decode_xml)) |
| 73559 | 271 |
case Some(_) => Prover.bad_chunks() |
272 |
} |
|
| 57916 | 273 |
} |
274 |
} |
|
275 |
catch {
|
|
276 |
case e: IOException => system_output("Cannot read message:\n" + e.getMessage)
|
|
| 73560 | 277 |
case e: Prover.Malformed => system_output(e.getMessage) |
| 57916 | 278 |
} |
| 73367 | 279 |
stream.close() |
| 56387 | 280 |
|
| 73559 | 281 |
system_output(thread_name + " terminated") |
| 57916 | 282 |
} |
283 |
} |
|
284 |
||
285 |
||
286 |
||
287 |
/** protocol commands **/ |
|
288 |
||
| 70666 | 289 |
var trace: Boolean = false |
290 |
||
| 70661 | 291 |
def protocol_command_raw(name: String, args: List[Bytes]): Unit = |
| 57916 | 292 |
command_input match {
|
| 74253 | 293 |
case Some(thread) if thread.is_active() => |
| 70666 | 294 |
if (trace) {
|
|
80357
fe123d033e76
clarified signature: pro-forma support for Bytes with size: Long;
wenzelm
parents:
77243
diff
changeset
|
295 |
val payload = args.foldLeft(0L) { case (n, b) => n + b.size }
|
| 70666 | 296 |
Output.writeln( |
297 |
"protocol_command " + name + ", args = " + args.length + ", payload = " + payload) |
|
298 |
} |
|
299 |
thread.send(Bytes(name) :: args) |
|
| 68805 | 300 |
case _ => error("Inactive prover input thread for command " + quote(name))
|
| 57916 | 301 |
} |
302 |
||
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
303 |
def protocol_command_args(name: String, args: List[XML.Body]): Unit = {
|
| 70661 | 304 |
receiver(new Prover.Input(name, args)) |
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
305 |
protocol_command_raw(name, args.map(arg => Bytes(Symbol.encode_yxml(arg)))) |
| 57916 | 306 |
} |
| 70661 | 307 |
|
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
308 |
def protocol_command(name: String, args: XML.Body*): Unit = |
| 70661 | 309 |
protocol_command_args(name, args.toList) |
| 56387 | 310 |
} |