Prolog lab: logic programming with facts, rules and questions, in the browser

This lab gives each student a Prolog interpreter of their own, with three small files to read and change: a file of facts about a family, a file of rules built on them and a file of rules over lists. A Prolog program does not say what to do step by step. It says what is true, and running it means asking a question.

It is not an attack lab. Every copy has no route to the internet and none to any other student, so nothing can be downloaded into it and nothing in it can reach anybody else. It is the standard interpreter only: no extra packages can be installed, and it is not a constraint solver, a web server or a database.

What students learn

That a program can be a set of facts and rules, and that a question is how it is run. Students read the facts, ask whether something is true, use a variable to ask who, read a rule that is built from two other statements, and follow a rule that calls itself to find every ancestor. They also meet the idea that no output means the answer cannot be shown from the facts written down, not that the program is broken. It suits an introduction to programming paradigms and to logic in computing; check your own awarding body’s unit for the exact wording, none is quoted here.

Run it in your browser

The lab starts one service, prolog, from the official swipl image, running as an unprivileged account. There is no web page, so the lab bar has no link under In your browser, and the terminal is how you ask questions. It reaches the interpreter with docker exec prolog, followed by swipl and the question. Each command in the lesson loads a file, asks one question with the -g option and stops with -t halt, so it always finishes. The answers are the same every time, in the order the facts are written. Everything printed is in English, whatever language the page is in.

For a conversation with the interpreter, press Open terminal and type docker exec -it prolog swipl -q family.pl. At the ?- prompt type a question ending in a full stop, such as parent(tom, X). It prints X = ann, and a semicolon asks for the next answer. Type halt. to leave.

  1. Open the Container page. Open Build on the bar and choose Run a container. Anybody may open it, and the Git lab and the SQL lab start with no account. Every other lab needs a free account: create one, sign in, then press Start.
  2. Find the lab. The Sample labs card lists every sample. Find Prolog: facts, rules and questions.
  3. Press Start. Start runs it as it is, in one press. Edit the file first opens the compose file so you can change it before you start.
  4. Open it. There is no web page to open. On the lab’s page press Open terminal and type docker exec prolog swipl -q -g "forall(parent(tom, X), writeln(X))" -t halt facts.pl to print the children of tom: ann, then ben.

What to ask students to hand in

The questions they asked and what each answered: a yes or no question, a question with a variable, and one that uses the grandparent or ancestor rule. Then a rule of their own added to family.pl, for example for an aunt or a cousin, with the question that shows it working, and a sentence or two in their own words on how a fact differs from a rule. Nothing is marked automatically: the rule and the sentence show whether it was understood. For stronger work ask why a question that prints nothing is not the same as a false statement.

A basic lesson on this lab

Prolog Basics is a small lesson worked in this lab. In it a student reads the facts, asks whether something is true, asks who with a variable, reads a rule and follows a rule that calls itself.

Free personal accounts have it in the Basics module, which is on Home, and every college can copy it from the Library’s Basics folder. It is deliberately small: it shows a teacher what a lesson worked in a lab is, and the lab does the rest. Copy it into a module of your own to change it.

The code on GitHub

The compose file has no Dockerfile: it runs one official public image, SWI-Prolog, pinned to version 9.2.9, as an unprivileged account with 128 MB of memory. The image is under the BSD-2-Clause licence and is not distributed by us. The file writes three small teaching files when it first starts and keeps them on a volume, so what a student changes is still there after a stop and a start. The limits are honest ones: the lab has no way out, so no extra packages can be installed, and it teaches the idea of logic programming with a family tree, not the large programs Prolog is used for.

Set it as work

Say in the task what to hand in.

  1. Make the lab. In your module add a task and choose the Container lab card, then In a GitHub repository. Give https://github.com/mitsurutech/lab-prolog-lab with the branch main. The compose file is the default, compose.yaml. Fork the repository first if you want to change it, for example to add more facts to the starting files.
  2. Publish and try it. Publish pins the commit you were shown, so a later change in the repository does not alter a lab you have already set. Start your own copy first.
  3. Hand it out. Assign it like any task, with a due date if you want one, and say in the task what to do and what to hand in.
  4. Mark what is handed in. Open a student’s copy and the work they handed in, mark it, and send it back with a reason if it needs more.

Try this lab, and what it costs

A free personal account can work through the basic lessons in this lab, with enough free credit each month to follow most lessons and no card. For a class, a school or college has its first 365 days free for up to 50 students, then pays per student a year, with every lab included and staff free.

Questions people ask

Why does a question sometimes print nothing?
Because the goal failed: it cannot be shown to be true from the facts and rules written down. That is not an error and not the same as a statement being false in real life. Add a fact and the same question can succeed. An unknown name is different: it prints an error that says Unknown procedure.
Why do the names and messages stay in English on a page in another language?
The facts, the file names and every message the interpreter prints are English, and the lab does not change them. The lesson says so and explains each answer in the page’s own language.
Are the changes students make kept?
Yes, for that copy: the files are on a volume, so they are still there after the lab is stopped and started again. A file a student deletes is written again, in its original form, the next time the lab starts.