Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 40 ยท Getting Started

Prolog Syntax and Terms: Atoms, Numbers, Variables, Compounds

Learn the building blocks of Prolog: atoms, numbers, strings, variables, compound terms, functor and arity, lists as terms, operators and comments.

Prolog has remarkably little syntax. Everything, whether data, a fact, a rule or a query, is built from one thing: the term. Once you can read terms, you can read any Prolog program. This lesson goes through each kind of term and then shows how they combine into clauses.

Atoms

An atom is a constant name. It stands for itself and has no other value. There are three ways to write one:

  • A lowercase letter followed by letters, digits and underscores: tom, red_apple, x1.
  • Any text in single quotes: 'Hello World', 'Tom', 'don''t' (a doubled quote stands for one). Quotes let an atom start with a capital letter or contain spaces.
  • A string of symbol characters: +, ->, =.., \=, @>=. Special atoms are [] (the empty list), {} and ;.

tom and 'tom' are the same atom. Tom is not an atom (see variables below). Atoms are not strings of characters you edit: they are indivisible names, though you can take them apart with predicates such as atom_chars/2 and atom_length/2.

Numbers

Integers (42, -7) have unlimited size in SWI-Prolog, so X is 2**200 just works. Floats are written with a decimal point and digits on both sides: 3.14, 1.0e10, -0.5. You can write other bases and character codes: 0xFF (255), 0b101 (5), 0'a (97, the code of a).

Variables

A variable starts with an uppercase letter or an underscore: X, Person, _result, _. A variable is a placeholder that may later be bound to a term. Two rules set Prolog apart from most languages:

  1. A variable is not a memory location. Once it is bound to a value inside one search path, it stays that value. You cannot do X = X + 1; that asks whether X equals a term containing itself (see unification).
  2. The scope of a variable is a single clause. X in one clause has nothing to do with X in another.

The lone underscore _ is the anonymous variable: every occurrence is a separate fresh variable, used when you do not care about a value, as in parent(_, ann).

Strings

In SWI-Prolog 7 and later, text in double quotes is a string object: "hello". It is different from an atom. Use string_concat/3, string_length/2, split_string/4 and format/2 for text. In older Prolog systems and under the ISO flag, double quotes make a list of character codes, so portable programs avoid them. Back-quoted text such as `abc` makes a list of codes.

Compound terms

A compound term has a name, called the functor, followed by arguments in parentheses with no space before the opening parenthesis:

PROLOG
parent(tom, bob)
point(3, 4)
person(name('Ann', 'Lee'), age(31))

The number of arguments is the arity. A functor is identified by name and arity written name/arity: parent/2, point/2. Two terms with the same name but different arities are unrelated: point/2 and point/3 can coexist. Arguments may be any terms, including other compounds, so terms form trees.

A compound is data, not a call. point(3, 4) is just a structure holding two numbers. Whether it is also a goal depends on the place where it appears.

Lists

A list is written [a, b, c]. Internally it is a compound term: in SWI-Prolog 7 and later the functor is '[|]' with arity 2 (older systems used '.'), and the empty list [] is a special constant. [a, b, c] is the same as '[|]'(a, '[|]'(b, '[|]'(c, []))). You write the notation [Head | Tail] to split off the first element. The whole next chapter is about lists, so here it is enough to know that they are ordinary terms.

Operators

Prolog lets you write some functors between their arguments. 3 + 4 is just a nicer way to write +(3, 4); it is a compound term with functor +/2. It is not evaluated unless you ask: X = 3 + 4 binds X to the structure 3+4, while X is 3 + 4 computes 7. Operators have precedence (how tightly they bind) and associativity:

Operators Meaning
:- --> clause neck (lowest precedence)
; -> or, if-then
, and
\+ not provable
= \= == is < > =< >= =:= comparison and unification
+ - addition, subtraction
* / // mod multiplication, division
** ^ power

So a + b * c is +(a, *(b, c)), because * binds tighter. Use parentheses when in doubt. You can inspect a term with functor/3, arg/3 and the univ operator =.., which converts between a term and a list:

PROLOG
?- X = a + b * c, X =.. L.
X = a+b*c,
L = [+, a, b*c].

?- functor(point(3, 4), Name, Arity).
Name = point,
Arity = 2.

?- arg(1, point(3, 4), A).
A = 3.

?- T =.. [likes, mary, wine].
T = likes(mary, wine).

Clauses and comments

Prolog source consists of clauses, each ended by a full stop followed by whitespace:

  • A fact: head.
  • A rule: head :- body1, body2.
  • A directive: :- goal. (run while loading)

The parts of a body are separated by commas, which mean "and". The comma is also what separates arguments, which is why foo(a, b) has two arguments but foo((a, b)) has one (a conjunction). Comments start with % and run to the end of the line, or sit between /* and */.

Common beginner errors: forgetting the final full stop, putting a space between the functor and ( (foo (a) is a syntax error), starting an atom with a capital letter, and ending the last goal of a rule with a comma.

Exploring terms with the stand-in

The mini-Prolog stand-in parses terms the same way: variables begin with a capital, compounds are tuples, lists are '.' pairs. The helper below prints the tree of a term so you can see how operators group. (The stand-in uses '.' for list cells; real SWI-Prolog uses '[|]'.)

Python Term trees (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should see a + b * c as + with the atom a and the compound *(b, c) as arguments: multiplication binds tighter. The list [x, y] appears as two nested . compounds ending in the atom [].

Functor and arity bookkeeping

Which predicates does a program define? Predicates are identified by name/arity. The next block lists them for a small database.

Python Predicate indicators (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note that point/2 and point/3 are listed separately, just as in real Prolog.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these is a variable?

    Names starting with an uppercase letter or underscore are variables. 'Tom' in quotes is an atom.
  2. 2. What is the functor and arity of likes(mary, wine)?

    The functor is likes and it has two arguments, so the predicate indicator is likes/2.
  3. 3. What does X = 3 + 4 do?

    = is unification, not evaluation. Use X is 3 + 4 to compute 7.
  4. 4. What is _ in a Prolog clause?

    Each _ is a fresh variable whose value is ignored.
  5. 5. Why is foo (a) a syntax error in standard Prolog?

    A compound term needs the parenthesis directly after the functor name.

Summary

  • Everything in Prolog is a term: atoms, numbers, strings, variables and compound terms.
  • Atoms start lowercase or are quoted; variables start with an uppercase letter or _.
  • A compound term has a functor and arguments; name/arity identifies a predicate.
  • Lists and operator expressions are ordinary compound terms; = does not evaluate arithmetic.
  • Clauses end with a full stop; % starts a comment.

Next: Your First Prolog Program