|Did you know ...||Search Documentation:|
The predicates of this section implement control structures. Normally the constructs in this section, except for repeat/0, are translated by the compiler. Please note that complex goals passed as arguments to meta-predicates such as findall/3 below cause the goal to be compiled to a temporary location before execution. It is faster to define a sub-predicate (i.e. one_character_atoms/1 in the example below) and make a call to this simple predicate.
one_character_atoms(As) :- findall(A, (current_atom(A), atom_length(A, 1)), As).
|% prunes a/0 and t0/0|
|% prunes a/0 and t1/0|
|% prunes b/0 and t2/0|
|% prunes a/0|
|% prunes a/0|
Goal1, Goal2 :- Goal1, Goal2.
Goal1 ; _Goal2 :- Goal1. _Goal1 ; Goal2 :- Goal2.
If -> Then; _Else :- If, !, Then. If -> _Then; Else :- !, Else. If -> Then :- If, !, Then.
Please note that (If
-> Then) acts as (If
fail), making the construct fail if the condition fails.
This unusual semantics is part of the ISO and all de-facto Prolog
Please note that (if
->then;else) is read as ((if
and that the combined semantics of this syntactic construct as
defined above is different from the simple nesting of the two
individual constructs, i.e., the semantics of
when embedded in ;/2. See
call(Condition), Action).67Note that the Condition is wrapped in call/1, limiting the scope of the cut (!/0 If Condition does not succeed, the semantics is that of (
\+Condition, Else). In other words, if Condition succeeds at least once, simply behave as the conjunction of
call(Condition)and Action, otherwise execute Else. The construct is known under the name if/3 in some other Prolog implementations.
The construct A
*-> B, i.e.,
without an Else branch, the semantics is the same as (
This construct is rarely used. An example use case is the implementation of optional in sparql. The optional construct should preserve all solutions if the argument succeeds as least once but still succeed otherwise. This is implemented as below.
optional(Goal) :- ( Goal *-> true ; true ).
Now calling e.g.,
optional(member(X, [a,b])) has the
X=a and X=b, while
succeeds without binding X.
refers to provable and the backslash (
) is normally used to indicate negation in Prolog).