src/Tools/jEdit/src/sledgehammer_dockable.scala
author wenzelm
Fri, 15 Nov 2024 13:31:36 +0100
changeset 81448 9b2e13b3ee43
parent 81398 f92ea68473f2
child 81492 480dffe5741f
permissions -rw-r--r--
more rebust mechanics of refresh (see also 82110cbcf9a1 and 2d9b6e32632d): painter.getFontRenderContext might be in undefined state (notably on macOS due to display scaling);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/sledgehammer_dockable.scala
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     3
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     4
Dockable window for Sledgehammer.
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     5
*/
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     6
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     8
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
     9
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    10
import isabelle._
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    11
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    12
import scala.swing.{Component, Label}
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    13
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    14
import java.awt.BorderLayout
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    15
import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent}
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    16
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    17
import org.gjt.sp.jedit.View
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    18
import org.gjt.sp.jedit.gui.HistoryTextField
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    19
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    20
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    21
class Sledgehammer_Dockable(view: View, position: String) extends Dockable(view, position) {
61207
wenzelm
parents: 60861
diff changeset
    22
  GUI_Thread.require {}
wenzelm
parents: 60861
diff changeset
    23
wenzelm
parents: 60861
diff changeset
    24
wenzelm
parents: 60861
diff changeset
    25
  /* text area */
wenzelm
parents: 60861
diff changeset
    26
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    27
  val pretty_text_area = new Pretty_Text_Area(view)
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    28
  set_content(pretty_text_area)
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    29
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71525
diff changeset
    30
  override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation
56918
a442dc6d244d clarified detach_operation: ignore empty output;
wenzelm
parents: 56715
diff changeset
    31
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    32
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    33
  /* query operation */
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    34
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    35
  private val process_indicator = new Process_Indicator
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    36
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 76610
diff changeset
    37
  private def consume_status(status: Query_Operation.Status): Unit = {
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    38
    status match {
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 76610
diff changeset
    39
      case Query_Operation.Status.waiting =>
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    40
        process_indicator.update("Waiting for evaluation of context ...", 5)
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 76610
diff changeset
    41
      case Query_Operation.Status.running =>
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    42
        process_indicator.update("Sledgehammering ...", 15)
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 76610
diff changeset
    43
      case Query_Operation.Status.finished =>
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    44
        process_indicator.update(null, 0)
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    45
    }
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    46
  }
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52933
diff changeset
    47
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    48
  private val sledgehammer =
81398
f92ea68473f2 clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents: 81394
diff changeset
    49
    new Query_Operation(PIDE.editor, view, "sledgehammer", consume_status, pretty_text_area.update)
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    50
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    51
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    52
  /* resize */
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    53
61207
wenzelm
parents: 60861
diff changeset
    54
  private val delay_resize =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 75854
diff changeset
    55
    Delay.first(PIDE.session.update_delay, gui = true) { handle_resize() }
61207
wenzelm
parents: 60861
diff changeset
    56
wenzelm
parents: 60861
diff changeset
    57
  addComponentListener(new ComponentAdapter {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    58
    override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke()
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
    59
    override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke()
61207
wenzelm
parents: 60861
diff changeset
    60
  })
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    61
81387
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 78595
diff changeset
    62
  private def handle_resize(): Unit = pretty_text_area.zoom()
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    63
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    64
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    65
  /* controls */
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    66
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    67
  private def hammer(): Unit = {
60861
fa77faa87d5f maintain history more often;
wenzelm
parents: 60750
diff changeset
    68
    provers.addCurrentToHistory()
56623
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    69
    PIDE.options.string("sledgehammer_provers") = provers.getText
60202
a95023a21725 added checkbox for try0;
wenzelm
parents: 57612
diff changeset
    70
    sledgehammer.apply_query(
a95023a21725 added checkbox for try0;
wenzelm
parents: 57612
diff changeset
    71
      List(provers.getText, isar_proofs.selected.toString, try0.selected.toString))
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    72
  }
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    73
52939
3b549ee12623 tuned GUI;
wenzelm
parents: 52935
diff changeset
    74
  private val provers_label = new Label("Provers:") {
54367
e358b79b533a tuned tooltips;
wenzelm
parents: 53872
diff changeset
    75
    tooltip =
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 55825
diff changeset
    76
      GUI.tooltip_lines(
60278
2a9bc6447779 clarified tooltip;
wenzelm
parents: 60202
diff changeset
    77
        "Automatic provers as space-separated list, e.g.\n" +
2a9bc6447779 clarified tooltip;
wenzelm
parents: 60202
diff changeset
    78
          PIDE.options.value.check_name("sledgehammer_provers").default_value)
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    79
  }
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    80
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    81
  private val provers = new HistoryTextField("isabelle-sledgehammer-provers") {
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    82
    override def processKeyEvent(evt: KeyEvent): Unit = {
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
    83
      if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) hammer()
52933
08bbd321ac4c more active "provers" field, which increases chances that its history is stored;
wenzelm
parents: 52931
diff changeset
    84
      super.processKeyEvent(evt)
08bbd321ac4c more active "provers" field, which increases chances that its history is stored;
wenzelm
parents: 52931
diff changeset
    85
    }
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    86
    setToolTipText(provers_label.tooltip)
53055
0fe8a9972eda some protocol to determine provers according to ML;
wenzelm
parents: 53049
diff changeset
    87
    setColumns(30)
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    88
  }
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
    89
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    90
  private def update_provers(): Unit = {
56623
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    91
    val new_provers = PIDE.options.string("sledgehammer_provers")
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    92
    if (new_provers != provers.getText) {
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    93
      provers.setText(new_provers)
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    94
      if (provers.getCaret != null)
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    95
        provers.getCaret.setDot(0)
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    96
    }
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    97
  }
