5
6:- module(julia, [
7 jl_exec/1
8 , jl_eval/2
9 , jl_call/3, jl_call_/2
10 , jl_call/4, jl_call_/3
11 , jl_call/5, jl_call_/4
12 , jl_apply/3, jl_apply_/2
13 , (!)/1, (?)/1, (<?)/2, (?>)/2, (??)/2
14 , op(900,fx,?)
15 , op(900,fx,!)
16 , op(900,xfy,<?)
17 , op(900,xfy,??)
18 , op(900,yfx,?>)
19 ]).
64:- use_foreign_library(foreign(julia4pl)). 65:- reexport(dcg/julia).
86jl_call(F,X,R) :- jl_apply(F,1,[X],R).
87jl_call(F,X,Y,R) :- jl_apply(F,2,[X,Y],R).
88jl_call(F,X,Y,Z,R) :- jl_apply(F,3,[X,Y,Z],R).
95jl_call_(F,X) :- jl_apply_(F,1,[X]).
96jl_call_(F,X,Y) :- jl_apply_(F,2,[X,Y]).
97jl_call_(F,X,Y,Z) :- jl_apply_(F,3,[X,Y,Z]).
102jl_apply(F,Args,R) :- length(Args,N), jl_apply(F,N,Args,R).
107jl_apply_(F,Args) :- length(Args,N), jl_apply_(F,N,Args).
115!(Expr) :-
116 term_jlstring(Expr,Str),
117 debug(julia, 'Executing: ~s',[Str]),
118 jl_exec(Str).
119
120%! ?(+E:expr) is det.
121% Execute Julia expression term E using (!)/1 and display the result using Julia.
122?(Expr) :- !(ans=Expr :>: display(ans)), nl.
128?>(Expr, Result) :-
129 term_jlstring(Expr,Str),
130 debug(julia, 'Evaluating: ~s',[Str]),
131 jl_eval(Str,Result).
136<?(Result, Expr) :- Expr ?> Result.
146??($(WS), Expr) :- jl_ws_alloc(WS,Name), !Name=Expr.
147
148dcg_julia:pl2jl_hook(WS,Name) :- current_blob(WS,ws), !, jl_ws_name(WS,Name).
149user:portray($WS) :- current_blob(WS,ws), !, jl_ws_name(WS,Name), format("ws<~w>",[Name]).
150
151prolog:message(error(julia_error,_)) --> ['A Julia exception was thrown.'].
152prolog:message(error(unsupported_julia_return_type(C,T),_)) -->
153 ['Cannot convert Julia type `~s\' to Prolog value in context ~s'-[T,C]]
Embedded Julia
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:These types are mapped to and from Julia types as follows:
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. */