src/Pure/PIDE/prover.scala
author wenzelm
Sun, 19 May 2024 18:43:45 +0200
changeset 80181 aa92c0f96036
parent 77243 629dce95bb5c
child 80357 fe123d033e76
permissions -rw-r--r--
provide scala-3.4.2, but do not activate it: scala-3.3.x is LTS version;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/PIDE/prover.scala
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
57923
cdae2467311d tuned comments;
wenzelm
parents: 57917
diff changeset
     3
    Options:    :folding=explicit:
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     4
57923
cdae2467311d tuned comments;
wenzelm
parents: 57917
diff changeset
     5
Prover process wrapping.
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     6
*/
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     7
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     8
package isabelle
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
     9
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    10
67835
c8e4ee2b5482 tuned imports;
wenzelm
parents: 65345
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    14
object Prover {
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    15
  /* messages */
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    16
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    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
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    19
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    20
  class Input(val name: String, val args: List[String]) extends Message {
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    21
    override def toString: String =
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    22
      XML.Elem(Markup(Markup.PROVER_COMMAND, List((Markup.NAME, name))),
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70666
diff changeset
    23
        args.flatMap(s =>
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70666
diff changeset
    24
          List(XML.newline, XML.elem(Markup.PROVER_ARG, YXML.parse_body(s))))).toString
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    25
  }
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    26
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    27
  class Output(val message: XML.Elem) extends Message {
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    28
    def kind: String = message.markup.name
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    29
    def properties: Properties.T = message.markup.properties
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    30
    def body: XML.Body = message.body
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    31
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    32
    def is_init: Boolean = kind == Markup.INIT
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    33
    def is_exit: Boolean = kind == Markup.EXIT
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    34
    def is_stdout: Boolean = kind == Markup.STDOUT
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    35
    def is_stderr: Boolean = kind == Markup.STDERR
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    36
    def is_system: Boolean = kind == Markup.SYSTEM
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    37
    def is_status: Boolean = kind == Markup.STATUS
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    38
    def is_report: Boolean = kind == Markup.REPORT
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    39
    def is_syslog: Boolean = is_init || is_exit || is_system || is_stderr
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    40
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    41
    override def toString: String = {
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    42
      val res =
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    43
        if (is_status || is_report) message.body.map(_.toString).mkString
71649
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71641
diff changeset
    44
        else Pretty.string_of(message.body, metric = Symbol.Metric)
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    45
      if (properties.isEmpty)
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    46
        kind + " [[" + res + "]]"
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    47
      else
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    48
        kind + " " +
73712
3eba8d4b624b clarified signature;
wenzelm
parents: 73702
diff changeset
    49
          (properties.map(Properties.Eq.apply)).mkString("{", ",", "}") + " [[" + res + "]]"
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    50
    }
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    51
  }
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    52
73560
a578ebf5b78d clarified exceptions;
wenzelm
parents: 73559
diff changeset
    53
  class Malformed(msg: String) extends Exn.User_Error("Malformed prover message: " + msg)
a578ebf5b78d clarified exceptions;
wenzelm
parents: 73559
diff changeset
    54
  def bad_header(print: String): Nothing = throw new Malformed("bad message header\n" + print)
a578ebf5b78d clarified exceptions;
wenzelm
parents: 73559
diff changeset
    55
  def bad_chunks(): Nothing = throw new Malformed("bad message chunks")
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    56
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    57
  def the_chunk(chunks: List[Bytes], print: => String): Bytes =
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    58
    chunks match {
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    59
      case List(chunk) => chunk
73560
a578ebf5b78d clarified exceptions;
wenzelm
parents: 73559
diff changeset
    60
      case _ => throw new Malformed("single chunk expected: " + print)
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    61
    }
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    62
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    63
  class Protocol_Output(props: Properties.T, val chunks: List[Bytes])
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    64
  extends Output(XML.Elem(Markup(Markup.PROTOCOL, props), Nil)) {
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    65
    def chunk: Bytes = the_chunk(chunks, toString)
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
    66
    lazy val text: String = chunk.text
56385
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    67
  }
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    68
}
76acce58aeab more general prover operations;
wenzelm
parents:
diff changeset
    69
56393
22f533e6a049 more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents: 56387
diff changeset
    70
65345
2fdd4431b30e clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents: 65317
diff changeset
    71
