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.
Awakening
How a koan checks your answers, and the three things Prolog is made of: atoms, numbers and variables.
Facts and rules
A Prolog program is a database: facts that are true, and rules that derive more.
Arithmetic
Evaluating with is, and comparing numbers.
Lists
Lists, written [a, b, c], and taken apart as [Head|Tail].
Recursion
Defining relations on lists and numbers by recursion.
Higher-order predicates
Passing goals around: call, maplist, foldl and lambdas.
Backtracking
How Prolog searches: trying alternatives, and generating answers to test them.
Cut and negation
Controlling the search: the cut, once, negation, and if-then-else.
Collecting solutions
findall, bagof, setof and aggregate_all.
Atoms and strings
Text in Prolog: atoms, strings, and the lists of characters between them.
Terms
Every piece of data is a term, and terms can be inspected and built.
Structures
Compound terms as records and trees, and SWI-Prolog's association lists.
The database
Adding and removing facts while the program runs.
Errors
Throwing and catching, and the standard error terms.
Grammars
Definite clause grammars: parsing and generating with -->.
Puzzles
Small programs to read and predict: the kind of problem Prolog was made for.