The whole, before the parts

Logic programming turns the last five topics into a way of computing: write down facts and rules about a problem as logical clauses, pose a query, and let a built-in proof search find the answers. The single idea that carries it is that a proof can be made to compute — the search for a derivation of "someone is X's grandparent" does not merely answer yes or no, it fills in who, because the proof procedure works by matching patterns and the matches are the output. Prolog is the language where this idea lives, and its whole behavior falls out of one rule format and one search strategy. Citation needed

Terminology and core concepts

  1. Facts, rules, and queries

    A logic program has exactly two kinds of clause — facts, which assert an atomic sentence outright, and rules, which assert a head conditional on a body of goals — and running the program means posing a query for the system to prove from them. Citation needed

    What does the fact parent(tom, bob). assert?
    that tom is a parent of bob, unconditionally Citation needed
    In head :- body, how does the rule read?
    head holds if every goal in the body holds Citation needed
    What does Prolog's :- correspond to logically?
    a conditional, written backwards Citation needed
    What is a query?
    a goal the system tries to prove from the program Citation needed
    What does a comma between body goals mean?
    "and" Citation needed
    How does Prolog spell a variable versus a constant?
    variables capitalized, constants lowercase Citation needed
  2. Horn clauses: the restriction that buys speed

    Facts and rules are Horn clauses — clauses with at most one positive conclusion; giving up full predicate logic's freedom (no disjunctive heads, no unrestricted negation) is what makes proof search efficient enough to be a programming language. Citation needed

    What limit defines a Horn clause?
    at most one positive (head) literal Citation needed
    Can a Prolog rule conclude "A or B"?
    no — one head only Citation needed
    What is a rule with an empty body?
    a fact Citation needed
    Why accept the restriction at all?
    it makes proof search efficient and directed Citation needed
    What do all variables in a clause implicitly carry?
    universal quantification Citation needed
  3. Unification

    Unification is two-way pattern matching: it finds a substitution for variables making two terms identical — parent(X, bob) unifies with parent(tom, bob) by X = tom — and it is the single mechanism behind parameter passing, binding, and data construction. Citation needed

    What does unification produce on success?
    a substitution making both terms identical Citation needed
    Do parent(X, bob) and parent(tom, Y) unify?
    yes: X = tom, Y = bob Citation needed
    Do foo(a) and foo(b) unify?
    no — distinct constants never match Citation needed
    Can two unbound variables unify?
    yes — they become aliases of one another Citation needed
    What is the most general unifier?
    the substitution that binds no more than necessary Citation needed
    How does unification differ from one-way pattern matching?
    variables may appear, and bind, on both sides Citation needed
  4. Resolution: one rule to run them all

    Resolution is a single inference rule that cancels a goal against a clause head when the two unify, replacing the goal with the clause's body; it generalizes modus ponens, and repeating it until no goals remain is the entire execution model. Citation needed

    What does one resolution step do to the current goal?
    replaces it with the body of a unifying clause Citation needed
    When has the query been proved?
    when no goals remain Citation needed
    What propositional rule does resolving a goal against a fact resemble?
    modus ponens Citation needed
    What carries the answer out of the proof?
    the accumulated variable bindings Citation needed
    How does resolution prove a query, directly or by refutation?
    by refutation — the negated query yields a contradiction Citation needed
  5. The search strategy: depth-first with backtracking

    Prolog tries clauses top to bottom and goals left to right, diving depth-first; on failure it backtracks — undoes the latest bindings and tries the next clause — so clause order changes efficiency and even termination, which is where the clean logic meets the machine. Citation needed

    In what order does Prolog try matching clauses?
    top to bottom, as written Citation needed
    In what order does it attack a rule's body goals?
    left to right Citation needed
    What does backtracking undo?
    the bindings made since the last choice point Citation needed
    Can reordering clauses turn a terminating program into a looping one?
    yes — depth-first search can dive forever Citation needed
    How does the user ask for another solution?
    force backtracking after a success Citation needed
    Where should a recursive rule's base case go?
    before the recursive clause Citation needed
  6. Declarative and procedural readings

    Every clause has two readings — declarative (a logical statement about the relation) and procedural (a recipe for the search) — and good logic programming keeps both true at once, because a relation unlike a function can be queried in any direction. Citation needed

    What is the declarative reading of a clause?
    a true statement about the relation Citation needed
    What is the procedural reading?
    a recipe: to prove the head, prove the body in order Citation needed
    What can append(X, Y, [1,2]) compute that a function could not?
    every split of the list, run backwards Citation needed
    Why can one predicate answer several differently shaped questions?
    any argument may be left as an unbound variable Citation needed
    When do the two readings come apart?
    when clause order makes a true relation loop Citation needed
  7. Negation as failure and the closed world

    Prolog's "not" means "I could not prove it": under the closed-world assumption — that the program states everything true — failure to prove counts as falsity, which is useful and correct exactly as far as that assumption is. Citation needed

    What does negation as failure actually test?
    that the proof search for the goal fails Citation needed
    What does the closed-world assumption say?
    anything not derivable from the program is false Citation needed
    When does negation as failure give wrong answers?
    when the program is missing true facts Citation needed
    Is negation as failure the logician's ¬?
    no — unprovable is weaker than false Citation needed
    Why should a negated goal be called with its variables already bound?
    unbound variables make its meaning shift to "no instance provable" Citation needed

