Artificial Intelligence with Prolog

Image courtesy of

Online IDE

I found an easy-to-use online compiler from rextester (, 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 ( I also recently discovered, 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.



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.

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

  • “It’s an animal IF it is a dog OR a cat”
  • “It’s a human IF it is a boy OR a girl”

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

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


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