koans.app

Prolog Koans

Prolog is a logic programming language: instead of saying how to compute an answer, you state facts and rules, and ask questions that Prolog answers by searching for every solution. It is used for parsing, expert systems, planning, and puzzles of every kind. These koans follow SWI-Prolog: facts and queries, unification, lists and recursion, arithmetic, backtracking and the cut, negation, collecting solutions, and grammars.

70 koans in 16 groups, tested with plunit. Each is a small program whose tests fail: replace every ___ until they pass.

Start the path

Awakening

How a koan checks your answers, and the three things Prolog is made of: atoms, numbers and variables.

  1. Tests
  2. Atoms and variables
  3. Unification
  4. Comparing terms

Facts and rules

A Prolog program is a database: facts that are true, and rules that derive more.

  1. Facts
  2. Multiple solutions
  3. Rules
  4. Recursive rules
  5. Or

Arithmetic

Evaluating with is, and comparing numbers.

  1. Is
  2. Division
  3. Comparing numbers
  4. Arithmetic functions
  5. Between, succ and plus

Lists

Lists, written [a, b, c], and taken apart as [Head|Tail].

  1. Head and tail
  2. Member and length
  3. Append
  4. List predicates
  5. Sorting
  6. Select and permutation
  7. Sets as lists

Recursion

Defining relations on lists and numbers by recursion.

  1. Recursion on lists
  2. Building lists
  3. Accumulators
  4. Recursion on numbers
  5. Relations run backward

Higher-order predicates

Passing goals around: call, maplist, foldl and lambdas.

  1. Call
  2. Maplist
  3. Lambdas
  4. Foldl

Backtracking

How Prolog searches: trying alternatives, and generating answers to test them.

  1. Backtracking
  2. Generate and test
  3. Order matters
  4. Forall

Cut and negation

Controlling the search: the cut, once, negation, and if-then-else.

  1. The cut
  2. Once
  3. Negation
  4. If-then-else

Collecting solutions

findall, bagof, setof and aggregate_all.

  1. Findall
  2. Bagof and setof
  3. Aggregate_all

Atoms and strings

Text in Prolog: atoms, strings, and the lists of characters between them.

  1. Atoms
  2. Characters
  3. Strings
  4. Converting
  5. Atomic_list_concat
  6. Format

Terms

Every piece of data is a term, and terms can be inspected and built.

  1. Type checks
  2. Inspecting terms
  3. Operators

Structures

Compound terms as records and trees, and SWI-Prolog's association lists.

  1. Terms as records
  2. Trees
  3. Association lists

The database

Adding and removing facts while the program runs.

  1. Assert and retract
  2. Keeping state
  3. Global variables

Errors

Throwing and catching, and the standard error terms.

  1. Catch and throw
  2. Error terms
  3. Throwing your own errors
  4. Cleaning up

Grammars

Definite clause grammars: parsing and generating with -->.

  1. DCG basics
  2. Grammars with arguments
  3. Parsing part of the input
  4. Counting with grammars

Puzzles

Small programs to read and predict: the kind of problem Prolog was made for.

  1. FizzBuzz
  2. Towers of Hanoi
  3. N queens
  4. Run-length encoding
  5. Map coloring
  6. A logic puzzle