| author | wenzelm | 
| Sat, 04 Jun 2022 16:54:24 +0200 | |
| changeset 75510 | 0106c89fb71f | 
| parent 73565 | 1aa92bc4d356 | 
| child 75577 | c51e1cef1eae | 
| permissions | -rw-r--r-- | 
| 71849 | 1  | 
(* Title: Pure/System/scala.ML  | 
| 43748 | 2  | 
Author: Makarius  | 
3  | 
||
| 
73226
 
4c8edf348c4e
clarified modules: allow early invocation of Scala functions;
 
wenzelm 
parents: 
73225 
diff
changeset
 | 
4  | 
Invoke Scala functions from the ML runtime.  | 
| 43748 | 5  | 
*)  | 
6  | 
||
| 71849 | 7  | 
signature SCALA =  | 
| 43748 | 8  | 
sig  | 
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
9  | 
exception Null  | 
| 73565 | 10  | 
val function: string -> string list -> string list  | 
11  | 
val function1: string -> string -> string  | 
|
| 43748 | 12  | 
end;  | 
13  | 
||
| 71849 | 14  | 
structure Scala: SCALA =  | 
| 43748 | 15  | 
struct  | 
16  | 
||
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
17  | 
exception Null;  | 
| 43749 | 18  | 
|
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
19  | 
local  | 
| 43748 | 20  | 
|
| 52537 | 21  | 
val new_id = string_of_int o Counter.make ();  | 
| 43748 | 22  | 
|
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
23  | 
val results =  | 
| 73565 | 24  | 
Synchronized.var "Scala.results" (Symtab.empty: string list Exn.result Symtab.table);  | 
| 43748 | 25  | 
|
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
26  | 
val _ =  | 
| 
73225
 
3ab0cedaccad
clarified modules: allow early definition of protocol commands;
 
wenzelm 
parents: 
72759 
diff
changeset
 | 
27  | 
Protocol_Command.define "Scala.result"  | 
| 73565 | 28  | 
(fn id :: args =>  | 
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
29  | 
let  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
30  | 
val result =  | 
| 73565 | 31  | 
(case args of  | 
32  | 
["0"] => Exn.Exn Null  | 
|
33  | 
| "1" :: rest => Exn.Res rest  | 
|
34  | 
| ["2", msg] => Exn.Exn (ERROR msg)  | 
|
35  | 
| ["3", msg] => Exn.Exn (Fail msg)  | 
|
36  | 
| ["4"] => Exn.Exn Exn.Interrupt  | 
|
37  | 
| _ => raise Fail "Malformed Scala.result");  | 
|
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
38  | 
in Synchronized.change results (Symtab.map_entry id (K result)) end);  | 
| 43748 | 39  | 
|
| 
73419
 
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
 
wenzelm 
parents: 
73245 
diff
changeset
 | 
40  | 
in  | 
| 
 
22f3f2117ed7
clarified signature: function_thread is determined in Isabelle/Scala, not Isabelle/ML;
 
wenzelm 
parents: 
73245 
diff
changeset
 | 
41  | 
|
| 73565 | 42  | 
fun function name args =  | 
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
43  | 
Thread_Attributes.uninterruptible (fn restore_attributes => fn () =>  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
44  | 
let  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
45  | 
val id = new_id ();  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
46  | 
fun invoke () =  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
47  | 
(Synchronized.change results (Symtab.update (id, Exn.Exn Match));  | 
| 73565 | 48  | 
Output.protocol_message (Markup.invoke_scala name id) (map (single o XML.Text) args));  | 
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
49  | 
fun cancel () =  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
50  | 
(Synchronized.change results (Symtab.delete_safe id);  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
51  | 
Output.protocol_message (Markup.cancel_scala id) []);  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
52  | 
fun await_result () =  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
53  | 
Synchronized.guarded_access results  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
54  | 
(fn tab =>  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
55  | 
(case Symtab.lookup tab id of  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
56  | 
SOME (Exn.Exn Match) => NONE  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
57  | 
| SOME result => SOME (result, Symtab.delete id tab)  | 
| 
73245
 
f69cbb59813e
more robust interrupt handling as in Future.forked_results (amending 64df1e514005);
 
wenzelm 
parents: 
73226 
diff
changeset
 | 
58  | 
| NONE => SOME (Exn.Exn Exn.Interrupt, tab)));  | 
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
59  | 
in  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
60  | 
invoke ();  | 
| 
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
61  | 
Exn.release (restore_attributes await_result ())  | 
| 
73245
 
f69cbb59813e
more robust interrupt handling as in Future.forked_results (amending 64df1e514005);
 
wenzelm 
parents: 
73226 
diff
changeset
 | 
62  | 
handle exn => (if Exn.is_interrupt exn then cancel () else (); Exn.reraise exn)  | 
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
63  | 
end) ();  | 
| 43748 | 64  | 
|
| 73565 | 65  | 
val function1 = singleton o function;  | 
66  | 
||
| 
72151
 
64df1e514005
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
 
wenzelm 
parents: 
72103 
diff
changeset
 | 
67  | 
end;  | 
| 
71871
 
28def00726ca
more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
 
wenzelm 
parents: 
71849 
diff
changeset
 | 
68  | 
|
| 43748 | 69  | 
end;  |