src/Pure/Tools/simplifier_trace.scala
author wenzelm
Tue, 29 Aug 2023 12:53:28 +0200
changeset 78592 fdfe9b91d96e
parent 75440 39011d0d2128
permissions -rw-r--r--
misc tuning: support "scalac -source 3.3";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54730
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/simplifier_trace.scala
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
     2
    Author:     Lars Hupel
54730
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     3
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     4
Interactive Simplifier trace.
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     5
*/
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     6
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     7
package isabelle
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
     8
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
     9
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    10
import scala.annotation.tailrec
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    11
import scala.collection.immutable.SortedMap
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    12
54730
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    14
object Simplifier_Trace {
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    15
  /* trace items from the prover */
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    16
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    17
  val TEXT = "text"
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    18
  val Text = new Properties.String(TEXT)
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    19
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    20
  val PARENT = "parent"
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    21
  val Parent = new Properties.Long(PARENT)
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    22
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    23
  val SUCCESS = "success"
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    24
  val Success = new Properties.Boolean(SUCCESS)
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    25
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    26
  val MEMORY = "memory"
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    27
  val Memory = new Properties.Boolean(MEMORY)
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    28
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    29
  object Item {
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    30
    case class Data(
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    31
      serial: Long, markup: String, text: String,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    32
      parent: Long, props: Properties.T, content: XML.Body
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    33
    ) {
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    34
      def memory: Boolean = Memory.unapply(props) getOrElse true
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    35
    }
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    36
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    37
    def unapply(tree: XML.Tree): Option[(String, Data)] =
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    38
      tree match {
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    39
        case XML.Elem(Markup(Markup.RESULT, Markup.Serial(serial)),
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    40
          List(XML.Elem(Markup(markup, props), content)))
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    41
        if markup.startsWith("simp_trace_") =>  // FIXME proper comparison of string constants
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    42
          (props, props) match {
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    43
            case (Text(text), Parent(parent)) =>
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    44
              Some((markup, Data(serial, markup, text, parent, props, content)))
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    45
            case _ => None
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    46
          }
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    47
        case _ => None
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    48
      }
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
    49
  }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    50
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    51
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    52
  /* replies to the prover */
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    53
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    54
  case class Answer private[Simplifier_Trace](val name: String, val string: String)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    55
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    56
  object Answer {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    57
    object step {
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    58
      val skip = Answer("skip", "Skip")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    59
      val continue = Answer("continue", "Continue")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    60
      val continue_trace = Answer("continue_trace", "Continue (with full trace)")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    61
      val continue_passive = Answer("continue_passive", "Continue (without asking)")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    62
      val continue_disable = Answer("continue_disable", "Continue (without any trace)")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    63
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    64
      val all = List(continue, continue_trace, continue_passive, continue_disable, skip)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    65
    }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    66
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    67
    object hint_fail {
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    68
      val exit = Answer("exit", "Exit")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    69
      val redo = Answer("redo", "Redo")
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    70
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    71
      val all = List(redo, exit)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    72
    }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    73
  }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    74
55555
9c16317c91d1 prefer concrete list append;
wenzelm
parents: 55553
diff changeset
    75
  val all_answers: List[Answer] = Answer.step.all ::: Answer.hint_fail.all
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    76
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    77
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    78
  /* GUI interaction */
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    79
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    80
  case object Event
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    81
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    82
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
    83
  /* manager thread */
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    84
56717
d96b10ec397c tuned whitespace;
wenzelm
parents: 56715
diff changeset
    85
  private case class Handle_Results(
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
    86
    session: Session, id: Document_ID.Command, results: Command.Results, slot: Promise[Context])
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
    87
  private case class Generate_Trace(results: Command.Results, slot: Promise[Trace])
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    88
  private case class Cancel(serial: Long)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    89
  private object Clear_Memory
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    90
  case class Reply(session: Session, serial: Long, answer: Answer)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    91
57593
2f7d91242b99 proper Swing buttons instead of active areas within text (by Lars Hupel);
wenzelm
parents: 56782
diff changeset
    92
  case class Question(data: Item.Data, answers: List[Answer])
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    93
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    94
  case class Context(
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    95
    last_serial: Long = 0L,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    96
    questions: SortedMap[Long, Question] = SortedMap.empty
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
    97
  ) {
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    98
    def +(q: Question): Context =
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
    99
      copy(questions = questions + ((q.data.serial, q)))
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   100
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   101
    def -(s: Long): Context =
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   102
      copy(questions = questions - s)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   103
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   104
    def with_serial(s: Long): Context =
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   105
      copy(last_serial = Math.max(last_serial, s))
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   106
  }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   107
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
   108
  case class Trace(entries: List[Item.Data])
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   109
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   110
  case class Index(text: String, content: XML.Body)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   111
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   112
  object Index {
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
   113
    def of_data(data: Item.Data): Index =
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   114
      Index(data.text, data.content)
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   115
  }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   116
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   117
  def handle_results(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   118
    session: Session,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   119
    id: Document_ID.Command,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   120
    results: Command.Results
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   121
  ): Context = {
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   122
    val slot = Future.promise[Context]
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   123
    the_manager(session).send(Handle_Results(session, id, results, slot))
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   124
    slot.join
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   125
  }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   126
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   127
  def generate_trace(session: Session, results: Command.Results): Trace = {
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   128
    val slot = Future.promise[Trace]
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   129
    the_manager(session).send(Generate_Trace(results, slot))
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   130
    slot.join
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   131
  }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   132
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   133
  def clear_memory(session: Session): Unit =
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   134
    the_manager(session).send(Clear_Memory)
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   135
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   136
  def send_reply(session: Session, serial: Long, answer: Answer): Unit =
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   137
    the_manager(session).send(Reply(session, serial, answer))
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   138
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   139
  def make_manager: Consumer_Thread[Any] = {
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   140
    var contexts = Map.empty[Document_ID.Command, Context]
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   141
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   142
    var memory_children = Map.empty[Long, Set[Long]]
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   143
    var memory = Map.empty[Index, Answer]
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   144
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   145
    def find_question(serial: Long): Option[(Document_ID.Command, Question)] =
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   146
      contexts collectFirst {
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   147
        case (id, context) if context.questions contains serial =>
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   148
          (id, context.questions(serial))
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   149
      }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   150
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   151
    def do_cancel(serial: Long, id: Document_ID.Command): Unit = {
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   152
      // To save memory, we could try to remove empty contexts at this point.
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   153
      // However, if a new serial gets attached to the same command_id after we deleted
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   154
      // its context, its last_serial counter will start at 0 again, and we'll think the
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   155
      // old serials are actually new
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   156
      contexts += (id -> (contexts(id) - serial))
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   157
    }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   158
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   159
    def do_reply(session: Session, serial: Long, answer: Answer): Unit = {
56717
d96b10ec397c tuned whitespace;
wenzelm
parents: 56715
diff changeset
   160
      session.protocol_command(
63805
c272680df665 clarified modules;
wenzelm
parents: 57593
diff changeset
   161
        "Simplifier_Trace.reply", Value.Long(serial), answer.name)
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   162
    }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   163
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   164
    Consumer_Thread.fork[Any]("Simplifier_Trace.manager", daemon = true)(
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   165
      consume = { (arg: Any) =>
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   166
        arg match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   167
          case Handle_Results(session, id, results, slot) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   168
            var new_context = contexts.getOrElse(id, Context())
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   169
            var new_serial = new_context.last_serial
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   170
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   171
            for ((serial, result) <- results.iterator if serial > new_context.last_serial) {
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   172
              result match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   173
                case Item(markup, data) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   174
                  memory_children +=
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   175
                    (data.parent -> (memory_children.getOrElse(data.parent, Set.empty) + serial))
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   176
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   177
                  markup match {
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   178
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   179
                    case Markup.SIMP_TRACE_STEP =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   180
                      val index = Index.of_data(data)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   181
                      memory.get(index) match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   182
                        case Some(answer) if data.memory =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   183
                          do_reply(session, serial, answer)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   184
                        case _ =>
57593
2f7d91242b99 proper Swing buttons instead of active areas within text (by Lars Hupel);
wenzelm
parents: 56782
diff changeset
   185
                          new_context += Question(data, Answer.step.all)
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   186
                      }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   187
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   188
                    case Markup.SIMP_TRACE_HINT =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   189
                      data.props match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   190
                        case Success(false) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   191
                          results.get(data.parent) match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   192
                            case Some(Item(Markup.SIMP_TRACE_STEP, _)) =>
57593
2f7d91242b99 proper Swing buttons instead of active areas within text (by Lars Hupel);
wenzelm
parents: 56782
diff changeset
   193
                              new_context += Question(data, Answer.hint_fail.all)
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   194
                            case _ =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   195
                              // unknown, better send a default reply
57593
2f7d91242b99 proper Swing buttons instead of active areas within text (by Lars Hupel);
wenzelm
parents: 56782
diff changeset
   196
                              do_reply(session, data.serial, Answer.hint_fail.exit)
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   197
                          }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   198
                        case _ =>
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   199
                      }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   200
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   201
                    case Markup.SIMP_TRACE_IGNORE =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   202
                      // At this point, we know that the parent of this 'IGNORE' entry is a 'STEP'
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   203
                      // entry, and that that 'STEP' entry is about to be replayed. Hence, we need
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   204
                      // to selectively purge the replies which have been memorized, going down from
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   205
                      // the parent to all leaves.
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   206
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   207
                      @tailrec
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   208
                      def purge(queue: Vector[Long]): Unit =
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   209
                        queue match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   210
                          case s +: rest =>
78592
fdfe9b91d96e misc tuning: support "scalac -source 3.3";
wenzelm
parents: 75440
diff changeset
   211
                            for (case Item(Markup.SIMP_TRACE_STEP, data) <- results.get(s))
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   212
                              memory -= Index.of_data(data)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   213
                            val children = memory_children.getOrElse(s, Set.empty)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   214
                            memory_children -= s
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   215
                            purge(rest ++ children.toVector)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   216
                          case _ =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   217
                        }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   218
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   219
                      purge(Vector(data.parent))
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   220
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   221
                    case _ =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   222
                  }
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   223
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   224
                case _ =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   225
              }
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   226
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   227
              new_serial = serial
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   228
            }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   229
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   230
            new_context = new_context.with_serial(new_serial)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   231
            contexts += (id -> new_context)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   232
            slot.fulfill(new_context)
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   233
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   234
          case Generate_Trace(results, slot) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   235
            // Since there are potentially lots of trace messages, we do not cache them here again.
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   236
            // Instead, everytime the trace is being requested, we re-assemble it based on the
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   237
            // current results.
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   238
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   239
            val items =
