Installing Prolog working environment for Ubuntu

In this blog, hopefully I will kickstart you to learn prolog using swi-prolog.

First, let’s add the repository we will use to install the stable version of swi-prolog:

sudo apt-add-repository ppa:swi-prolog/stable

Update your repository:

sudo apt-get update

And the last step of the installation, installing the swi-prolog:

sudo apt-get install swi-prolog

Let’s test our prolog working environment,

Prolog needs a knowledge base to understand the rules,

Create a knowledge-base:

touch knowledgebasetest

Enter some rules:

 
animal(cat).
animal(bird).
animal(human).
insect(ant).

average(X,Y):- S is (X + Y) / 2, write(S).
sum(X,Y):- S is X + Y, write(S).
sub(X,Y):- S is X - Y, write(S).
multiply(X,Y):- S is X * Y, write(S).
divide(X,Y):- S is X / Y, write(S).

After we save our knowledge base, run the swipl with it to load it to our program:

swipl knowledgebasetest

In sumamry:
” :- “, here is an “if”,
” , ” here is an “and”,
” . ” here is a “;” (semi-colon)

In addition, you can just capitalize the first letter of a word to make it a variable.

After we load the program we will have a screen giving the following:

Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- 

In here, let’s send a query to our program test the rules.

 ?- animal(cat).

We can think it as asking if a cat is an animal.

Rest is same so I will pass to functions.

 ?- divide(600,15).
40 true.