author | wenzelm |
Mon, 07 Sep 2009 22:17:51 +0200 | |
changeset 34720 | ac61bdd7f598 |
parent 34719 | f5b408849dcc |
child 34721 | 4f3e352dde8b |
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 |
|
34497 | 12 |
import scala.collection.mutable |
34703 | 13 |
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
|
14 |
|
34671 | 15 |
import javax.swing.JTextArea |
34456 | 16 |
|
34501 | 17 |
import isabelle.jedit.Isabelle |
34660 | 18 |
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
|
19 |
|
34703 | 20 |
|
34672 | 21 |
class Prover(isabelle_system: Isabelle_System, logic: String, change_receiver: Actor) |
34692
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
22 |
extends Actor |
34453 | 23 |
{ |
34504
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
24 |
/* prover process */ |
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
25 |
|
34533
35308320713a
preliminary/failed attempt to use the new IsarDocument access model to the prover;
wenzelm
parents:
34509
diff
changeset
|
26 |
private val process = |
34504
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
27 |
{ |
34692
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
28 |
val receiver = new Actor { |
34709 | 29 |
def act() { |
34692
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
30 |
loop { react { case res: Isabelle_Process.Result => handle_result(res) } } |
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
31 |
} |
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
32 |
}.start |
34709 | 33 |
new Isabelle_Process(isabelle_system, receiver, "-m", "xsymbols", logic) |
34 |
with Isar_Document |
|
34504
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
35 |
} |
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
36 |
|
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
37 |
def stop() { process.kill } |
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
38 |
|
4bd676662792
eliminated Prover.start -- part of main constructor;
wenzelm
parents:
34503
diff
changeset
|
39 |
|
34720 | 40 |
/* outer syntax keywords and completion */ |
41 |
||
42 |
@volatile private var _keyword_decls = Set[String]() |
|
43 |
def keyword_decls() = _keyword_decls |
|
44 |
||
45 |
@volatile private var _command_decls = Map[String, String]() |
|
46 |
def command_decls() = _command_decls |
|
47 |
||
48 |
@volatile private var _completion = Isabelle.completion |
|
49 |
def completion() = _completion |
|
50 |
||
51 |
||
34509 | 52 |
/* document state information */ |
53 |
||
34690 | 54 |
private val states = new mutable.HashMap[Isar_Document.State_ID, Command_State] with |
55 |
mutable.SynchronizedMap[Isar_Document.State_ID, Command_State] |
|
56 |
private val commands = new mutable.HashMap[Isar_Document.Command_ID, Command] with |
|
57 |
mutable.SynchronizedMap[Isar_Document.Command_ID, Command] |
|
34654 | 58 |
val document_0 = |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
59 |
ProofDocument.empty. |
34720 | 60 |
set_command_keyword((s: String) => command_decls().contains(s)). |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
61 |
set_change_receiver(change_receiver) |
34650 | 62 |
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
|
63 |
|
34690 | 64 |
def command(id: Isar_Document.Command_ID): Option[Command] = commands.get(id) |
65 |
def document(id: Isar_Document.Document_ID) = document_versions.find(_.id == id) |
|
34509 | 66 |
|
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
67 |
|
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
68 |
/* event handling */ |
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
69 |
|
34671 | 70 |
val output_text_view = new JTextArea |
71 |
||
34719
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
72 |
private case object Ready |
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
73 |
|
34615 | 74 |
private def handle_result(result: Isabelle_Process.Result) |
34489
7b7ccf0ff629
replaced java.util.Property by plain association list;
wenzelm
parents:
34485
diff
changeset
|
75 |
{ |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
76 |
val state = |
34509 | 77 |
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
|
78 |
case None => None |
34509 | 79 |
case Some((_, id)) => |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
else None |
34509 | 83 |
} |
34720 | 84 |
Swing_Thread.later { output_text_view.append(result.toString + "\n") } // slow? |
85 |
||
34692
3c0a8bece8b8
Isabelle_Process: receiver as Actor -- requires Scheduler.shutdown in the end;
wenzelm
parents:
34690
diff
changeset
|
86 |
val message = Isabelle_Process.parse_message(isabelle_system, result) |
34677 | 87 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
88 |
if (state.isDefined) state.get ! message |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
89 |
else result.kind match { |
34404
98155c35d252
delayed repainting new phase in buffer and overview;
immler@in.tum.de
parents:
34401
diff
changeset
|
90 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
91 |
case Isabelle_Process.Kind.STATUS => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
92 |
//{{{ 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
|
93 |
message match { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
94 |
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
|
95 |
for (elem <- elems) { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
96 |
elem match { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
97 |
//{{{ |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
98 |
// command and keyword declarations |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
99 |
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
|
100 |
(Markup.NAME, name) :: (Markup.KIND, kind) :: _, _) => |
34720 | 101 |
_command_decls += (name -> kind) |
102 |
_completion += name |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
103 |
case XML.Elem(Markup.KEYWORD_DECL, (Markup.NAME, name) :: _, _) => |
34720 | 104 |
_keyword_decls += name |
105 |
_completion += name |
|
34404
98155c35d252
delayed repainting new phase in buffer and overview;
immler@in.tum.de
parents:
34401
diff
changeset
|
106 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
107 |
// process ready (after initialization) |
34719
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
108 |
case XML.Elem(Markup.READY, _, _) => this ! Ready |
34453 | 109 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
110 |
// document edits |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
for { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
115 |
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
|
116 |
<- edits |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
117 |
} { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
118 |
if (commands.contains(cmd_id)) { |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
119 |
val cmd = commands(cmd_id) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
120 |
val state = new Command_State(cmd) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
121 |
states(state_id) = state |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
122 |
doc.states += (cmd -> state) |
34685
93f4978fe2a8
global event buses for changes concerning commands and document edits
immler@in.tum.de
parents:
34677
diff
changeset
|
123 |
cmd.changed() |
34509 | 124 |
} |
34676
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 |
} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
127 |
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
|
128 |
// 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
|
129 |
// 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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
if (outer && begin.isDefined && end.isDefined && markup_id.isDefined) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
134 |
commands.get(markup_id.get).map (cmd => cmd ! message) |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
135 |
case _ => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
136 |
//}}} |
34509 | 137 |
} |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
138 |
} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
139 |
case _ => |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
140 |
} |
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
141 |
//}}} |
34453 | 142 |
|
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34674
diff
changeset
|
143 |
case _ => |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
144 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
145 |
} |
34453 | 146 |
|
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
147 |
def act() { |
34719
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
148 |
var ready = false |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
149 |
loop { |
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
150 |
react { |
34719
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
151 |
case Ready => ready = true |
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
152 |
case change: Change if ready => |
34709 | 153 |
val old = document(change.parent.get.id).get |
154 |
val (doc, structure_change) = old.text_changed(change) |
|
155 |
document_versions ::= doc |
|
156 |
edit_document(old, doc, structure_change) |
|
157 |
change_receiver ! doc |
|
34719
f5b408849dcc
eliminated ProverEvents.Activate -- handle "ready" within Prover;
wenzelm
parents:
34709
diff
changeset
|
158 |
case bad if ready => System.err.println("prover: ignoring bad message " + bad) |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
159 |
} |
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
160 |
} |
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
161 |
} |
34709 | 162 |
|
34672 | 163 |
def set_document(path: String) { |
34650 | 164 |
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
|
165 |
} |
34453 | 166 |
|
34660 | 167 |
private def edit_document(old: ProofDocument, doc: ProofDocument, |
34709 | 168 |
changes: ProofDocument.StructureChange) = |
169 |
{ |
|
170 |
val id_changes = changes map { case (c1, c2) => |
|
34660 | 171 |
(c1.map(_.id).getOrElse(document_0.id), |
172 |
c2 match { |
|
173 |
case None => None |
|
174 |
case Some(cmd) => |
|
175 |
commands += (cmd.id -> cmd) |
|
176 |
process.define_command(cmd.id, isabelle_system.symbols.encode(cmd.content)) |
|
177 |
Some(cmd.id) |
|
178 |
}) |
|
179 |
} |
|
180 |
process.edit_document(old.id, doc.id, id_changes) |
|
34650 | 181 |
} |
34453 | 182 |
} |