Terms
Everything in Prolog is a term.
Everything in Prolog is a term.
term
├── atomic
│ ├── atom foo, 'Hello World', [], +, 'don''t'
│ ├── number 42, -7, 3.14, 1.0e10, 0'a (char code), 0xFF, 1r3 (rational, SWI)
│ └── string "text" (SWI-7+ default; ISO: list of codes)
├── variable X, _Name, _
└── compound term point(1, 2), f(g(X), [a]), [H|T], a + b5.1 Atoms
Start with a lowercase letter: hello, john_smith, x1.
Quoted: 'Hello World', '123', 'don''t'.
Symbol sequences: +, ->, :-, ===.
Special: [], {}, !, ;, , (quoted as ',').
5.2 Numbers
?- X = 42, Y = -3.5, Z = 1.0e3, C = 0'a, H = 0xff, B = 0b101, O = 0o17.
?- X is 2 ** 100. % unbounded integers (SWI, Scryer)
?- X is 1 rdiv 3. % rationals (SWI): X = 1r35.3 Variables
Start with an uppercase letter or _: X, Result, _Temp.
_ alone is the anonymous variable; each occurrence is distinct.
_Name variables are named but suppress "singleton" warnings.
Variables are logical: once bound (instantiated), they don't change (until backtracking undoes the binding).
Scope: a single clause.
5.4 Compound terms (structures)
Functor point/2, arguments 3 and 4. Compound terms represent data structures — no declarations needed.
point(3, 4)
date(2026, october, 25)
book(title('SICP'), author(abelson), year(1985))
tree(leaf, 5, tree(leaf, 7, leaf))5.5 Operators are compound terms
?- X = 1 + 2 * 3, X = +(1, *(2, 3)).
true.
?- write_canonical(a :- b, c). % shows ':-'(a, ','(b, c))5.6 Strings, chars & codes (SWI defaults)
Configure with :- set_prolog_flag(double_quotes, codes). (or chars, atom, string). See §18.
"hello" % string object (SWI-7); ISO default would be a list of codes
`hello` % list of codes in SWI: [104,101,108,108,111]
'hello' % atom
[h,e,l,l,o] % list of chars5.7 Type-checking predicates
var/1, nonvar/1, atom/1, number/1, integer/1, float/1, atomic/1, compound/1, callable/1, is_list/1, string/1, ground/1, is_dict/1, rational/1.
?- atom(foo), number(3), var(X), compound(f(x)), is_list([1,2]), ground(f(a)).
true.