100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Prolog Syntax and Terms

A tour of Prolog's core data representation: atoms, numbers, variables, compound terms, and the [Head|Tail] list notation.

FoundationsBeginner10 min readJul 10, 2026
Analogies

Prolog Syntax and Terms

Every piece of data in Prolog — from a single number to an entire program — is represented as a term. Prolog recognizes four basic term types: atoms (lowercase-starting constants like paris or 'hello world' in quotes), numbers (integers like 42 or floats like 3.14), variables (uppercase-starting or underscore-starting placeholders like X or _Total), and compound terms (a functor with arguments, like point(3, 4) or date(2026, 7, 10)).

🏏

Cricket analogy: Prolog's term types are like a scorecard's data categories: an atom is a fixed label like 'boundary,' a number is a stat like 254, a variable is a placeholder like 'Batsman' before a name is filled in, and a compound term is a structured record like delivery(boundary, 4runs, over12).

Atoms, Numbers, and Variables

Atoms are Prolog's simplest constant type — symbolic names like red, john, or 'multi word atom' (quoted when they contain spaces or start uppercase) that always refer to themselves, never to something else. Numbers are standard integers and floating-point values usable in arithmetic via the is/2 operator, as in ?- Y is 3 * 7 + 1., while variables are single-binding placeholders within a proof — once a variable is bound during unification, it keeps that binding for the rest of that particular proof, and an unbound variable written as the underscore _ (the anonymous variable) means 'I don't care what matches here.'

🏏

Cricket analogy: An atom is like a fixed team name 'mumbai_indians' that always means the same franchise, a number is a stat like 187 (highest score), and the anonymous variable _ is like a scorecard placeholder for 'any bowler' when you only care that a wicket fell, not who took it.

Compound Terms and Lists

A compound term combines a functor (an atom) with one or more arguments, written functor(Arg1, Arg2, ...) — for instance, book(prolog_programming, sterling, 2019) has functor book and arity 3 (three arguments), and Prolog treats terms with the same functor but different arity as entirely distinct, e.g. point/2 and point/3 are unrelated predicates. Lists are a special, heavily-used compound term written with square brackets, such as [milk, eggs, bread] or the recursive [Head|Tail] notation, where Head unifies with the first element and Tail unifies with the remaining list — this pattern is the foundation of nearly all list-processing predicates in Prolog.

🏏

Cricket analogy: A compound term is like a structured stat entry innings(kohli, 82, 2026); a list is like a batting order [rohit, gill, kohli, iyer], and [Opener|Rest] unifying pulls out rohit as Opener while Rest becomes [gill, kohli, iyer], mirroring how a scorer peels off the first batter from the lineup.

prolog
% atoms, numbers, and a compound term as facts
color(sky, blue).
temperature(paris, 22.5).

% arithmetic with is/2
?- Y is 3 * 7 + 1.
Y = 22.

% compound term with functor book/3
book(prolog_programming, sterling, 2019).

% list processing with [Head|Tail]
first_item([Head|_], Head).

?- first_item([milk, eggs, bread], First).
First = milk.

A term's identity in Prolog is defined by both its functor name and its arity (number of arguments) together — written as name/arity, like book/3 or first_item/2. This is why documentation and error messages always cite predicates as name/arity, since book/2 and book/3 are entirely separate, unrelated predicates even though they share a name.

Lists in Prolog are internally just compound terms built from a list functor, so [1,2,3] is syntactic sugar for a nested structure equivalent to '[|]'(1, '[|]'(2, '[|]'(3, []))) in SWI-Prolog — you rarely need to think about this directly, but it explains why list operations are naturally written as recursive predicates matching [Head|Tail] rather than using index-based loops as in Python.

  • Prolog has four basic term types: atoms, numbers, variables, and compound terms.
  • Atoms are lowercase-starting symbolic constants that always refer to themselves.
  • Variables start with an uppercase letter or underscore; the anonymous variable _ matches anything without binding.
  • A compound term is a functor plus arguments, e.g. book(title, author, year).
  • A predicate's identity combines its name and arity, written name/arity, e.g. book/3.
  • Lists use [Head|Tail] notation to split the first element from the rest recursively.
  • Arithmetic requires the is/2 operator, as in Y is 3 * 7 + 1.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PrologStudyNotes#PrologSyntaxAndTerms#Prolog#Syntax#Terms#Atoms#StudyNotes#SkillVeris#ExamPrep