src/Pure/System/scala.ML
author wenzelm
Fri, 14 Aug 2020 13:26:12 +0200
changeset 72151 64df1e514005
parent 72103 7b318273a4aa
child 72156 065dcd80293e
permissions -rw-r--r--
clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71849
265bbad3d6af clarified modules;
wenzelm
parents: 70991
diff changeset
     1
(*  Title:      Pure/System/scala.ML
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     3
71849
265bbad3d6af clarified modules;
wenzelm
parents: 70991
diff changeset
     4
Support for Scala at runtime.
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     5
*)
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     6
71849
265bbad3d6af clarified modules;
wenzelm
parents: 70991
diff changeset
     7
signature SCALA =
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     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
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    10
  val function: string -> string -> string
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    11
  val functions: unit -> string list
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    12
  val check_function: Proof.context -> string * Position.T -> string
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    13
end;
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    14
71849
265bbad3d6af clarified modules;
wenzelm
parents: 70991
diff changeset
    15
structure Scala: SCALA =
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    16
struct
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    17
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    18
(** protocol for Scala function invocation from ML **)
71849
265bbad3d6af clarified modules;
wenzelm
parents: 70991
diff changeset
    19
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    20
exception Null;
43749
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    21
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    22
local
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    23
52537
4b5941730bd8 more uniform Counter in ML and Scala;
wenzelm
parents: 52111
diff changeset
    24
val new_id = string_of_int o Counter.make ();
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    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 results =
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    27
  Synchronized.var "Scala.results" (Symtab.empty: string Exn.result Symtab.table);
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    28
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    29
val _ =
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    30
  Isabelle_Process.protocol_command "Scala.result"
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    31
    (fn [id, tag, res] =>
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    32
      let
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    33
        val result =
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    34
          (case tag of
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    35
            "0" => Exn.Exn Null
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    36
          | "1" => Exn.Res res
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    37
          | "2" => Exn.Exn (ERROR res)
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    38
          | "3" => Exn.Exn (Fail res)
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    39
          | "4" => Exn.Exn Exn.Interrupt
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    40
          | _ => raise Fail ("Bad tag: " ^ tag));
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    41
      in Synchronized.change results (Symtab.map_entry id (K result)) end);
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    42
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    43
val _ = Session.protocol_handler "isabelle.Scala";
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    44
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    45
in
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    46
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    47
fun function name arg =
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    48
  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
    49
    let
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    50
      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
    51
      fun invoke () =
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    52
       (Synchronized.change results (Symtab.update (id, Exn.Exn Match));
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    53
        Output.protocol_message (Markup.invoke_scala name id) [XML.Text arg]);
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    54
      fun cancel () =
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    55
       (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
    56
        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
    57
      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
    58
        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
    59
          (fn tab =>
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    60
            (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
    61
              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
    62
            | SOME result => SOME (result, Symtab.delete id tab)
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    63
            | NONE => SOME (Exn.Exn Exn.Interrupt, tab)))
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    64
        handle exn => (if Exn.is_interrupt exn then cancel () else (); Exn.reraise exn);
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    65
    in
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    66
      invoke ();
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    67
      Exn.release (restore_attributes await_result ())
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    68
    end) ();
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    69
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    70
end;
71871
28def00726ca more robust isabelle.Functions --- avoid Java reflection with unclear class/object treatment;
wenzelm
parents: 71849
diff changeset
    71
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    72
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    73
72151
64df1e514005 clarified protocol: ML worker thread blocks and awaits result from Scala, to avoid excessive replacement threads;
wenzelm
parents: 72103
diff changeset
    74
(** registered Scala functions **)
71872
b5191ededb6c check Scala source snippets from ML;
wenzelm
parents: 71871
diff changeset
    75
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    76
fun functions () = space_explode "," (getenv "ISABELLE_SCALA_FUNCTIONS");
71872
b5191ededb6c check Scala source snippets from ML;
wenzelm
parents: 71871
diff changeset
    77
71911
d25093536482 clarified signature;
wenzelm
parents: 71898
diff changeset
    78
fun check_function ctxt arg =
71912
b9fbc93f3a24 clarified markup;
wenzelm
parents: 71911
diff changeset
    79
  Completion.check_entity Markup.scala_functionN
b9fbc93f3a24 clarified markup;
wenzelm
parents: 71911
diff changeset
    80
    (functions () |> sort_strings |> map (rpair Position.none)) ctxt arg;
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    81
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    82
val _ = Theory.setup
71898
4df341249348 discontinued pointless document antiquotation;
wenzelm
parents: 71881
diff changeset
    83
 (ML_Antiquotation.inline_embedded \<^binding>\<open>scala_function\<close>
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    84
    (Args.context -- Scan.lift Parse.embedded_position
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    85
      >> (uncurry check_function #> ML_Syntax.print_string)) #>
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    86
  ML_Antiquotation.value_embedded \<^binding>\<open>scala\<close>
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    87
    (Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, arg) =>
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    88
      let val name = check_function ctxt arg
71de0a253842 proper check of registered Scala functions;
wenzelm
parents: 71876
diff changeset
    89
      in ML_Syntax.atomic ("Scala.function " ^ ML_Syntax.print_string name) end)));
71872
b5191ededb6c check Scala source snippets from ML;
wenzelm
parents: 71871
diff changeset
    90
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    91
end;