|- Up -||Next >>|
Remember what we said about terms in Prolog in the very first lecture. We saw that there are different kinds of terms, namely variables, atoms, numbers and complex terms and what they look like. Furthermore, we said that atoms and numbers are grouped together under the name constants and constants and variables constitute the simple terms. The following picture summarizes this:
Sometimes it is useful to know of which type a given term is. You might, for instance, want to write a predicate that has to deal with different kinds of terms, but has to treat them in different ways. Prolog provides a couple of built-in predicates that test whether a given term is of a certain type. Here they are:
Tests whether the argument is an atom.
Tests whether the argument is an integer, such as
Tests whether the argument is a floating point number, such as
Tests whether the argument is a number, i.e. an integer or a float
Tests whether the argument is a constant.
Tests whether the argument is uninstantiated.
Tests whether the argument is instantiated.
So, let's see how they behave.
These three examples for the behavior of
atom/1 is pretty much what one would expect of a predicate for testing whether a term is an atom. But what happens, when we call
atom/1 with a variable as argument?
This makes sense, since an uninstantiated variable is not an atom. If we, however, instantiate
X with an atom first and then ask
atom(X), Prolog answers `yes'.
?- X = a, atom(X).
X = a
But it is important that the instantiation is done before the test:
?- atom(X), X = a.
float/1 behave analogously. Try it!
atomic/1 tests whether a given term is a constant, i.e. whether it is either an atom or a number. So
atomic/1 will evaluate to true whenever either
number/1 evaluate to true and it fails when both of them fail.
Finally there are two predicates to test whether the argument is an uninstantiated or instantiated variable. So:
Note that a complex term which contains uninstantiated variables, is of course not an uninstantiated variable itself (but a complex term). Therefore:
And again, when the variable
X gets instantiated
nonvar(X) behave differently depending on whether they are called before or after the instantiation.
?- X = a, var(X).
?- var(X), X = a.
X = a
|- Up -||Next >>|