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
- 01Facts: what the program knowsNot started
The shape of a Prolog program: facts you state, the starting rule the page runs, and printing answers with write and nl.
- 02Rules: facts that reasonNot started
Rules with a head and a body, variables that start with a capital letter, and the search that reports one answer after another.
- 03Numbers and arithmetic with isNot started
Working out values with is, the difference between a slash and div, and the comparisons that ask questions about numbers.
- 04Matching: the four equals signsNot started
The single equals sign makes two terms the same, the signs beside it compare them without binding anything, and structures match piece by piece.
- 05Recursion: a rule that calls itselfNot started
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.
- 06Decisions: guards, cuts and notNot started
Choosing between clauses, branching inside one clause with if-then-else, the cut that commits to an answer, and negation as failure.
- 07Lists: heads, tails and helpersNot started
One pattern that matches every list, the two clauses every list helper needs, and why you write those helpers yourself in this sandbox.
- 08Generating numbers and collecting answersNot started
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.
- 09A knowledge base: a family treeNot started
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.
- 10Terms up close: functors, atoms and sortingNot started
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.
- 11A knowledge base that growsNot started
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.
- 12Expert: a rule-based adviserNot started
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.
