Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 40 ยท Getting Started

Installing and Running SWI-Prolog

Install SWI-Prolog, start the interactive toplevel, load a file with consult, read answers and press semicolon for more. Includes editors and online options.

To write real Prolog you need a Prolog system. There are several, and they all share the same core language. This course uses SWI-Prolog, the most popular free implementation: it is actively maintained, has excellent documentation, a large library, a good debugger and works the same on Windows, macOS and Linux. Other systems you may meet are GNU Prolog (compiles to native code), Scryer Prolog (modern, written in Rust), Trealla, SICStus Prolog (commercial, very fast) and Ciao. Programs that stick to standard Prolog run on all of them.

Remember that this site cannot run Prolog itself. The prolog code blocks are read-only. Use this lesson to set up a real system on your computer, and use the mini-Prolog stand-in (Python) in the editors on this site for quick experiments.

Installing

  • Windows: download the installer from the SWI-Prolog website (swi-prolog.org, "Download"), run it, and let it add swipl to the PATH if it offers.
  • macOS: brew install swi-prolog, or the disk image from the website.
  • Linux: most distributions package it: sudo apt install swi-prolog on Debian or Ubuntu, sudo dnf install swi-prolog on Fedora. The project also provides a PPA with newer versions.
  • Docker: docker run -it swipl gives you a toplevel without installing anything.
  • In the browser: the SWISH web notebook runs SWI-Prolog on a server, so you need nothing locally.

Check the install by typing swipl --version in a terminal. It prints something like SWI-Prolog version 9.x for ....

The interactive toplevel

Type swipl and you land in the toplevel, also called the REPL or interpreter. It prints a welcome banner and a prompt:

TEXT
?- 

You type a query after the prompt and end it with a full stop (a period followed by Enter). The full stop is required: without it Prolog waits for more input. Prolog replies with an answer:

PROLOG
?- X = 3, Y is X * 2.
X = 3,
Y = 6.

?- member(X, [a, b, c]).
X = a ;
X = b ;
X = c.

?- 2 > 5.
false.

Three things to notice:

  1. When a query succeeds with variables, Prolog prints the bindings that make it true.
  2. If there may be more answers, Prolog stops after the first one and waits. Press ; (or Space, or n) to ask for the next answer. Press Enter or . to accept and stop. The last answer ends with a ., because no alternatives remain.
  3. If a query cannot be proved, Prolog prints false.. If it succeeds with nothing to show it prints true.

To leave the toplevel type halt. or press Ctrl+D.

Writing a program file

Programs live in plain text files, traditionally with the extension .pl (some people prefer .pro to avoid a clash with Perl). Create family.pl:

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

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

Every clause ends with a full stop. A line starting with % is a comment; /* ... */ spans many lines.

Loading a file (consulting)

Loading a file into the system is called consulting. Start the toplevel in the folder that holds the file and use any of these:

PROLOG
?- consult('family.pl').
true.

?- [family].
true.

?- ['family.pl'].
true.

or from the shell: swipl family.pl, which starts the toplevel with the file already loaded. If you edit the file while the toplevel is open, type make. and SWI-Prolog reloads the files that changed. Now you can ask grandparent(tom, Who). and the answer is Who = ann.

If a clause has a syntax error, the system prints the file, the line number and a description, and continues. Warnings are common too. A very frequent one is singleton variables: a variable that appears only once in a clause, which is usually a typo. Write _ (or a name starting with an underscore, like _Ignored) when you really mean "any value".

Running a program from the command line

For scripts, ask SWI-Prolog to run a goal and exit:

TEXT
swipl -g main -t halt family.pl

Here -g main runs the predicate main after loading and -t halt is the toplevel to use afterwards, so the process ends. You can also place a directive in the file, a clause starting with :-:

PROLOG
:- initialization(main).

main :-
    write('Hello from Prolog'), nl.

With initialization(main) the goal runs once the file has loaded. For command-line tools, :- initialization(main, main). runs main and then halts, which is the usual way to make a stand-alone script.

Useful toplevel commands

Command Purpose
help(append). Open the manual page for a predicate.
apropos(sort). Search the manual for a word.
listing(grandparent). Print the clauses of a predicate.
trace. / notrace. Turn step-by-step tracing on or off (see tracing).
make. Reload changed source files.
halt. Quit.

Editors

Any editor works. VS Code has a Prolog extension. Emacs has prolog-mode, and SWI-Prolog ships with a simple editor (edit(File) opens your editor of choice). Indent rule bodies by four spaces and put each goal on its own line; Prolog is whitespace-insensitive except inside quoted text.

Mimicking a session with the stand-in

You cannot type queries at a real prompt here, but the stand-in's load and ask do the same two jobs as consulting and querying. Press Run to see answers including the ; and . endings.

Python Consult and query (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.

The final query printed false.: Ann has no grandchildren in the database.

Seeing every answer at once

In real Prolog, findall/3 collects all answers into a list, which saves you from pressing ;. The stand-in lets us do the same collection by iterating over solve and reading bindings with show.

Python Collecting all answers (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 your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must every Prolog query typed at the prompt end with?

    The full stop tells Prolog the query is complete. Without it the toplevel keeps waiting for input.
  2. 2. At the toplevel, what does pressing ; after an answer do?

    The semicolon means 'or': give me another solution, which makes Prolog backtrack.
  3. 3. Which command loads the file family.pl?

    consult/1 loads a file. [family]. and swipl family.pl do the same job.
  4. 4. What does a singleton-variable warning usually point to?

    A variable used once cannot link anything. If it is intentional, write _ or _Name.

Summary

  • SWI-Prolog is the system used in this course; it runs on all major platforms, in Docker and online via SWISH.
  • You type queries after ?-, end them with a full stop and press ; to see more answers.
  • Programs are .pl files loaded with consult/1, [file]. or swipl file.pl.
  • swipl -g main -t halt file.pl and initialization(main, main) run a program as a script.
  • On this site the Python stand-in lets you run small Prolog-like experiments.

Next: Prolog Syntax and Terms