57593
2f7d91242b99 proper Swing buttons instead of active areas within text (by Lars Hupel);
wenzelm
parents: 56782
diff changeset
   240
              results.iterator.collect { case (_, Item(_, data)) => data }.toList
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   241
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   242
            slot.fulfill(Trace(items))
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   243
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   244
          case Cancel(serial) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   245
            find_question(serial) match {
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   246
              case Some((id, _)) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   247
                do_cancel(serial, id)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   248
              case None =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   249
            }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   250
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   251
          case Clear_Memory =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   252
            memory_children = Map.empty
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   253
            memory = Map.empty
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   254
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   255
          case Reply(session, serial, answer) =>
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   256
            find_question(serial) match {
57593
2f7d91242b99 proper Swing buttons instead of active areas within text (by Lars Hupel);
wenzelm
parents: 56782
diff changeset
   257
              case Some((id, Question(data, _))) =>
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   258
                if (data.markup == Markup.SIMP_TRACE_STEP && data.memory) {
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   259
                  val index = Index.of_data(data)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   260
                  memory += (index -> answer)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   261
                }
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   262
                do_cancel(serial, id)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   263
              case None =>
56782
433cf57550fa more systematic Isabelle output, like in classic Isabelle/ML (without markup);
wenzelm
parents: 56718
diff changeset
   264
                Output.warning("send_reply: unknown serial " + serial)
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   265
            }
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   266
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   267
            do_reply(session, serial, answer)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   268
            session.trace_events.post(Event)
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   269
        }
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   270
        true
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   271
      },
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   272
      finish = () => contexts = Map.empty
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   273
    )
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   274
  }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   275
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   276
  private val managers = Synchronized(Map.empty[Session, Consumer_Thread[Any]])
