Scryer Prolog documentation

Module ffi

:- use_module(library(ffi)).

Foreign Function Interface

This module contains predicates used to call native code (exposed by the C ABI). It uses libffi under the hood. The bridge is very simple and is very unsafe and should be used with care. FFI isn't the only way to communicate with the outside world in Prolog: sockets, pipes and HTTP may be good enough for your use case.

The main predicate is use_foreign_module/2. It takes a library name (which depending on the operating system could be a .so, .dylib or .dll file). and a list of functions. Each function is defined by its name, a list of the type of the arguments, and the return argument.

Types available are: sint8, uint8, sint16, uint16, sint32, uint32, sint64, uint64, f32, f64, cstr, void, bool, ptr and custom structs, which can be defined with foreign_struct/2.

After that, each function on the lists maps to a predicate created in the ffi module which are used to call the native code. The predicate takes the functor name after the function name. Then, the arguments are the input arguments followed by a return argument. However, functions with return type void or bool don't have that return argument. Predicates with void always succeed and bool predicates depend on the return value on the native side.

ffi:FUNCTION_NAME(+InputArg1, ..., +InputArgN, -ReturnArg). % for all return types except void and bool
ffi:FUNCTION_NAME(+InputArg1, ..., +InputArgN). % for void and bool


For example, let's see how to define a function from the raylib library.

?- use_foreign_module("./", ['InitWindow'([sint32, sint32, cstr], void)]).

This creates a 'InitWindow' predicate under the ffi module. Now, we can call it:

?- ffi:'InitWindow'(800, 600, "Scryer Prolog + Raylib").

And a new window should pop up!

foreign_struct(+Name, +Elements).

Defines a new struct type with name Name, composed of the elements Elements, which is a list of other types.

The name of the types doesn't matter, but the order of Elements must match the ones in the native code.


?- foreign_struct(color, [uint8, uint8, uint8, uint8]).