#swi-prolog #blob #interface #embedding #extension #definition #applications

swipl

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

19 releases

0.3.16 Feb 20, 2024
0.3.15 Oct 18, 2023
0.3.14 Aug 3, 2023
0.3.13 Jan 17, 2023
0.3.4 Jul 6, 2021

#245 in Rust patterns

Download history 33/week @ 2023-12-04 29/week @ 2023-12-11 9/week @ 2023-12-18 15/week @ 2023-12-25 14/week @ 2024-01-01 59/week @ 2024-01-08 16/week @ 2024-01-15 15/week @ 2024-01-22 21/week @ 2024-01-29 16/week @ 2024-02-05 23/week @ 2024-02-12 246/week @ 2024-02-19 110/week @ 2024-02-26 39/week @ 2024-03-04 82/week @ 2024-03-11 54/week @ 2024-03-18

294 downloads per month

Apache-2.0/MIT

355KB
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).

Documentation

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:

#[no_mangle]
pub extern "C" fn install() {
    register_predicate_a();
    register_predicate_b();
    // ..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').
true.

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

Testing

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.

Dependencies

~2.8–5.5MB
~103K SLoC