Many predicates can sensibly be called in different ways, e.g. with a
specific argument as input or as output. The header of the documentation
of a predicate consists of one or more templates, each
representing a specific way of calling the predicate.
A template can contain information about types, argument
instantiation patterns, determinism and more. The syntax is informally
|<template> ||::=||<head>['//']’is' <determinism> |
|<determinism> ||::=||’det' |
|<head> ||::=||<functor>’('<argspec> ’,' <argspec>’)' |
|<argspec> ||::=||[<instantiation>]<argname>[':'<type>] |
|<instantiation> ||::=||’++' | ’+' | ’-' | ’--' | ’?'
| ’:' | ’@' | ’!' |
|<type> ||::=||<term> |
The determinism values originate from Mercury. Their meaning
is explained in the table below. Informally,
det is used
for deterministic transformations (e.g. arithmetic),
multi for generators.
failure indicator is rarely used. It mostly appears in
hooks or the recovery goal of catch/3.
|Determinism||Predicate behaviour |
|Succeeds exactly once
without a choice point |
|Fails or Succeeds exactly once
without a choice-point |
|Always fails |
|No constraints on the number of
times the predicate succeeds and whether or not it leaves choice-points
on the last success. |
nondet, but succeeds
at least one time.
|Well founded semantics third
value. See undefined/0. |
The meanings of the instantiation patterns for individual
|++||Argument is ground at call-time, i.e., the argument
does not contain a variable anywhere. |
|+||Argument is fully instantiated at call-time, to a term
that satisfies the type. This is not necessarily ground, e.g.,
[_] is a list, although its only member is
|-||Argument is an output argument. It may be
unbound at call-time, or it may be bound to a term. In the latter case,
the predicate behaves as if the argument was unbound, and then unified
with that term after the goal succeeds. For example, the goal |
Goal, [T]) is good style and equivalent to
Xs), Xs = [T]3The ISO
standard dictates that
findall(X, Goal, 1) raises a
exception, breaking this semantic relation. SWI-Prolog does not follow
the standard here. Determinism declarations assume that the
argument is a free variable at call-time. For the case where the
argument is bound or involved in constraints,
det effectively becomes
|--||Argument is unbound at call-time. Typically used by
predicates that create‘something' and return a handle to the
created object, such as open/3
which creates a stream. |
|?||Argument is bound to a partial term of the
indicated type at call-time. Note that a variable is a partial term for
any type. |
|:||Argument is a meta-argument. Implies |
|@||Argument will not be further instantiated than it is
at call-time. Typically used for type tests. |
|!||Argument contains a mutable structure that may be
setarg/3 or nb_setarg/3. |
Users should be aware that calling a predicate with arguments
instantiated in a way other than specified by one of the templates may
result in errors or unexpected behavior.
Developers should ensure that predicates are steadfast with
respect to output arguments (marked - in the template). This means that
instantiation of output arguments at call-time does not change the
semantics of the goal (it may be used for optimization, though). If this
steadfast behavior cannot be guaranteed, -- should be used instead.
In the current version, argument types are represented by an
arbitrary term without formal semantics. In future versions we may adopt
a formal type system that allows for runtime verification and static
2000, Mycroft &
O'Keefe, 1984, Jeffery et
%! length(+List:list, -Length:int) is det.
%! length(?List:list, -Length:int) is nondet.
%! length(?List:list, +Length:int) is det.
% True if List is a list of length Length.
% @compat iso