Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 40 ยท Facts, Rules and Queries

Prolog Facts and Queries

Learn how to write Prolog facts, build a small knowledge base and ask yes/no and variable queries. Covers predicates, arity, argument order and unknown procedures.

A fact is the simplest thing you can say in Prolog: a statement that is simply true. A set of facts is a database, and a query asks that database whether something holds, or for which values it holds. Facts and queries are the foundation of everything else, including rules.

The prolog blocks are read-only because Prolog is not available in the site editors. The Python runnable examples use the mini-Prolog stand-in, a small engine that reads the same facts and queries and prints answers in the style of the real toplevel.

Writing facts

A fact is a compound term (or a plain atom) followed by a full stop:

PROLOG
likes(mary, wine).
likes(mary, pizza).
likes(john, mary).
likes(john, wine).
male(john).
female(mary).
raining.

The first four lines define the predicate likes/2: four facts, each with two arguments. male/1 and female/1 have one argument, and raining/0 has none; it is an atom used as a fact, a simple switch. All the facts for one predicate together are its definition, and each fact is one clause. Keep the clauses of a predicate together in the file. If you spread them out, SWI-Prolog warns about discontiguous clauses.

Choosing predicate names and argument order

Prolog does not attach meaning to names or order. likes(mary, wine) could mean Mary likes wine or wine likes Mary. The meaning is only what you decide, so decide one convention and keep it. A helpful habit is to read the fact as a sentence: parent(tom, bob) reads "tom is a parent of bob", so the first argument is always the parent. Write a comment above each predicate stating the reading:

PROLOG
% parent(Parent, Child): Parent is a parent of Child.
parent(tom, bob).

Use lowercase names with underscores (has_color/2, part_of/2). A good test of an interface: does the predicate read well with every argument filled in?

Asking yes/no questions

A query with no variables asks "is this true?". Prolog searches the database from the top for a matching fact.

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

?- likes(wine, mary).
false.

?- likes(mary, beer).
false.

?- raining.
true.

?- snowing.
ERROR: Unknown procedure: snowing/0 (DWIM could not correct goal)

The second answer is false because the order of arguments matters: no fact has wine first. The fourth query, raining, is true because the zero-argument fact is in the database; snowing has no clauses at all, which is an error (see below). Prolog only knows what is in the database: a goal it cannot prove is reported as false, even if it is true in the real world. This is the closed-world assumption you will revisit in negation.

Asking with variables

If you put a variable in a query, you are asking "for which values is this true?":

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

?- likes(Who, wine).
Who = mary ;
Who = john.

?- likes(Who, What).
Who = mary, What = wine ;
Who = mary, What = pizza ;
Who = john, What = mary ;
Who = john, What = wine.

In SWI-Prolog the answers print on separate lines as Who = mary, then What = wine ;, but the content is the same. Solutions come in the order of the facts in the file, because Prolog searches top to bottom. If you swap two facts, the answers swap.

The anonymous variable _ lets you ask without caring about a value: likes(john, _) is true if John likes anything, and no binding is printed. When the same variable appears twice in a query, both places must have the same value: likes(X, X) asks for something that likes itself, and fails here.

How a query finds a fact

Prolog goes through the facts of the predicate from top to bottom. For each, it tries to make the fact and the query identical by choosing values for the variables; this matching is called unification. The first fact that matches gives the first answer. If you ask for another, Prolog resumes after that fact and looks for the next match. This is the simplest case of backtracking.

From query to answer

Take the query likes(mary, What)
Scan facts Top to bottom
Match What = wine
Ask for more Resume below

Matching a query with a fact is called unification.

1 / 2

Try it

Python Queries against a small database (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.

Check each answer against the database. The last query, male(X), likes(X, wine), is a conjunction: two goals joined by a comma. It is covered in the next lesson.

Order matters for answers

The order of facts decides the order of answers. Reverse the facts and run the same query:

Python Answer order follows fact order (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.

Errors: unknown procedures

If you ask for a predicate that has no clauses at all, SWI-Prolog does not say false; it raises an existence error:

PROLOG
?- hobby(mary, X).
ERROR: Unknown procedure: hobby/2 (DWIM could not correct goal)

That is helpful, because a typo in a predicate name is reported instead of silently failing. (If a predicate exists but no clause matches, you get false.) The mini-Prolog stand-in is simpler and answers false in both cases. A predicate you want to keep empty but known can be declared :- dynamic hobby/2., which makes "no clauses" a normal state.

Representing data with facts

Facts are not only for people. You can store records:

PROLOG
book(title('Dune'), author('Herbert'), year(1965)).
book(title('Emma'), author('Austen'), year(1815)).

and query any part: book(title(T), _, year(Y)) returns each title with its year. Using nested structure instead of many arguments keeps related values together. For larger data, many programmers load facts from CSV files, but the idea is the same.

Common mistakes

  • Starting an atom with a capital letter by accident (Mary) so it becomes a variable.
  • Forgetting the full stop, which merges two facts into a syntax error.
  • Mixing up argument order between facts and queries.
  • Expecting a missing fact to mean "no" in the real world instead of "not provable".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many arguments does likes(mary, wine) have, and what is its predicate indicator?

    likes has two arguments, so the predicate is likes/2.
  2. 2. What does the query likes(Who, wine) ask?

    A variable in a query asks for the values that make the goal true.
  3. 3. In what order does Prolog return answers from facts?

    Prolog scans clauses top to bottom, so answers follow the order of facts.
  4. 4. What does the toplevel print when a goal cannot be proved but its predicate has clauses?

    false means no solution was found. An existence error is for predicates that do not exist at all.

Summary

  • A fact is a term followed by a full stop; all clauses with the same name and arity form a predicate.
  • Choose a clear argument order and document it.
  • Queries without variables answer true or false; queries with variables list the bindings that make them true.
  • Answers come in the order of the clauses; Prolog only knows what is in its database.

Next: Variables and Conjunction