Solving a Murder Mystery using Prolog

murder-mystery-cluedo
In the middle of last winter, eight guests were invited to a luxurious retreat at the Duke of York Grand Hotel. On the last day of their three-day getaway, the guests were free to vacate to their own occupations. Mrs White and Reverend Green did some “gardening” walking alongside the water fountains, Colonel Mustard and Professor Plum played golf (alone though, purposefully avoiding each other). The other guests spent their days either in their rooms or in the lounge, by the log fire. Later on in the afternoon, all the guests were indoors and Colonel Mustard was seen playing cards with Reverend Green and Mrs Peacock.

As the guests were called for dinner, they soon realised that Dr Black was missing. He was later found lying down on the floor of his bedroom. Dr Black had been shot dead using an old fashion revolver. Except from a few muddy footprints at the entrance of his bedroom, there was no other evidence left by the murderer.

Here is the list of all the guests for the weekend and the rooms they were staying in. Note that the hotel consists of twin bedrooms accommodating two guests per room. We also know that three of the guests (Reverend Green, Colonel Mustard and Madame Rose) own a revolver that they brought with them and kept in their room.

murder-mystery-clues

– Click on the picture to zoom in/enlarge –

Prolog?

Prolog is a language built around the Logical Paradigm: a declarative approach to problem-solving.

There are only three basic constructs in Prolog: facts, rules, and queries.

A collection of facts and rules is called a knowledge base (or a database) and Prolog programming is all about writing knowledge bases. That is, Prolog programs simply are knowledge bases, collections of facts and rules which describe some collection of relationships that we find interesting.

So how do we use a Prolog program? By posing queries. That is, by asking questions about the information stored in the knowledge base. The computer will automatically find the answer (either True or False) to our queries.

Source: http://www.learnprolognow.org/

Knowledge Base (Facts & Rules)

Let’s see how a detective with some good computer science skills would solve this murder mystery. Their first step would be to write all the facts about the case using the Prolog syntax:

Then our detective would write some rules and apply them to these facts.

A suspect is either a man or a woman who is not the victim!

#Rule #1:

This is how this rule would be implemented in Prolog:

Any suspect who was seen playing cards at the time of the crime has a valid alibi!

#Rule #2:

This is how this rule would be implemented in Prolog:

Anyone who did some gardening, played golf or is a smoker has been outside at least once throughout the day.

#Rule #3:

This is how this rule would be implemented in Prolog:

The rooms of the hotel are twin rooms that can accomomdate two guests. Two guests share the same room if they both are booked in the same room.

#Rule #4:

This is how this rule would be implemented in Prolog:

Anyone who owns a revolver or share a room with another guest who owns a revolver, had access to a revolver!

#Rule #5:

This is how this rule would be implemented in Prolog:

Solving this murder case!

Step 1: Can you use all the above facts & rules to work out the actual murderer?

Step 2: Can you write one final rule needed to solve this murder mystery in just one query?

To find our culprit we need to find a suspect, who went outside (we found some muddy footprints on the floor next to Dr Black’s body), who has no alibi and who had access to a revolver!

You can access the detective’s knowledge base:
https://swish.swi-prolog.org/p/murder-mystery.pl
Murder Mystery CodeOpen in New Window

Solution:

This is how our final rule would be implemented in Prolog:
View Solution!

Going a step further…

Now that you have solved this task, update the case by adding additional characters/suspects and additional clues. Make sure that at the end, the murderer of your case can still be found in just one query.

As additional suspects you could add some additional guests:

  • Mr Peach staying in room 25
  • Dr Orchid staying in room 25

Decide whether they went outside or not, whether they own a revolver or not, whether they were seen at the table playing card games or not! You can also add new indoors or outdoors activities.

You may also consider adding hotel staff members e.g.:

  • A barman,
  • A receptionist.

Staff members would have access to all the rooms!

Share Button
Tagged with: