Quick & Dirty Introduction to Prolog

This tutorial aims to provide some basic information for those who are completely new to Prolog, by offering basic tips for interacting with the console and writing code. It is not to be considered a thorough introduction to Prolog. For more information about Prolog specifically, I suggest heading over to LearnPrologNow.org to get started.

Prerequisites

To get the most out of this tutorial, you will need access to SWI-Prolog. Some basic familiarity with the command line interface (CLI) for your environment is also highly recommended.

Using the Console

You will often be interacting with Prolog via the command line, from the interactive console. To begin, visit the command line and type swipl.

swipl

 

This should start up the SWI-Prolog interactive console. From here you can start typing Prolog to get immediate feedback.

It can help to think of writing Prolog like writing sentences. Every command in Prolog must end with a period. To start getting comfortable with this, try doing some basic math. If all goes well, you should get the correct answers.

?- X is 1+1.
X = 2.

?- Y is 6*7.
Y = 42.

 

Loading External Files into the Console

More often though, you will need to load an external file containing a knowledge base of facts, rules and queries for Prolog to use.

Open up your favorite text editor and write some rules.

color(blue).
color(red).
color(black).
color(white).

 

Save this as “colors.pl”. Go back to your Prolog terminal to load this knowledge base.

Tip: If colors.pl is not in the path where you started your console from, you will need to use the full path to colors.pl to access it through the console. Alternatively, you can change directories inside the Prolog console.

From the console, type the following. Be sure to enclose the path in quotes!

?- cd('~/path/to/prolog/files').
true.

 

If all goes well, Prolog will return “true”, which means success!

Now, you should be able to load colors.pl using the consult predicate. Consult automatically looks for a file called colors.pl, if you pass it the argument: colors.

?- consult(colors).
true.

 

Once the file is loaded, you can interact with the knowledge base by posing some queries. Try the following.

?- color(blue).
true.

?- color(green).
false.

?- color(cake).
false.

 

Explanation: 
Since color(blue) is a fact our knowledge base, Prolog answers “true”.
Since color(green) is not a fact in our knowledge base, Prolog has no idea that green is a color. It answers “false”.
Likewise, since color(cake) is not a fact in our knowledge base, Prolog also answers false.

Now try typing:

?- color(Color).
Color = blue

 

You should see something like the above and notice that your cursor isn’t returned to the next line to pose another query. This is because there are multiple solutions to the query. Type a semi-colon (;) to see another solution. Keep typing ; until you run out of solutions.

Tip: If you have so many solutions that you get tired of seeing them all, you can go ahead and hit the Enter key to get Prolog to stop offering solutions.

?- color(Color).
Color = blue ;
Color = red ;
Color = black ;
Color = white.

 

Making Changes to External Files

Now that we know our knowledge base can be loaded by Prolog, it might be time to add a few more colors. Go back to your text editor and add some facts. Try for something like this:

color(blue).
color(red).
color(black).
color(white).
color(green).
color(transparent).

 

Save the file and return to the Prolog console. Now you need to reload the file to have access to your new facts. Type make. and then query to see if your new colors are available.

?- make.
% colors compiled 0.00 sec, 3 clauses
true.

?- color(transparent).
true.

 

Now you should have some basic knowledge of working with the SWI-Prolog console and loading files.

For more SWI-Prolog fun, continue on to Creating Webpages with SWI-Prolog and PWP: Starting a Server.

Further Reading