Skip to content

Clausal Prolog

Clausal Prolog is the language of .clausal files: ISO Prolog syntax, ISO builtin names and ISO error terms, without cut. It is the default surface for writing logic programs, and the project is working towards ISO Prolog conformity. Where ISO is silent, Clausal Prolog follows Scryer Prolog.

The engine reads three source surfaces:

Extension Surface Syntax Read by
.clausal Clausal Prolog ISO Prolog, cut-free the native ISO front end, always
.pl ISO Prolog Regular ISO Prolog, cut included external Prolog systems (the Scryer and Trealla embeddings); experimental in-process import through the native ISO front end (CLAUSAL_PL_FRONTEND=translator selects the legacy translator)
.seam the seam Python syntax PredicateLoader

The seam is the boundary with Python. It is where the ++expr escape, hosted Python statements and the adapters over Python libraries live. Most of the language pages in these docs still show seam syntax, and they say so. The semantics they describe (unification, search, tabling, constraints, modules) are shared by all three surfaces.

The extension flip

Before 1.0, .clausal was 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. Move the file rather than copying it: a stale name.clausal beside name.seam is a different module, written in Prolog. In one directory, name.seam beats name.clausal, which beats name.pl.


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

:- 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).
% A finite domain makes the search terminate, so no once/1 is needed.
test(which_fib_is_55) :- N in 0..20, 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).

Query 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

The goal's variables become ordinary Python locals, bound to each answer in turn. Atoms used in a .seam file are declared, here with -private.

Run its tests with python -m clausal.testing family.clausal, or let pytest collect them (see Testing).


Libraries, constraints and DCGs

Load libraries with use_module/1,2. Grammar rules use --> and run with phrase/2,3.

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

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(reified_if) :- classify(a, yes), classify(b, no).
test(dif) :- dif(X, a), X = b.
test(double_quotes_are_strings) :- X = "abc", string(X).

:- end_module(puzzles).

The rules

Clausal Prolog is ISO Prolog with a few deliberate rules. Each one is checked when the file loads, and a violation is a load error naming the file and line.

Cut-free

! and -> (if-then, and if-then-else) are refused. *-> (soft cut) is not an operator in Clausal Prolog, so writing it is a syntax error:

c.clausal:2: `!` (cut) is refused: Clausal is cut-free with no committed
choice, by design (ruling): !, -> and *-> are refused

Use the pure alternatives instead. These are dif/2, if_/3 and the reified predicates from library(reif) (see If-Then-Else), constraints, and first-argument indexing (see Purity); or write the predicate so it is deterministic in the first place. The condition of if_/3 must be reifiable: a closure that answers true or false as its last argument, such as memberd_t(X, Xs) or X = Y, never a plain goal. The same rule applies to {!} in a grammar body.

Refused: \+, once/1, forall/2, call_nth/2 and the cleanup pair

From release 2.0, \+/1, once/1, forall/2, call_nth/2, setup_call_cleanup/3 and call_cleanup/2 are refused too (operator rulings 2026-11-28): each commits to a first solution or tests by failure, which gives up monotonicity (call_nth(G, 1) is once(G); setup_call_cleanup/3 runs its Setup as once/1, and the Cleanup of either runs once, at a time only the execution decides). A body that uses one does not load:

c.clausal:3: `\+` (negation as failure) is not provided in Clausal Prolog;
write dif/2, if_/3 or a deterministic predicate, or put the code in a .pl
module, which runs it as ISO

once/1, call_nth/2, setup_call_cleanup/3 and call_cleanup/2 are named "(committed choice)" and forall/2 "forall/2 (negation as failure)". A goal built and called at run time (call/N, the goal of findall/3, -g) is refused with error(existence_error(procedure, (\+)/1), _), and the same with the other five, the error an ISO system gives for a procedure it does not provide. A file that defines its own forall/2, call_nth/2, setup_call_cleanup/3 or call_cleanup/2 (none is an ISO built-in), or imports one, calls that predicate. All six remain in a .pl (full ISO) module; the seam keeps not Goal, once(), call_nth() and the cleanup pair, and the Python API is unchanged.

Kept, by the same rulings: memberchk/2 and \=/2, which are sound on ground terms and used everywhere (use memberd/2 and dif/2 when an argument can be unbound: memberchk/2 commits to the first match and \=/2 tests by failure, so with an unbound argument they can lose answers that a later binding would have given); and call_with_time_limit/2, a resource guard, which also keeps only a first answer.

