Skip to content

Lean by the Rules

This course teaches Lean, a programming language in which you can also prove things about your programs, starting from nothing. It does it through the rules of real card games, as they are written in a working game engine: three games you can play in a browser against bots, whose every rule is Lean code and whose most important properties are proven.

You don't need to know anything about Lean, proofs or logic. You do need to be comfortable with some programming language and a terminal.

How it works

The engine in this repository runs three games, each with its own rules:

  • Snap, the children's game: two players, one deck, shout when the top cards match.
  • Lol ("bring the runestone to the Jarl"), a Viking card game of deduction for two to four players, with cards that let you look at, duel and knock out other players.
  • Alba, a hidden-role game for five to ten players, set in Scotland: the Licht against the Mirk, proposals, secret votes and a final accusation.

Each game is a step up. Snap needs nothing but data and functions, so it is where you learn the language and your first proofs. Lol's cards have effects that interact, so it is where rules become statements you can reason about. Alba has secrets and simultaneous moves, so it is where proofs about what players can and cannot know come in. The last section looks at the engine the three games share.

Every piece of code on these pages is the real thing:

  • The engine's code is quoted from the repository when the site is built, so it is always the code that runs.
  • The course's own examples live in learn/Learn/, and every one is checked by Lean whenever the site is built. When a page says an expression prints 52, Lean has checked that it does.

Changing the rules

The best way to learn how rules become code is to change them. Each game's section ends with a chapter that does exactly that: you edit the real rules, run the tests, play the changed game in your browser, and see which proofs still hold and which break, and why. The repository is yours to break: git puts it back.

Where to start

Start with Setting up, then work through the chapters in order. Each chapter introduces a few ideas and ends with exercises; their solutions are on the page, folded away.