A small chess engine written in Lean 4, with:
- FEN parsing
- Legal move generation
- Alpha-beta search with move ordering
- Quiescence search, iterative deepening, and a transposition table
- A UCI interface (so you can test it vs other engines)
- A
perftexecutable for basic correctness checks
- Install Lean 4 + Lake (recommended:
elan). - Build:
lake build./.lake/build/bin/leanchessYou can plug it into any UCI-compatible GUI, or run automated matches with tools like cutechess-cli.
./.lake/build/bin/perft 4Expected for start position:
- depth 1: 20
- depth 2: 400
- depth 3: 8902
- depth 4: 197281
- Make the engine speak UCI (this repo already does).
- Pick a reference engine with an Elo limiter (easy option: Stockfish with
UCI_LimitStrength+UCI_Elo). - Run a match (example with
cutechess-cli):
cutechess-cli \
-engine cmd=./.lake/build/bin/leanchess name=LeanChess proto=uci \
-engine cmd=stockfish name=SF proto=uci option.UCI_LimitStrength=true option.UCI_Elo=1320 \
-each tc=10+0.1 \
-games 200 -rounds 1 -pgnout results.pgnThen adjust option.UCI_Elo=... until you get ~50% score; that gives a rough “Stockfish-limited Elo equivalent”.
For tighter estimates, run more games (e.g. 500–2000) and/or use a proper Elo tool (BayesElo/ordo) on the PGN.
This repo includes scripts/run_match_and_elo.sh to run matches and summarize Elo directly from PGNs.
Run match + summary:
scripts/run_match_and_elo.sh --games 300 --tc 10+0.1 --anchor-elo 1320Summarize an existing PGN:
scripts/run_match_and_elo.sh --summary-only \
--pgn matches/match-20260210-090000.pgn \
--target-name LeanChess \
--anchor-name Stockfish \
--anchor-elo 1320Notes:
- In run mode, the script overwrites an existing
--pgnfile by default. Use--append-pgnto keep old games. - By default, Elo summary excludes very short games (
PlyCount < 6) and time-forfeit/abandoned games. - For more reliable strength estimates, prefer at least
--tc 10+0.1(or slower) and start with 40-80 games, then scale up. - By default, match runs use
matches/openings-mini.pgnwith random paired openings to reduce white/black and opening bias. - Use
--seed Nfor reproducible randomized opening order, or--no-openingsto disable opening randomization. - To diagnose raw search strength separately from clock effects, use fixed-depth mode (
--engine-depth N). - Current engine settings prioritize speed and stable match execution (notably for fixed-depth runs), so use timed mode (
--tc ...without fixed depth) for the main Elo estimate and use fixed-depth runs as quick smoke checks.
Show all options:
scripts/run_match_and_elo.sh --help