Written by Zhiwei Wang, 1996
Students taking class in the Department of Computer Science, University of Regina are permitted to reproduce this manual for the purpose of studying.

## 7.17.3. SYNTAX

Prolog programs are built from terms. A term is either a constant, a variable, or a structure. Each term is written as a sequence of valid characters.

### 7.17.3.1. CHARACTERS

The following are four categories of valid characters used in Prolog:

upper-case letters: A B ... Z

lower-case letters: a b ... z

digits: 0 1 2 3 4 5 6 7 8 9

symbols: + - * / \ ^ < > = ` " : . ? @ # \$ &

### 7.17.3.2. CONSTANTS

There are two kinds of constants: atoms and numbers.

#### 7.17.3.2.1. ATOMS

An atom is a constant constructed in one of the three ways below:

(1). Sequences of letters and digits. They must begin with a lower-case letter. Underscore might be used to improve the readability. For example:

michael
cs230
aBC
george_washington

(2). Sequences of symbols. For example:

:-
?-
=
<==>
<--
::=
-&-

(3). Arbitrary sequences of characters enclosed in a pair of single quotes. For example:

'Michael'
'George Washington'
'X + Y = Z'

#### 7.17.3.2.2. NUMBERS

Since logic programming deals mainly with formulas that are either true or false, real numbers are rarely used. However, many Prolog implementations support real numbers. The syntax of numbers can be seen from the following examples:

0
1996
-100
3.14
-100.01

### 7.17.3.3. VARIABLES

A variable is a sequences of letters, digits and underscores starting with an upper-case letter or an underscore. Examples of variables are:

Who
X
Job
Street_No
_33

A variable is instantiated if there is a value assigned to it, otherwise it is uninstantiated.

In some cases, we might not need to use a name for a variable. We can use the so-called anonymous variable, which is written as a single underscore character. When Prolog answers questions, the value of an anonymous variable is not output. Here is an example.

| ?- [example2].

{consulting /u2/zhiwei/language/prolog/example2...}
{/u2/zhiwei/language/prolog/example2 consulted, 50 msec 2592 bytes}
yes

| ?- child(Who, david).

Who = bob ? ;[return]

Who = jason ? ;[return]

no

| ?- child(_, david).

yes

The first query contains a variable 'Who'. To answer this query, Prolog looks at the consulted file 'example2', finds out a matching value 'bob', instantiates the variable 'Who' with the value, and output it. If we want to find out more values, we type a semicolon. Prolog will uninstantiate 'Who' from the current value 'bob', looks at the rest of the file, finds out another matching value 'jason', and output the new value. This process can be continued until there is no matching value. At that time Prolog output a 'no'.

However, if we only want to find out if David has any child but don't care who they are, we use anonymous variable as we did in the second query. To answer this query, Prolog confirms the fact with a 'yes' without telling who the children are.

### 7.17.3.4. STRUCTURES

Structures are objects that have several components. Here are some examples:

name(Given_name, Family_name)

date(Day, Month, Year)

dinner(Appetizer, Soup, Main_dish, Dessert)

To denote a structure we have to choose a functor. In the preceding examples, 'name', 'date' , and 'dinner' are functors. Components enclosed in brackets are called arguments. If more than one arguments are presented, they are separated by commas. The number of arguments is called the arity of the functor. The above structures represent unspecified items from a set of objects described by the functor. We can replace variables with constants to represent particular objects, for example:

name(Richard, Wang)

date(30, January, 1978)

dinner(appetizer, beef_soup, main_dish, cake)

The components themselves can, in turn, be structures. For example, the main dish may consist of meat and vegetables. In a particular case we might have the structure:

main_dish(roast_beef, broccoli)

In this way, the 'dinner' structure will become:

dinner(appetizer, beef_soup, main_dish(roast_beef,broccoli),cake)

### 7.17.3.5. LISTS

The list is an ordered sequence of any number of items. Elements of a list are written between square brackets and separated by commas. For example:

[mon, tue, wen, thu, fri, sat, sun]

is a list of the days of a week.

[1, 3, 5, 7, 9]

is a list of integers.

[a]

is a list containing only one element.

[]

is a list with no elements and is called an empty list .

Elements in a list can be any Prolog terms. For example:

[date(30, January, 1978),
date(23, August, 1945),
date(12, March, 1976)]

is a list of structures.

Elements in a list can also be lists:

[[a,b],[a],[b],[]]

The first element of a list is called the head. The rest of the list is called the tail. Here are some examples:

```          list             head                       tail
[a,b,c]                    a                          [b,c]
[a,b]                      a                          [b]
[[a],b]                    [a]                        [b]
[[a,b],[a],[b],[]]         [a,b]                      [[a],[b],[]]
[a]                        a                          []
```
`A vertical bar '|' can be used to separate the head from the tail. For example, the same list [a,b,c] can be written in the following ways:`
[a,b,c]
[a|[b,c]]
[a,b|[c]]
[a,b,c|[]]

To check that they are identical, you can use the identical equality sign '==':

| ?- [a,b,c] == [a|[b,c]].

yes

| ?- [a,b,c] == [a,b|[c]].

yes

| ?- [a,b,c|[]] == [a,b,c].

yes

| ?-

More about the different equalities will be shown in 7.17.3.7.

The operator '=..' can be used for constructing structures or instantiating lists. The syntax is:

X =.. L

which means: "L is the list consisting of the functor of X followed by the arguments of X". Here are some examples:

| ?- X =.. [parent, mary, tom].

X = parent(mary, tom)

| ?- main_dish(roast_beef, broccoli) =.. L.

L = [main_dish, roast_beef, broccoli]

A special case of lists is strings of characters. If a string of characters is enclosed in double quotes, the string is represented as a list of ASCII codes. For example, the string:

"system"

is written by Prolog as:

[115,121,115,116,101,109].

### 7.17.3.6. OPERATORS

#### 7.17.3.6.1. ARITHMETIC OPERATORS

Arithmetic operators can be used to perform arithmetic calculations:
```+          addition
-          subtraction
*          multiplication
/          division          The result is the whole number part of a division
mod        remainder         The result is the remainder of a division

