百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

trealla

> 编程语言
开源

这是一个紧凑、高效的 Prolog 解释器,使用的是普通的 C 语言编写的。

378 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

这是一个紧凑、高效的 Prolog 解释器,使用的是普通的 C 语言编写的。

Trealla Prolog

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.

Logo

Usage

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/sieve

Invocation 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.

Installing

On macOS and Linux, Trealla is in Homebrew core:

bash
brew install trealla-prolog

That 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.

Building

Written in plain-old C99.

bash
git clone https://github.com/trealla-prolog/trealla.git
cd trealla

On Debian-like systems, you will need to install (if not alread( the following packages to set up a build environment:

bash
sudo apt install build-essential git libedit-dev libffi-dev libssl-dev

Then...

make

To build without FFI:

make NOFFI=1

To build without SSL:

make NOSSL=1

To build without pre-emptive multi-threading support:

make NOTHREADS=1

To 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=1

Older compilers may require:

make NOPEDANTIC=1

to avoid issues with newer flags.

Finally...

make install

to install locally.

Optionally...

make test

and there should be no errors.

Further, to check for memory errors (out-of-bounds, use-after-free, null-pointer):

make clean && make sanitize && make test

Should 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 leaks

make 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:

bash
brew install libffi openssl coreutils

Building with Cosmopolitan

Cosmopolitan uses the included ISOCLINE.

make cosmo

Cosmopolitan can also boot bare-metal or Qemu via boot-sector.

Freestanding and bare-metal ports

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-smoke

Raspberry Pi 4

The 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 QEMU

Arduino Nano ESP32

The 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.

bash
source ~/.espressif/tools/activate_idf_v6.0.2.sh
make arduino-nano-esp32
cd ports/arduino-nano-esp32
idf.py -p /dev/cu. flash monitor

The 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.

Building with MUSL

On Ubuntu:

bash
sudo apt install musl-tools
make CC=musl-gcc OPT=-static NOFFI=1 NOSSL=1 ISOCLINE=1

WebAssembly (WASI)

Trealla 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 wasm

Setting WASI_CC also works as an alternative to CC.

Cross-compile for Windows x64

To cross-compile on Linux and produce a Windows/x86-64 executable...

bash
sudo apt install mingw-w64
make WIN=1 NOFFI=1 NOSSL=1
bash
	$ file tpl.exe
	tpl.exe: PE32+ executable (console) x86-64, for MS Windows

Some have reported success with a native Windows build using msys2.

Cross-compile for Linux x86

To cross-compile on Linux and produce a Linux/x86-32 executable...

bash
sudo apt install gcc-multilib
sudo apt install libssl-dev:i386 libffi-dev:i386 libreadline-dev:i386
make OPT=-m32
bash
	$ 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 stripped

Contributions

Contributions are welcome.

Acknowledgements

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)

Unbounded integers (Bigints) and Rationals

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

Strings

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.

Tabling

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.

bash
	:- 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.

bash
	:- 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,

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Cciso-prolog-standardprologprolog-implementation

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言