Download SICStus Prolog User`s Manual

Transcript
Chapter 8: Built-In Predicates
+P -> +Q
171
[ISO]
When occurring as a goal, this construction is read as equivalent to
(P -> Q; fail)
if(+P,+Q,+R)
Analogous to
if P then Q else R
but differs from P -> Q ; R in that if(P, Q, R) explores all solutions to the
goal P. There is a small time penalty for this—if P is known to have only one
solution of interest, the form P -> Q ; R should be preferred.
In sicstus execution mode no cuts are allowed in P. In iso execution mode
cuts are allowed in P and their scope is the goal P.
once(+P)
otherwise
true
false
fail
repeat
[ISO]
Finds the first solution, if any, of goal P. Fails if no solutions are found. Will
not explore further solutions on backtracking. Equivalent to
(P -> true; fail)
[ISO]
These always succeed. Use of otherwise/0 is discouraged, because it is not as
portable as true/0, and because the former may suggest a completely different
semantics than the latter.
[ISO]
These always fail. Use of false/0 is discouraged, because it is not as portable
as fail/0, and because the latter has a more procedural flavor to it.
[ISO]
Generates an infinite sequence of backtracking choices. In sensible code,
repeat/0 is hardly ever used except in repeat loops. A repeat loop has the
structure
Head :...
save_state(OldState),
repeat,
generate(Datum),
action(Datum),
test(Datum),
!,
restore_state(OldState),
...
The purpose is to repeatedly perform some action on elements which are somehow generated, e.g. by reading them from a stream, until some test becomes
true. Usually, generate, action, and test are all determinate. Repeat loops
cannot contribute to the logic of the program. They are only meaningful if the
action involves side-effects.