Fork of lean-game with a probability theory course
README.md

lean-game — a playable Probability & Information Theory course (in Lean) #

This repository contains:

  • probinfo/ — the main deliverable: Probability & Information Theory, an 18-level interactive Lean course game (4 worlds: finite probability spaces, conditional probability & Bayes, entropy, information theory & KL divergence).
  • Makefile + patches/ — the run tooling and the local patch for the game platform (fast, reliable lesson starts; see below).

The game platform itself (lean4game/ — client + relay + gameserver) is an installed dependency, not part of this repo: make deps fetches it into a sibling lean4game/ folder at a pinned commit (PLATFORM_REV in the Makefile), and make patches applies the local adaptation on top.

You play the game in a browser; each level shows a Lean statement whose proof you complete with a small inventory of tactics, and the (real) Lean kernel verifies every step.

Repository layout #

lean-game/
├── Makefile                  ← everything you normally need
├── AGENTS.md                 ← deep context + authoring pitfalls
├── patches/                  ← the local lean4game patch (session pool + client recovery)
├── probinfo/                 ← the course game (this repo's main content)
└── lean4game/                ← NOT committed: installed by `make deps` (pinned commit + local patch)

The game must stay a sibling folder of lean4game/ — in dev mode the server lists every subdirectory of the shared parent folder that has been built (i.e. has .lake/gamedata/game.json) as a "local" game.

Optional: the public reference games nng4 (Natural Number Game) and GameSkeleton (the official template) are good to clone next to your game as extra local games while authoring levels. This repo does not include them.

Requirements #

  • git, make, bash
  • [elan] (Lean's toolchain manager) — one line: curl https://elan.lean-lang.org/elan.sh -sSf | sh (then re-open your shell). The right Lean toolchain (v4.23.0) is fetched automatically via Lean's dependency resolver. (If you run lake by hand, ~/.elan/bin must be in your PATH; the Makefile adds it for you automatically, so make works either way on a sane setup.)
  • Node.js ≥ 20 with npm (for the web platform)
  • Internet access on first build (Lean toolchain + Mathlib + dependencies download)
  • Disk: ~ and ~/.elan plus the per-game .lake/ directory grow to a few GB (Mathlib cache); each warm Lean server holds the game's import closure in RAM (several GB)

Quick start #

git clone <this-repo-url> lean-game && cd lean-game
make setup          # prerequisites + install the platform + patch it + build probinfo
make serve          # starts the full stack (foreground; Ctrl-C stops it)

Open http://localhost:3000/#/g/local/probinfo — the Probability & Information Theory game.

First build note: the first lake build on a fresh machine downloads Lean and compiles Mathlib — this can take a long time (tens of minutes to a couple of hours, depending on the machine). Subsequent builds are incremental and fast.

Background server (optional) #

make start          # runs the stack in the background (log: lean4game.log)
make status         # ports + which games are being served
make stop           # stop it

What make offers #

target action
help show all targets
check verify prerequisites (git/make/node/elan) and layout
deps install the lean4game platform at the pinned commit (idempotent)
patches apply the local platform patch (session pool + client recovery) and rebuild the relay; idempotent
setup check + deps + patches + build the game
build build probinfo (generates its game data)
serve / start / stop run the platform (fg / bg / kill; serve+start also verify the patch)
status show what's running + the game list
clean delete the .lake/ build artifacts

Why the patches target? Out of the box the platform spawns a fresh Lean server for every lesson and kills it when the lesson closes — that server has to load the game's full import closure (~4 GB) and replay the game on each start, and several concurrent lessons contend for disk/CPU — hence the long wait on each start. The patch (in patches/) turns the relay into a session pool: it keeps a small pool of fully warmed servers (a fake player plays level 1 at boot) and reuses finished lessons' servers for the next lesson instead of killing them, so lesson opens respond in seconds instead of tens of seconds. It also hardens the client/server retry paths (hold-and-resend + RPC-session recovery) so a lesson is never left looking frozen. Details: AGENTS.md → Session model + warm pool.

Developing the game #

cd probinfo
lake build          # ~a few seconds per changed level (Mathlib is cached by then)

A successful build is a strong correctness signal: for each level the game engine replays the sample solution step by step (look for Replayed Game.Levels.<World>.L<n>... in the log) and fails if any step is invalid. Refresh the browser tab after a rebuild.

Level files live in probinfo/Game/Levels/<World>/L<nn>_<name>.lean. All authoring conventions, the import-graph rules, and the Mathlib/Lean + GameServer gotchas actually hit while building this game are documented in AGENTS.md — read it before adding or editing levels.

How it works (short version) #

  • client (React, Vite, :3000) — the editor UI you play in.
  • relay (Express, :8080) — API + static game data + WebSocket hub.
  • A per-game gameserver (Lean) — spawned per session from the game's own build; does the live Lean elaboration / proof replay / hints.

The game is a normal Lake package whose GameServer dependency is pinned to the Lean v4.23.0 tag of the lean4game repo, so the build is reproducible independent of the local lean4game/ install.

One local deviation from upstream: patches/lean4game-local-adaptations.patch (run a session pool of Lean servers + harden the client/server retry paths; see above) — applied by make patches at setup/serve/start time, idempotent.

Troubleshooting #

Symptom Fix
make says elan/node missing install elan (see Requirements) and/or Node ≥ 20; re-run make check
The game isn't listed at :3000 it must be built: cd probinfo && lake build (or make build); then refresh
Opening a lesson stalls for a long time the session pool isn't active/warm yet — run make patches (or make setup), then make stop && make start. Wait ~60–120 s for the probes to warm the pool (grep "\[warm\]" lean4game.log → expect probe ok + requeued … ready=true) ; afterwards lessons should open in ~10–15 s (that's the game's own replay, not cold loading)
Platform won't patch / won't start after an upgrade rm -rf lean4game && make deps && make patches (reinstalls at the pinned commit)
World appears with empty title see AGENTS.md → import-graph rule 2 (the World.merge trap)
"No local games in production mode" restart in dev mode: cd lean4game && npm start
Ports 3000/8080 busy make status, make stop, then make start/make serve
Build fails after a toolchain bump cd probinfo && lake update && lake build (re-pins deps to the Lean tag)