1. Introduction

What Prolog is

Prolog is a programming language based on logic. It was born in 1972, and it is still used for rules engines, language processing, and puzzles.

A Prolog program has no loops and no assignments. It is a list of things that are true. For example, "Alice likes Prolog" and "a person who likes Prolog likes logic" are true. When you ask "who likes logic?", Prolog finds the answer from what it knows.

likes(alice, prolog).
likes_logic(Person) :- likes(Person, prolog).

You don't write how to search for the answer. Prolog does the search for you. This is the biggest difference from JavaScript, and the rest of this tutorial is about how to use this search well.

How to use the boxes

Each example in this tutorial is in a box that you can run. Try this one. Press Run.

This is what happens.

  1. The lines that don't start with ?- are clauses. C#Prolog adds them to its database one by one, and says how many it added, such as "Added 3 clauses".
  2. A line that starts with ?- is a query, a question. C#Prolog shows the first answer, Who = alice.
  3. Prolog may know more answers. Press Next answer to see the next one, Who = carol. Press Done when you have seen enough.
  4. When there are no more answers, Prolog says false.

Some more things to know about the boxes.

  • Every clause and every query ends with a period.
  • You can edit the text. Change it and press Run again. Each run starts from an empty database, so the result depends only on the text in the box.
  • Reset brings back the original text.
  • If a query runs too long, press Stop.
  • A line that starts with % is a comment.

Your first change

Add a line likes(dave, prolog). before the query, and run the box again. Who = dave is a new answer. Then change the query so that it asks what bob likes, and run it once more.

Other ways to run Prolog

The boxes are enough for this tutorial. When you want more room, there are other ways.

  • The playground on the home page works like the console of a Prolog system.
  • The command line tool csprolog runs on your computer and can load Prolog files.
  • C# programs can use the engine through the C# API.
An unhandled error has occurred. Reload 🗙