Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท Getting Started

What Is Prolog?

Meet Prolog, the logic programming language: you state facts and rules, and the system finds answers by searching. See how it differs from Python, Java or C.

Prolog is a programming language built on logic. In most languages you write a recipe: do this, then that, repeat until done. In Prolog you describe a world. You write down what is true (facts) and how new truths follow from old ones (rules). Then you ask questions, and Prolog works out the answers by searching through everything you described.

The name comes from the French programmation en logique. The language was created in the early 1970s in Marseille and Edinburgh, and it became the best-known language of symbolic artificial intelligence. It is still used for natural-language processing, planning, scheduling, expert systems, theorem provers and rule engines. Learning it will change how you think about problems, even if you write Python or Java at work.

Prolog cannot run inside this site's code editors. Blocks marked prolog are read-only, and their output is shown only when we are certain of it. Where you can press "Run" you will see a Python program called the mini-Prolog stand-in: a small engine we wrote that reads Prolog facts, rules and queries and searches for answers the way Prolog does. It is a teaching tool, not a full Prolog. To run the real thing, install SWI-Prolog in the next lesson.

Declarative versus imperative

Imperative languages (C, Java, Python) describe how to compute a result: variables change, loops count, functions return values. Prolog is declarative: you describe what is true and let the system decide how to find answers.

Here is a tiny Prolog program:

PROLOG
parent(tom, bob).
parent(bob, ann).

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

The first two lines are facts: Tom is a parent of Bob, Bob is a parent of Ann. The last line is a rule. Read :- as "if": X is a grandparent of Z if X is a parent of some Y and Y is a parent of Z. There is no loop and no function body that "returns" something.

Now you can ask the question grandparent(tom, Who). Prolog answers Who = ann. It found the answer by matching and searching, not by following instructions you wrote.

Programs are relations, not functions

A function maps inputs to an output. A Prolog predicate describes a relation between things, and relations can be used in any direction. If you define how to join two lists, the same definition can also split a list into all possible pairs of parts. You will see this in lists and recursion. The same parent facts answer "who are Tom's children?", "who is Ann's parent?" and "list every parent pair", with no extra code.

The three ingredients

Every Prolog program is built from three things:

  1. Facts: statements that are unconditionally true. likes(mary, pizza).
  2. Rules: statements that are true if conditions hold. happy(X) :- likes(X, pizza).
  3. Queries: questions you ask the system. ?- happy(mary).

A collection of facts and rules is called a knowledge base or a database. The Prolog system itself supplies the engine: unification (a pattern-matching process) and backtracking (automatic search with undo). Those two ideas, covered in chapter 3, are the heart of the language.

Prolog in one picture

Facts What is true
Rules What follows
Queries What you ask
Search Prolog finds answers

You describe the world; Prolog does the searching.

1 / 2

Your first look, using the stand-in

Press Run. The program below stores three facts and two rules, then asks several questions. Each answer ends with ; when more answers follow and . after the last one, just like the interactive prompt of a real Prolog.

Python A first knowledge base (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what happened. parent(bob, tom) printed false.: Prolog could not prove it from the database. That does not mean it is untrue in the real world; it means it is unprovable from what you told Prolog. This closed-world assumption is explored in rules and negation.

Asking the same rule in different directions

The query parent(Who, ann) asked "who is Ann's parent?" using the very same facts that answered "who are Tom's children?". Try the next block, which compares the two directions and lists every pair at once.

Python One relation, many questions (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What Prolog is good at

  • Search problems: puzzles, planning, scheduling, configuration. You state the constraints and Prolog explores.
  • Symbolic data: trees, expressions, grammars. Pattern matching on structures is built in.
  • Rule-based reasoning: legal rules, medical rules, access policies.
  • Prototyping languages: parsers and interpreters are short to write.

What Prolog is not good at

Number crunching, graphics, and big loops over mutable arrays are better done elsewhere. Prolog programs can also be slow if the search space is huge, and you must learn to guide the search. Still, a lot of real systems embed Prolog for the part that is logic and use another language for the rest.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Prolog programmer mainly write?

    Prolog is declarative: you describe what is true with facts and rules, and the system searches for answers.
  2. 2. How is the symbol :- read in a rule?

    The head of a rule is true if the body (after :-) is true. Read :- as 'if'.
  3. 3. A query that Prolog cannot prove from the database prints:

    Prolog answers false when it fails to prove a goal from the facts and rules it knows (the closed-world assumption).
  4. 4. Which two mechanisms form the core of Prolog's execution?

    Unification matches terms and binds variables; backtracking undoes choices and tries alternatives.

Summary

  • Prolog is a declarative, logic-based language: you write facts and rules, then ask queries.
  • A knowledge base of facts and rules is searched by unification and backtracking.
  • Relations can be used in several directions with the same definition.
  • Anything Prolog cannot prove is reported as false (closed-world assumption).
  • On this site Prolog blocks are read-only; the mini-Prolog stand-in runs examples in Python.

Next: Installing and Running SWI-Prolog