Mode Declarations

The user may declare input and output arguments of predicates using mode declarations. These declarations, for an n-ary predicate p, are of the form

:- mode p( Mode ).
mode/3 (D) where Mode consists of n mode values; or
:- mode(p, n, ModeList)
where ModeList is a list of mode values of length n. Mode values may be the following:
c, ++
Indicates that the corresponding argument position is always a ground term in any call to the predicate. The argument is therefore an input argument.

nv, +
Indicates that the corresponding argument position is always a nonvariable term (i.e. is instantiated) in any call in any call to the predicate. The argument is therefore an input argument.

f,
Indicates that the corresponding argument position is always an uninstantiated variable in any call to the predicate. The argument is therefore an output argument.

d, ?
Indicates that the corresponding argument may be any term in calls to the predicate.

For example, a 3-ary predicate p whose first argument is always a ground term in a call, whose second argument is always uninstantiated, and whose third argument can be any term, may have its mode declared as

:- mode p(++, –, d)
or as
:- mode(p, 3, [c, f, d]).
Currently, mode information is used by the compiler in two ways. First, it often allows more compact code to be generated. The second use is in guiding program transformations that allow faster code to be generated. For example, the predicate
part([], _, [], []).
part([E|L], M, [E|U1], U2) :- E =< M, part(L, M, U1, U2).
part([E|L], M, U1, [E|U2]) :- E  > M, part(L, M, U1, U2).
executes about 30% faster with the mode declaration
:- mode part(++, ++, -, -).
than without.