Built-in predicates for testing whether a given term is of a particular type
Prolog provides a number of built-in predicates for testing whether a term is an atom, a variable, and similar things. Here are some of these predicates and their definition as given in the manual of SWI-Prolog.
atom(+Term)
- Succeeds if
Term
is bound to an atom. number(+Term)
- Succeeds if
Term
is bound to an integer or a floating point number. var(+Term)
- Succeeds if Term currently is a free variable.
nonvar(+Term)
- Succeeds if Term currently is not a free variable.
functor(?Term, ?Functor, ?Arity)
- Succeeds if Term is a term with functor Functor and arity Arity. If Term is a variable it is unified with a new term holding only variables. functor/3 silently fails on instantiation faults If Term is an atom or number, Functor will be unified with Term and arity will be unified with the integer 0 (zero).
arg(?Arg, ?Term, ?Value)
- Term should be instantiated to a term, Arg to an integer between 1 and the arity of Term. Value is unified with the Arg-th argument of Term. Arg may also be unbound. In this case Value will be unified with the successive arguments of the term. On successful unification, Arg is unified with the argument number. Backtracking yields alternative solutions. The predicate arg/3 fails silently if Arg = 0 or Arg > arity and raises the exception domain_error(not_less_then_zero, Arg) if Arg < 0.
Play a bit with these predicates to see how they work.
Note that there is not predicate for testing whether a term is a
complex term. Define such a predicate complex/1
.
Hint