author | wenzelm |
Sat, 09 Apr 2022 12:29:39 +0200 | |
changeset 75429 | 436747f1f632 |
parent 75417 | 2c861b196d52 |
child 75440 | 39011d0d2128 |
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 |
|
75393 | 16 |
object Scala { |
72193 | 17 |
/** registered functions **/ |
18 |
||
75393 | 19 |
abstract class Fun(val name: String, val thread: Boolean = false) { |
72193 | 20 |
override def toString: String = name |
73565 | 21 |
def multi: Boolean = true |
72756 | 22 |
def position: Properties.T = here.position |
23 |
def here: Scala_Project.Here |
|
73565 | 24 |
def invoke(args: List[Bytes]): List[Bytes] |
25 |
} |
|
26 |
||
27 |
abstract class Fun_Strings(name: String, thread: Boolean = false) |
|
75393 | 28 |
extends Fun(name, thread = thread) { |
73565 | 29 |
override def invoke(args: List[Bytes]): List[Bytes] = |
30 |
apply(args.map(_.text)).map(Bytes.apply) |
|
31 |
def apply(args: List[String]): List[String] |
|
32 |
} |
|
33 |
||
34 |
abstract class Fun_String(name: String, thread: Boolean = false) |
|
75393 | 35 |
extends Fun_Strings(name, thread = thread) { |
73565 | 36 |
override def multi: Boolean = false |
37 |
override def apply(args: List[String]): List[String] = |
|
73571 | 38 |
List(apply(Library.the_single(args))) |
72193 | 39 |
def apply(arg: String): String |
40 |
} |
|
41 |
||
42 |
class Functions(val functions: Fun*) extends Isabelle_System.Service |
|
43 |
||
44 |
lazy val functions: List[Fun] = |
|
45 |
Isabelle_System.make_services(classOf[Functions]).flatMap(_.functions) |
|
46 |
||
47 |
||
48 |
||
72152 | 49 |
/** demo functions **/ |
50 |
||
75393 | 51 |
object Echo extends Fun_String("echo") { |
72756 | 52 |
val here = Scala_Project.here |
72193 | 53 |
def apply(arg: String): String = arg |
54 |
} |
|
72152 | 55 |
|
75393 | 56 |
object Sleep extends Fun_String("sleep") { |
72756 | 57 |
val here = Scala_Project.here |
75393 | 58 |
def apply(seconds: String): String = { |
72193 | 59 |
val t = |
60 |
seconds match { |
|
61 |
case Value.Double(s) => Time.seconds(s) |
|
62 |
case _ => error("Malformed argument: " + quote(seconds)) |
|
63 |
} |
|
64 |
val t0 = Time.now() |
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73576
diff
changeset
|
65 |
t.sleep() |
72193 | 66 |
val t1 = Time.now() |
67 |
(t1 - t0).toString |
|
68 |
} |
|
72152 | 69 |
} |
70 |
||
71 |
||
72 |
||
71870 | 73 |
/** compiler **/ |
71850 | 74 |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73702
diff
changeset
|
75 |
def class_path(): List[String] = |
73988 | 76 |
for { |
77 |
prop <- List("isabelle.scala.classpath", "java.class.path") |
|
78 |
elems = System.getProperty(prop, "") if elems.nonEmpty |
|
79 |
elem <- space_explode(JFile.pathSeparatorChar, elems) if elem.nonEmpty |
|
80 |
} yield elem |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73702
diff
changeset
|
81 |
|
75393 | 82 |
object Compiler { |
71868 | 83 |
def context( |
84 |
error: String => Unit = Exn.error, |
|
75393 | 85 |
jar_dirs: List[JFile] = Nil |
86 |
): Context = { |
|
71864 | 87 |
def find_jars(dir: JFile): List[String] = |
88 |
File.find_files(dir, file => file.getName.endsWith(".jar")). |
|
89 |
map(File.absolute_name) |
|
90 |
||
71868 | 91 |
val settings = new GenericRunnerSettings(error) |
92 |
settings.classpath.value = |
|
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73702
diff
changeset
|
93 |
(class_path() ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator) |
71850 | 94 |
|
71868 | 95 |
new Context(settings) |
71864 | 96 |
} |
71850 | 97 |
|
71868 | 98 |
def default_print_writer: PrintWriter = |
99 |
new NewLinePrintWriter(new ConsoleWriter, true) |
|
100 |
||
75393 | 101 |
class Context private [Compiler](val settings: GenericRunnerSettings) { |
71869 | 102 |
override def toString: String = settings.toString |
103 |
||
71868 | 104 |
def interpreter( |
105 |
print_writer: PrintWriter = default_print_writer, |
|
75393 | 106 |
class_loader: ClassLoader = null |
107 |
): IMain = { |
|
108 |
new IMain(settings, new ReplReporterImpl(settings, print_writer)) { |
|
71868 | 109 |
override def parentClassLoader: ClassLoader = |
110 |
if (class_loader == null) super.parentClassLoader |
|
111 |
else class_loader |
|
112 |
} |
|
113 |
} |
|
71850 | 114 |
|
75429
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
115 |
def toplevel(interpret: Boolean, source: String): List[String] = { |
71868 | 116 |
val out = new StringWriter |
117 |
val interp = interpreter(new PrintWriter(out)) |
|
73356 | 118 |
val marker = '\u000b' |
75429
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
119 |
val ok = |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
120 |
interp.withLabel(marker.toString) { |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
121 |
if (interpret) interp.interpret(source) == Results.Success |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
122 |
else (new interp.ReadEvalPrint).compile(source) |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
123 |
} |
73367 | 124 |
out.close() |
71864 | 125 |
|
71868 | 126 |
val Error = """(?s)^\S* error: (.*)$""".r |
127 |
val errors = |
|
73356 | 128 |
space_explode(marker, Library.strip_ansi_color(out.toString)). |
71868 | 129 |
collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) }) |
130 |
||
131 |
if (!ok && errors.isEmpty) List("Error") else errors |
|
132 |
} |
|
71864 | 133 |
} |
72194 | 134 |
} |
71850 | 135 |
|
75393 | 136 |
object Toplevel extends Fun_String("scala_toplevel") { |
72756 | 137 |
val here = Scala_Project.here |
75429
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
138 |
def apply(arg: String): String = { |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
139 |
val (interpret, source) = |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
140 |
YXML.parse_body(arg) match { |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
141 |
case Nil => (false, "") |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
142 |
case List(XML.Text(source)) => (false, source) |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
143 |
case body => import XML.Decode._; pair(bool, string)(body) |
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
144 |
} |
72194 | 145 |
val errors = |
75429
436747f1f632
revert 2c861b196d52: still required in HOL/Library/Code_Test.thy;
wenzelm
parents:
75417
diff
changeset
|
146 |
try { Compiler.context().toplevel(interpret, source) } |
72194 | 147 |
catch { case ERROR(msg) => List(msg) } |
148 |
locally { import XML.Encode._; YXML.string_of_body(list(string)(errors)) } |
|
72193 | 149 |
} |
71872 | 150 |
} |
151 |
||
71850 | 152 |
|
153 |
||
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
154 |
/** invoke Scala functions from ML **/ |
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
155 |
|
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
156 |
/* invoke function */ |
43748 | 157 |
|
75393 | 158 |
object Tag extends Enumeration { |
65638 | 159 |
val NULL, OK, ERROR, FAIL, INTERRUPT = Value |
43748 | 160 |
} |
43744
2c7e1565b4a3
some support to invoke Scala methods under program control;
wenzelm
parents:
diff
changeset
|
161 |
|
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
162 |
def function_thread(name: String): Boolean = |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
163 |
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
|
164 |
case Some(fun) => fun.thread |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
165 |
case None => false |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
166 |
} |
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
167 |
|
73565 | 168 |
def function_body(name: String, args: List[Bytes]): (Tag.Value, List[Bytes]) = |
71873 | 169 |
functions.find(fun => fun.name == name) match { |
170 |
case Some(fun) => |
|
73565 | 171 |
Exn.capture { fun.invoke(args) } match { |
172 |
case Exn.Res(null) => (Tag.NULL, Nil) |
|
43748 | 173 |
case Exn.Res(res) => (Tag.OK, res) |
73565 | 174 |
case Exn.Exn(Exn.Interrupt()) => (Tag.INTERRUPT, Nil) |
175 |
case Exn.Exn(e) => (Tag.ERROR, List(Bytes(Exn.message(e)))) |
|
43748 | 176 |
} |
73565 | 177 |
case None => (Tag.FAIL, List(Bytes("Unknown Isabelle/Scala function: " + quote(name)))) |
43748 | 178 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
179 |
|
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
180 |
|
72157 | 181 |
/* protocol handler */ |
182 |
||
75393 | 183 |
class Handler extends Session.Protocol_Handler { |
72157 | 184 |
private var session: Session = null |
185 |
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
|
186 |
|
72213 | 187 |
override def init(session: Session): Unit = |
188 |
synchronized { this.session = session } |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
189 |
|
75380 | 190 |
override def exit(): Unit = synchronized { |
72157 | 191 |
for ((id, future) <- futures) cancel(id, future) |
192 |
futures = Map.empty |
|
193 |
} |
|
65219 | 194 |
|
73565 | 195 |
private def result(id: String, tag: Scala.Tag.Value, res: List[Bytes]): Unit = |
75380 | 196 |
synchronized { |
72157 | 197 |
if (futures.isDefinedAt(id)) { |
73565 | 198 |
session.protocol_command_raw("Scala.result", Bytes(id) :: Bytes(tag.id.toString) :: res) |
72157 | 199 |
futures -= id |
200 |
} |
|
201 |
} |
|
65220 | 202 |
|
75393 | 203 |
private def cancel(id: String, future: Future[Unit]): Unit = { |
73367 | 204 |
future.cancel() |
73565 | 205 |
result(id, Scala.Tag.INTERRUPT, Nil) |
72157 | 206 |
} |
207 |
||
75380 | 208 |
private def invoke_scala(msg: Prover.Protocol_Output): Boolean = synchronized { |
72157 | 209 |
msg.properties match { |
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
210 |
case Markup.Invoke_Scala(name, id) => |
75393 | 211 |
def body: Unit = { |
73565 | 212 |
val (tag, res) = Scala.function_body(name, msg.chunks) |
72332
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
213 |
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
|
214 |
} |
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
215 |
val future = |
73419
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
wenzelm
parents:
73418
diff
changeset
|
216 |
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
|
217 |
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
|
218 |
} |
319dd5c618a5
allow Scala function execution on separate thread: better reactivity, but potential overloading of the JVM;
wenzelm
parents:
72294
diff
changeset
|
219 |
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
|
220 |
futures += (id -> future) |
72157 | 221 |
true |
222 |
case _ => false |
|
56387 | 223 |
} |
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
224 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
225 |
|
75380 | 226 |
private def cancel_scala(msg: Prover.Protocol_Output): Boolean = synchronized { |
72157 | 227 |
msg.properties match { |
228 |
case Markup.Cancel_Scala(id) => |
|
229 |
futures.get(id) match { |
|
230 |
case Some(future) => cancel(id, future) |
|
231 |
case None => |
|
232 |
} |
|
233 |
true |
|
234 |
case _ => false |
|
235 |
} |
|
236 |
} |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
237 |
|
72212 | 238 |
override val functions = |
72157 | 239 |
List( |
240 |
Markup.Invoke_Scala.name -> invoke_scala, |
|
241 |
Markup.Cancel_Scala.name -> cancel_scala) |
|
52111
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
242 |
} |
1fd184eaa310
explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents:
49470
diff
changeset
|
243 |
} |
71871
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents:
71870
diff
changeset
|
244 |
|
72193 | 245 |
class Scala_Functions extends Scala.Functions( |
246 |
Scala.Echo, |
|
247 |
Scala.Sleep, |
|
72194 | 248 |
Scala.Toplevel, |
73576 | 249 |
Bytes.Decode_Base64, |
250 |
Bytes.Encode_Base64, |
|
72760
042180540068
clarified protocol: Doc.check at run-time via Scala function;
wenzelm
parents:
72756
diff
changeset
|
251 |
Doc.Doc_Names, |
72763 | 252 |
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
|
253 |
Isabelle_System.Make_Directory, |
73322 | 254 |
Isabelle_System.Copy_Dir, |
255 |
Isabelle_System.Copy_File, |
|
256 |
Isabelle_System.Copy_File_Base, |
|
73324 | 257 |
Isabelle_System.Rm_Tree, |
73323 | 258 |
Isabelle_System.Download, |
73523
2cd23d587db9
further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents:
73431
diff
changeset
|
259 |
Isabelle_System.Isabelle_Id, |
73418
7d7d959547a1
support for SystemOnTPTP in Isabelle/ML and Isabelle/Scala (without perl);
wenzelm
parents:
73367
diff
changeset
|
260 |
Isabelle_Tool.Isabelle_Tools, |
73431
f27d7b12e8a4
support for SystemOnTPTP.run_system, with strict error following scripts/remote_atp;
wenzelm
parents:
73419
diff
changeset
|
261 |
isabelle.atp.SystemOnTPTP.List_Systems, |
f27d7b12e8a4
support for SystemOnTPTP.run_system, with strict error following scripts/remote_atp;
wenzelm
parents:
73419
diff
changeset
|
262 |
isabelle.atp.SystemOnTPTP.Run_System) |