class Prover(
62556
c115e69f457f more abstract Session.start, without prover command-line;
wenzelm
parents: 62310
diff changeset
    72
  receiver: Prover.Receiver,
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 71969
diff changeset
    73
  cache: XML.Cache,
65316
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
    74
  channel: System_Channel,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    75
  process: Bash.Process
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    76
) extends Protocol {
57923
cdae2467311d tuned comments;
wenzelm
parents: 57917
diff changeset
    77
  /** receiver output **/
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    78
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    79
  private def system_output(text: String): Unit = {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    80
    receiver(new Prover.Output(XML.Elem(Markup(Markup.SYSTEM, Nil), List(XML.Text(text)))))
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    81
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    82
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    83
  private def protocol_output(props: Properties.T, chunks: List[Bytes]): Unit = {
73562
c5a390b9ae00 clarified cache;
wenzelm
parents: 73561
diff changeset
    84
    receiver(new Prover.Protocol_Output(props, chunks))
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    85
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    86
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    87
  private def output(kind: String, props: Properties.T, body: XML.Body): Unit = {
59713
6da3efec20ca clarified modules;
wenzelm
parents: 59702
diff changeset
    88
    val main = XML.Elem(Markup(kind, props), Protocol_Message.clean_reports(body))
6da3efec20ca clarified modules;
wenzelm
parents: 59702
diff changeset
    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
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    91
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    92
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    93
  private def exit_message(result: Process_Result): Unit = {
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
    94
    output(Markup.EXIT, Markup.Process_Result(result),
71747
1dd514c8c1df clarified signature;
wenzelm
parents: 71692
diff changeset
    95
      List(XML.Text(result.print_return_code)))
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    96
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    97
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
    98
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
    99
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   100
  /** process manager **/
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   101
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   102
  private val process_result: Future[Process_Result] =
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   103
    Future.thread("process_result") {
74141
bba35ad317ab tuned signature;
wenzelm
parents: 74140
diff changeset
   104
      val rc = process.join()
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   105
      val timing = process.get_timing
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   106
      Process_Result(rc, timing = timing)
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   107
    }
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   108
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   109
  private def terminate_process(): Unit = {
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   110
    try { process.terminate() }
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   111
    catch {
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   112
      case exn @ ERROR(_) => system_output("Failed to terminate prover process: " + exn.getMessage)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   113
    }
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   114
  }
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   115
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   119
    val (startup_failed, startup_errors) = {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   120
      var finished: Option[Boolean] = None
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   121
      val result = new StringBuilder(100)
65316
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
   122
      while (finished.isEmpty && (process.stderr.ready || !process_result.is_finished)) {
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
   123
        while (finished.isEmpty && process.stderr.ready) {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   124
          try {
65316
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
   125
            val c = process.stderr.read
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   126
            if (c == 2) finished = Some(true)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   127
            else result += c.toChar
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   128
          }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   129
          catch { case _: IOException => finished = Some(false) }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   130
        }
73702
7202e12cb324 tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents: 73562
diff changeset
   131
        Time.seconds(0.05).sleep()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   132
      }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   133
      (finished.isEmpty || !finished.get, result.toString.trim)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   134
    }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   135
    if (startup_errors != "") system_output(startup_errors)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   136
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   137
    if (startup_failed) {
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   138
      terminate_process()
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   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
629dce95bb5c clarified signature: avoid adhoc constants;
wenzelm
parents: 75393
diff changeset
   141
      exit_message(Process_Result.startup_failure)
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   142
    }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   143
    else {
65316
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
   144
      val (command_stream, message_stream) = channel.rendezvous()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   145
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   146
      command_input_init(command_stream)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   147
      val stderr = physical_output(true)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   148
      val message = message_output(message_stream)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   149
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   150
      val result = process_result.join
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   151
      system_output("process terminated")
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   152
      command_input_close()
74140
8a5e02ef975c clarified signature;
wenzelm
parents: 73712
diff changeset
   153
      for (thread <- List(stdout, stderr, message)) thread.join()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   154
      system_output("process_manager terminated")
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65316
diff changeset
   155
      exit_message(result)
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   156
    }
69572
09a6a7c04b45 more robust system channel via options that are private to the user;
wenzelm
parents: 68805
diff changeset
   157
    channel.shutdown()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   158
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   159
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   160
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   161
  /* management methods */
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   162
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73031
diff changeset
   163
  def join(): Unit = process_manager.join()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   164
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   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
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   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
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   175
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   176
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   177
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   178
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   179
  /** process streams **/
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   180
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   181
  /* command input */
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   182
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   183
  private var command_input: Option[Consumer_Thread[List[Bytes]]] = None
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   184
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   185
  private def command_input_close(): Unit = command_input.foreach(_.shutdown())
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   186
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   187
  private def command_input_init(raw_stream: OutputStream): Unit = {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   188
    val name = "command_input"
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   189
    val stream = new BufferedOutputStream(raw_stream)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   190
    command_input =
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   191
      Some(
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   192
        Consumer_Thread.fork(name)(
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   193
          consume =
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   194
            {
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   195
              case chunks =>
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   196
                try {
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 63584
diff changeset
   197
                  Bytes(chunks.map(_.length).mkString("", ",", "\n")).write_stream(stream)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 63584
diff changeset
   198
                  chunks.foreach(_.write_stream(stream))
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   199
                  stream.flush
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   200
                  true
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   201
                }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   202
                catch { case e: IOException => system_output(name + ": " + e.getMessage); false }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   203
            },
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   204
          finish = { case () => stream.close(); system_output(name + " terminated") }
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   205
        )
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   206
      )
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   207
  }
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   208
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   209
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   210
  /* physical output */
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   211
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   212
  private def physical_output(err: Boolean): Thread = {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   213
    val (name, reader, markup) =
65316
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
   214
      if (err) ("standard_error", process.stderr, Markup.STDERR)
c0fb8405416c simplified signature (despite 448325de6e4f);
wenzelm
parents: 65309
diff changeset
   215
      else ("standard_output", process.stdout, Markup.STDOUT)
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   216
71692
f8e52c0152fe clarified names;
wenzelm
parents: 71685
diff changeset
   217
    Isabelle_Thread.fork(name = name) {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   218
      try {
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   219
        var result = new StringBuilder(100)
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   220
        var finished = false
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   221
        while (!finished) {
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   222
          //{{{
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   223
          var c = -1
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   224
          var done = false
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70666
diff changeset
   225
          while (!done && (result.isEmpty || reader.ready)) {
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   226
            c = reader.read
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   227
            if (c >= 0) result.append(c.asInstanceOf[Char])
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   228
            else done = true
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   229
          }
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70666
diff changeset
   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
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   232
            result.clear()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   233
          }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   234
          else {
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   235
            reader.close()
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   236
            finished = true
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   237
          }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   238
          //}}}
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   239
        }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   240
      }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   241
      catch { case e: IOException => system_output(name + ": " + e.getMessage) }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   242
      system_output(name + " terminated")
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   243
    }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   244
  }
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   245
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   246
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   247
  /* message output */
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   248
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   249
  private def message_output(stream: InputStream): Thread = {
73561
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73560
diff changeset
   250
    def decode_chunk(chunk: Bytes): XML.Body =
73562
c5a390b9ae00 clarified cache;
wenzelm
parents: 73561
diff changeset
   251
      Symbol.decode_yxml_failsafe(chunk.text, cache = cache)
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   252
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   253
    val thread_name = "message_output"
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   254
    Isabelle_Thread.fork(name = thread_name) {
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   255
      try {
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   256
        var finished = false
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   257
        while (!finished) {
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   258
          Byte_Message.read_message(stream) match {
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   259
            case None => finished = true
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   260
            case Some(header :: chunks) =>
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   261
              decode_chunk(header) match {
73562
c5a390b9ae00 clarified cache;
wenzelm
parents: 73561
diff changeset
   262
                case List(XML.Elem(Markup(kind, props), Nil)) =>
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   263
                  if (kind == Markup.PROTOCOL) protocol_output(props, chunks)
73562
c5a390b9ae00 clarified cache;
wenzelm
parents: 73561
diff changeset
   264
                  else output(kind, props, decode_chunk(Prover.the_chunk(chunks, kind)))
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   265
                case _ => Prover.bad_header(header.toString)
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   266
              }
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   267
            case Some(_) => Prover.bad_chunks()
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   268
          }
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   269
        }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   270
      }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   271
      catch {
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   272
        case e: IOException => system_output("Cannot read message:\n" + e.getMessage)
73560
a578ebf5b78d clarified exceptions;
wenzelm
parents: 73559
diff changeset
   273
        case e: Prover.Malformed => system_output(e.getMessage)
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   274
      }
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
   275
      stream.close()
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   276
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73367
diff changeset
   277
      system_output(thread_name + " terminated")
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   278
    }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   279
  }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   280
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   281
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   282
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   283
  /** protocol commands **/
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   284
70666
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   285
  var trace: Boolean = false
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   286
70661
9c4809ec28ef tuned signature;
wenzelm
parents: 70301
diff changeset
   287
  def protocol_command_raw(name: String, args: List[Bytes]): Unit =
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   288
    command_input match {
74253
45dc9de1bd33 tuned signature;
wenzelm
parents: 74141
diff changeset
   289
      case Some(thread) if thread.is_active() =>
70666
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   290
        if (trace) {
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   291
          val payload = args.foldLeft(0) { case (n, b) => n + b.length }
70666
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   292
          Output.writeln(
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   293
            "protocol_command " + name + ", args = " + args.length + ", payload = " + payload)
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   294
        }
e56ec28fc5e8 optional trace output;
wenzelm
parents: 70661
diff changeset
   295
        thread.send(Bytes(name) :: args)
68805
57455c561849 clarified message;
wenzelm
parents: 67835
diff changeset
   296
      case _ => error("Inactive prover input thread for command " + quote(name))
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   297
    }
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   298
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   299
  def protocol_command_args(name: String, args: List[String]): Unit = {
70661
9c4809ec28ef tuned signature;
wenzelm
parents: 70301
diff changeset
   300
    receiver(new Prover.Input(name, args))
9c4809ec28ef tuned signature;
wenzelm
parents: 70301
diff changeset
   301
    protocol_command_raw(name, args.map(Bytes(_)))
57916
2c2c24dbf0a4 generic process wrapping in Prover;
wenzelm
parents: 57915
diff changeset
   302
  }
70661
9c4809ec28ef tuned signature;
wenzelm
parents: 70301
diff changeset
   303
9c4809ec28ef tuned signature;
wenzelm
parents: 70301
diff changeset
   304
  def protocol_command(name: String, args: String*): Unit =
9c4809ec28ef tuned signature;
wenzelm
parents: 70301
diff changeset
   305
    protocol_command_args(name, args.toList)
56387
d92eb5c3960d more general prover operations;
wenzelm
parents: 56386
diff changeset
   306
}