In the example above:
| ?- Lorry = scammell. Lorry = scammell ?
the message that Prolog returns shows that the variable "Lorry"
has acquired the value "scammell". If you are used to other programming
languages like Pascal you will be reminded of constructs like:
Lorry := scammell;
In Pascal, the variable has to be on the left-hand side of the ":=",
but look at this Prolog example:
| ?- scammell = Lorry.
Lorry = scammell ?
The variable doesn't have to be on the left-hand side of "=".You
may now be wondering what happens if both sides of "=" are
variables? In some programming languages, the notion of trying to
equate two variables without value will cause an instant failure message
- usually something like "Undefined scalar value". Prolog is far more
tolerant than most languages. Try an example, eg:
| ?- Lorry = Truck.
Truck = Lorry ?
yes
| ?- write(Truck).
_70
true ?
The example you try might well look generally the same, but the replies
might look different. What does "_70" mean? Prolog does some
neat work keeping track of what variables you use and what values
they have. To do this, one tactic used is for Prolog to have its own
internal variables. It makes these by starting them with an underscore
character ("_") and then appending a unique integer to it. So we now
know that variables may start either with an upper case letter or
an underscore character.
In the following example, Prolog has given Lorry and Truck
different internal variables. The last part of the query (Lorry=Truck)
essentially ensures that these two variables point to the same internal
variable, but this implementation of Prolog hides the detail from
us by using the names we've given to the variables. Theinternal
variable doesn't yet have a value itself, but when it does, Lorry
and Truck will also get this value.
| ?- write(Truck), write(Lorry), Lorry=Truck.
_70_100
Truck = Lorry ?
As a point of good practice, always use an upper case letter to start
your variables and try not to start them with an underscore followed
by other letters or numbers. If you start calling your variables things
like "_123" or "_65398" you will find that you will
muddle your own variables with Prolog's internal variables.
So far, our repertoire of Prolog is a bit limited. We'll extend
it further by allowing more than one goal in our queries. To have
more than one goal, we have to indicate how we want the goals connected.
For the time being we will use the "and" relation which in Prolog
is written as ",". We'll run two previous examples together:
| ?- Lorry = Truck, Lorry = scammell.
Lorry = scammell,
Truck = scammell ?
Lorry certainly will get the value "scammell", but
so also does Truck. Prolog does this by setting Truck
to whatever value Lorry has (or vice versa). If Lorry
or Truck doesn't have a value, then Prolog simply waits until
(if ever) one of the two variables gets a value. When one of the variables
gets a value, then the other variable also gets the value. (Prolog
manages to do this in part by simply making both variables have the
same value as the internal variable; once one of the variables gets
a value, Prolog gives that value to the internal variable, and so
any other variables that point to the internal variable also get the
value.)
We will give another example, this time using a new built-in predicate
called "write": what it does is fairly obvious.
| ?- Lorry = Truck, Lorry = scammell, write(Truck).
scammell
Lorry = scammell,
Truck = scammell ?
Here Truck has the same value as Lorry. The Prolog
interpreter works its way through each of the goals in the query,
starting from the left and working to the right. So with the query:
| ?- write(Lorry), nl, scammell = Lorry, write(Lorry).
_70
scammell
Lorry = scammell ?
in the first write, Lorry doesn't yet have a value,
but by the second write, it has acquired a value. (nl
simply makes sure that any output after it starts on a new
line.)
There remains one more part to this topic. What happens if there
is a contradiction in the query?
As a simple example, consider the following example:
| ?- car = nova.
no
Clearly the two atoms are not the same. An example with a conjunction
is only slightly more complicated:
| ?- Car = nova, fiesta = Car.
no
This makes an important point that in Prolog a variable can only have
one value during the execution of a query. So once Car has
the value "nova", it cannot reset the variable's value to
something else, eg "fiesta". This is completely unlike many
languages where you can assign different values to a variable at will.
This next example shows that Prolog doesn't remember the values of
variables between queries:
| ?- nova = Car.
Car = nova ?
| ?- Car = fiesta.
Car = fiesta ?
The effects of the query last only as long as the processing of the
current query. Put briefly, all the information in variables is lost
to Prolog after it has processed the final full stop of a query.
|