:- module(testmodule, []). :- use_module(library(lists)). :- use_module(library(math)). % require foreign code /* Extern declaration for builtins used by our C code */ :- extern(writeq(+string, +term)). :- extern(flush_output(+string)). :- extern(nl(+string)). /* Just an example of some other code callable from C */ :- extern(do_something(+string, -string)). do_something(Indata, Result) :- format(user_error, 'Prolog: do_something(~w, ~w)~n', [Indata, Result]),flush_output(user_error), name(Indata, L), reverse(L, R), name(Result, R). :- extern(do_something_illegal(+integer, -integer)). do_something_illegal(Indata, Result) :- format(user_error, 'Prolog: do_something_illegal(~w, ~w)~n', [Indata, Result]),flush_output(user_error), Result is integer(1000/Indata). :- extern(do_something_nondet(-double)). do_something_nondet(Result) :- format(user_error, 'Prolog: do_something_nondet clause 1~n', []),flush_output(user_error), sqrt(42,Result). do_something_nondet(Result) :- format(user_error, 'Prolog: do_something_nondet clause 2~n', []),flush_output(user_error), Result = 2.2. do_something_nondet(Result) :- format(user_error, 'Prolog: do_something_nondet clause 3~n', []),flush_output(user_error), Result = 3.3. %% specify that the predicate callback_pred(X,Y) should call the C %% function callback_fun(X) and set Y to the (integer) result. foreign(callback_fun, c, callback_pred(+integer, [-integer])). foreign_file('myforeign.o', [callback_fun]). % :- load_foreign_executable(myforeign), % myforeign.so must be available at run-time % abolish(foreign/3), abolish(foreign_file/2). :- load_foreign_files(['myforeign.o'], []), % myforeign.o must be available at qpc compile time abolish(foreign/3), abolish(foreign_file/2). :- extern(do_something_callback(+integer, -integer)). do_something_callback(Indata, Result) :- format(user_error, 'In Prolog do_something_callback(~w, ~w).~n', [Indata,Result]),flush_output(user_error), X1 is Indata+1, format(user_error, 'Prolog: calling callback_pred(~w, ~w).~n', [X1,X2]),flush_output(user_error), callback_pred(X1, X2), % call into C format(user_error, 'Prolog: called callback_pred(~w, ~w).~n', [X1,X2]),flush_output(user_error), Result is X2+1.