next up previous contents index
Next: Directives Up: Programs Previous: Rules   Contents   Index

Control constructs

Prolog provides an operator, called cut, for controlling backtracking. A cut is written as $!$ in programs. A cut in the body of a clause has the effect of removing the choice points created by the goals to the left of it.

There are several control constructs for specifying conjunction, negation, disjunction, if-then-else, etc.

The construct (P,Q) denotes conjunction. It succeeds if both P and Q succeed.

The construct (not P) and fail_if(P) denote negation. It succeeds if P fails; otherwise, it fails. No negation is transparent to cuts. In other words, the cuts in a negation are effective only in the negation. No cut in a negation can remove choice points created for the goals to the left of the negation.

The construct (P;Q) denotes disjunction. It succeeds if either P or Q succeeds. When P fails, Q will be solved on backtracking. All disjunctions are transparent to cuts. A cut in P or Q will remove not only the choice points created for the goals to the left of the cut in P or Q but also the choice points created for the goals to the left of the disjunction.

The control construct (If->Then;Else) succeeds if (1) If and Then succeed, or (2) If fails and Else succeeds. If is not transparent to cuts, but Then and Else are transparent to cuts. The control construct (If->Then) succeeds if If and Then both succeed. Procedurally, it is equivalent to (If->Then;Fail).


next up previous contents index
Next: Directives Up: Programs Previous: Rules   Contents   Index
Neng-Fa Zhou
1999-11-24