
error.pl -- Error generating supportThis module provides predicates to simplify error generation and checking. It's implementation is based on a discussion on the SWI-Prolog mailinglist on best practices in error handling. The utility predicate must_be/2 provides simple run-time type validation. The *_error predicates are simple wrappers around throw/1 to simplify throwing the most common ISO error terms.
type_error(+ValidType, +Culprit)Suppose an argument must be a non-negative integer. If the actual argument is not an integer, this is a type_error. If it is a negative integer, it is a domain_error.
Typical borderline cases are predicates accepting a compound term,
e.g., point(X,Y). One could argue that the basic type is a
compound-term and any other compound term is a domain error. Most
Prolog programmers consider each compound as a type and would
consider a compound that is not point(_,_) a type_error.
domain_error(+ValidDomain, +Culprit)
existence_error(+ObjectType, +Culprit)
existence_error(+ObjectType, +Culprit, +Set)existence_error(ObjectType,
Culprit, Set)
permission_error(+Operation, +PermissionType, +Culprit)
instantiation_error(+FormalSubTerm)
uninstantiation_error(+Culprit)open(File, read, input) cannot succeed because the system will
allocate a new unique stream handle that will never unify with
input.
representation_error(+Flag)
syntax_error(+Culprit)
resource_error(+Resource)
must_be(+Type, @Term) is detatom, atomic, between, boolean, callable,
chars, codes, text, compound, constant, float,
integer, nonneg, positive_integer, negative_integer,
nonvar, number, oneof, list, list_or_partial_list,
symbol, var, rational, encoding, dict and string.
Most of these types are defined by an arity-1 built-in predicate of the same name. Below is a brief definition of the other types.
| acyclic | Acyclic term (tree); see acyclic_term/1 |
| any | any term |
between(FloatL,FloatU) | Number [FloatL..FloatU] |
between(IntL,IntU) | Integer [IntL..IntU] |
| boolean | One of true or false |
| callable | Atom or compound term |
| char | Atom of length 1 |
| chars | Proper list of 1-character atoms |
| code | Representation Unicode code point (0..0x10ffff) |
| codes | Proper list of Unicode character codes |
| compound | compound term |
compound(Term) | Compound with same name/arity as term; checks arguments |
| constant | Same as atomic |
| cyclic | Cyclic term (rational tree); see cyclic_term/1 |
| dict | A dictionary term; see is_dict/1 |
| encoding | Valid name for a character encoding; see current_encoding/1 |
| list | A (non-open) list; see is_list/1 |
list(Type) | Proper list with elements of Type |
| list_or_partial_list | A list or an open list (ending in a variable); see is_list_or_partial_list/1 |
| negative_integer | Integer < 0 |
| nonneg | Integer >= 0 |
oneof(L) | Ground term that is member of L |
| pair | Key-Value pair. Same as compound(any-any) |
| positive_integer | Integer > 0 |
| proper_list | Same as list |
| stream | A stream name or valid stream handle; see is_stream/1 |
| symbol | Same as atom |
| text | One of atom, string, chars or codes |
| type | Term is a valid type specification |
In addition, types may be composed using TypeA,TypeB,
TypeA;TypeB and negated using \Type.
is_of_type(+Type, @Term) is semidet
has_type(+Type, @Term) is semidet[multifile]
current_encoding(?Name) is nondet
current_type(?Type, @Var, -Body) is nondet