Prolog Logic Programming: A Detailed Explanation

Category Data Engineering

Prolog logic programming language is a general-purpose programming language. If you have never heard of this language and are a SQL aficionado, you will recognize the way of writing code in this language, which feels quite like a query. This is a declarative language or a rule-based language like SQL. Note that in “normal” languages that you encounter, like C, Java, and Python, you generally write out the ways in which you retrieve the data or do the computation. Here, you will just be defining what you want instead of writing out explicitly how to get it. Feels wonderful, isn’t it? Now, since this is a vast language by itself, only a minuscule part will be looked at.

1.install swipl
2.take a look at some basic examples to have some idea on how prolog code looks like
3.ruminate on some basic ways in which we can reason about code

SWIPL
SWI-Prolog is one of the most comprehensive implementation for prolog. It is also very widely used. You can use the SWI-Prolog interpreter to run the programs. To install the interpreter in ubuntu you can run the following commands.

sudo add-apt-repository ppa:swi-prolog/stable
sudo apt-get update
sudo apt-get install swi-prolog

Now, create a new hello.pl and type the below lines:

 /*
 * usage: swipl -q -f prolog_hello.pl -t main
 */
 main :-
 write("Hello world!"), nl, fail.

You have created a hello world program for prolog. In the terminal, run the following command to run this file and you should get the output in the terminal.

➜ swipl -q -f hello.pl -t main
Hello World

Some code examples
Take a look at some code samples to understand the flow of logic in a prolog code. To show relationships between data, write in the following format.

verb(noun1, noun2)

which means that there is a relationship from noun1 to noun2 via relationship verb . Using this define the following relationships.

 has(jack,apples). % jack has apples.
 has(jack,plums). % jack has plums.
 has(ann,plums). % ann has plums.
 has(dan,money). % dan has money.
 fruit(apples). % apple is fruit.
 fruit(plums). % plums is fruit.
view rawprolog1.pl hosted with ❤ by GitHub

This is logic programming where the facts and rules of the problem domain is defined. The following code will list the members of fruit class.

https://gist.github.com/infinite-Joy/infinite-Joy/0cb2efac38507593cd0380e6d504e2ae

This is called a query. Save this to a listing.pl file and then execute the query. The command is written as:

swipl -q -f -t

➜ swipl -q -f listing.pl -t main
fruit(apples).
fruit(plums).

This shows a listing of all the relationships in the data.
Similarly, based on the existing relationships, write the names of the people who have apples or plums.

 main :-
 has(X,apples), % who has apples
 has(Y,plums), % who has plums
 write(X), % write the apple lovers
 put(10), % new line
 write(Y), % write the plum lovers
 nl, halt. % new line and halt
view rawprolog3.pl hosted with ❤ by GitHub

Run the file in the terminal using the interpreter

➜ prolog swipl -q -f listing1.pl -t main
jack
ann

You can find the whole file in this github gist.
Now as an exercise can you find the people who have both apples and plums. Share the gist in the comments below.
In case you found this interesting and would like to talk more on this, just drop me a message @alt227Joydeep. I would be glad to discuss this further. You can also hit the like button and follow me here, on Medium.

Ready to embark on a transformative journey? Connect with our experts and fuel your growth today!