Problems that prove it

  1. Given parent(tom, bob). parent(bob, ann). parent(bob, pat). and the rule grandparent(X, Z) :- parent(X, Y), parent(Y, Z)., trace the query grandparent(tom, Who).

    Drills: resolution plus unification producing answers, not just yes.

    Derivation steps
    1. The query unifies with the rule head: X = tom, Z = Who.
    2. New goals: parent(tom, Y), parent(Y, Who).
    3. First goal matches parent(tom, bob): Y = bob.
    4. Second goal becomes parent(bob, Who); first match: Who = ann.
    5. No goals remain: answer Who = ann.
    6. Forcing backtracking retries the last choice: Who = pat. No further matches, so two answers in all.

    Citation needed

  2. Compute the most general unifier of f(X, g(Y), a) and f(b, g(Z), W), or show none exists.

    Drills: unification argument by argument, inside nested terms.

    Derivation steps
    1. Outer symbols match: both f with three arguments.
    2. Argument 1: X against b — bind X = b.
    3. Argument 2: g(Y) against g(Z) — same symbol, so unify insides: Y and Z alias (Y = Z).
    4. Argument 3: a against W — bind W = a.
    5. Unifier: {X = b, Y = Z, W = a}. It is most general: Y and Z stay unconstrained beyond aliasing.

    Citation needed

  3. With append([], L, L). and append([H|T], L, [H|R]) :- append(T, L, R)., trace append(X, Y, [1,2]) and list every answer.

    Drills: running a relation backwards — the declarative reading cashed out.

    Derivation steps
    1. Clause 1 unifies: X = [], Y = [1,2]. First answer.
    2. Backtrack to clause 2: X = [1|T], and the goal becomes append(T, Y, [2]).
    3. Clause 1 again: T = [], Y = [2] — so X = [1], Y = [2]. Second answer.
    4. Backtrack into clause 2 again: T = [2|T'], goal append(T', Y, []).
    5. Clause 1: T' = [], Y = [] — so X = [1,2], Y = []. Third answer.
    6. Clause 2 cannot match an empty third list, so search ends: exactly the three splits of [1,2].

    Citation needed

Sources

  • Ivan Bratko, Prolog Programming for Artificial Intelligence — the standard teaching text, strong on the two readings of a clause.
  • Leon Sterling and Ehud Shapiro, The Art of Prolog — the craft of logic programs, from unification up.
  • Robert Kowalski, Logic for Problem Solving — the founding view of computation as controlled deduction.

Learning path

Continue through Logic in order, using the concepts above to connect this topic to the next.