4675df68450e more elementary option sledgehammer_provers, avoiding complications of defaults from ML side (NB: guessing at number of cores does not make sense in PIDE);
wenzelm
parents: 56622
diff changeset
    98
75854
2163772eeaf2 tuned signature;
wenzelm
parents: 75853
diff changeset
    99
  private val isar_proofs = new GUI.Check("Isar proofs") {
60202
a95023a21725 added checkbox for try0;
wenzelm
parents: 57612
diff changeset
   100
    tooltip = "Specify whether Isar proofs should be output in addition to \"by\" one-liner"
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   101
  }
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   102
75854
2163772eeaf2 tuned signature;
wenzelm
parents: 75853
diff changeset
   103
  private val try0 = new GUI.Check("Try methods", init = true) {
60202
a95023a21725 added checkbox for try0;
wenzelm
parents: 57612
diff changeset
   104
    tooltip = "Try standard proof methods like \"auto\" and \"blast\" as alternatives to \"metis\""
a95023a21725 added checkbox for try0;
wenzelm
parents: 57612
diff changeset
   105
  }
a95023a21725 added checkbox for try0;
wenzelm
parents: 57612
diff changeset
   106
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   107
  private val apply_query = new GUI.Button("<html><b>Apply</b></html>") {
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   108
    tooltip = "Search for first-order proof using automatic theorem provers"
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   109
    override def clicked(): Unit = hammer()
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   110
  }
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   111
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   112
  private val cancel_query = new GUI.Button("Cancel") {
52939
3b549ee12623 tuned GUI;
wenzelm
parents: 52935
diff changeset
   113
    tooltip = "Interrupt unfinished sledgehammering"
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   114
    override def clicked(): Unit = sledgehammer.cancel_query()
52931
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52908
diff changeset
   115
  }
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52908
diff changeset
   116
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   117
  private val locate_query = new GUI.Button("Locate") {
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   118
    tooltip = "Locate context of current query within source text"
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   119
    override def clicked(): Unit = sledgehammer.locate_query()
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   120
  }
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   121
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   122
  private val controls =
66205
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   123
    Wrap_Panel(
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   124
      List(provers_label, Component.wrap(provers), isar_proofs, try0,
81387
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 78595
diff changeset
   125
        process_indicator.component, apply_query, cancel_query, locate_query,
c677755779f5 more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents: 78595
diff changeset
   126
        pretty_text_area.zoom_component))
66205
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   127
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   128
  add(controls.peer, BorderLayout.NORTH)
53787
e64389fe2d2c focus on default component according to jEdit window management;
wenzelm
parents: 53711
diff changeset
   129
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   130
  override def focusOnDefaultComponent(): Unit = provers.requestFocus()
56624
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   131
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   132
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   133
  /* main */
56624
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   134
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   135
  private val main =
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   136
    Session.Consumer[Session.Global_Options](getClass.getName) {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
   137
      case _: Session.Global_Options => GUI_Thread.later { update_provers(); handle_resize() }
56624
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   138
    }
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   139
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   140
  override def init(): Unit = {
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   141
    PIDE.session.global_options += main
56624
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   142
    update_provers()
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   143
    handle_resize()
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   144
    sledgehammer.activate()
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   145
  }
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   146
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   147
  override def exit(): Unit = {
56624
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   148
    sledgehammer.deactivate()
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   149
    PIDE.session.global_options -= main
56624
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   150
    delay_resize.revoke()
7eed0fee0241 clarified actor plumbing;
wenzelm
parents: 56623
diff changeset
   151
  }
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents:
diff changeset
   152
}