Artificial Intelligence with Prolog

Image courtesy of https://voicebot.ai/2021/11/11/ibm-watson-upgrades-enterprise-natural-language-processing/

Online IDE

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.

Facts

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.

dog(charlie).
dog(chip).
cat(milo).
cat(leo).

Queries

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.

dog(milo).
cat(zeus).
  • all commands end in a full stop
  • lower-case is used for specifying facts

Rules

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.

dog(charlie).
dog(chip).
cat(milo).
cat(leo).
boy(john).
girl(mary).
is_animal(X):-dog(X);cat(X).
is_human(X):-boy(X);girl(X).
  • “It’s an animal IF it is a dog OR a cat”
  • “It’s a human IF it is a boy OR a girl”
is_animal(leo).
is_human(mary).
is_human(chip).

More Examples

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.

What’s next?

Whilst I was updating my knowledge of prolog (excuse the pun!), I kept pondering how I could share my work with others — online. I then stumbled upon Tau-Prolog’s javascript utility, which allows you to execute prolog code from the client front-end using javascript, therefore I was able to create a simple web interface demo, found at https://chekulhan.github.io/

Demo app using tau-prolog javascript
Knowledge base using tau-prolog javascript

References

Lecture Notes: An Introduction to Prolog Programming by Ulle Endriss, University of Amsterdam. Institute for Logic, Language and Computation. 2014.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Che Kulhan

Che Kulhan

Technology and Financial Consultant