Module 1
Prolog data types 1

 

 
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".
 


Finding out if a term is an uninstantiated variable or not?
 

It is sometimes useful to find out if a variable has been instantiated. We say a variable has been "instantiated" if it has acquired a value. If it is "empty" it is said to be "uninstantiated".

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(Term)

This is true if Term is a variable that has not been instantiated. Hence the following four examples:
     | ?- 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(Term)

This is the opposite of var(Term).
     | ?- 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.
 

 


 

What is the "type" of the term?
 

Programming languages generally have more than one data type. A data type is a description of the kind of values that a data object may have. To give a simple example. Arithmetic can only be done on numbers. So if we have the statement in a hypothetical language:
        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)

This is true if Term is instantiated to either an integer or a float.
     | ?- number(Term).

     no
     | ?- number(1991).

     yes
     | ?- number(17.5).

     yes
     | ?- number(twenty).

     no
 
integer(Term)

This is true if Term is instantiated to an integer.
     | ?- 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)

This is true if Term is instantiated to a float.
     | ?- float(Term).

     no
     | ?- float(1991).

     no
     | ?- float(17.5).

     yes
     | ?- float(three_point_one).

     no
 
atom(Term)

An atom in Prolog refers to a constant that is not a number. All the atoms we have seen thus far have been rather unimaginative. The rules for atoms are as follows:
  1. Any sequence of alphanumeric characters (ie A-Z, a-z, 0-9, and including _), providing the sequence starts with a lower-case letter.
  2. Any sequence from the set of characters: + - * / \ ^ < > = ' ~ : . ? @ # $ &. These characters cannot be mixed in with (1), so "alpha+beta" is not an atom.
  3. Any sequence of characters between single quotes: eg 'Quintus Prolog'. If you want to include an apostrophe in some text, then the apostrophe has to be entered twice: eg
  4. | ?- write('John''s bank balance is: ').
    John's bank balance is:
  5. Either ! or ;
  6. Either of the pairs of brackets [] or {}.
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)

This is true if Term has been instantiated to an atom or a number.
     | ?- 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

Take time to work through the following self-test: