Announcing swipl-rs - a high-level interface to SWI-Prolog for rust

Hello friends,

I’ve just published swipl-rs version 0.3.0. swipl-rs is a rust library that allows you to easily write foreign libraries for SWI-Prolog in rust, or write binaries that embed SWI-Prolog in a larger rust program.

The library is set up in such a way that all interaction with SWI-Prolog is ‘safe’, meaning, it should be impossible to use the rust library in such a way that drives SWI-Prolog into undefined behavior, such as bad memory accesses and thread race conditions, unless you explicitely opt in to unsafe code.

Among other things, this means it’s impossible to use a term for a frame that no longer exists, or to retrieve the name of an atom that has (or could have) been garbage collected.

This crate defines a lot of macros that should make it easy to work with the library and do things like create complex nested terms, call arbitrary prolog code, and most importantly, define your own foreign predicates in rust.

Please have a look!

Crate
Repository
Documentation

You can check out some basic examples under the example dir of the repository here.
Or for a more involved example, since I’m currently rewriting terminus_store_prolog using swipl-rs, you can have a look at that ongoing work here.

Features:

  • Easy definition of foreign predicates.
  • Easy calling into prolog from rust
  • Proper term management with compile-time checking for validity of term references
  • Support for marshalling of all the basic data that can go into terms
  • Complex term creation
  • Support for defining blob types

I’ve been hinting at this project for a long time, and now it’s finally in a state where I feel like I can show it off to others and they may actually get some use out of it.

There’s still quite some work to be done. For example, right now it is impossible to define a transparent foreign predicate (one which knows from which module it was called). Also, I feel some code could be more ergonomic, but I’d first like to get some hands-on experience with actually using the library before making changes to existing interfaces.

As for big features, the next big things I’ll be working on in this library are integration of rust’s async/await mechanism with prolog engines, support for auto-generation of foreign module initializers, and fully featured pack support. Please look forward to that!

I would very much like to hear what people think about this, so please tell me any feedback you can think of!

7 Likes