We have already seen three Prolog built-in predicates: =, write and nl. In this section we introduce the built-ins that are used to "inspect terms", as the manuals often put it. In Prolog, "term" is used to denote all the data objects in the language. So far, we have seen atoms and variables.
The previous section introduced Prolog's variable. It showed how Prolog's variables acquire values by unification. A variable that doesn't have a value is said to be "uninstantiated": once a variable has a value, it is said to be "instantiated".
In the following descriptions, the word "Term" is frequently used. A Term can be any piece of the Prolog language. As we have seen, Prolog variables usually begin with an upper-case letter, whereas literals usually begin with lower-case letters or numbers.
There are two built-in predicates var and nonvar. The meaning of these would be more obvious if they had been called uninstantiated_variable and not_uninstantiated_variable respectively. There would be, of course, the minor disadvantage that the names are somewhat longer and far more difficult to type.
| ?- var(Variable).
true ?
This example succeeds because Variable is an uninstantiated variable.
| ?- Car = bugatti, var(Car).
no
This fails because Car is instantiated before the statement var(Car).
| ?- var(Car), Car = bugatti.
Car=bugatti ?
This succeeds because Car is uninstantiated until after the statement var(Car).
| ?- var(unix).
no
This fails because unix is not a variable, but a Prolog atom.
| ?- nonvar(Variable).
no
As Variable is an uninstantiated variable, this fails.
| ?- Car = bugatti, nonvar(Car).
Car=bugatti ?
This succeeds because the variable Car is first instantiated and then tested.
| ?- nonvar(Car), Car = bugatti.
no
This fails because the variable Car is uninstantiated when it is tested.
| ?- nonvar(unix).
yes
This succeeds because the atom unix is by definition not an uninstantiated variable.
Variable1 + Variable2 = Result
We know from elementary arithmetic that we can only add two numbers together and that the result must be a number. Therefore Variable1, Variable2 and Result must be of the number data type.
We will start with numbers.
| ?- number(Term).
no
| ?- number(1991).
yes
| ?- number(17.5).
yes
| ?- number(twenty).
no
| ?- integer(Term).
no
| ?- integer(1991).
yes
| ?- integer(17.5).
no
| ?- integer(twenty).
no
In the implementation of Prolog that these notes were tested on, integers are restricted to the range -8388608 to 8388607. In addition to this base 10 notation, integers may also be written in any base between 2 and 9. The form is:
{-}<base>'<integer>
So 29 in base 2 would be 2'11101 and -29 in base 8 would be -8'35.
| ?- float(Term).
no
| ?- float(1991).
no
| ?- float(17.5).
yes
| ?- float(three_point_one).
no
Hence the following examples:
| ?- atom(sqUIrrEl).
yes
| ?- atom(SQuiRReL).
no
| ?- atom((debit)).
yes
| ?- atom('Robert Fitz Ralph (1190-1193)').
yes
| ?- atom(===>).
yes
| ?- atom([]).
yes
| ?- atom([squirrel]).
no
| ?- atomic(Term).
no
| ?- atomic(breakfast).
yes
| ?- atom(breakfast), atomic(breakfast).
yes
| ?- number(1991), atomic(1991).
yes
| ?- integer(1991), atomic(1991).
yes
| ?- float(17.5), atomic(17.5).
yes
These Pages are maintained by
Dr Peter Hancox
Last updated October 1998