Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 40 ยท Facts, Rules and Queries

Prolog Variables, Conjunction and Disjunction

Understand Prolog variables, shared variables across goals, conjunction with a comma and disjunction with a semicolon. Learn how queries combine goals to join facts.

Single-fact queries are only the start. The real power comes when you combine goals, sharing variables so information flows from one goal to the next. This is Prolog's equivalent of a database join, and it is how rules are built.

Prolog is read-only on this site. The Python stand-in engine answers the same conjunctive queries and can print a call trace.

A database to work with

PROLOG
likes(mary, wine).
likes(mary, pizza).
likes(john, wine).
likes(john, mary).
likes(sue, pizza).

male(john).
female(mary).
female(sue).

Conjunction: the comma means "and"

A conjunction joins goals with a comma. It succeeds only if every goal succeeds, with consistent values for any shared variables.

PROLOG
?- likes(john, wine), likes(mary, wine).
true.

?- female(X), likes(X, pizza).
X = mary ;
X = sue.

In the second query the variable X appears in both goals, and it means the same thing in both. First Prolog solves female(X), which gives X = mary. With that binding, the second goal becomes likes(mary, pizza), which succeeds. That is the first answer. Asking for another answer, Prolog backs up and tries X = sue from the first goal, and likes(sue, pizza) succeeds too.

Goals are tried left to right, and a variable bound by one goal is visible in the goals to its right. Think of nested loops: the outer loop enumerates the first goal's solutions and, for each, the inner loop enumerates the second goal's solutions.

Variables are shared within a query, not across

The same name always denotes the same variable inside one clause or query, but different clauses never share them. In the query

PROLOG
?- likes(X, wine), likes(X, mary).

X must be someone who likes both wine and Mary. Let us trace it. likes(X, wine) first gives X = mary, and then likes(mary, mary) fails, so Prolog backtracks. The next solution of the first goal is X = john, and likes(john, mary) is a fact, so the query succeeds:

PROLOG
?- likes(X, wine), likes(X, mary).
X = john.

Keep this habit: when an answer surprises you, walk through the goals one at a time.

Anonymous variables

If you do not need a value, use _:

PROLOG
?- likes(_, pizza).
true ;
true.

Each _ is a fresh variable. Here two facts match, so Prolog finds two proofs and prints true twice; SWI-Prolog stops with true. once no more remain. To ask "does anyone like pizza?" without caring how many, once/1 or \+ \+ can be used; you will meet them later.

Two underscores in one clause are independent: likes(_, _) matches any fact. Two occurrences of X are the same: likes(X, X) matches only reflexive facts.

Disjunction: the semicolon means "or"

A disjunction A ; B succeeds if A succeeds or if B succeeds. Prolog tries A first and, when backtracking, then tries B.

PROLOG
?- likes(mary, X) ; likes(sue, X).
X = wine ;
X = pizza ;
X = pizza.

Put parentheses around a disjunction inside a longer body because ; binds looser than ,:

PROLOG
snack(X) :- likes(sue, X), ( X = pizza ; X = cake ).

Without parentheses, a, b ; c means (a, b) ; c. Often it is clearer to write two clauses instead of a disjunction. These two definitions are equivalent:

PROLOG
parent_or_guardian(X, Y) :- parent(X, Y) ; guardian(X, Y).

parent_or_guardian(X, Y) :- parent(X, Y).
parent_or_guardian(X, Y) :- guardian(X, Y).

Separate clauses read better and are the idiomatic choice.

Joining facts

A conjunction with shared variables is a join. Imagine two tables:

PROLOG
employee(ann, sales).
employee(bob, it).
employee(cy, it).
manager(sales, dora).
manager(it, eve).

?- employee(Who, Dept), manager(Dept, Boss). finds each employee with the manager of their department: Who = ann, Dept = sales, Boss = dora, and so on. Dept is the join key. You wrote no join code, only a shared variable name. This is why Prolog fits database-style queries so well, and why Datalog, a database query language, is a subset of Prolog.

Try it

The first block runs conjunctions. The second turns on a trace so you can watch the left-to-right order and see backtracking in action: each Call line is a goal being attempted.

Python Conjunction queries (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.

Python Watching the order of goals (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.

In the trace, female(X) is called first. After it binds X, the printed goal likes(mary,pizza) shows the variable already replaced. After the first answer, the engine tries the next solution of female(X), and calls likes(sue,pizza).

The tracing habit

Whenever a query returns something unexpected:

  1. Write the database clauses in the order they appear.
  2. Solve the leftmost goal; write down each binding.
  3. Substitute the binding into the remaining goals and continue.
  4. When a goal fails, back up to the previous goal that had another match.

Prolog does exactly this. Practicing it by hand makes backtracking easy to understand later.

Common mistakes

  • Using and or or as words; Prolog uses , and ;.
  • Putting the full stop inside a conjunction: a, b. ends the query after b.
  • Using a different variable name when you mean the same value (X and x: the second is an atom).
  • Forgetting parentheses around a disjunction in a rule body.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the comma mean in female(X), likes(X, pizza)?

    A comma is conjunction: both goals must succeed with the same value for X.
  2. 2. In the query p(X), q(X), which goal is tried first?

    Prolog works left to right, so p(X) is solved first and its binding is used for q(X).
  3. 3. What do two anonymous variables _ in one goal share?

    Every _ is distinct. Only named variables with the same name are the same variable.
  4. 4. How is a disjunction written?

    The semicolon means or. A comma binds tighter, so use parentheses in longer bodies.
  5. 5. Which is the idiomatic way to express 'X is a parent or a guardian of Y'?

    Two clauses each give one way to prove the head; Prolog tries them in order.

Summary

  • A conjunction A, B needs both goals to succeed; shared variables carry values from left to right.
  • Variables with the same name inside one clause are the same variable; _ is always fresh.
  • A disjunction A ; B tries A first, then B on backtracking. Prefer separate clauses.
  • Shared variables across goals act as joins between facts, like a database query.
  • When a result surprises you, trace the goals by hand.

Next: Rules and Clauses