Prev Next Up Home Keys Figs Search New

Run-time Errors

Appeared in Volume 8/2, May 1995

Keywords: standard.

ss@seg.npl.co.uk
Roger Scowen
25th November 1994

The International Standard for Prolog (ISO/IEC 13211-1) distinguishes different kinds of failure and regards some of them as errors so that such mistakes will be easier to track down and correct.

The definition of each built-in predicate defines a set of error conditions. If any of them are satisfied, an error occurs which can be caught by programmers who wish to retain control, but by default will cause the program to take some implementation dependent action, probably outputting an error message.

But one of the reasons why it has taken so long to complete an International Standard has been the need to agree on the precise error conditions and outcome for every built-in predicate.

Some of the objectives have been:

Users can define their own error conditions by making the tests, and then calling: throw(E) where E is an appropriate term identifying the error.

For example, with the normal definition of reverse/2, a goal reverse(X, [1,2]) will go into a loop. By adding a clause to its definition, the programmer can make such cases give an instantiation error. For example:

% reverse(L1, L2) is true if 'L1' is a list, and
%  'L2' unifies with a list identical to 'L1'
%  except that the elements occur in opposite order.
reverse(X, _) :-
   var(X),
   throw(error(instantiation_error, ' argument 1 of reverse/2')).
reverse(XYZ, ZYX) :-
   reverse(XYZ, [], ZYX).

reverse([X | YZ], Acc, ZYX) :-
   reverse(YZ, [X | Acc], ZYX).
reverse([], ZYX, ZYX).
Prev Next Up Home Keys Figs Search New