01 — facts & rules
A program is a knowledge base: facts, and rules over facts.
man(socrates).
mortal(X) :- man(X).§ 02 — Eras · 1972
state the facts, ask the question — logic, executable
Left: era context, features, and the 101 hello. Right: 3 core concepts, 3 deeper 201 lessons, the factorial specimen, and where Prolog lives today — each column scrolls on its own.
Natural-language questions and theorem-proving didn’t fit control flow. Prolog made logic itself executable: facts, rules, unification.
The engine’s search is opaque, performance is a puzzle, and the fifth-generation hype wrote checks it couldn’t cash.
Session one is the smallest program that still counts as speaking Prolog: what every tutorial prints first, in the dialect that mattered when the language arrived (1972).
?- write('Hello, world'), nl.101First contact — hello world and 3 ideas that define how Prolog reads. 201 — 3 lessons on mechanisms you meet after the factorial specimen: the sharp edges practitioners actually use.
3 fragments, each naming a habit of mind in Prolog. Read the note, then the code — together they are the 101 in one sitting.
01 — facts & rules
A program is a knowledge base: facts, and rules over facts.
man(socrates).
mortal(X) :- man(X).02 — the query
You ask; Prolog searches and unifies to find the proof.
?- mortal(socrates).
true.03 — recursion over lists
Pattern matching IS the control flow — each clause is one case.
len([], 0).
len([_|T], N) :- len(T, M), N is M + 1.The 201 assumes you have read the 101 and the specimen. Each lesson is one mechanism — what it is for, how it looks in code, and the trap or payoff that makes Prolog distinctive at scale.
Naive list recursion is quadratic; the classic fix is a difference list with an accumulator — the same shape as a tail call in a functional language.
app([], Ys, Ys).
app([X|Xs], Ys, [X|Zs]) :- app(Xs, Ys, Zs).The cut prunes the search space, and negation-as-failure turns failure into a goal — powerful, and a reliable source of “it works except when…” bugs.
parent(X, Y) :- mother(X, Y), !. % commit to first solution
not_mother(X, Y) :- \+ mother(X, Y).Memoized tables turn runaway recursion into a lookup, and CLP(FD) adds arithmetic constraints solved by propagation — the practical Prolog toolkit.
:- table f/2.
f(N, F) :- N > 0, F is N * F1, F1 = F(N-1).The rule: state the relation, ask the question — and the machine searches for its own proof. Logic itself, executable.
mortal(X) :- man(X).
man(socrates).
?- mortal(socrates).
true.installbrew install swi-prolog
runswipl fact.pl
IBM’s Watson, code analyzers, scheduling and constraint systems — anywhere the problem is really search.