| author | wenzelm |
| Sat, 13 Mar 2021 12:36:24 +0100 | |
| changeset 73419 | 22f3f2117ed7 |
| parent 73418 | 7d7d959547a1 |
| child 73431 | f27d7b12e8a4 |
| permissions | -rw-r--r-- |
| 71849 | 1 |
/* Title: Pure/System/scala.scala |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
3 |
|
| 71849 | 4 |
Support for Scala at runtime. |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
6 |
|
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
8 |
|
|
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
9 |
|
| 71864 | 10 |
import java.io.{File => JFile, StringWriter, PrintWriter}
|
|
56730
e723f041b6d0
tuned signature -- separate pool for JFuture tasks, which can be canceled;
wenzelm
parents:
56667
diff
changeset
|
11 |
|
| 71868 | 12 |
import scala.tools.nsc.{GenericRunnerSettings, ConsoleWriter, NewLinePrintWriter}
|
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
13 |
import scala.tools.nsc.interpreter.{IMain, Results}
|
| 73136 | 14 |
import scala.tools.nsc.interpreter.shell.ReplReporterImpl |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
15 |
|
| 71849 | 16 |
object Scala |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
17 |
{
|
| 72193 | 18 |
/** registered functions **/ |
19 |
||
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
20 |
abstract class Fun(val name: String, val thread: Boolean = false) |
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
21 |
extends Function[String, String] |
| 72193 | 22 |
{
|
23 |
override def toString: String = name |
|
| 72756 | 24 |
def position: Properties.T = here.position |
25 |
def here: Scala_Project.Here |
|
| 72193 | 26 |
def apply(arg: String): String |
27 |
} |
|
28 |
||
29 |
class Functions(val functions: Fun*) extends Isabelle_System.Service |
|
30 |
||
31 |
lazy val functions: List[Fun] = |
|
32 |
Isabelle_System.make_services(classOf[Functions]).flatMap(_.functions) |
|
33 |
||
34 |
||
35 |
||
| 72152 | 36 |
/** demo functions **/ |
37 |
||
| 72193 | 38 |
object Echo extends Fun("echo")
|
39 |
{
|
|
| 72756 | 40 |
val here = Scala_Project.here |
| 72193 | 41 |
def apply(arg: String): String = arg |
42 |
} |
|
| 72152 | 43 |
|
| 72193 | 44 |
object Sleep extends Fun("sleep")
|
| 72152 | 45 |
{
|
| 72756 | 46 |
val here = Scala_Project.here |
| 72193 | 47 |
def apply(seconds: String): String = |
48 |
{
|
|
49 |
val t = |
|
50 |
seconds match {
|
|
51 |
case Value.Double(s) => Time.seconds(s) |
|
52 |
case _ => error("Malformed argument: " + quote(seconds))
|
|
53 |
} |
|
54 |
val t0 = Time.now() |
|
55 |
t.sleep |
|
56 |
val t1 = Time.now() |
|
57 |
(t1 - t0).toString |
|
58 |
} |
|
| 72152 | 59 |
} |
60 |
||
61 |
||
62 |
||
| 71870 | 63 |
/** compiler **/ |
| 71850 | 64 |
|
| 71864 | 65 |
object Compiler |
| 71850 | 66 |
{
|
| 71868 | 67 |
def context( |
68 |
error: String => Unit = Exn.error, |
|
69 |
jar_dirs: List[JFile] = Nil): Context = |
|
| 71864 | 70 |
{
|
71 |
def find_jars(dir: JFile): List[String] = |
|
72 |
File.find_files(dir, file => file.getName.endsWith(".jar")).
|
|
73 |
map(File.absolute_name) |
|
74 |
||
75 |
val class_path = |
|
76 |
for {
|
|
| 71882 | 77 |
prop <- List("isabelle.scala.classpath", "java.class.path")
|
| 71864 | 78 |
path = System.getProperty(prop, "") if path != "\"\"" |
79 |
elem <- space_explode(JFile.pathSeparatorChar, path) |
|
80 |
} yield elem |
|
81 |
||
| 71868 | 82 |
val settings = new GenericRunnerSettings(error) |
83 |
settings.classpath.value = |
|
84 |
(class_path ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator) |
|
| 71850 | 85 |
|
| 71868 | 86 |
new Context(settings) |
| 71864 | 87 |
} |
| 71850 | 88 |
|
| 71868 | 89 |
def default_print_writer: PrintWriter = |
90 |
new NewLinePrintWriter(new ConsoleWriter, true) |
|
91 |
||
92 |
class Context private [Compiler](val settings: GenericRunnerSettings) |
|
| 71864 | 93 |
{
|
| 71869 | 94 |
override def toString: String = settings.toString |
95 |
||
| 71868 | 96 |
def interpreter( |
97 |
print_writer: PrintWriter = default_print_writer, |
|
98 |
class_loader: ClassLoader = null): IMain = |
|
99 |
{
|
|
| 73136 | 100 |
new IMain(settings, new ReplReporterImpl(settings, print_writer)) |
| 71868 | 101 |
{
|
102 |
override def parentClassLoader: ClassLoader = |
|
103 |
if (class_loader == null) super.parentClassLoader |
|
104 |
else class_loader |
|
105 |
} |
|
106 |
} |
|
| 71850 | 107 |
|
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
108 |
def toplevel(interpret: Boolean, source: String): List[String] = |
| 71868 | 109 |
{
|
110 |
val out = new StringWriter |
|
111 |
val interp = interpreter(new PrintWriter(out)) |
|
| 73356 | 112 |
val marker = '\u000b' |
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
113 |
val ok = |
| 73356 | 114 |
interp.withLabel(marker.toString) {
|
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
115 |
if (interpret) interp.interpret(source) == Results.Success |
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
116 |
else (new interp.ReadEvalPrint).compile(source) |
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
117 |
} |
| 73367 | 118 |
out.close() |
| 71864 | 119 |
|
| 71868 | 120 |
val Error = """(?s)^\S* error: (.*)$""".r |
121 |
val errors = |
|
| 73356 | 122 |
space_explode(marker, Library.strip_ansi_color(out.toString)). |
| 71868 | 123 |
collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) })
|
124 |
||
125 |
if (!ok && errors.isEmpty) List("Error") else errors
|
|
126 |
} |
|
| 71864 | 127 |
} |
| 72194 | 128 |
} |
| 71850 | 129 |
|
| 72194 | 130 |
object Toplevel extends Fun("scala_toplevel")
|
131 |
{
|
|
| 72756 | 132 |
val here = Scala_Project.here |
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
133 |
def apply(arg: String): String = |
| 72193 | 134 |
{
|
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
135 |
val (interpret, source) = |
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
136 |
YXML.parse_body(arg) match {
|
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
137 |
case Nil => (false, "") |
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
138 |
case List(XML.Text(source)) => (false, source) |
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
139 |
case body => import XML.Decode._; pair(bool, string)(body) |
|
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
140 |
} |
| 72194 | 141 |
val errors = |
|
72294
25c6423ec538
clarified signature: proper eval/print via interpret;
wenzelm
parents:
72213
diff
changeset
|
142 |
try { Compiler.context().toplevel(interpret, source) }
|
| 72194 | 143 |
catch { case ERROR(msg) => List(msg) }
|
144 |
locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) }
|
|
| 72193 | 145 |
} |
| 71872 | 146 |
} |
147 |
||
| 71850 | 148 |
|
149 |
||
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
150 |
/** invoke Scala functions from ML **/ |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
151 |
|
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
152 |
/* invoke function */ |
| 43748 | 153 |
|
154 |
object Tag extends Enumeration |
|
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
155 |
{
|
| 65638 | 156 |
val NULL, OK, ERROR, FAIL, INTERRUPT = Value |
| 43748 | 157 |
} |
|
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
158 |
|
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
159 |
def function_thread(name: String): Boolean = |
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
160 |
functions.find(fun => fun.name == name) match {
|
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
161 |
case Some(fun) => fun.thread |
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
162 |
case None => false |
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
163 |
} |
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
164 |
|
|
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
165 |
def function_body(name: String, arg: String): (Tag.Value, String) = |
| 71873 | 166 |
functions.find(fun => fun.name == name) match {
|
167 |
case Some(fun) => |
|
| 72193 | 168 |
Exn.capture { fun(arg) } match {
|
| 43748 | 169 |
case Exn.Res(null) => (Tag.NULL, "") |
170 |
case Exn.Res(res) => (Tag.OK, res) |
|
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56387
diff
changeset
|
171 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, "") |
| 44158 | 172 |
case Exn.Exn(e) => (Tag.ERROR, Exn.message(e)) |
| 43748 | 173 |
} |
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
174 |
case None => (Tag.FAIL, "Unknown Isabelle/Scala function: " + quote(name)) |
| 43748 | 175 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
176 |
|
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
177 |
|
| 72157 | 178 |
/* protocol handler */ |
179 |
||
180 |
class Handler extends Session.Protocol_Handler |
|
181 |
{
|
|
182 |
private var session: Session = null |
|
183 |
private var futures = Map.empty[String, Future[Unit]] |
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
184 |
|
| 72213 | 185 |
override def init(session: Session): Unit = |
186 |
synchronized { this.session = session }
|
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
187 |
|
| 72157 | 188 |
override def exit(): Unit = synchronized |
189 |
{
|
|
190 |
for ((id, future) <- futures) cancel(id, future) |
|
191 |
futures = Map.empty |
|
192 |
} |
|
| 65219 | 193 |
|
| 72157 | 194 |
private def result(id: String, tag: Scala.Tag.Value, res: String): Unit = |
195 |
synchronized |
|
196 |
{
|
|
197 |
if (futures.isDefinedAt(id)) {
|
|
198 |
session.protocol_command("Scala.result", id, tag.id.toString, res)
|
|
199 |
futures -= id |
|
200 |
} |
|
201 |
} |
|
| 65220 | 202 |
|
| 73340 | 203 |
private def cancel(id: String, future: Future[Unit]): Unit = |
| 72157 | 204 |
{
|
| 73367 | 205 |
future.cancel() |
| 72157 | 206 |
result(id, Scala.Tag.INTERRUPT, "") |
207 |
} |
|
208 |
||
209 |
private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized |
|
| 56387 | 210 |
{
|
| 72157 | 211 |
msg.properties match {
|
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
212 |
case Markup.Invoke_Scala(name, id) => |
| 73340 | 213 |
def body: Unit = |
|
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
214 |
{
|
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
215 |
val (tag, res) = Scala.function_body(name, msg.text) |
|
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
216 |
result(id, tag, res) |
|
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
217 |
} |
|
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
218 |
val future = |
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
219 |
if (Scala.function_thread(name)) {
|
|
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
220 |
Future.thread(name = Isabelle_Thread.make_name(base = "invoke_scala"))(body) |
|
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
221 |
} |
|
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
222 |
else Future.fork(body) |
|
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
223 |
futures += (id -> future) |
| 72157 | 224 |
true |
225 |
case _ => false |
|
| 56387 | 226 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
227 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
228 |
|
| 72157 | 229 |
private def cancel_scala(msg: Prover.Protocol_Output): Boolean = synchronized |
230 |
{
|
|
231 |
msg.properties match {
|
|
232 |
case Markup.Cancel_Scala(id) => |
|
233 |
futures.get(id) match {
|
|
234 |
case Some(future) => cancel(id, future) |
|
235 |
case None => |
|
236 |
} |
|
237 |
true |
|
238 |
case _ => false |
|
239 |
} |
|
240 |
} |
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
241 |
|
| 72212 | 242 |
override val functions = |
| 72157 | 243 |
List( |
244 |
Markup.Invoke_Scala.name -> invoke_scala, |
|
245 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
246 |
} |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
247 |
} |
|
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
248 |
|
| 72193 | 249 |
class Scala_Functions extends Scala.Functions( |
250 |
Scala.Echo, |
|
251 |
Scala.Sleep, |
|
| 72194 | 252 |
Scala.Toplevel, |
|
72760
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
72756
diff
changeset
|
253 |
Doc.Doc_Names, |
|
73228
0575cfd2ecfc
support multi-threaded Bash.process invocation on Apple Silicon, where Poly/ML is running on Rosetta 2;
wenzelm
parents:
73136
diff
changeset
|
254 |
Bash.Process, |
| 72763 | 255 |
Bibtex.Check_Database, |
|
73314
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73228
diff
changeset
|
256 |
Isabelle_System.Make_Directory, |
| 73322 | 257 |
Isabelle_System.Copy_Dir, |
258 |
Isabelle_System.Copy_File, |
|
259 |
Isabelle_System.Copy_File_Base, |
|
| 73324 | 260 |
Isabelle_System.Rm_Tree, |
| 73323 | 261 |
Isabelle_System.Download, |
|
73418
7d7d959547a1
support for SystemOnTPTP in Isabelle/ML and Isabelle/Scala (without perl);
wenzelm
parents:
73367
diff
changeset
|
262 |
Isabelle_Tool.Isabelle_Tools, |
|
7d7d959547a1
support for SystemOnTPTP in Isabelle/ML and Isabelle/Scala (without perl);
wenzelm
parents:
73367
diff
changeset
|
263 |
isabelle.atp.SystemOnTPTP.List_Systems) |