Logic, control and exceptions
Author: Daniel Diaz
Keywords: GNU Prolog, manual, Prolog, compiler, constraints, finite domains

### 8.18 Logic, control and exceptions

- abort/0, stop/0, top_level/0, break/0, halt/1, halt/0
- false/0, once/1, (\+)/1 - not provable, call/2-11, call_with_args/1-11, call_det/2, forall/2
- repeat/0
- between/3, for/3

#### 8.18.1 abort/0, stop/0, top_level/0, break/0, halt/1, halt/0

Templates

  abort
  stop
  top_level
  break
  halt(+integer)
  halt

Description

abort aborts the current execution. If this execution was initiated under a top-level the control is given back to the top-level and the message {execution aborted} is displayed. Otherwise, e.g. execution started by a initialization/1 directive (section 7.1.14), abort/0 is equivalent to halt(1) (see below).

stop stops the current execution. If this execution was initiated under a top-level the control is given back to the top-level. Otherwise, stop/0 is equivalent to halt(0) (see below).

top_level starts a new recursive top-level (including the banner display). To end this new top-level simply type the end-of-file key sequence (Ctl-D) or its term representation: end_of_file.

break invokes a recursive top-level (no banner is displayed). To end this new level simply type the end-of-file key sequence (Ctl-D) or its term representation: end_of_file.

halt(Status) causes the GNU Prolog process to immediately exit back to the shell with the return code Status.

halt is equivalent to halt(0).

Errors

Status is a variable |  | instantiation_error
Status is neither a variable nor an integer |  | type_error(integer, Status)

Portability

halt/1 and halt/0 are ISO predicates. abort/0, stop/0, top_level/0 and break/0 are GNU Prolog predicates.

#### 8.18.2 false/0, once/1, (\+)/1 - not provable, call/2-11, call_with_args/1-11, call_det/2, forall/2

Templates

  false
  once(+callable_term)
  \+(+callable_term)
  call(+callable_term, +term,…, +term)
  call_with_args(+atom, +term,…, +term)
  call_det(+callable_term, ?boolean)
  forall(+callable_term, +callable_term)

Description

false always fails and enforces backtracking. It is equivalent to the fail/0 control construct (section 7.2.1).

once(Goal) succeeds if call(Goal) succeeds. However once/1 is not re-executable on backtracking since all alternatives of Goal are cut. once(Goal) is equivalent to call(Goal), !.

\+ Goal succeeds if call(Goal) fails and fails otherwise. This built-in predicate gives negation by failure.

call(Closure, Arg1,…, ArgN) calls the goal call(Goal) where Goal is constructed by appending Arg1,…, ArgN (1 ≤ N ≤ 10) additional arguments to the arguments (if any) of Closure.

call_with_args(Functor, Arg1,…, ArgN) calls the goal whose functor is Functor and whose arguments are Arg1,…, ArgN (0 ≤ N ≤ 10).

call_det(Goal, Deterministic) succeeds if call(Goal) succeeds and unifies Deterministic with true if Goal has not created any choice-points, with false otherwise.

forall(Condition, Action) succeeds if for all alternative bindings of Condition, Action can be proven. It is equivalent to \+ (Condition, \+ Action).

\+ is a predefined prefix operator (section 8.14.10).

Errors

Goal (or Condition or Action) is a variable |  | instantiation_error
Goal (or Condition or Action) 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 8.22.1) |  | existence_error(procedure, Pred)
Functor is a variable |  | instantiation_error
Functor is neither a variable nor an atom |  | type_error(atom, Functor)
Deterministic is neither a variable nor a boolean |  | type_error(boolean, Deterministic)
for call/2-11 the resulting arity of Goal (arity of Closure + N) is an integer > max_arity flag (section 8.22.1) |  | representation_error(max_arity)

Portability

false/0, call/2-8, once/1 and (\+)/1 are ISO predicates. call/9-11, call_with_args/1-11, call_det/2 and forall/2 are GNU Prolog predicates.

#### 8.18.3 repeat/0

Templates

  repeat

Description

repeat generates an infinite sequence of backtracking choices. 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. Repeat loops cannot contribute to the logic of the program. They are only meaningful if the action involves side-effects. The only reason for using repeat loops instead of a more natural tail-recursive formulation is efficiency: when the test fails back, the Prolog engine immediately reclaims any working storage consumed since the call to repeat/0.

Errors

None.

Portability

ISO predicate.

#### 8.18.4 between/3, for/3

Templates

  between(+integer, +integer, ?integer)
  for(?integer, +integer, +integer)

Description

between(Lower, Upper, Counter) generates an sequence of backtracking choices instantiating Counter to the values Lower, Lower+1,…, Upper.

for(Counter, Lower, Upper) is equivalent to between(Lower, Upper, Counter). This predicate is deprecated and new code should use between/3.

Errors

Counter is neither a variable nor an integer |  | type_error(integer, Counter)
Lower is a variable |  | instantiation_error
Lower is neither a variable nor an integer |  | type_error(integer, Lower)
Upper is a variable |  | instantiation_error
Upper is neither a variable nor an integer |  | type_error(integer, Upper)

Portability

GNU Prolog predicate.

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