Exception and Signal handling

Author(s): The CLIP Group.

This module includes predicates related to exceptions and signals, which alter the normal flow of Prolog.

Usage and interface

Documentation on exports

PREDICATE
catch(Goal,Error,Handler)

Executes Goal. If an exception is raised during its execution, Error is unified with the exception, and if the unification succeeds, the entire execution derived from Goal is aborted, and Handler is executed. The execution resumes with the continuation of the catch/3 call. For example, given the code

p(X) :- throw(error), display('---').
p(X) :- display(X).
the execution of "catch(p(0), E, display(E)), display(.), fail." results in the output "error.".
Meta-predicate with arguments: catch(goal,?,goal).

Usage: ISO

  • Calls should, and exit will be compatible with:
    (basic_props:term/1)Error is any term.
    (basic_props:callable/1)Handler is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold at call time:
    (basic_props:callable/1)Goal is a term which represents a goal, i.e., an atom or a structure.
  • The following properties hold upon exit:
    (basic_props:term/1)Error is any term.
    (basic_props:callable/1)Handler is a term which represents a goal, i.e., an atom or a structure.
  • The following properties hold globally:
    (basic_props:native/1)This predicate is understood natively by CiaoPP.

PREDICATE
intercept(Goal,Signal,Handler)

Executes Goal. If a signal is send during its execution, Signal is unified with the exception, and if the unification succeeds, Handler is executed and then the execution resumes after the point where the exception was thrown. To avoid infinite loops if Handler raises an exception which unifies with Error, the exception handler is deactivated before to execute Handler. Note the difference with builtin catch/3, given the code

p(X) :- send_signal(signal), display('---').
p(X) :- display(X).
the execution of "intercept(p(0), E, display(E)), display(.), fail." results in the output "error---.0.".
Meta-predicate with arguments: intercept(goal,?,goal).

Usage:

  • Call and exit should be compatible with:
    (basic_props:term/1)Signal is any term.
  • The following properties should hold at call time:
    (basic_props:callable/1)Goal is a term which represents a goal, i.e., an atom or a structure.
    (basic_props:callable/1)Handler is a term which represents a goal, i.e., an atom or a structure.
  • The following properties should hold upon exit:
    (basic_props:term/1)Signal is any term.

PREDICATE
throw(Ball)

Raises an error, throwing the exception Ball, to be caught by an ancestor catch/3. The closest matching ancestor is chosen. Exceptions are also thrown by other builtins in case of error.

Usage: ISOthrow(Term)

  • The following properties should hold at call time:
    (term_typing:nonvar/1)Term is currently a term which is not a free variable.

PREDICATE
send_signal(Signal)

Emits a signal, to be intercept by an ancestor intercept/3. The closest matching ancestor is chosen. If the signal is not intercepted, throws the error error(unintercepted_signal(Signal), send_signal/1-1).

Usage: send_signal(Term)

  • The following properties should hold at call time:
    (term_typing:nonvar/1)Term is currently a term which is not a free variable.

PREDICATE
send_silent_signal(Signal)

Emits a signal as send_signal/1, but do not throws an error if the signal is not intercepted (i.e. just suceeds silently)

Usage: send_silent_signal(Term)

  • The following properties should hold at call time:
    (term_typing:nonvar/1)Term is currently a term which is not a free variable.

PREDICATE
halt

Halt the system, exiting to the invoking shell.

Usage: ISO

  • The following properties should hold globally:
    (basic_props:native/1)This predicate is understood natively by CiaoPP.

PREDICATE
halt(Code)

Halt the system, exiting to the invoking shell, returning exit code Code.

Usage: ISO

  • The following properties should hold at call time:
    (basic_props:int/1)Code is an integer.
  • The following properties should hold upon exit:
    (basic_props:int/1)Code is an integer.

PREDICATE
abort

Abort the current execution.

Known bugs and planned improvements

  • Run-time checks have been reported not to work with this code. That means that either the assertions here, or the code that implements the run-time checks are erroneous.