Introduction to Programming Languages/Simple Predicates

Prolog is a programming language built around the notion of symbols: an atom is a symbol. Hence, unless we bestow some particular semantics into symbols like '1', '2' and '+', they will be treated in the same way as 'a', '@hi' and 'dcc024'. To make this notion of symbol a bit more clear, let's consider the predicate below, which was designed with the intended purpose of computing the length of a list:

Even though this predicate has a pretty obvious implementation, if we run it, then, unless we are familiar with Prolog, we should get some unexpected result. For instance, in swi-prolog we get the following:

?- myLength([a, b, c], X). X = 0+1+1+1.

That is not quite what we wanted, was it? The point is that '0', '+' and '1' are just atoms for unification purposes. Consequently, '0 + 1' has no more meaning than 'a + b', as we can check in swi-prolog:

?- X = a + b. X = a+b.

?- X = 0 + 1. X = 0+1.

Of course, Prolog offers ways to understand '1' as the number that this symbol represents. Similarly, the language provides ways to interpret '+' as arithmetic addition. One such way is to use the keyword is. A term like X is 1 + 1 has the following semantics: the right side of the equation, in this case, '1 + 1' is evaluated using the traditional interpretation of addition, and the result is then unified with the variable 'X'. Given this new keyword, we can re-write the length predicate in the following way:

Notice that this predicate is not a tail recursive function. To make it so, we could add a third parameter to it, following the technique that we saw in the last chapter:

Using the keyword is we can write several simple predicates that work on lists. These predicates rely on the same principles seen earlier, when we looked into Functional Programming. We have a base case, that determines what must be done once we reach the empty list, and we have an inductive step, which deals with non-empty lists. For instance, below we have an implementation of the predicate sum, which adds up the elements in a list. For completeness, we show also its tail recursive version:

Below we have an implementation of the ever present factorial predicate. Notice that in this example we check if an expression encodes an integer. Prolog is a dynamically typed programming language. Thus, atoms are just atoms. In principle, they are not treated as any special type. This means that we can have lists of numbers and other symbols, e.g., [1, +, a], for instance.

In addition to the operator is, Prolog offers also the operator =:= to evaluate arithmetic expressions. The semantics of this operator is slightly different than the semantics of <tt>is</tt>. The term <tt>X =:= Y</tt> forces the evaluation of both, <tt>X</tt> and <tt>Y</tt>. The result of this evaluation is then unified. For instance:

?- 1 + 2 is 4 - 1. false.

?- 1 + 2 =:= 4 - 1. true.

?- X is 4 - 1. X = 3.

?- X =:= 4 - 1. ERROR: =:=/2: Arguments are not sufficiently instantiated

In the last query we got an error, because variable 'X' was not defined. We cannot evaluate an undefined variable for its arithmetic value. We would get the same error if that variable were the right side of an 'is' expression:

?- 1 is X. ERROR: is/2: Arguments are not sufficiently instantiated

Below we have an implementation of a naive greatest common divisor algorithm, which uses the <tt>=:=</tt> operator. We say that this algorithm is naive because it converges slowly, as we are using subtraction to equal the input numbers.

The faster Euclidean Algorithm would use division and remainders to reach a result faster.