Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5

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.

Succeeds if Term is bound to an atom.
Succeeds if Term is bound to an integer or a floating point number.
Succeeds if Term currently is a free variable.
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.
The + and ? that appear before the arguments of predicates are a common notation and mean the following. + indicates that the immediately following argument must always be instantiated when calling the predicate. ? means that the argument may but need not be instantiated. Not surprisingly, - means that the corresponding argument should not be instantiated when the predicate is called.

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.

Back to the practical session of day 1.