Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 40 ยท Facts, Rules and Queries

Prolog Rules and Clauses: Heads, Bodies and Inference

Learn how Prolog rules work: head :- body, variables shared between head and body, several clauses as alternatives, helper predicates and how Prolog proves a goal.

Facts alone give you a lookup table. Rules let Prolog derive new knowledge from what it already has. A rule says: this is true provided those other things are true. Rules turn a database into a reasoning system, and they are where programming in Prolog really begins.

Prolog blocks are read-only on this site. The runnable examples use the mini-Prolog stand-in (Python), which handles rules, conjunctions, \= and arithmetic comparisons.

Anatomy of a rule

PROLOG
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
  • grandparent(X, Z) is the head: what the rule proves.
  • :- is the neck, read "if".
  • parent(X, Y), parent(Y, Z) is the body: a conjunction of goals.
  • The final full stop ends the clause.

Read it as logic: for all X, Y and Z, X is a grandparent of Z if X is a parent of Y and Y is a parent of Z. Variables in a rule are universally quantified, and variables that occur only in the body (here Y) are read as "there exists such a Y".

A fact is a rule with an empty body: likes(mary, wine). means likes(mary, wine) :- true. Facts and rules are both clauses.

How Prolog uses a rule

Suppose the database holds:

PROLOG
parent(tom, bob).
parent(bob, ann).
parent(bob, pat).

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

Ask ?- grandparent(tom, Who). Prolog proceeds in these steps:

  1. It looks for clauses whose head matches grandparent(tom, Who). The rule matches with X = tom and Z = Who.
  2. The query is replaced by the body, with those bindings: parent(tom, Y), parent(Y, Who). The body goals become new subgoals.
  3. parent(tom, Y) matches the fact parent(tom, bob), giving Y = bob.
  4. parent(bob, Who) matches parent(bob, ann): Who = ann. First answer.
  5. On backtracking parent(bob, pat) gives Who = pat.
PROLOG
?- grandparent(tom, Who).
Who = ann ;
Who = pat.

This replacement of a goal by the body of a matching clause is called resolution. Each time Prolog uses a clause it works with a fresh copy of the clause's variables, so the same rule can be used many times in one proof without its variables getting mixed up.

flowchart TD
    Q["grandparent(tom, Who)"] --> R["parent(tom, Y), parent(Y, Who)"]
    R --> F1["parent(tom, Y): Y = bob"]
    F1 --> F2["parent(bob, Who)"]
    F2 --> A1["Who = ann"]
    F2 --> A2["Who = pat"]

A rule turns one goal into several smaller goals.

Several clauses for one predicate

A predicate often has several clauses. They are alternatives: if the first does not lead to a proof, Prolog tries the next. This is how you express "or".

PROLOG
parent(X, Y) :- mother(X, Y).
parent(X, Y) :- father(X, Y).

You can mix facts and rules for the same predicate; order is respected from top to bottom. A common order is: specific facts first, general rules after.

Rules that compare things

Rules may use built-in goals. Siblings share a parent but are different people:

PROLOG
sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y.

Without X \= Y, every person would be their own sibling, because parent(P, X), parent(P, X) is trivially true. \= succeeds when its two sides cannot be unified. Because it only checks, put it after the goals that bind X and Y. The stand-in below lets you see the difference.

Helper predicates

If a body grows long or you repeat a pattern, name it. Instead of repeating parent(P, X), parent(P, Y), introduce:

PROLOG
same_parent(X, Y) :- parent(P, X), parent(P, Y).
sibling(X, Y)     :- same_parent(X, Y), X \= Y.

Small predicates with good names are easier to read and test. Prolog programs are layers of such definitions: simple facts at the bottom, rules above, and one or two main predicates at the top.

Rules with data constraints

Rules work with numbers as well:

PROLOG
adult(Person) :- age(Person, A), A >= 18.

age(Person, A) binds A, and then the comparison checks it. Comparison and arithmetic goals need their variables already bound (instantiated). Otherwise you see an instantiation error. This is one reason the order of goals in a body matters: bindings flow from left to right.

Seeing rules in the stand-in

Python Rules in action (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice same_parent(ann, S): it gives S = ann and S = pat. Without the \= check, Ann would count as her own sibling.

Numeric rules

Python Rules with comparisons (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Inspecting your program

In SWI-Prolog, listing(sibling/2). prints the clauses of sibling/2 as the system stores them:

PROLOG
?- listing(sibling/2).
sibling(X, Y) :-
    same_parent(X, Y),
    X\=Y.

Variable names are kept only for display. Use listing to check what is loaded after you edit and reload a file.

Common mistakes

  • Using a variable only once (singleton warning): usually a typo.
  • Writing X = Y when you meant "different" and X \= Y when you meant to bind.
  • Putting a comparison before the goal that binds its variables.
  • Forgetting that a clause with an unbound variable in the head can match almost anything.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which part of a rule is the head?

    The head is what the rule proves; the body after :- lists the conditions.
  2. 2. What is a fact, in terms of rules?

    A fact is a clause with no conditions, equivalent to head :- true.
  3. 3. Why does sibling(X, Y) need X = Y?

    Without the check, a shared parent makes X and Y the same person a valid solution.
  4. 4. If a predicate has several clauses, how are they treated?

    Each clause is another way to prove the goal; Prolog tries them in order and backtracks into the next one.

Summary

  • A rule has a head, :- (if) and a body of goals joined by commas; a fact is a rule with no body.
  • Prolog proves a goal by replacing it with the body of a matching clause (resolution), using fresh variables each time.
  • Several clauses of one predicate are alternatives tried in order.
  • Use \= and comparisons after the goals that bind their variables; add helper predicates for readability.

Next: A Worked Example: The Family Tree