author | wenzelm |
Wed, 16 Sep 2009 17:13:14 +0200 | |
changeset 34740 | ec35626a2aa5 |
parent 34730 | 819862460a12 |
child 34741 | 4431c498726d |
permissions | -rw-r--r-- |
34407 | 1 |
/* |
34501 | 2 |
* Higher-level prover communication: interactive document model |
34407 | 3 |
* |
4 |
* @author Johannes Hölzl, TU Munich |
|
5 |
* @author Fabian Immler, TU Munich |
|
34453 | 6 |
* @author Makarius |
34407 | 7 |
*/ |
8 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
9 |
package isabelle.prover |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
10 |
|
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
11 |
|
34703 | 12 |
import scala.actors.Actor, Actor._ |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
13 |
|
34671 | 14 |
import javax.swing.JTextArea |
34456 | 15 |
|
34501 | 16 |
import isabelle.jedit.Isabelle |
34660 | 17 |
import isabelle.proofdocument.{ProofDocument, Change, Token} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
18 |
|
34703 | 19 |
|
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
20 |
class Prover(system: Isabelle_System, logic: String) extends Actor |
34453 | 21 |
{ |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
22 |
/* incoming messages */ |
34730 | 23 |
|
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
24 |
private var prover_ready = false |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
25 |
|
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
26 |
def act() { |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
27 |
loop { |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
28 |
react { |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
29 |
case result: Isabelle_Process.Result => handle_result(result) |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
30 |
case change: Change if prover_ready => handle_change(change) |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
31 |
case bad if prover_ready => System.err.println("prover: ignoring bad message " + bad) |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
32 |
} |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
33 |
} |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
34 |
} |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
35 |
|
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
36 |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
37 |
/* outgoing messages */ |
34730 | 38 |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
39 |
val command_change = new Event_Bus[Command] |
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
40 |
val document_change = new Event_Bus[ProofDocument] |
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
41 |
|
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
42 |
|
34504
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
43 |
/* prover process */ |
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
44 |
|
34533
35308320713a
preliminary/failed attempt to use the new IsarDocument access model to the prover;
wenzelm
parents:
34509
diff
changeset
|
45 |
private val process = |
34730 | 46 |
new Isabelle_Process(system, this, "-m", "xsymbols", logic) with Isar_Document |
34504
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
47 |
|
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
48 |
def stop() { process.kill } |
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
49 |
|
34730 | 50 |
|
34720 | 51 |
/* outer syntax keywords and completion */ |
52 |
||
34723 | 53 |
@volatile private var _command_decls = Map[String, String]() |
54 |
def command_decls() = _command_decls |
|
55 |
||
34720 | 56 |
@volatile private var _keyword_decls = Set[String]() |
57 |
def keyword_decls() = _keyword_decls |
|
58 |
||
59 |
@volatile private var _completion = Isabelle.completion |
|
60 |
def completion() = _completion |
|
61 |
||
62 |
||
34509 | 63 |
/* document state information */ |
64 |
||
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
65 |
@volatile private var states = Map[Isar_Document.State_ID, Command_State]() |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
66 |
@volatile private var commands = Map[Isar_Document.Command_ID, Command]() |
34654 | 67 |
val document_0 = |
34740 | 68 |
ProofDocument.empty. |
69 |
set_command_keyword((s: String) => command_decls().contains(s)) |
|
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
70 |
@volatile private var document_versions = List(document_0) |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
71 |
|
34690 | 72 |
def command(id: Isar_Document.Command_ID): Option[Command] = commands.get(id) |
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
73 |
def document(id: Isar_Document.Document_ID): Option[ProofDocument] = |
34740 | 74 |
document_versions.find(_.id == id) |
34730 | 75 |
|
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
76 |
|
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
77 |
/* prover results */ |
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
78 |
|
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
79 |
val output_text_view = new JTextArea // FIXME separate jEdit component |
34671 | 80 |
|
34615 | 81 |
private def handle_result(result: Isabelle_Process.Result) |
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
82 |
{ |
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
83 |
// FIXME separate jEdit component |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
84 |
Swing_Thread.later { output_text_view.append(result.toString + "\n") } |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
85 |
|
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
86 |
val message = Isabelle_Process.parse_message(system, result) |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
87 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
88 |
val state = |
34740 | 89 |
Position.id_of(result.props) match { |
90 |
case None => None |
|
91 |
case Some(id) => commands.get(id) orElse states.get(id) orElse None |
|
92 |
} |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
93 |
if (state.isDefined) state.get ! (this, message) |
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
94 |
else if (result.kind == Isabelle_Process.Kind.STATUS) { |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
95 |
//{{{ global status message |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
96 |
message match { |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
97 |
case XML.Elem(Markup.MESSAGE, _, elems) => |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
98 |
for (elem <- elems) { |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
99 |
elem match { |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
100 |
// document edits |
34740 | 101 |
case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) => |
102 |
document_versions.find(_.id == doc_id) match { |
|
103 |
case Some(doc) => |
|
104 |
for { |
|
105 |
XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _) |
|
106 |
<- edits |
|
107 |
} { |
|
108 |
if (commands.contains(cmd_id)) { |
|
109 |
val cmd = commands(cmd_id) |
|
110 |
val state = new Command_State(cmd) |
|
111 |
states += (state_id -> state) |
|
112 |
doc.states += (cmd -> state) |
|
113 |
command_change.event(cmd) |
|
114 |
} |
|
115 |
} |
|
116 |
case None => |
|
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
117 |
} |
34730 | 118 |
|
119 |
// command and keyword declarations |
|
34740 | 120 |
case XML.Elem(Markup.COMMAND_DECL, (Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) => |
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
121 |
_command_decls += (name -> kind) |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
122 |
_completion += name |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
123 |
case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) => |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
124 |
_keyword_decls += name |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
125 |
_completion += name |
34453 | 126 |
|
34730 | 127 |
// process ready (after initialization) |
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
128 |
case XML.Elem(Markup.READY, _, _) => prover_ready = true |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
129 |
|
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
130 |
case _ => |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
131 |
} |
34728
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
132 |
} |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
133 |
case _ => |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
134 |
} |
e3df9c8699ea
handle_result: no special treatment of outer markup (it is now properly identified by the prover);
wenzelm
parents:
34724
diff
changeset
|
135 |
//}}} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
136 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
137 |
} |
34453 | 138 |
|
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
139 |
|
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
140 |
/* document changes */ |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
141 |
|
34729 | 142 |
def begin_document(path: String) { |
34650 | 143 |
process.begin_document(document_0.id, path) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
144 |
} |
34453 | 145 |
|
34729 | 146 |
def handle_change(change: Change) { |
147 |
val old = document(change.parent.get.id).get |
|
148 |
val (doc, changes) = old.text_changed(change) |
|
149 |
document_versions ::= doc |
|
150 |
||
34709 | 151 |
val id_changes = changes map { case (c1, c2) => |
34730 | 152 |
(c1.map(_.id).getOrElse(document_0.id), |
153 |
c2 match { |
|
154 |
case None => None |
|
155 |
case Some(command) => |
|
156 |
commands += (command.id -> command) |
|
157 |
process.define_command(command.id, system.symbols.encode(command.content)) |
|
158 |
Some(command.id) |
|
159 |
}) |
|
34660 | 160 |
} |
161 |
process.edit_document(old.id, doc.id, id_changes) |
|
34729 | 162 |
|
163 |
document_change.event(doc) |
|
34650 | 164 |
} |
34453 | 165 |
} |