#term #serialization #prolog #atom #dictionary


A high-level library for building SWI-Prolog extensions and embedding SWI-Prolog in rust applications

17 releases

0.3.14 Aug 3, 2023
0.3.13 Jan 17, 2023
0.3.12 Nov 10, 2022
0.3.7 May 9, 2022
0.3.4 Jul 6, 2021

#265 in Parser implementations

Download history 116/week @ 2023-06-07 113/week @ 2023-06-14 97/week @ 2023-06-21 121/week @ 2023-06-28 102/week @ 2023-07-05 126/week @ 2023-07-12 95/week @ 2023-07-19 129/week @ 2023-07-26 173/week @ 2023-08-02 128/week @ 2023-08-09 75/week @ 2023-08-16 64/week @ 2023-08-23 54/week @ 2023-08-30 74/week @ 2023-09-06 65/week @ 2023-09-13 81/week @ 2023-09-20

280 downloads per month


7.5K SLoC

swipl-rs swipl crate

This is the central swipl-rs crate, which implements a high-level interface to SWI-Prolog (wrapping the swipl-fli crate), and which exposes all macros used to generate bindings, foreign predicate definitions and blob types (exposing the swipl-macros crate).


Using swipl-rs

add the following line under your dependencies in your Cargo.toml file:

swipl = "0.3"

Then import swipl in your code using

use swipl::prelude::*;

See the examples in this repository and the documentation for more guidance.

Building SWI-Prolog foreign libraries

In order to use this library to build something that SWI-Prolog can use, you have to set your crate-type to cdylib, and provide a main function like so:

pub extern "C" fn install() {
    // ..etc

All predicates defined through the predicates! macro have a corresponding register_<name> function, which you'll have to call inside the install function to make this predicate known to prolog. In addition, you can do whatever else needs to happen at load time here.

For an example, have a look at the example project.

After building, your foreign library can be loaded in prolog like so:

?- use_foreign_library('target/debug/libswipl_module_example.so').

(Substitute your project name, and substitute release for debug if you did a release build).


At build time, the low-level swipl-fli crate will auto-discover your swipl installation and link against that. The shared objects of this installation may not be on your path though. In fact, usually they aren't. This means you cannot just run cargo test to run your unit tests, as this link location is not stored anywhere.

In order to run your unit tests, consider installing cargo-swipl like so:

cargo install cargo-swipl

You can then do cargo swipl test to run your tests. cargo-swipl will auto-discover your swipl installation in exactly the same way as the swipl-fli build, and adds it to the load path before calling cargo test.


~100K SLoC