When loaded, this module starts an embedded instance of Julia.
Expressions may be executed (either discarding the return value or
printing it in julia) or evaluated, unifying certain Julia return types
with Prolog terms.
See library(dcg/julia)
for description of valid expression terms.
The type val
denotes values that can be exchanged with Julia by
direct conversion, rather than via strings. It is a union:
val == integer | float | string | bool | symbol | void | arr_int64(_) | arr_float64(_)
| tuple() | tuple(A) | tuple(A,B) | tuple(A,B,C) | ...
bool ---> true; false.
symbol ---> :atom.
void ---> nothing.
arr_int64(D) ---> int64(shape(D), nested(D,integer)).
arr_float64(D) ---> float64(shape(D), nested(D,float)).
tuple() ---> #()
tuple(A) ---> #(A)
tuple(A,B) ---> #(A,B)
These types are mapped to and from Julia types as follows:
Int64 <--> integer
Float64 <--> float
String <--> string
Symbol <--> symbol
Bool <--> bool
Void <--> void
Tuple{...} <--> tuple(...)
A D-dimensional arrays is represented using a shape(D)
, which is list of
exactly D integers, in REVERSE ORDER from the Julia form, and a D-level nested
list containing the values, where the first dimension is the length of the outer
list, the second dimension is the length of each 1st level nested list, and so on.
These arrays can be passed to jl_call/N, returned from jl_eval, and also
passed to the Julia DCG via the higher-level term level evaluators/executors.
- jl_exec(+S:text) is det
- Execute Julia expression S, which may be string, atom, codes or chars.
If any Julia exceptions are raised, the Julia function
showerror
is used
to print the details, and a julia_error
exception is raised.
- jl_eval(+S:text, -X:val) is det
- Evaluate Julia expression S, which may be string, atom, codes or chars.
The following Julia return types are recognised and are converted to Prolog values
and unified with X:
Any other return type raises an
unsupported_julia_return_type_error
exception.
If any Julia exceptions are raised, the Julia function showerror
is used
to print the details, and a Prolog julia_error
exception is raised.
- jl_call(+F:text, +Arg:val, -Result:val) is det
- jl_call(+F:text, +Arg1:val, +Arg2:val, -Result:val) is det
- jl_call(+F:text, +Arg1:val, +Arg2:val, +Arg3:val, -Result:val) is det
- Evaluates F to get a function and applies it to given arguments.
The arguments are converted to Julia values directly in C code, which
should be faster than conversion via DCG formatting and parsing in Julia.
- jl_call_(+F:text, +Arg:val) is det
- jl_call_(+F:text, +Arg1:val, +Arg2:val) is det
- jl_call_(+F:text, +Arg1:val, +Arg2:val, +Arg3:val) is det
- Same as jl_call/3, jl_call/4 etc but ignoring the return value.
This can be useful when the return value is not supported by pljulia.
- jl_apply(+F:text, +Args:list(val), -Result:val) is det
- Evaluates Julia expression F to a function and applies it to any number
of arguments in Args, unifying the return value with Result.
- jl_apply_(+F:text, +Args:list(val)) is det
- Evaluates Julia expression F to a function and applies it to any number
of arguments in Args and discarding the return value.
- !(+E:expr) is det
- Execute Julia expression E using jl_exec/1. No output is printed.
E is converted to a string using term_jlstring/2.
Use
debug(julia)
to see the Julia expression before execution.
- ?(+E:expr) is det
- Execute Julia expression term E using (!)/1 and display the result using Julia.
- ?>(+E:expr, -X:val) is det
- Evaluate the Julia expression term E using jl_eval/2 and unify result with X.
E is converted to a string using term_jlstring/2.
Use
debug(julia)
to see the Julia expression before execution.
- <?(-X:val, +E:expr) is det
- Evalute Julia expression E and unify result with X.
X <? E
is the
same as E ?> X
, but may be more convenient for interactive use.
- ??(-X:val, +E:expr) is det
- Evalute Julia expression E as with (<?)/2, but instead of converting the
result into a Prolog representation, store it in a freshly allocated Julia
workspace variable and unify X with a term containing the name of the new
variable. This can be used in subsequent Julia expressions, but it cannot
be passed to jl_apply/4 and jl_apply_/3. When X goes out of scope, garbage
collection on the Prolog side releases the value in the Julia workspace
so that it becomes available for garbage collection on the Julia side.
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
- jl_call(Arg1, Arg2, Arg3, Arg4)
- jl_call_(Arg1, Arg2, Arg3)
- jl_call(Arg1, Arg2, Arg3, Arg4, Arg5)
- jl_call_(Arg1, Arg2, Arg3, Arg4)
- term_jlstring(Arg1, Arg2)