A compact, efficient Prolog interpreter written in plain old C.
A compact, efficient Prolog interpreter written in plain old C.
A compact, efficient ISO Prolog interpreter. Written in plain old C and using a plain old Makefile.
MIT licensed
Runs on Linux, Android, MacOS, *BSD, Windows, RISC/OS, Haiku, OpenIndiana, Solaris & Tribblix
Runs on many bare boards eg. RISC-V, ESP-32
Integers & Rationals are unbounded
Atoms and strings are UTF-8 of unlimited length
The default double-quoted representation is *chars* list
Strings & slices are efficient (especially with mmap'd files)
Effectively unlimited arity for compounds
REPL with history
Builds for Cosmopolitan, WebAssembly (WASI) & Go
Cosmopolitan builds can also boot with no host OS (bare-metal)
API for calling from C (or by using WASM from Go & JS)
Foreign function interface (FFI) for calling out to user code
Access SQLITE databases using builtin module (uses FFI)
FFIs for Raylib & Raymath
Concurrency via threads / tasks / futures / engines (aka. generators) & actors
Definite Clause Grammar (DCGs)
Attributed variables with freeze/2, dif/2 & when/2
Constraints: CLP(B) & CLP(Z)
Blackboarding primitives
Delimited continuations
Tabling
Socket(s) library
...
Rational trees ##EXPERIMENTAL##
FFIs for GNU Scientific Library (GSL) ##EXPERIMENTAL##
FFIs for PLplot ##EXPERIMENTAL##
Bi-directional Python interface (Janus) ##EXPERIMENTAL##Trealla Prolog has reached a stable state and is feature-complete as much as is planned. Only bug fixes and incremental improvements are to remain ongoing.
Available from: https://github.com/trealla-prolog/trealla.
Runs with Jupyter Notebooks.
tpl [options] [files] [-- args]where options can be:
-O0, --noopt - no optimization
-f - *.tplrc* not loaded
-l file - load file
file - load file
-g goal - query goal (only used once)
--library path - alt to TPL_LIBRARY_PATH env var
-t, --trace - trace
-q, --quiet - quiet mode (no banner)
-v, --version - version
-h, --help - help
-d, --daemonize - daemonize
-w, --watchdog - create watchdog
--autofail - autofail queries at the toplevel
--consult - consult from STDIN
--nolimit - no memory limit
--index-check - verify indexed lookups against a linear scan (debug, slow)For example:
tpl -g test2,halt samples/sieveInvocation without any goal presents the REPL.
The default path to the library is relative to the executable location.
The file ~/.tplrc is consulted on startup unless the -f option is present.
When consulting, reconsulting and deconsulting files the .pl version of the filename is always preferred (if not specified) when looking for a file.
On macOS and Linux, Trealla is in Homebrew core:
brew install trealla-prologThat puts tpl on the path, the library under share/trealla, and
trealla.1 in the man pages. The C embedding API comes with it, as
libtrealla.a and trealla.h, so samples/embed.c builds against it
directly.
Bottles are prebuilt for Apple Silicon and for Linux on both architectures, so those need no compiler. An Intel Mac has no bottle and builds from source, which wants the packages below.
Build it from source instead if you want a configuration the bottle does not carry - no FFI, no SSL, ISOCLINE in place of EDITLINE - or if you are targeting anything freestanding.
Written in plain-old C99.
git clone https://github.com/trealla-prolog/trealla.git
cd treallaOn Debian-like systems, you will need to install (if not alread( the following packages to set up a build environment:
sudo apt install build-essential git libedit-dev libffi-dev libssl-devThen...
makeTo build without FFI:
make NOFFI=1To build without SSL:
make NOSSL=1To build without pre-emptive multi-threading support:
make NOTHREADS=1To build (as a last resort) with the included ISOCLINE sources (most native builds default to EDITLINE; WASI uses its own simple line reader).
make ISOCLINE=1Older compilers may require:
make NOPEDANTIC=1to avoid issues with newer flags.
Finally...
make installto install locally.
Optionally...
make testand there should be no errors.
Further, to check for memory errors (out-of-bounds, use-after-free, null-pointer):
make clean && make sanitize && make testShould ideally show none (there may a few spurious errors). Note this does not check for leaks on macOS - AddressSanitizer's LeakSanitizer is Linux-only.
To check for leaks, on either platform:
make clean && make leakcheck && make leaksmake leaks picks the right tool for the platform it's run on:
valgrind on Linux, macOS's own leaks command elsewhere (valgrind
has no current Apple Silicon support). Either way it needs the
leakcheck build, not debug or sanitize - neither tool can inspect
a sanitizer-instrumented binary.
On macOS:
brew install libffi openssl coreutilsCosmopolitan uses the included ISOCLINE.
make cosmoCosmopolitan can also boot bare-metal or Qemu via boot-sector.
Trealla also has a freestanding profile, a QEMU RV32 reference firmware and a generic board adapter template. See the freestanding porting guide for the service contract, build shape and validation checklist.
make freestanding
make port-template-smoke
make qemu-riscv32-smokeThe Pi 4 adapter boots the BCM2711 bare metal, with no
operating system: it parks the spare cores, drops to EL1, brings up the MMU and
caches, and drives PL011 UART0 as the console. It needs the Arm GNU bare-metal
toolchain for aarch64-none-elf.
make rpi4 # ports/rpi4/kernel8.img, for the boot partition
make rpi4-smoke # build and boot it under QEMUThe freestanding profile also includes an ESP-IDF adapter for the Arduino
Nano ESP32. It targets the board's ESP32-S3, places Trealla's static BSS and
owned heap in the 8 MB PSRAM, embeds a Prolog smoke program in flash and uses
the native USB Serial/JTAG console.
source ~/.espressif/tools/activate_idf_v6.0.2.sh
make arduino-nano-esp32
cd ports/arduino-nano-esp32
idf.py -p /dev/cu. flash monitorThe run is successful when the serial console ends with
TREALLA NANO ESP32 COMPLETE. See the
Nano ESP32 port notes for memory figures,
configuration details and the full validation procedure.
On Ubuntu:
sudo apt install musl-tools
make CC=musl-gcc OPT=-static NOFFI=1 NOSSL=1 ISOCLINE=1Trealla has support for WebAssembly System Interface (WASI).
For an easy build envrionment, set up wasi-sdk. Binaryen is needed for optimization.
To build the WebAssembinary binary, set CC to wasi-sdk's clang:
make CC=/opt/wasi-sdk/bin/clang wasmSetting WASI_CC also works as an alternative to CC.
To cross-compile on Linux and produce a Windows/x86-64 executable...
sudo apt install mingw-w64
make WIN=1 NOFFI=1 NOSSL=1 $ file tpl.exe
tpl.exe: PE32+ executable (console) x86-64, for MS WindowsSome have reported success with a native Windows build using msys2.
To cross-compile on Linux and produce a Linux/x86-32 executable...
sudo apt install gcc-multilib
sudo apt install libssl-dev:i386 libffi-dev:i386 libreadline-dev:i386
make OPT=-m32 $ file tpl
tpl: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, BuildID[sha1]=31f643d7a4cfacb0a34e81b7c12c78410493de60, for GNU/Linux 3.2.0, with debug_info, not strippedContributions are welcome.
This project (in current incarnation) started in March 2020 and it would not be where it is today without help from these people:
- [Xin Wang](https://github.com/dram)
- [Paulo Moura](https://github.com/pmoura)
- [Markus Triska](https://github.com/triska)
- [Jos De Roo](https://github.com/josd)
- [Ulrich Neumerkel](https://github.com/uwn)
- [Guregu](https://github.com/guregu)For unbounded arithmetic Trealla uses a modified fork of the imath library, which is partially included in the source. Note, unbounded integers (aka. bigints) are for arithmetic purposes only and will give a type_error when used in places not expected. The imath library has a bug whereby printing large numbers becomes exponentially slower (100K+ digits).
Double-quoted strings, when set_prolog_flag(double_quotes,chars) is set (which is the default) are stored as packed UTF-8 byte arrays. This is compact and efficient. Such strings emulate a list representation and from the programmer point of view are very much indistinguishable from lists.
A good use of such strings is open(filename,read,Str,[mmap(Ls)) which gives a memory-mapped view of a file as a string Ls. List operations on files are now essentially zero-overhead! DCG applications will gain greatly (phrase_from_file/[2-3] uses this).
Both strings and atoms make use of low-overhead reflist-counted byte slices where appropriate.
A predicate declared :- table p/2 is memoized: each distinct call variant is computed once, its answers stored, and later calls read them back. That makes left-recursive definitions terminate where ordinary SLD resolution loops forever, and collapses exponential recomputation into lookup. Tables are keyed by variant — p(X,Y) and p(A,B) share one, p(X,X) does not — and are private to the thread that built them unless declared otherwise.
:- use_module(library(tabling)).
:- table path/2.
path(X,Y) :- path(X,Z), edge(Z,Y). % left-recursive
path(X,Y) :- edge(X,Y).
edge(a,b). edge(b,c). edge(c,a).
?- findall(Y, path(a,Y), Ys).
Ys = [b,c,a]Such a table is frozen once complete: it will not notice later changes to edge/2, and each thread builds its own. Declaring it incremental lifts the first restriction. Both halves opt in — the table, and each dynamic predicate it consults — after which an assert or retract invalidates the table and the next call recomputes it, rather than quietly serving stale answers.
:- use_module(library(tabling)).
:- dynamic(edge/2).
:- incremental(edge/2).
:- table path/2 as incremental.
path(X,Y) :- path(X,Z), edge(Z,Y).
path(X,Y) :- edge(X,Y).
edge(a,b). edge(b,c).
?- findall(Y, path(a,Y), Ys).
Ys = [b,c]
?- assertz(edge(c,d)), findall(Y, path(a,Y), Ys).
Ys = [b,c,d]Two other declarations are available. A mode-directed spec such as :- table cost(,,min) aggregates at insert instead of storing every answer: the argument written as min or max is combined rather than distinguished, so the table keeps one best answer per key made from the remaining arguments — turning "all paths" into "shortest path" with no separate minimisation pass. And :- table p/1 as shared publishes a table once complete so other threads reuse it instead of each building their own. Shared and incremental are mutually exclusive: invalidation rewrites a table, which is exactly what publication promises never happens.
Tables are dropped with abolish_table/1 for one predicate or abolish_all_tables/0 for all. set_prolog_flag(tabling,false) runs tabled predicates as plain calls,
No open issues yet, or sync has not completed.