Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Prolog Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Container lab11 min read8 commands

Who is Cat's grandparent?

You can see it in a family tree by following two lines upwards. A program written in most languages would need step-by-step instructions to do the same. Prolog works differently: you write down what is true, then you ask a question and it works out the answer.

By the end of this lesson

Note

I can ask a Prolog program questions about facts and rules.

Key words

  • Fact: something the program is told is true.
  • Rule: a statement that is true if other statements are true.
  • Query: a question you ask the program.
  • Variable: a name that starts with a capital letter and stands for an answer to find, such as X.
  • Recursion: a rule that uses itself.

Starter: what you already know

Note

Answer in your head. 1. If x + 2 = 5, what is x? 2. In "if it rains, take an umbrella", what must be true before you take one? 3. In a family tree, what is a grandparent? Answers: 3, because x stands for a number that makes it true; it must be raining; a parent of a parent.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. There is one container, prolog. Nothing opens in a browser, so use the terminal, where docker exec prolog runs one command inside it. Each command here loads a file and asks one question, then stops. Everything printed is in English, whatever language this page is in. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

A fact is a line that is true

This prints the file of facts. Each line such as parent(tom, ann). is one fact, read as "tom is a parent of ann", and it ends with a full stop. Lines beginning with % are comments. Prolog knows nothing except what the file says.

Command
docker exec prolog cat facts.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A query asks whether something is true

This loads facts.pl and asks whether tom is a parent of ann. The -g option is the goal to run, and -t halt stops afterwards. It prints yes, because the fact is there. The second asks whether ann is a parent of dan. That is not in the facts, so the goal fails and nothing is printed: no output means "cannot be shown to be true".

Command
docker exec prolog swipl -q -g "parent(tom, ann), writeln(yes)" -t halt facts.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
docker exec prolog swipl -q -g "parent(ann, dan), writeln(yes)" -t halt facts.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A variable asks "who?"

A capital letter makes a variable, which Prolog fills in by finding every fact that fits. forall goes through every answer and writeln prints each. The first command prints the children of tom, ann then ben, in the order the facts are written. The second turns the question round: who is a parent of ben? It prints tom.

Command
docker exec prolog swipl -q -g "forall(parent(tom, X), writeln(X))" -t halt facts.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
docker exec prolog swipl -q -g "forall(parent(X, ben), writeln(X))" -t halt facts.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check

Tip

Which children does ben have, and in what order would they print? Answer: dan then eve, because those facts are written in that order. Nothing is printed for a person with no children.

A rule is true if its parts are true

family.pl holds rules. The rule grandparent(X, Z) :- parent(X, Y), parent(Y, Z). reads "X is a grandparent of Z if X is a parent of some Y and Y is a parent of Z". The :- means "if" and the comma means "and". Nobody wrote a fact about grandparents. This asks for tom's grandchildren and prints cat, dan and eve.

Command
docker exec prolog swipl -q -g "forall(grandparent(tom, X), writeln(X))" -t halt family.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A rule can use itself

An ancestor is a parent, or a parent of an ancestor. That second rule calls itself, so it follows the tree as far down as it goes. findall collects every answer into a list. The first command prints [ann,ben,cat,dan,eve], and the second, for ann, prints [cat].

Command
docker exec prolog swipl -q -g "findall(X, ancestor(tom, X), L), writeln(L)" -t halt family.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
docker exec prolog swipl -q -g "findall(X, ancestor(ann, X), L), writeln(L)" -t halt family.pl

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a Prolog program runs from the top, line by line, like a recipe. It does not. The facts and rules say what is true, and the question decides what is worked out. And a query that prints nothing does not mean the program is broken or that the answer is false in real life: it means the answer cannot be proved from the facts written down. Add a fact and the same question can succeed.

Exit check

Tip

Answer without looking back. 1. What does parent(tom, ann). say? 2. What makes X different from tom? 3. What does the :- in a rule mean? 4. What is it called when a rule uses itself? Answers: tom is a parent of ann, and it is a fact; X starts with a capital letter, so it is a variable to find; "if"; recursion.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one container with the SWI-Prolog interpreter and three small files, and nothing here marks itself: the student hands in the questions they asked and what each answered, a rule of their own added to family.pl and a question that shows it working, and a sentence on how a fact differs from a rule. Key learning points: 1. A fact is a statement that is true. 2. A query asks whether something is true, and a variable asks "who". 3. A rule is true if its parts are true. 4. A rule that uses itself follows a structure as deep as it goes. Common mistake: reading a program as a list of instructions, or reading no output as "wrong". Equipment: the lab and its terminal. Tip: ask students to predict the answers before they run the command, then to add one fact and see what changes. The answers are exact and the same every time, in the order the facts are written. The names and every message are in English, whatever language the page is in. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

About this lab and its source: Prolog lab: logic programming with facts, rules and questions, in the browser

Teaching a class? Set these lessons for your students with marking and a lab for every student.