The predicates of this section implement control structures. Normally these constructs are translated into virtual machine instructions by the compiler. It is still necessary to implement these constructs as true predicates to support meta-calls, as demonstrated in the example below. The predicate finds all currently defined atoms of 1 character long. Note that the cut has no effect when called via one of these predicates (see !/0).
one_character_atoms(As) :- findall(A, (current_atom(A), atom_length(A, 1)), As). |
t1 :- (a, !, fail ; b). | % cuts a/0 and t1/0 |
t2 :- (a -> b, ! ; c). | % cuts b/0 and t2/0 |
t3 :- call((a, !, fail ; b)). | % cuts a/0 |
t4 :- | % cuts 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 ->
Then ;
fail), making the construct fail if the condition fails.
This unusual semantics is part of the ISO and all de-facto Prolog
standards.
\+
Condition, Else).
In other words, If
Condition succeeds at least once, simply behave as the
conjunction of Condition and Action, otherwise
execute Else.
The construct A *->
B, i.e. without
an
Else branche, is translated as the normal conjunction A,
B. (19)
+
refers to provable and the backslash (\
)
is normally used to indicate negation in Prolog).