Control Predicates

The backtracking control flow can be modified by the cut (!)/0. The cut will remove the choice points from the head to the cut, including a head choice point. Common programming patterns involving the cut are provided in the forms of the predicate once/1 and (\+)/1. Both predicates will remove any backtracking from the goal argument.

The interpreter has the capability to interrupt its normal flow by exception handling. An interruption happens when an exception is thrown or when a signal is raised. An exception can be an arbitrary Prolog term. Some exception terms are recognized by the interpreter so as to display a user-friendly stack trace. In particular we recognize:

error(Message, Context):
The exception is an error.
warning(Message, Context):
The exception is a warning.
cause(Primary, Secondary):
The exception is a composite of a primary exception and a secondary exception.

The predicate throw/1 can be used to throw an exception. If the context is a variable the predicate will automatically instantiate the variable with the current stack trace. The predicate catch/1 can be used to catch a thrown exception. The predicate will not catch reserved exceptions. Currently system errors are the only reserved exceptions. System errors are used when interrupting the execution of the interpreter.

The following control predicates are provided:
fail: [ISO 7.8.2]
false: [TC2 8.15.5]
The predicate fails.
true: [ISO 7.8.1]
otherwise:
The predicate succeeds once.
!: [ISO 7.8.4]
The predicate removes pending choice points between the first non-cut-transparent parent goal invocation and this goal and then succeeds once.
once(A): [ISO 8.15.2]
The predicate succeeds once if A succeeds. Otherwise, the predicate fails.
\+ A: [ISO 8.15.1]
When A succeeds, then the predicate fails. Otherwise, the predicate succeeds.
throw(E): [ISO 7.8.9]
The predicate fills the stack trace if necessary and then raises the exception E.
sys_raise(E):
The predicate raises the exception E.
catch(A, E, B): [ISO 7.8.9]
The predicate succeeds whenever A succeeds. When an exception is thrown during the execution of A, this exception is non-reserved and this exception unifies with E then the predicate succeeds whenever B succeeds. Otherwise, the exception is re-thrown.
sys_trap(A, E, B):
The predicate succeeds whenever A succeeds. When an exception is thrown during the execution of A and this exception unifies with E then the predicate succeeds whenever B succeeds. Otherwise, the exception is rethrown.
sys_error_type(E, T):
The predicate succeeds in T with the error type of exception E.

The following predicate properties for control predicates are provided:

sys_notrace:
The property indicates that the predicate does not appear in an exception context. The property can be changed for user predicates.

The following source properties for kernel predicates are provided:

sys_notrace:
The property indicates that a call from the given definition scope does not appear in an exception context. The property can be changed for user sources.

Kommentare