Logic Programming

Every paradigm so far has told the computer how to compute something — a sequence of steps (structured), a set of messages between objects (object-oriented), a chain of function applications (functional). Logic programming instead tells the computer what is true, as a set of facts and rules, and leaves the "how" — the search for an answer — to the language's inference engine.

Facts and Rules

A logic program is a database of two kinds of statements:

  • Facts — unconditional statements: parent(tom, bob). means "tom is a parent of bob," full stop.
  • Rules — conditional statements: ancestor(X, Y) :- parent(X, Y). means "X is an ancestor of Y if X is a parent of Y."

There is no assignment, no loop, and no call stack in the sense other paradigms use those words. You ask a query — "is this true? for which values is this true?" — and the engine searches the facts and rules for an answer, trying different combinations of values and backtracking when a path doesn't lead anywhere.

Unification and Backtracking

Two mechanisms do all the work:

  • Unification — matching a query against facts/rules by finding values for variables that make both sides identical. Asking parent(tom, Who) unifies Who with every value that makes a stored fact true.
  • Backtracking — when a chosen path fails to satisfy the rest of the query, the engine automatically rewinds to the last choice point and tries a different value, instead of the programmer writing that retry logic by hand.

This combination is what lets a two-line rule like ancestor (see Prolog and Demo 9) answer every ancestor of a person, several generations deep, without a single explicit loop.

Declarative, Not Imperative

Logic programming is one flavor of the broader declarative style — you describe the problem's structure and let the system figure out execution, rather than prescribing execution yourself (the imperative style every paradigm on the "how" side of this list shares, structured programming included). SQL is declarative in the same sense, for a narrower domain: a SELECT describes the shape of the answer, not the steps to compute it.

Representative Languages

These four languages cover the classic form, the database form, the solver form and the engineered form of logic programming.

LanguageWhy study it
PrologThe original and canonical logic language (1972): facts, rules and queries that run in every direction.
DatalogA restricted subset that always terminates. It is widely used in databases and static analysis.
Clingo (Answer Set Programming)Describe a combinatorial problem with constraints and let the solver enumerate solutions.
MercuryLogic programming with static types, modes and determinism checking for large, fast programs.

See it in a full demo: Demo 9 — Prolog.

Where Logic Programming Shows Up

Pure logic languages are a small niche today, but the ideas are everywhere once you know to look: SQL's declarative queries, Datalog inside modern database engines and program analyzers, rule engines in expert systems, and constraint solvers used for scheduling and planning all inherit directly from this paradigm.