5.2 | Prolog control constructs

5.2.1 | `true/0`, `fail/0`, `!/0`

Templates

  `true
  fail
  !`

Description
`true` always succeeds.
`fail` always fails (enforces backtracking).
`!` always succeeds and the for side-effect of removing all choice-points created since the invocation of the predicate activating it.
Errors
None.
Portability
ISO control constructs.

5.2.2 | `(',')/2` - conjunction, `(;)/2` - disjunction, `(-``>``)/2` - if-then

Templates

  `','(+callable_term, +callable_term)
  ;(+callable_term, +callable_term)
  -``>``(+callable_term, +callable_term)`

Description
`Goal1 , Goal2` executes `Goal1` and, in case of success, executes `Goal2`.
`Goal1 ; Goal2` first creates a choice-point and executes `Goal1`. On backtracking `Goal2` is executed.
`Goal1 -``>``Goal2` first executes `Goal1` and, in case of success, removes all choice-points created by `Goal1` and executes `Goal2`. This control construct acts like an if-then (`Goal1` is the test part and `Goal2` the then part). Note that if `Goal1` fails `-``>``/2` fails also. `-``>``/2` is often combined with `;/2` to define an if-then-else as follows: `Goal1 -``>``Goal2 ; Goal3`. Note that `Goal1 -``>``Goal2` is the first argument of the `(;)/2` and `Goal3` (the else part) is the second argument. Such an if-then-else control construct first creates a choice-point for the else-part (intuitively associated to `;/2`) and then executes `Goal1`. In case of success, all choice-points created by `Goal1` together with the choice-point for the else-part are removed and `Goal2` is executed. If `Goal1` fails then `Goal3` is executed.
`','`, `;` and `-``>` are predefined infix operators (section 6.14.10).
Errors

`Goal1` or `Goal2` is a variable |  | `instantiation_error`
`Goal1` is neither a variable nor a callable term |  | `type_error(callable, Goal1)`
`Goal2` is neither a variable nor a callable term |  | `type_error(callable, Goal2)`
The predicate indicator `Pred` of `Goal1` or `Goal2` does not correspond to an existing procedure and the value of the `unknown` Prolog flag is `error` (section 6.22.1) |  | `existence_error(procedure, Pred)`

Portability
ISO control constructs.

5.2.3 | `call/1`

Templates

  `call(+callable_term)`

Description
`call(Goal)` executes `Goal`. `call/1` succeeds if `Goal` represents a goal which is true. When `Goal` contains a cut symbol `!` (section 5.2.1) as a subgoal, the effect of `!` does not extend outside `Goal`.
Errors

`Goal` is a variable |  | `instantiation_error`
`Goal` is neither a variable nor a callable term |  | `type_error(callable, Goal)`
The predicate indicator `Pred` of `Goal` does not correspond to an existing procedure and the value of the `unknown` Prolog flag is `error` (section 6.22.1) |  | `existence_error(procedure, Pred)`

Portability
ISO control construct.

5.2.4 | `catch/3`, `throw/1`

Templates

  `catch(?callable_term, ?term, ?term)
  throw(+nonvar)`

Description
`catch(Goal, Catcher, Recovery)` is similar to `call(Goal)` (section 5.2.3). If this succeeds or fails, so does the call to `catch/3`. If however, during the execution of `Goal`, there is a call to `throw(Ball)`, the current flow of control is interrupted, and control returns to a call of `catch/3` that is being executed. This can happen in one of two ways:

- implicitly, when an error condition for a built-in predicate is satisfied.
- explicitly, when the program executes a call of `throw/1` because the program wishes to abandon the current processing, and instead to take an alternative action.

`throw(Ball)` causes the normal flow of control to be transferred back to an existing call of `catch/3`. When a call to `throw(Ball)` happens, `Ball` is copied and the stack is unwound back to the call to `catch/3`, whereupon the copy of `Ball` is unified with `Catcher`. If this unification succeeds, then `catch/3` executes the goal `Recovery` using `call/1` (section 5.2.3) in order to determine the success or failure of `catch/3`. Otherwise, in case the unification fails, the stack keeps unwinding, looking for an earlier invocation of `catch/3`. `Ball` may be any non-variable term.
Errors

`Goal` is a variable |  | `instantiation_error`
`Goal` is neither a variable nor a callable term |  | `type_error(callable, Goal)`
The predicate indicator `Pred` of `Goal` does not correspond to an existing procedure and the value of the `unknown` Prolog flag is `error` (section 6.22.1) |  | `existence_error(procedure, Pred)`
`Ball` is a variable |  | `instantiation_error`

If `Ball` does not unify with the `Catcher` argument of any call of `catch/3`, a system error message is displayed and `throw/1` fails.
When `catch/3` calls `Recovery` it uses `call/1` (section 5.2.3), an `instantiation_error`, a `type_error` or an `existence_error` can then occur depending on `Recovery`.
Portability
ISO control constructs.

Copyright (C) 1999,2000 Daniel Diaz
Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.
More about the copyright