Control¶
Control predicates manage execution flow — determinism, measuring performance, limiting time, and controlling search. For delayed execution, see Coroutining.
Examples on this page are Clausal Prolog
(.clausal): ISO Prolog syntax, without cut. (The seam,
Clausal's Python-syntax surface, spells the same goals in Python syntax.)
Determinism¶
once/1¶
once(Goal) is ISO's committing construct: call Goal and keep only its
first solution.
Refused in Clausal Prolog from 2.0
From release 2.0, Clausal Prolog refuses once/1, as it refuses \+/1,
forall/2, call_nth/2, setup_call_cleanup/3 and call_cleanup/2
(call_nth(G, 1) is once(G), and the cleanup pair commits too;
operator rulings 2026-11-28). It commits to a first
solution, which gives up monotonicity (adding a constraint can lose the
answer you wanted). A .clausal module that calls it does not load:
`once/1` (committed choice) 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
A goal built and called at run time (call/N, the goal of findall/3,
-g) throws error(existence_error(procedure, once/1), _):
test("once/1 is refused at run time") :-
G = once(true),
catch(call(G), error(existence_error(procedure, once/1), _), E = refused),
E == refused.
once/1 is still there in a .pl (full ISO) module and in the
seam and the Python API. In Clausal Prolog, write a predicate
that is deterministic by construction, or use if_/3
with a reifiable condition. Both are shown below.
Where once(in_(X, List)) took the first member, write the predicate so it
has only the one solution you want:
and, where once stood in for a condition (( Cond -> Then ; Else )),
use if_/3 with a reified condition such as memberd_t:
:- use_module(library(reif)).
listed(X, XS, R) :-
if_(memberd_t(X, XS), R = yes, R = no).
test("listed") :-
listed(2, [1, 2, 3], yes),
listed(9, [1, 2, 3], no).
Timing¶
time_goal/1¶
time_goal(Goal) — execute Goal and print the solution count, wall-clock
and CPU time to stderr. The goal's solutions pass through unchanged; the line
is printed once Goal is exhausted, so a caller that stops at the first
solution (once/1 in a .pl module, or an if -- goal in a seam file)
prints nothing.
Output (to stderr):
time_goal/2¶
time_goal(Goal, Elapsed) — execute Goal and unify Elapsed with the
wall-clock time in seconds (as a float). Useful for programmatic benchmarking.
call_with_time_limit/2¶
call_with_time_limit(Time, Goal) — run Goal for its first answer only
(its choice points are discarded, as ISO's once/1 does), but if
it has not answered after Time seconds of wall-clock time, throw the ball
time_limit_exceeded. This is SWI-Prolog's call_with_time_limit/2
(library(time)), with SWI's ball: the one existing Prolog code catches.
A timeout is never a failure.
spin(N) :- N1 is N + 1, spin(N1).
test("first answer only") :-
findall(X, call_with_time_limit(1, member(X, [a, b, c])), XS),
XS == [a].
test("a goal that runs out throws the ball") :-
catch(call_with_time_limit(0.2, spin(0)), BALL, true),
BALL == time_limit_exceeded.
Timeis a number of seconds, an integer or a float, greater than 0. Unbound:instantiation_error. Not a number:type_error(number, Time). Zero or less:domain_error(positive_number, Time).Goalis run ascall/1runs it, with call/1's errors.- Nesting. An inner limit never extends an outer one: the earlier
deadline wins. When the OUTER deadline passes while an inner call runs,
the outer call throws, and no
catch/3on the way takes the alarm — not one around the inner call, not a catch-all inside the goal. Only the call whose deadline passed throwstime_limit_exceeded; from there on it is an ordinary ball. (SWI-Prolog throws the same ball from wherever the alarm lands, so a catch-all inside the goal can take it there.) - What a timeout undoes is what any ball undoes, at the
catch/3that takes it: bindings and constraints made since are undone;assertzandretractare not (ISO). A tabled evaluation cut short leaves no table that a later call reads as complete. - Cleanup runs to its end. Once the alarm is on its way out, it is
not raised again: a
setup_call_cleanup/3cleanup, afinallyin Python code and a tabled evaluation's close run to completion, as for any other ball (SWI-Prolog does the same). - A swallowed alarm. If Python code inside the goal catches the alarm
and drops it (a library's
except Exception), it strikes again 50 ms later: inside a Python predicate in a formexcept Exceptiondoes not catch, elsewhere as the ordinary alarm, so either way it reaches its call and everycatch/3around that call can take the ball. If Python code turns the alarm into another error (except Exception: raise ValueError(...)), the timeout is the outcome where that error is converted or caught. The call throws the ball whatever the goal then answered: a result produced after the alarm struck is not trusted. (Python code that catches the alarm and keeps a reference to it, in a list say, is taken to be still unwinding; the goal then runs on until it returns, and its call throws the ball.) - A huge
Time(1.0e300,10**400) is capped at 10^9 seconds (about 31 years), which is no limit in practice. - Main thread only. The limit is enforced with
SIGALRM, which only the main thread receives. In another thread, or on a platform withoutSIGALRM, the call is refused witherror(system_error(time_limit_not_main_thread), call_with_time_limit/2)(time_limit_no_alarmwithoutSIGALRM); it never runs unlimited. - Alarms already set (the quad runner's, the test runner's, pytest-timeout's) keep working: while a limit is active the earliest of all deadlines is armed, an outer timer's handler runs at its own time, and the handler and the time left are restored when the last limit ends.
- Granularity. The alarm lands between Python bytecodes: a long call into C (a huge integer multiplication, a regular expression) finishes before the ball is thrown.
The sandbox uses the same mechanism, and the same ball, for the per-call budget of a vetted package adapter.
Patterns & Recipes¶
Benchmark two approaches¶
compare_approaches(GOAL_A, GOAL_B) :-
time_goal(GOAL_A, TIME_A),
time_goal(GOAL_B, TIME_B),
format("A: ~ws, B: ~ws~n", [TIME_A, TIME_B]).
Guard a test¶
A test passes when its body has a solution, so there is nothing to guard:
call the predicate with its inputs and its expected output, and no once is
needed.
Gotchas¶
once/1is refused in Clausal Prolog from 2.0 — at load time, and at run time withexistence_error(procedure, once/1), like\+/1,forall/2,call_nth/2,setup_call_cleanup/3andcall_cleanup/2. Clausal Prolog has no cut and no committed choice; see the alternatives above. A.plmodule, the seam and the Python API keep it.time_goal/1prints to stderr — not stdout. It won't interfere with write/writeln output.time_goal/2measures wall time — on a loaded system, wall time may be higher than CPU time. Use/1to see both.
See also: If-Then-Else — if_/3 and the reified conditionals,
Tabling — automatic memoization for performance,
Exception Handling — catch/throw for error control flow.