The built-in predicate 'is' is used to evaluate an expression. For example, if we type:```
| ?- X is 1 + 2.

X = 3

#### 7.17.3.6.2. COMPARISON OPERATORS

The comparison operators are

>, <, >=, =<, =:=, =\=

Their meanings are listed below:

```X > Y               X is greater than Y
X < Y               X is less than Y
X >= Y              X is greater than or equal to Y
X =< Y              X is less than or equal to Y
X =:= Y             the values of X and Y are equal
X =\= Y             the values of X and Y are not equal```
`7.17.3.6.3. 'OP' DIRECTIVE`
New operators can be defined by inserting directives into the program. The general form for a directive is:

:- op(precedence, type, name).

The operator definition must appear before any expression using that operator. For example, the operator love can be defined as below:

:- op(600, xfx, love).

This clause tells Prolog system that we want to define an operator, the precedence of which is 600, the type is 'xfx', and the name is 'love'.

The form 'xfx' is a type specifier which suggests that the operator is between two arguments. In a type specifier, we use 'f' to denote an operator, 'x' and 'y' to denote arguments. 'x' represents an argument whose precedence is strictly lower than that of the operator, 'y' represents an argument whose precedence is lower than or equal to that of the operator. . There are three groups of operator types:

(1). infix operators:

xfx xfy yfx

(2). prefix operators:

fx fy

(3), postfix operators:

xf yf

Note that operations do not define any action, they only introduce new notation.

### 7.17.3.7. DIFFERENT TYPES OF EQUALITY

Equality in Prolog can be interpreted in various ways. The operators concerned are

=, ==, \==, =:=, =\=

Their meanings are listed below:

### X = Y               X is equal to Y, if X and Y match                            X == Y              X is literally equal to Y, if X and Y are identical          X \== Y             X is not literally equal to Y                                X =:= Y             the values of X and Y are equal                              X =\= Y             the values of X and Y are not equal                          X is Y              X is Y, if X matches the value of Y (where X is a                                variable or a constant and Y is an arithmetic expression    7.17.3.8. COMMENTS

Comments may be added to a program to make it more understandable. There are two kinds of comments: end-of-line comments and bracketed comments. An end-of-line comment begins with the character % and ends at the end of the current line. An end of line comment can also appear on a line by it self. A bracketed comment is any sequence of characters surrounded by comment brackets /* and */. Bracketed comments may cross line boundaries. Look at the example:

/* This is a bracketed comment */
/* A bracketed comment
can cross lines */
male(larry).
male(david).
% This is an end-of-line comment appearing on a line
child(david,larry).
father(X,Y) :- % An end-of-line comment
child(Y,X),
male(X).