Course

Prolog from the ground up

Tell the computer what is true, and let it search

  • Beginner
  • 12 chapters
  • 211 min read

Learn Prolog by writing it: facts and rules, arithmetic with is, unification, recursion instead of loops, cuts and negation, lists with the helpers you write yourself, generators that produce answers, a family tree, terms taken apart, a knowledge base that grows while it runs, and a small expert system at the end.

What You'll Learn

  • Write facts and rules and let the engine search them for answers
  • Print what you computed with write and nl instead of guessing
  • Work out values with is, and keep div, mod and the comparisons apart
  • Match terms with the single equals sign and tell it apart from the other three
  • Repeat work with a rule that calls itself, ending in a clause that stops it
  • Choose between clauses, commit with the cut, and negate a goal honestly
  • Walk a list with the head and tail pattern, writing your own helpers
  • Generate numbers with between and collect answers with findall and forall
  • Model a small knowledge base and ask it questions with rules
  • Take a term apart with functor, arg and univ, and treat atoms as text
  • Add and remove facts at run time, with the dynamic directive that allows it
  • Build a rule-based adviser that reports a verdict and explains it

Prerequisites

  • No programming experience needed — the first chapter starts at the first fact
  • If you know another language, the search behaviour is the new part

Course Content

  1. 01
    Facts: what the program knows

    The shape of a Prolog program: facts you state, the starting rule the page runs, and printing answers with write and nl.

    Beginner · 13 min read

    Not started
  2. 02
    Rules: facts that reason

    Rules with a head and a body, variables that start with a capital letter, and the search that reports one answer after another.

    Beginner · 14 min read

    Not started
  3. 03
    Numbers and arithmetic with is

    Working out values with is, the difference between a slash and div, and the comparisons that ask questions about numbers.

    Beginner · 15 min read

    Not started
  4. 04
    Matching: the four equals signs

    The single equals sign makes two terms the same, the signs beside it compare them without binding anything, and structures match piece by piece.

    Beginner · 16 min read

    Not started
  5. 05
    Recursion: a rule that calls itself

    How repetition works in a language without loops: a rule that asks itself a smaller question, a base case that stops the descent, and answers built on the way back.

    Intermediate · 17 min read

    Not started
  6. 06
    Decisions: guards, cuts and not

    Choosing between clauses, branching inside one clause with if-then-else, the cut that commits to an answer, and negation as failure.

    Intermediate · 18 min read

    Not started
  7. 07
    Lists: heads, tails and helpers

    One pattern that matches every list, the two clauses every list helper needs, and why you write those helpers yourself in this sandbox.

    Intermediate · 18 min read

    Not started
  8. 08
    Generating numbers and collecting answers

    Goals that invent values with between, the failure-driven loop that prints one line per answer, and findall, forall and once to keep the output under control.

    Intermediate · 18 min read

    Not started
  9. 09
    A knowledge base: a family tree

    Facts as rows and rules as questions, a join that runs along a shared variable, and the careful way to ask about something that was never recorded.

    Advanced · 20 min read

    Not started
  10. 10
    Terms up close: functors, atoms and sorting

    Taking a structure apart with functor, arg and univ, turning a term into a list and back, and the helpers that treat atoms as text.

    Advanced · 20 min read

    Not started
  11. 11
    A knowledge base that grows

    Adding and removing facts while the program runs, the dynamic directive that allows it, and the add-once rule that keeps a list from filling with copies.

    Advanced · 20 min read

    Not started
  12. 12
    Expert: a rule-based adviser

    A small expert system built from signs, causes and a rule that checks a whole set at once, ending with a report that explains the verdict.

    Advanced · 22 min read

    Not started