Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Clausal Prolog

Prolog in Python for Neurosymbolic AI

Documentation: clausal.pl · start with the Tutorial.

Neural networks are good at perception, language and pattern-matching, but are slow, resource-hungry, unreliable and need to be constrained. Symbolic AI is good at rules, reasoning and guarantees but can't deal with the real world. Clausal Prolog puts both in one Python process: a neural network can call symbolic logic that in turn calls neural predicates, nested as deeply as you like, with no serialisation, IPC or second runtime in between. Combining the two gives systems that are:

  • Explainable: conclusions come from rules you can read and a derivation you can trace, not only from weights.
  • Reliable: hard constraints such as regulations, safety rules and business policy hold exactly, every time, rather than approximately.
  • Data-efficient: knowledge you can state as a rule doesn't have to be learned from examples.
  • Correctable: change a rule and the behaviour changes, with no retraining.

Clausal Prolog is a Prolog implemented in Python that aims for ISO Prolog conformity. Logic programs go in .clausal files, written in Clausal Prolog: ISO syntax without cut or committed choice, which guarantees properties of programs that let you realise The Power of Prolog. Existing ISO Prolog goes in .pl files. The seam (.seam) is a Python-syntax surface for the adapters that give controlled access* to the whole Python ecosystem. All three import with Python's standard import system, and the import rules preserve each surface's guarantees. The engine includes constraint solving, tabling, DCGs and a large standard library.

*Python seam adaptors assume the Python programmer takes full responsibility for correctness (in the Pythonic way).

The package is clausal: pip install clausal, import clausal.

Three source surfaces

Extension Surface Syntax Use it for
.clausal Clausal Prolog ISO Prolog, without cut Your logic programs. The default choice.
.pl ISO Prolog Regular ISO Prolog, cut included External Prolog code, for Prolog systems such as Scryer or Trealla
.seam Seam Python syntax The boundary with Python: ++ escapes, hosted Python, adapters

All three are importable modules once clausal is imported. In one directory, name.seam beats name.clausal, which beats name.pl.

Changed in 1.0 (the extension flip). .clausal used to be the Python-syntax seam language. It is now Clausal Prolog, and seam source is named .seam. A seam file still named .clausal fails to load with a Prolog syntax error. To migrate, run git mv name.clausal name.seam for each one. Move it rather than copying it: a name.clausal left beside name.seam is a different module, written in Prolog. See CHANGELOG.md.

Features

  • Clausal Prolog: ISO syntax, ISO builtin names and error terms, "…" as a string, and modules with module/2, use_module/1,2 and end_module/1
  • Purity by design: no cut, -> or *->; use dif/2, if_/3 from library(reif), constraints and first-argument indexing instead
  • Constraint solving: CLP(Z) for integers, CLP(B), CLP(Q), CLP(R) and dif/2
  • SLG tabling and well-founded semantics for negation over tabled predicates
  • DCGs: --> grammar rules with phrase/2,3
  • Regular ISO Prolog alongside: .pl files, run in the Scryer or Trealla embeddings, or imported directly (experimental)
  • Python interop through the seam: library(...) facades over the engine's Python modules (library(json), library(datetime), …), Python-free .seam modules, and allowlisted Python bridges
  • ISO direction: ISO builtin names, ISO error terms and ISO modules; where ISO is silent, Scryer Prolog's behaviour
  • C extensions: logic variables, trail-based backtracking and a trampoline, with no WAM

Installation

pip install clausal

Requires Python ≥ 3.13 and a C compiler (pip uses it automatically when building from source).

Quick start

A Clausal Prolog module

% family.clausal
:- module(family, [grandparent/2, fib/2, path/2]).
:- use_module(library(clpz)).

parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

% CLP(Z) arithmetic: fib/2 runs in both directions.
fib(0, 0).
fib(1, 1).
fib(N, F) :-
    N #> 1,
    N1 #= N - 1, N2 #= N - 2,
    F #>= N1, F #= F1 + F2,
    fib(N1, F1), fib(N2, F2).

% Tabling terminates on the cycle.
:- table(path/2).
edge(1, 2).
edge(2, 3).
edge(3, 1).
path(X, Y) :- edge(X, Y).
path(X, Y) :- edge(X, Z), path(Z, Y).

test("tom's grandchildren") :- grandparent(tom, ann), grandparent(tom, pat).
test("fib(10) = 55") :- fib(10, 55).
test("which fib is 55?") :- once(fib(N, 55)), N == 10.
test("path reaches the whole cycle") :- findall(Y, path(1, Y), Ys), msort(Ys, [1, 2, 3]).
test("tom has no grandparent", fail) :- grandparent(_, tom).

:- end_module(family).

A Clausal Prolog module file must end with :- end_module(Name)..

Running it

clausal family.clausal -g "grandparent(tom, X)"   # X = ann.  X = pat.
clausal --test family.clausal                      # runs the test clauses

