PrologEZ
An interactive course

Learn Prolog by running it.

A notebook for logic programming. Start with facts and queries, finish with grammars, puzzles and meta-interpreters. Every idea comes with live code you can edit and execute in your browser, real SWI-Prolog, no install.

Foundations

Data & computation

Natural numbers and PeanoNumbers as terms: z and s/1, the nat/1 and sum/3 relations, and equations that run both ways.Peano arithmeticsucc, sum, mul, dec and factorial on zero and s/1, plus predicates as boolean functions.Booleans as a data typePropositional logic as universal facts, then improved with rules.A program as a databaseQuerying facts, existential and universal queries, records and the wildcard variable.ArithmeticWhy `X = 1 + 2` is not 3, and how `is` fixes it.Built-in operators and mathOperators as predicates, evaluation with is/2, comparison, and the resolution of sum/2.ListsThe workhorse data structure: [Head|Tail] patterns and the standard library.Lists from scratchLists as cons/nil terms, the built-in [H|T] syntax, and member, find, position, concat and count.Full relationalityOne predicate, many functions: variables anywhere, and the pitfalls of enumeration.RecursionBase case, recursive case, and accumulators for speed.Performance, tail recursion and immutabilityCounting resolution steps, tail and non-tail recursion, and how terms are shared.Generating combinations and searchingExploration as a generator: join, permutation, combinations of solutions, and grid links.Structures & treesCompound terms as records, and a binary search tree from scratch.Algorithms on other data typesDatabase tables, binary and n-ary trees, bidirectional lists and lazily expanding lists.Inspecting and managing termsType checks, comparison, copy_term, terms as strings, univ, functor, arg, and console output.Text & outputAtoms, strings, character lists, and printing with format/2.

Control & the database

Advanced