git clone https://a.pseudo.top/lean-101.git

lean-101: the Natural Number Game in your own editor #

The Natural Number Game teaches Lean by building the natural numbers from scratch: addition, multiplication, powers, inequalities, one small theorem at a time. In this repository we copy the levels of this game into ordinary Lean files, so you can play the game locally on your own computer.

Each level is a theorem whose proof is sorry (sorry is a special keyword which lets you "prove" any theorem for free). To beat a level, one should replace the sorry with a genuine proof. The project needs nothing but core Lean (i.e., mathlib is not required), so it builds quickly.

Getting started #

  1. Install Lean. Lean comes through its version manager, elan:

    curl https://elan.lean-lang.org/elan-init.sh -sSf | sh
    

    This puts lean and lake (Lean's build tool) in ~/.elan/bin. Open a new terminal afterwards so they are on your PATH.

  2. Clone the git repository and build the levels.

    git clone https://a.pseudo.top/lean-101.git
    cd lean-101
    lake build
    

    The first time you run lake build, it will first download Lean; this may take a minute or so. You will also see "sorry" warnings for each level; this is normal.

    Remark. The goal of the game is to be able to run lake build without any warnings.

  3. Start playing. Open Game/Tutorial.lean in your editor (see below for Emacs, VS Code or a plain terminal) and begin with level 1.

The worlds #

Play them in this order. Each world can use everything proved in the worlds before it, so your earlier theorems become tools for the later ones.

File What you learn
Game/Tutorial.lean rfl, rw
Game/Addition.lean induction
Game/Multiplication.lean repeat, choosing what to induct on
Game/Power.lean rewriting at a chosen spot (rw [mul_comm a b])
Game/Implication.lean exact, apply, intro, have, symm
Game/AdvAddition.lean cases
Game/LessOrEqual.lean exists, left, right
Game/AdvMultiplication.lean contradiction, by_cases, induction … generalizing
Game/Algorithm.lean simp only, a home-made tactic, trivial, decide

Each file starts with an introduction to its new tactics and theorems, and every level comes with a hint in a comment just above it. Stuck? Read the hint again, and look at the goal (below) after each step.

Playing in a terminal #

Run bin/watch Game/Tutorial.lean in one terminal and edit the file in another, with any editor. Each time you save, the file is checked again (after rebuilding any earlier world you changed), and Lean's messages are printed, followed by the number of levels left. To see the goal, type your tactics above the level's sorry: watch shows the goal at that sorry, just as the game shows it after your last step. The sorry of a level you haven't started stays quiet. To see the goal anywhere else, put trace_state on a line of its own there. lake build checks all the files at once.

Playing in VS Code #

Install the "Lean 4" extension and open the lean-101 folder. The goal appears in the Lean Infoview panel as you move the cursor.

Playing in Emacs #

emacs/lean.el sets everything up: nael on eglot, a goal window, and indentation that understands Lean. It needs Emacs 29.1 or later and works on a fresh one: eglot, use-package and ElDoc come with Emacs, and lean.el adds MELPA if your Emacs doesn't have it and installs nael from there. Load it from your init.el:

(load "/path/to/lean-101/emacs/lean.el")

The first time, Emacs downloads nael and may show a *Compile-Log* buffer with warnings about free variables: they are harmless, close it. Then open a .lean file. Lean starts in the background (the first time takes a few seconds; the mode line shows [eglot:lean] when it is ready).

Credits and licence #

The levels, their statements and most hints come from NNG4 (Game/Levels/) by Kevin Buzzard, Jon Eugster and the Lean community, under the Apache License 2.0. This repository is under the same licence (LICENSE).

Remark on MyNat.lean #

Game/MyNat.lean is not a world: it builds the game's natural numbers (MyNat), with succ, the numerals, +, *, ^ and ≤. Feel free to read it, but don't edit it, since every world depends on it. As in the game, +, * and ^ are sealed and come with their defining equations (add_zero, add_succ, mul_zero, …) as axioms, so Lean can't simply compute 2 + 2.