Getting started with Prolog
Prolog is a declarative and logic programming language which has it’s roots in first-order logic. The logic is expressed in terms of relations, represented as facts and rules. A computation is initiated by running a query over these relations. It is highly used in artificial intelligence(AI). It is also used for pattern matching over natural language parse trees.
Functional programming language and prolog have some similarities like Hugs. A logic program is used to consist of relation definition. A functional programming language is used to consist of a sequence of function definitions. Both the logical programming and functional programming rely heavily on recursive definitions.
Why Prolog?
Prolog is a programming language centred around a small set of basic mechanisms, including pattern matching, tree-based data structuring and automatic backtracking. This small set constitutes a surprisingly powerful and flexible programming framework. Prolog is especially well suited for problems that involve objects — in particular, structured objects — and relations between them.
In Prolog, the program contains a sequence of one or more clauses. The clauses can run over many lines. Using a dot character, a clause can be terminated. This dot character is followed by at least one ‘white space’ character. The clauses are of two types: facts and rules.
Facts
A fact is like a predicate expression. Clauses with empty bodies are called facts. An example of a fact is:
cat(tom).
which is equivalent to the rule:
cat(tom) :- true.
The built-in predicate true/0
is always true.
Given the above fact, one can ask:
is tom a cat? or is thomas a cat?
?- cat(tom).
Yes
Explanation : As our knowledge base contains the above fact, so output was ‘Yes’, otherwise it would have been ‘No’.
what things are cats?
?- cat(thomas).
No
?- cat(X).
X = tom
Note: Rules works on the basis of facts we define. As you can see that in the above example we didn’t define any fact for
thomas
so when we tried to ask whetherthomas
is a cat or not, then it said No.
Rules
Clauses with bodies are called rules. An example of a rule is:
animal(X) :- cat(X).
If we add that rule and ask what things are animals?
?- animal(X).
X = tom
Installation
To install the SWI-Prolog based on your OS, please visit the following link https://www.swi-prolog.org/Download.html
You can also use the available container image present on DockerHub.
Demo
Now let’s try to define some facts and rules that answers questions about whether it is possible to travel from one place to another by ‘chaining together’ car, train, plane journey.
byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).
byTrain(metz, frankfurt).
byTrain(saarbruecken, frankfurt).
byTrain(metz, paris).
byTrain(saarbruecken, paris).
byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(losAngeles,auckland).
onestep(X,Y) :- byCar(X,Y).
onestep(X,Y) :- byTrain(X,Y).
onestep(X,Y) :- byPlane(X,Y).
travel(X,Y) :- onestep(X,Y).
travel(X,Y) :- onestep(X,Z),
travel(Z,Y).
travelbyt(X,Y,byCar) :- byCar(X,Y).
travelbyt(X,Y,byTrain) :- byTrain(X,Y).
travelbyt(X,Y,byPlane) :- byPlane(X,Y).
save the above in a file with extension as .pl
(eg travel.pl). Now let’s try to compile and run it. Let say, Your file name is question.pl
. If you are in Mac or Ubuntu:
- Open the terminal in the directory where your
.pl
file is saved and then runswipl
orswi-prolog.swipl
which will take you to prolog terminal - Now to compile the file run
consult(filename).
You’ll see something like
If the output is true.
that means the code has been compiled successfully.
3. Now to perform a query you can do
?- travel(auckland,raglan).
true .
or if you want to list all the places which you can visit from Auckland
then you can do
?- travel(frankfurt,X).
and then press Enter
key and then subsequently tab
key until you see false.
Now as an exercise can you find all the places which you can visit by plane from frankfurt
. In case you found this interesting don’t forget to press the clap button 😉