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

Prolog Quick Reference

A condensed reference for Prolog syntax, essential built-in predicates, operators, and list notation for quick lookup while coding.

PracticeBeginner8 min readJul 10, 2026
Analogies

Prolog Quick Reference

This reference collects the syntax and built-in predicates you reach for constantly once you're actually writing Prolog: how facts, rules, and queries are structured, the handful of built-ins (findall/3, member/2, append/3, length/2) that show up in nearly every program, and the operators and list notation that trip up newcomers coming from imperative languages. It's meant to be skimmed for a specific answer, not read start to finish — jump to the section you need.

🏏

Cricket analogy: A quick reference for built-in predicates is like a bowler's mental cheat sheet of field settings for common match situations — you don't derive the right slip cordon from first principles every over, you recall the standard setup instantly.

Core Syntax: Facts, Rules, and Queries

A fact is a clause with no body, written as head. — e.g., likes(mary, wine). A rule adds a body after :- (read 'if'), with comma-separated goals meaning conjunction: happy(X) :- likes(X, wine), likes(X, cheese). means X is happy if X likes wine and X likes cheese. A query is typed at the top-level prompt as ?- Goal. and either succeeds with variable bindings, fails, or (for multiple solutions) can be re-run with ; to request the next answer via backtracking. Semicolon (;) inside a rule body means disjunction (or), and every clause must end with a period followed by whitespace, a detail that trips up newcomers when a period gets attached to the end of a number like 3.14 or a filename atom.

🏏

Cricket analogy: A fact like likes(mary, wine). is a settled entry in the record book, no conditions attached, whereas a rule like happy(X) :- likes(X, wine), likes(X, cheese). is a conditional statistic such as 'a batter's average only counts if they faced at least 10 innings' — true only when its conditions hold.

prolog
% Fact
likes(mary, wine).
likes(mary, cheese).

% Rule: comma = conjunction
happy(X) :- likes(X, wine), likes(X, cheese).

% Rule with disjunction
weekend_plan(X) :- likes(X, hiking) ; likes(X, cinema).

?- happy(mary).
true.

?- happy(X).
X = mary ;
false.

Essential Built-in Predicates

member(X, List) succeeds once per occurrence of X in List and can also be used to generate list elements via backtracking. append(A, B, C) both concatenates (when A and B are bound) and splits a list into every possible prefix/suffix pair (when C is bound and A, B are unbound) — this dual-direction behavior is a hallmark of well-written Prolog predicates. length(List, N) computes or checks the length, and can even generate lists of a given length when List is unbound. findall(Template, Goal, List) collects every solution of Goal into List (with duplicates and no bindings preserved outside the call), while bagof/3 and setof/3 are similar but fail if there are no solutions and support grouping with ^/2 to ignore free variables, with setof/3 additionally sorting and deduplicating the results.

🏏

Cricket analogy: append/3 working both to concatenate and to split, depending on what's bound, is like a scorer who can both merge two innings' ball-by-ball logs into one file and, given the combined file, work backward to figure out every possible split point between the two innings.

prolog
% member/2 checks membership or generates elements
?- member(2, [1,2,3]).
true.
?- member(X, [1,2,3]).
X = 1 ;
X = 2 ;
X = 3.

% append/3 concatenates or splits, depending on binding
?- append([1,2], [3,4], C).
C = [1, 2, 3, 4].
?- append(A, B, [1,2,3]).
A = [], B = [1, 2, 3] ;
A = [1], B = [2, 3] ;
A = [1, 2], B = [3] ;
A = [1, 2, 3], B = [].

% findall vs setof
?- findall(X, member(X, [3,1,2,1]), L).
L = [3, 1, 2, 1].
?- setof(X, member(X, [3,1,2,1]), S).
S = [1, 2, 3].

Operators, Arithmetic, and List Notation

Lists are written [H|T] for head/tail decomposition, [1,2,3] as sugar for nested cons cells ending in [], and [] is the empty list, distinct from the atom nil used in some other Lisp-family languages. Arithmetic comparison operators (<, >, =<, >=, =:=, =\=) evaluate both sides as expressions, unlike = and == which compare terms structurally without evaluation — notably =< (not <=) is the correct Prolog syntax for 'less than or equal', a common typo for newcomers from C-family languages. String-like data appears as either atoms (like 'hello'), character-code lists, or a dedicated string type in SWI-Prolog specifically, and which one a given program uses depends heavily on the Prolog implementation, so checking your system's flags (e.g., double_quotes) matters before assuming behavior.

🏏

Cricket analogy: The [H|T] list decomposition is like separating the current striker (head) from the rest of the batting order still to come (tail), a natural way to reason about who's up next versus everyone still waiting.

Common gotcha: =:= compares evaluated arithmetic values, so 3 =:= 3.0 succeeds, while = performs structural unification and 3 = 3.0 fails since an integer and a float are structurally different terms.

  • A fact is head. with no body; a rule is head :- body. where comma means conjunction and semicolon means disjunction.
  • Queries return bindings on success; typing ; requests the next solution via backtracking.
  • member/2, append/3, and length/2 all work in multiple directions depending on which arguments are bound — a hallmark of idiomatic Prolog.
  • findall/3 collects all solutions including duplicates; setof/3 additionally sorts and removes duplicates but fails on no solutions.
  • Lists use [H|T] notation for head/tail decomposition; [] is the empty list.
  • Use =< (not <=) for 'less than or equal', and remember =:= evaluates arithmetic while = does structural unification without evaluation.
  • String-like data (atoms, code lists, or SWI-Prolog's string type) varies by implementation — check your system's double_quotes flag before assuming behavior.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PrologStudyNotes#PrologQuickReference#Prolog#Quick#Reference#Core#StudyNotes#SkillVeris#ExamPrep