Artificial Intelligence with Prolog
I was having lunch with a friend a few weeks ago and, as usual, the conversation started to drift towards AI. He mentioned that, through University secondments, he had worked with fellow researchers using the prolog programming language in the early 80’s. I was fortunate enough to have used it at University, a decade later, and thought now would be a great time to be nostalgic and tinker with the past.
On further investigations I also found out that, in 2011, the IBM Watson System for Natural Language Processing (NLP) was developed using prolog. Wow!
Prolog is a declarative programming language, widely used in artificial intelligence research, as opposed to the imperative programming paradigm such as Java or C. “Rather than using control-flow statements to specify how to achieve a certain goal in a certain situation, we specify the situation (rules and facts) and the goal (query) and let the Prolog interpreter derive the solution for us.” (Endriss, U).
I found an easy-to-use online compiler from rextester (https://rextester.com/l/prolog_online_compile), which allowed me to carry out some basic commands and get the feel for the language (remember the last time I used prolog was in the early 90's), without having to download and install any software. The examples provided in this article have been developed using rextester. Other downloadable resources include SWI-Prolog (www.swi-prolog.org). I also recently discovered http://tau-prolog.org/, which looks awesome!
The Knowledge Base
A prolog program is broken up into 2 parts. Firstly we create a knowledge base, made up of facts and rules, and then we query that knowledge through the query interpreter.
Let’s build our knowledge base first through some facts. As you can see, “charlie” and “chip” are names of dogs whereas “milo” and “leo” are cats.
When we run the program by clicking on the Run it (F8) button, nothing happens at this moment. We will need to consult / query the knowledge base for results to be produced.
By clicking on the [+] Show Input, we can open up a query window and find out if “charlie” is really a dog by consulting the knowledge base. When you click on the Run it (F8) button, the result true is returned, indicating that “charlie” is really a dog as expected.
What results would you expect if you queried the following (note: do not add these commands to your knowledge base, they are queries so should be added to the input query window. In other IDE’s, queries begin with ?- characters):
A few points that you will have noticed already:
- all commands end in a full stop
- lower-case is used for specifying facts
Now that we are getting the hang of facts and how to query using the IDE, let’s add a few more facts and some rules, which are used to describe the relationship among facts.
We can read the above rules as:
- “It’s an animal IF it is a dog OR a cat”
- “It’s a human IF it is a boy OR a girl”
You can figure out that :- symbol is equivalent to an IF statement, and the semi-colon ; is equivalent to an OR. An AND logic statement is written with a comma , rather than a semi colon. Also note the Capital Letters for X and Y, as we are passing in variables.
We can now query the knowledge base (facts and rules) in the input window using commands such as:
As mentioned previously, capital letters indicate variables. Therefore, we can use them as “wild-card” characters to find out Who is human:
Strangely, only the first fact is returned, however by using the semi-colon ; character, I can extract further answers:
The following example shows some facts (John likes Mary, etc) and rules (If they are friends, then person X likes person Y AND person Y likes person X. Therefore, Adam is not a friend of Mary despite liking her. However, as John and Mary like each other, they are friends.
I recently tried creating a simple loan application knowledge base, and decided to try out tau’s prolog sandbox. In the following example, Jon and Mary take out loans (car, mortgage or student loans), and we can query using the has_loan rule, which uses an OR “;” condition to determine if the user has one of the types of loans. Pretty cool!
In conclusion, I have found this exercise in nostalgia entertaining and I really enjoyed working with prolog. Using the online IDE ‘s made it easy to get up-to-speed with the basic syntax and structure of the prolog language. Drop me a line if you do carry out this exercise and don’t forget to tell me about your experience.
Lecture Notes: An Introduction to Prolog Programming by Ulle Endriss, University of Amsterdam. Institute for Logic, Language and Computation. 2014.