clausal FILE on its own runs the program's main/0. See docs/cli.md.

Querying it from Python

Python asks the questions from a .seam file, with a goal in for position after --:

# app.seam
-import_from(family, [grandparent, fib])
-private([tom])

for GRANDCHILD in --grandparent(tom, GRANDCHILD):
    print(GRANDCHILD)               # ann, then pat

for N in --fib(N, 55):
    print(N)                        # 10
    break
python -c "import clausal, app"     # import clausal installs the import hook

See Python Integration for more ways to query.

Constraints, DCGs and reified conditions

% puzzles.clausal
:- module(puzzles, [greeting//0, send_more/1, classify/2]).
:- use_module(library(clpz)).
:- use_module(library(reif)).

greeting --> [hello], name.
name --> [world].
name --> [prolog].

send_more([S,E,N,D,M,O,R,Y]) :-
    Vars = [S,E,N,D,M,O,R,Y],
    Vars ins 0..9, all_different(Vars),
    S #\= 0, M #\= 0,
    1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E #=
        10000*M + 1000*O + 100*N + 10*E + Y,
    label(Vars).

% if_/3 instead of (Cond -> Then ; Else): it stays correct when X is unbound.
classify(X, R) :- if_(X = a, R = yes, R = no).

test("dcg") :- phrase(greeting, [hello, prolog]).
test("send more money") :- send_more([9,5,6,7,1,0,8,2]).
test("if_") :- classify(a, yes), classify(b, no).
test("dif") :- dif(X, a), X = b.

:- end_module(puzzles).

What Clausal Prolog restricts

Clausal Prolog is ISO Prolog with a few deliberate rules. Each one is enforced when the file loads:

  • Cut-free. ! and -> are refused, and *-> is not an operator. Use dif/2, if_/3, once/1, constraints or first-argument indexing.
  • Modules close. A module file must end with :- end_module(Name).. A file can opt out with :- set_prolog_flag(require_end_module, false)..
  • One-way dependency on ISO Prolog. A .clausal module may not import a .pl module, which may use cut. It is refused with permission_error(access, prolog_module, M). A .pl module may import a .clausal one.
  • Python only through the seam. A .clausal file reaches Python through engine-shipped library(...) facades (for example :- use_module(library(datetime), [date_add/3]).), through Python-free .seam modules, or through .seam modules listed under [tool.clausal] python_bridges in the importing project's pyproject.toml. Anything else is refused with permission_error(import, python_bridge, M).

See docs/importing_prolog.md for the full rules.

ISO Prolog .pl files

A .pl file is regular, external ISO Prolog: it is not restricted to the cut-free subset, and that is why a .clausal module may not import one. To run full ISO Prolog, cut included, alongside Clausal Prolog, use the Scryer or Trealla embeddings.

Importing a .pl file straight into the engine (put it on sys.path and import it) is experimental and outside the 1.0 compatibility promise. That in-process loader does not run cut yet: a .pl file that uses ! or -> fails to load there. CLAUSAL_PL_FRONTEND selects its front end (translator by default, or native, the ISO reader that .clausal files always use).

The seam: .seam files

Code that must call Python lives in .seam files. These use the older Python-syntax surface, with the ++expr escape, hosted Python statements and adapters over Python libraries. A .clausal module imports a .seam module like any other (subject to the Python-bridge rules). Keep .seam modules small and write your logic in Clausal Prolog. clausal-fmt and clausal-rewrite work on .seam files only. See docs/clausal_prolog.md for how the two surfaces differ.

Testing

A module carries its tests inline as test/1 clauses, or as test/2 with the option fail for a goal that must have no solution:

test("fib(5) = 5") :- fib(5, 5).
test("no grandparent", fail) :- grandparent(_, tom).

Standalone runner:

python -m clausal.testing path/to/dir/              # every .seam, .clausal and .pl file
python -m clausal.testing family.clausal            # a single file
python -m clausal.testing -v clausal/examples/      # verbose

Via pytest: test clauses in .seam, .clausal and .pl files are collected automatically.

python -m pytest tests/ clausal/examples/ -q

Requirements

  • Python ≥ 3.13 (no other runtime dependencies)
  • C compiler (for building from source)

YAML support is the optional package clausal-yaml (pip install clausal[yaml]).

License

MIT

Metadata

Release files for clausal 1.0.0rc2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clausal 1.0.0rc2
File Size Uploaded
clausal-1.0.0rc2.tar.gz 3.7 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for clausal 1.0.0rc2
File
clausal-1.0.0rc2-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
clausal-1.0.0rc2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
clausal-1.0.0rc2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
clausal-1.0.0rc2-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
clausal-1.0.0rc2-cp314-cp314-macosx_10_15_x86_64.whl CPython 3.14 CPython 3.14 macOS 10.15+ x86-64 Details
clausal-1.0.0rc2-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
clausal-1.0.0rc2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
clausal-1.0.0rc2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
clausal-1.0.0rc2-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
clausal-1.0.0rc2-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details

Total release size: 30.0 MB

Release files / clausal-1.0.0rc2.tar.gz

Download URL clausal-1.0.0rc2.tar.gz
Size 3.7 MB
Tags Source
SHA-256 checksum
How to use checksums
ebe9162c4fb76c1b128e6b223afe40c917d4759cc6ae89571acd02e160e4afc7
BLAKE2b-256 checksum
How to use checksums
0489d6bdd9a62609f77b84eea7a8a4a0799ee700bb8fce81f1ce78e2664bd35a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp314-cp314-win_amd64.whl

Download URL clausal-1.0.0rc2-cp314-cp314-win_amd64.whl
Size 2.4 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
312e7827e77759e80ad401d0e442aa01376c3af53aca8263e6eae1d641600f68
BLAKE2b-256 checksum
How to use checksums
6240fb01e0f9f8d6143e4c50367cee9de86f4fd77a89f8653d7ccbb74d0e767d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL clausal-1.0.0rc2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 3.0 MB
Tags CPython 3.14 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
7bbf4c12370de4d9085ba14f2323cc2b981115a6a81c0fc0f39be97269529cf9
BLAKE2b-256 checksum
How to use checksums
48ded3e79830e62b556abd27f4e90eb22c40694e178b6371e01b2a79a46d6913
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL clausal-1.0.0rc2-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 2.9 MB
Tags CPython 3.14 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
b5b388832c470bde3f379726d5d2e0185fae23bbcc87a3805d2baf40420b91cc
BLAKE2b-256 checksum
How to use checksums
3c403118a3bea79ceedb66a0feca222c82070dc205e4d1707d13eea8b976e348
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp314-cp314-macosx_11_0_arm64.whl

Download URL clausal-1.0.0rc2-cp314-cp314-macosx_11_0_arm64.whl
Size 2.4 MB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5406f7af0448f0cd257610928f1209fd6ba5d4feedce3885a134d35af2762948
BLAKE2b-256 checksum
How to use checksums
587b4b5922a13d4007986bad1fc3b4b7b8cdee5087883a56ec68923377f5d4e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp314-cp314-macosx_10_15_x86_64.whl

Download URL clausal-1.0.0rc2-cp314-cp314-macosx_10_15_x86_64.whl
Size 2.4 MB
Tags CPython 3.14 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
4cdeacd6c504d597c63cdca9cea0b99fd63e30682dbb1c2ffbad149ca4be661d
BLAKE2b-256 checksum
How to use checksums
c7f3889e31b91741740c9c136f3af48fceedcc14be95336d7af5e626eaa04263
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp313-cp313-win_amd64.whl

Download URL clausal-1.0.0rc2-cp313-cp313-win_amd64.whl
Size 2.4 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
8a5b1acceee0d91619646faf24421516a237127a4ff7894e21fad5f8ddef310a
BLAKE2b-256 checksum
How to use checksums
078eb34d6f035b8372c323e068cf996da903665ab43af47743ac30c9650a86b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL clausal-1.0.0rc2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 3.0 MB
Tags CPython 3.13 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
8f0c70272287af51dc1b515f0b3d037551c29998f71d6558992a31ec3b06eea9
BLAKE2b-256 checksum
How to use checksums
483fa1bac01e7f061818ddaa55e4c6229284f98ddf0d14023195f7aef042f86e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL clausal-1.0.0rc2-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 2.9 MB
Tags CPython 3.13 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
6304824516099f630906c9244401720fa3e092d0db06edd09efa40c3d56501d8
BLAKE2b-256 checksum
How to use checksums
0e853fd6546eb005468d66f0c263ac7edc6aaf8591953120e9f0ef8483949117
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp313-cp313-macosx_11_0_arm64.whl

Download URL clausal-1.0.0rc2-cp313-cp313-macosx_11_0_arm64.whl
Size 2.4 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6aff6dbaad6b83f1f0a81f794c7a73150eea2b1422b81cc14166b4833a97d27f
BLAKE2b-256 checksum
How to use checksums
5aa520144c7f877ff9123a210878fb7aa8ce14cc5b4ee4a8b26c380aad62728d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release files / clausal-1.0.0rc2-cp313-cp313-macosx_10_13_x86_64.whl

Download URL clausal-1.0.0rc2-cp313-cp313-macosx_10_13_x86_64.whl
Size 2.4 MB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
71f723761937d6be2cd7de873509be34880e073b263076dbae253a93e2827cf6
BLAKE2b-256 checksum
How to use checksums
249a34adf74acbf367989a4084ec897fe8251d899959a82176dbcafaa76e7d74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 6, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0rc2 This release

11 release files

0.4.0

1 release file

0.3.1

1 release file

0.3.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page