author | wenzelm |
Tue, 08 Sep 2009 22:38:01 +0200 | |
changeset 34724 | b1079c3ba1da |
parent 34723 | 740755bfef08 |
child 34728 | e3df9c8699ea |
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 |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
20 |
class Prover(isabelle_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 */ |
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
23 |
|
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 */ |
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
38 |
|
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 = |
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
46 |
new Isabelle_Process(isabelle_system, this, "-m", "xsymbols", logic) |
34709 | 47 |
with Isar_Document |
34504
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
48 |
|
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
49 |
def stop() { process.kill } |
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
50 |
|
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
51 |
|
34720 | 52 |
/* outer syntax keywords and completion */ |
53 |
||
34723 | 54 |
@volatile private var _command_decls = Map[String, String]() |
55 |
def command_decls() = _command_decls |
|
56 |
||
34720 | 57 |
@volatile private var _keyword_decls = Set[String]() |
58 |
def keyword_decls() = _keyword_decls |
|
59 |
||
60 |
@volatile private var _completion = Isabelle.completion |
|
61 |
def completion() = _completion |
|
62 |
||
63 |
||
34509 | 64 |
/* document state information */ |
65 |
||
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
66 |
@volatile private var states = Map[Isar_Document.State_ID, Command_State]() |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
67 |
@volatile private var commands = Map[Isar_Document.Command_ID, Command]() |
34654 | 68 |
val document_0 = |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
69 |
ProofDocument.empty. |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
70 |
set_command_keyword((s: String) => command_decls().contains(s)) |
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
71 |
@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
|
72 |
|
34690 | 73 |
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
|
74 |
def document(id: Isar_Document.Document_ID): Option[ProofDocument] = |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
75 |
document_versions.find(_.id == id) |
34509 | 76 |
|
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
77 |
|
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
78 |
/* prover results */ |
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
79 |
|
34671 | 80 |
val output_text_view = new JTextArea |
81 |
||
34615 | 82 |
private def handle_result(result: Isabelle_Process.Result) |
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
83 |
{ |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
84 |
val state = |
34509 | 85 |
result.props.find(p => p._1 == Markup.ID) match { |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
86 |
case None => None |
34509 | 87 |
case Some((_, id)) => |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
88 |
if (commands.contains(id)) Some(commands(id)) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
89 |
else if (states.contains(id)) Some(states(id)) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
90 |
else None |
34509 | 91 |
} |
34720 | 92 |
Swing_Thread.later { output_text_view.append(result.toString + "\n") } // slow? |
93 |
||
34692
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
94 |
val message = Isabelle_Process.parse_message(isabelle_system, result) |
34677 | 95 |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
96 |
if (state.isDefined) state.get ! (this, message) |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
97 |
else result.kind match { |
34404
98155c35d252
delayed repainting new phase in buffer and overview;
immler@in.tum.de
parents:
34401
diff
changeset
|
98 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
99 |
case Isabelle_Process.Kind.STATUS => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
100 |
//{{{ handle all kinds of status messages here |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
101 |
message match { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
102 |
case XML.Elem(Markup.MESSAGE, _, elems) => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
103 |
for (elem <- elems) { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
104 |
elem match { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
105 |
//{{{ |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
106 |
// command and keyword declarations |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
107 |
case XML.Elem(Markup.COMMAND_DECL, |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
108 |
(Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) => |
34720 | 109 |
_command_decls += (name -> kind) |
110 |
_completion += name |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
111 |
case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) => |
34720 | 112 |
_keyword_decls += name |
113 |
_completion += name |
|
34404
98155c35d252
delayed repainting new phase in buffer and overview;
immler@in.tum.de
parents:
34401
diff
changeset
|
114 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
115 |
// process ready (after initialization) |
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
116 |
case XML.Elem(Markup.READY, _, _) => prover_ready = true |
34453 | 117 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
118 |
// document edits |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
119 |
case XML.Elem(Markup.EDITS, (Markup.ID, doc_id) :: _, edits) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
120 |
if document_versions.exists(_.id == doc_id) => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
121 |
val doc = document_versions.find(_.id == doc_id).get |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
122 |
for { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
123 |
XML.Elem(Markup.EDIT, (Markup.ID, cmd_id) :: (Markup.STATE, state_id) :: _, _) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
124 |
<- edits |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
125 |
} { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
126 |
if (commands.contains(cmd_id)) { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
127 |
val cmd = commands(cmd_id) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
128 |
val state = new Command_State(cmd) |
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
129 |
states += (state_id -> state) |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
130 |
doc.states += (cmd -> state) |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
131 |
command_change.event(cmd) |
34509 | 132 |
} |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
133 |
} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
134 |
case XML.Elem(kind, attr, body) => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
135 |
// TODO: This is mostly irrelevant information on removed commands, but it can |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
136 |
// also be outer-syntax-markup since there is no id in props (fix that?) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
137 |
val (begin, end) = Position.offsets_of(attr) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
138 |
val markup_id = Position.id_of(attr) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
139 |
val outer = isabelle.jedit.DynamicTokenMarker.is_outer(kind) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
140 |
if (outer && begin.isDefined && end.isDefined && markup_id.isDefined) |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
141 |
commands.get(markup_id.get).map(cmd => cmd ! (this, message)) |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
142 |
case _ => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
143 |
//}}} |
34509 | 144 |
} |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
145 |
} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
146 |
case _ => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
147 |
} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
148 |
//}}} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
149 |
case _ => |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
150 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
151 |
} |
34453 | 152 |
|
34721
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
153 |
|
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
154 |
/* document changes */ |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
155 |
|
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
156 |
def handle_change(change: Change) { |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
157 |
val old = document(change.parent.get.id).get |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
158 |
val (doc, structure_change) = old.text_changed(change) |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
159 |
document_versions ::= doc |
4f3e352dde8b
Prover: just one actor -- single message dispatch;
wenzelm
parents:
34720
diff
changeset
|
160 |
edit_document(old, doc, structure_change) |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34723
diff
changeset
|
161 |
document_change.event(doc) |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
162 |
} |
34709 | 163 |
|
34672 | 164 |
def set_document(path: String) { |
34650 | 165 |
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
|
166 |
} |
34453 | 167 |
|
34660 | 168 |
private def edit_document(old: ProofDocument, doc: ProofDocument, |
34709 | 169 |
changes: ProofDocument.StructureChange) = |
170 |
{ |
|
171 |
val id_changes = changes map { case (c1, c2) => |
|
34660 | 172 |
(c1.map(_.id).getOrElse(document_0.id), |
173 |
c2 match { |
|
174 |
case None => None |
|
175 |
case Some(cmd) => |
|
176 |
commands += (cmd.id -> cmd) |
|
177 |
process.define_command(cmd.id, isabelle_system.symbols.encode(cmd.content)) |
|
178 |
Some(cmd.id) |
|
179 |
}) |
|
180 |
} |
|
181 |
process.edit_document(old.id, doc.id, id_changes) |
|
34650 | 182 |
} |
34453 | 183 |
} |