66461
0b55fbc51f76 reinit the manager thread, e.g. after restart of the Isabelle/jEdit plugin;
wenzelm
parents: 65220
diff changeset
   277
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   278
  def the_manager(session: Session): Consumer_Thread[Any] =
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   279
    managers.value.get(session) match {
74253
45dc9de1bd33 tuned signature;
wenzelm
parents: 73340
diff changeset
   280
      case Some(thread) if thread.is_active() => thread
66461
0b55fbc51f76 reinit the manager thread, e.g. after restart of the Isabelle/jEdit plugin;
wenzelm
parents: 65220
diff changeset
   281
      case _ => error("Bad Simplifier_Trace.manager thread")
0b55fbc51f76 reinit the manager thread, e.g. after restart of the Isabelle/jEdit plugin;
wenzelm
parents: 65220
diff changeset
   282
    }
0b55fbc51f76 reinit the manager thread, e.g. after restart of the Isabelle/jEdit plugin;
wenzelm
parents: 65220
diff changeset
   283
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   284
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   285
  /* protocol handler */
54730
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
   286
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   287
  class Handler extends Session.Protocol_Handler {
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   288
    private var the_session: Session = null
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   289
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   290
    override def init(session: Session): Unit = {
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   291
      try { the_manager(session) }
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   292
      catch { case ERROR(_) => managers.change(map => map + (session -> make_manager)) }
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   293
      the_session = session
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   294
    }
56718
096139bcfadd replaced manager Actor by Consumer_Thread, which is lazy to defer its start to actual Handler init time;
wenzelm
parents: 56717
diff changeset
   295
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74253
diff changeset
   296
    override def exit(): Unit = {
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   297
      val session = the_session
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   298
      if (session != null) {
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   299
        val manager = the_manager(session)
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   300
        manager.send(Clear_Memory)
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   301
        manager.shutdown()
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   302
        managers.change(map => map - session)
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   303
        the_session = null
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   304
      }
65220
wenzelm
parents: 65219
diff changeset
   305
    }
wenzelm
parents: 65219
diff changeset
   306
65219
ed4b47b8c7dc misc tuning and simplification;
wenzelm
parents: 63805
diff changeset
   307
    private def cancel(msg: Prover.Protocol_Output): Boolean =
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   308
      msg.properties match {
55553
99409ccbe04a more standard names for protocol and markup elements;
wenzelm
parents: 55390
diff changeset
   309
        case Markup.Simp_Trace_Cancel(serial) =>
71650
95ab607398bd support multiple sessions, notably for "isabelle build -P -j2";
wenzelm
parents: 71610
diff changeset
   310
          the_manager(the_session).send(Cancel(serial))
55316
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   311
          true
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   312
        case _ =>
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   313
          false
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   314
      }
885500f4aa6a interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
Lars Hupel <lars.hupel@mytum.de>
parents: 54730
diff changeset
   315
75440
39011d0d2128 tuned signature;
wenzelm
parents: 75394
diff changeset
   316
    override val functions: Session.Protocol_Functions =
39011d0d2128 tuned signature;
wenzelm
parents: 75394
diff changeset
   317
      List(Markup.SIMP_TRACE_CANCEL -> cancel)
54730
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
   318
  }
de2d99b459b3 skeleton for Simplifier trace by Lars Hupel;
wenzelm
parents:
diff changeset
   319
}