Use the forms above instead: dif/2 for \+ X = Y, if_/3 with a reified condition, a predicate that is deterministic by construction, findall/3 with maplist/2 for forall/2, and findall/3 with nth1/3 for call_nth/2. A test that a goal fails is test(Name, fail) :- Goal.:

% pure.clausal
:- module(pure, [first_member/2, listed/3, positive/1]).
:- use_module(library(reif)).

% member(X, Xs), !   becomes a predicate that is deterministic by construction.
first_member(X, [X|_]).

% ( memberchk(X, Xs) -> R = yes ; R = no )   becomes if_/3 with a reified test.
listed(X, Xs, R) :- if_(memberd_t(X, Xs), R = yes, R = no).

test(first_member) :- first_member(X, [a, b]), X == a.
test(listed) :- listed(b, [a, b], yes), listed(c, [a, b], no).
test(listed_most_general) :- findall(R, listed(_, [a, b], R), [yes, yes, no]).
test(not_listed, fail) :- listed(c, [a, b], yes).

% forall(member(X, Xs), X > 0)   becomes maplist/2.
positive(X) :- X > 0.
test(all_positive) :- maplist(positive, [1, 2, 3]).

:- end_module(pure).

Modules end with end_module/1

A module file (one with :- module(Name, Exports).) must end with :- end_module(Name).. Only comments and layout may follow it. A missing directive is the ISO error error(existence_error(directive, end_module(Name)), load/1).

Whether it is required is decided by, most specific first:

  1. the file's own :- set_prolog_flag(require_end_module, true|false).;
  2. the process-wide setting: set_prolog_flag(require_end_module, V) run as a goal, or the environment variable CLAUSAL_REQUIRE_END_MODULE;
  3. the surface default: required for .clausal, not required for .pl.

See Importing Prolog for the full error table.

No imports of ISO Prolog

A .clausal module may not import a .pl module, because a .pl module may use cut. The refusal is permission_error(access, prolog_module, M). It applies to use_module and to the run-time routes (M:G, call/N of a qualified goal). The dependency is one-way: a .pl module may import a .clausal one. To reuse .pl code, convert it to .clausal, or wrap it in a .seam module.

Python only through the seam

Clausal Prolog reaches Python only through:

  1. engine-shipped modules: the library(...) facades over the engine's Python modules (library(json), library(datetime), library(units), …), the standard library and the engine's own adapters;
  2. Python-free .seam modules: Clausal code written in seam syntax. These are checked transitively;
  3. Python bridges: .seam modules that do run Python, listed in [tool.clausal] python_bridges of the nearest pyproject.toml above the importing .clausal file, optionally pinned by sha256.
:- use_module(library(datetime), [date_add/3]).   % a facade: allowed
:- use_module(helpers, [double/2]).               % Python-free helpers.seam: allowed

Anything else is refused at load with permission_error(import, python_bridge, M), and a direct import of a Python module by path with permission_error(access, python_module, M). See Importing Prolog: Python bridges and Python Integration.


Differences from the seam

If you know the seam syntax from the other pages of these docs, these are the spellings that change:

Seam (.seam) Clausal Prolog (.clausal)
Fact parent(tom, bob), parent(tom, bob).
Rule p(X) <- (q(X), r(X)) p(X) :- q(X), r(X).
Variables ALL_CAPS ISO: X, Rest, _
Arithmetic N1 == N - 1 N1 #= N - 1 (CLP(Z), preferred; works in both directions), or ISO N1 is N - 1
Unify X is Y X = Y
Module -module(m, [p(X)]) :- module(m, [p/1]). … :- end_module(m).
Import -import_from(lib, [p]) :- use_module(lib, [p/1]).
Table -table(path/2) :- table(path/2).
Grammar rule s >> (np, vp) s --> np, vp.
Test test("t") <- goal test(t) :- goal. (a string name, test("t"), also works)
Python ++expr, hosted Python not available: go through a .seam module

Tools

  • python -m clausal.testing and the pytest plugin run test/1 and test/2 clauses in .clausal files (Testing).
  • clausal-fmt and clausal-rewrite work on .seam files only, and refuse Prolog-syntax files.
  • To run unrestricted ISO Prolog (with cut) alongside Clausal Prolog, use the Scryer or Trealla embeddings.