Learning Prolog (really basic starting point)

What do I need to get started?

I’ve decided to use SWI Prolog and I’m running this on a Linux box. Why ? Somehow it felt right to use Linux for this – I don’t have a better reason than that, but either way it also runs on Windows as far as the website says.

So after I’ve downloaded and installed the SWI Prolog package using

sudo apt install swi-prolog-nox

We can run the installed application by simply typing prolog at the command line.

Note: I will refer to both the language and the command line tool as Prolog for here on.

Using Prolog

The best way to work with Prolog is by creating .pl files and loading into the Prolog interpreter, although we can enter facts into the Prolog interpreter if we prefer.

Let’s begin with some basic commands

Before we begin, we terminate commands using the dot . thus help(halt).

Loading a Prolog file

[myprogam].

enclosing the filename of your Prolog file (excluding the .pl extension) and terminating with the . will load the file myprogram.pl.

The response from Prolog will be true. basically meaning Prolog has successfully loaded the file.

Alternatively simply execute the prolog command at the command line and supply the file there, for example

prolog myprogram.pl

Exiting Prolog

Before we look at executing queries against our Prolog program, let’s look at cleanly exiting the Prolog environment.

halt.

Listing a program

After loading a program we might want to list it within the Prolog environment, we can use

listing.

Executing queries

Before we can execute a query we need a Prolog program, so here’s a simple one (we’ll discuss how it works later).

gender(leonard,male).
gender(sheldon,male).
gender(howard,male).
gender(rajesh,male).
gender(penny,female).
gender(bernadette,female).
gender(amy,female).
gender(stuart,male).
gender(leslie,female).

save this as bigbang.pl. Next from the folder where you saved this file, run swipl, load the file using [bigbang]. and the response from swipl should be true.

Now query for all male cast members using

gender(X,male).

Basically we’re saying, query for those cast members of Big Bang Theory where the gender is male. We supply a capitalised variable name (in this case X, but it could be any valid variable name). Prolog returns

X = leonard

if we want to view further evaluations of the query then we type a semi-colon and Prolog will then list the next male cast member until eventually outputting the last cast member (in this case X = stuart) terminated with a dot .

Basics of our source code

When we write something like

gender(leonard,male).

we are defining a predicate gender which takes two arguments each of which must begin with lowercase text. An uppercase is used as a variable for querying. Hence

gender(X,male).

is a query whereby we’re asking Prolog to supply the results for the variable X.

The predicate show above is also known as a fact i.e. we’ve defined the fact that leanord is male.

A Prolog program (as shown earlier) can be said to consist of multiple predicates which have one or more arguments (or clauses).

As mentioned, variables begin with an uppercase/capital letter or and underscore.

Each predicate must be terminated with a .

More on syntax

Comments

Comments are declared in the usual C/C++ way using /* … */ or for a single line using %, i.e.

/* This is multi
   line a comment */

% This is a single line comment

if or provided that

Let’s see some code where this is used first

square(X,Y) :- Y = X * X.

@ Example usage
square(3,Y).
Y = 9.

so in this case the square(X,Y) can be thought of as being true if Y is X * X. In this example we cannot use square(X, 9) to find values that when squared equal 9. We can, however, use square(X,3*3) which returns X = 3.

Assignment

We can assign values to variables using the standard = operator, you can also use is for evaluating arithmetic operations, for example

square(X,Y) :- Y =X * X.

@ or we could write

square(X,Y) :- Y is X * X.

This new version of the square predicate will fail if we try to use square(X,3*3) as Y is not instantiated.

Equality and Inequality

X == Y

is true if both X and Y are identical whereas

X \== Y

is true if X and Y are not identical.

And and Or operators

The comma is used as an AND operator, for example

abs(X,Y) :- X < 0, Y is -X
abs(X,X) :- X >= 0

In the first case we’re saying if X < 0 AND Y is -X then abs is true and using an overload (to use OO language) of the predicate we state that X equates to X if X >= 0.

The semi-colon is used for OR scenarios. For example

oneOrTwo(X) :- X = 1 ; X = 2.

Okay, not a great example but basically this says, if X = 1 OR X = 2 then the result is true otherwise its false.

I think that’s enough to a just get some very basic programs up and running.