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.
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:
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.
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:
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.