Prolog is a logical programming language perfect for description of artificial intelligence knowledge base I was working on for one of my AI projects. The idea is that you make a knowledge base and then ask your knowledge base what ever you need to know. As Linux user I needed Linux tool so I went on Google and inquired about my options. I really like GNU software so I've opted for GNU Prolog implementation called Gprolog. In this little article I will show you how to get it working on Debian based operating systems.
It is really simple. First we will install Gprolog and create folder for our source code. You just open your terminal and enter:
sudo apt-get install gprolog |
Now we create directory where we will keep our prolog source code:
mkdir prolog_src cd prolog_src |
After this process is done you will have access to the Gprolog by typing, you would never guess, gprolog
in your terminal. But before you fire up the Gprolog you should create knowledge base. You do this in any text editor by using Prolog syntax to create .pl file. As an example we will create Prolog file called family.pl with source code from Wikipedia Prolog site and then load this file into a Gprolog. First lets start our text editor with family.pl as command line argument.
gedit family.pl |
Next we paste our knowledge base from Wikipedia or whatever source code you have. Here's the contents of family.pl file:
mother_child(trude, sally). father_child(tom, sally). father_child(tom, erica). father_child(mike, tom). sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). parent_child(X, Y) :- father_child(X, Y). parent_child(X, Y) :- mother_child(X, Y). |
We hit save and close our text editor. Now that we have our knowledge base we can run Gprolog from the same directory where we have created our family.pl knowledge base, like this:
gprolog |
GNU Prolog 1.3.0 By Daniel Diaz Copyright (C) 1999-2007 Daniel Diaz | ?- |
Now we load our knowledge base by typing following and pressing enter.
[family]. |
The dot at the end is like semicolon in other programming languages in a way that every prolog "sentence" ends with dot. This is the response from Gprolog:
compiling /home/marko/prolog_src/family.pl for byte code... /home/marko/prolog_src/family.pl compiled, 0 lines read - 132 bytes written, 6 ms |
Now we can ask our knowledge base and receive logical answers. If we use Wikipedia example we can ask our knowledge base to give as all father-child combinations defined in our knowledge base:
father_child(Father, Child). |
Gprolog logical response would be:
Child = sally Father = tom ? ; Child = erica Father = tom ? ; Child = tom Father = mike yes |
To continue with the next result press ;
key and if you want to exit Gprolog press ctrl+c
and then e
. To find out more about Gprolog you can consult Gprolog manual. You can find it on /usr/share/doc/gprolog-doc/
path inside gprolog.pdf
file. Hope that's enough to get you started with prolog on Linux.
Thank you very much. Your instructions have been very useful. I am a Computer Science student and i wanted to find a free version of Prolog because at the university we use Sicstus Prolog. I am also an Ubuntu user and i wanted to find something for my OS. The installation and usage is straightforward and easyer than i had expected :).
Hello Christina,
I’m glad you found Prolog and Ubuntu useful. Amongst other open source alternatives I prefer GNU software because it is always top quality. GNU Prolog isn’t exception. I’m also computer science student and my college unfortunately uses proprietary Prolog implementations on proprietary operating system. I’d like to change that 😉
Thanks, it was really helpful to start quickly working on prolog.Nice tutorial.
Thank you for the useful tutorial
Thank you for this wonderful and easy to learn tutorail. I really appreciate!
Started learning the Prolog language in the university today. Thank a lot for this tutorial! Now will work on at home)
thaks you so much i learn to prolog.
Hi,
I’m very new at prolog. I would like to run a program in prolog from a unix command, and access to the result without human intervention ; something like:
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
?-father_child(tom, Child).
which would return what follows in the unix console
Child = sally
Father = tom ?
And better than that, have a query like
?- findall(C, father_child(tom,C), ...).
Is it possible ? how ?