writing-lean
Skill2 starsChanged 4 months agoMITView on GitHub
Write, test, prove, review or debug Lean 4 programs, proofs and Lake packages. Covers pinning the toolchain, a proof workflow for agents without an editor, Lean traps and recent breaking changes, axiom and trust audits, offline Nix builds including Mathlib, and proving code written in other languages (Aeneas for Rust, Lean specifications with differential testing). Use for Lean source, lakefile or lean-toolchain changes, theorem proving, refinement proofs or Lean version migrations; not merely for running an existing Lean executable.
In this repository34 other files it gives its agents.
- capture-collaboration-evidenceSkill
- clarify-before-buildingSkill
- cross-platform-project-build-archSkill
- deep-code-reviewSkill
- dispatchSkill
- erect-agent-stackSkill
- fix-zig-deps-hashSkill
- gather-project-intentionsSkill
- handoffSkill
- hulk-bug-handoffSkill
- i18nSkill
- llvm-guided-optimizationSkill
- mechatron-ciSkill
- memoriesSkill
- mficSkill
- multi-implementation-contractsSkill
- no-ai-slopSkill
- onboardSkill
- performance-profilingSkill
- planning-workSkill
- scaffold-zig-projectSkill
- ship-itSkill
- test-isolationSkill
- universal-app-frontendSkill
- upgrading-zig-16-to-17Skill
- walking-decisionsSkill
- writing-bendSkill
- writing-elmSkill
- writing-nixSkill
- writing-rocSkill
- writing-rustSkill
- writing-webassemblySkill
- writing-zigSkill
- zig-microbenchmarksSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
20 rules. About 0 are common, 12 are distinctive, and 8 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈12
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 18
Use Lean as both a programming language and a proof checker, without conflating successful execution, cross-implementation agreement and a …
- Distinctive: line 28
Do not search the Nix store or silently use an ambient Elan toolchain.
- Distinctive: line 40
Keep these boundaries explicit:
- Very specific: line 67
Never invent a lemma name.
- Very specific: line 78
Remove every sorry, run #print axioms on the headline theorems, and mutation-check one: break the implementation and watch the proof fail.
- Distinctive: line 104
Before implementation, add at least one failing executable control derived from the external contract and one theorem statement that cannot …
- Distinctive: line 108
use external official vectors or an independent implementation for exact differential checks.
- Distinctive: line 110
use Lean theorems for invariants and refinement properties.
- Distinctive: line 111
use exhaustive finite checks, metamorphic properties, or mutation controls where no external oracle exists.
- Distinctive: line 113
keep cross-language tests outside Lean so Lean cannot redefine its own oracle.
- Distinctive: line 115
Prefer small proved components composed into a larger result.
- Very specific: line 122
do not use sorry, admit, new axiom declarations, or unsafe definitions in their dependency path.
- Very specific: line 124
prefer ordinary proof terms, simp, omega, grind, and decidable finite reasoning that produces kernel-checkable terms.
- Very specific: line 126
treat native_decide, compiler execution, FFI results, benchmarks, and generated expected values as tests unless their additional trust …
- Very specific: line 131
run #print axioms TheoremName for headline theorems and record every remaining axiom rather than assuming none.
- Distinctive: line 142
No checker confirms that a theorem says what you meant.
- Distinctive: line 158
Do not claim overflow safety from test coverage alone.
- Very specific: line 166
Use nix build as the canonical build path, with Lake invoked inside the derivation.
- Very specific: line 185
Use lake shake only as a dependency diagnostic, not as proof.
- Distinctive: line 235
Never describe cryptographic security, distribution quality, constant-time behavior, absence of compiler bugs, or OS entropy quality as …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Writing Lean
Researched on 2026-11-27, when nixpkgs shipped Lean 4.35.0 and the latest upstream release was 4.36.0. Every file in examples/ compiles with nixpkgs' Lean and passes an axiom audit in this repository's CI. Claims marked researched in the references come from documentation and were not run.
Review after: 2027-02-27. Lean releases monthly, so past that date read the release notes since 4.35, re-check the version-specific claims in the references, then update this date and the research date.
Use Lean as both a programming language and a proof checker, without conflating successful execution, cross-implementation agreement and a kernel-checked theorem.
Pin the language before writing code
Lean releases monthly and does not keep compatibility between releases.
Lemma names, tactic behavior and library APIs changed many times between 4.22
and 4.35. Read the project's lean-toolchain, Nix input or package derivation
first, and run lean --version and lake --version through that environment.
Do not search the Nix store or silently use an ambient Elan toolchain.
For this fleet, require the project's Nix derivation. If none exists, add one and pin an exact Lean version before adopting syntax or APIs. When memory and the compiler disagree, the compiler wins: read the reference manual and the release notes for every version between what you remember and the pin. Read traps and changes before writing proofs.
Separate four artifacts
Keep these boundaries explicit:
- A mathematical specification using
Nat,Int, finite types, or predicates chosen for tractable reasoning. - A pure executable implementation with no CLI, environment, filesystem, or entropy I/O.
- Refinement theorems connecting executable results to the specification.
- A thin executable adapter that owns parsing, formatting, entropy, and other effects.
An executable implementation is not automatically a specification. A theorem about a second model is not evidence about production code until a refinement theorem connects them. Make constructors of invariant-carrying types private and export checked factories, so theorem preconditions are enforced by types rather than by documentation.
Prove with the goal in view
An agent without an editor cannot see goal states, and guessing tactics against an unseen goal wastes most attempts. Work like this:
- State the theorem with
:= by sorryand compile. A statement that does not elaborate is the first bug. - Inspect the goal: through lean-lsp-mcp
if it is configured, through the JSON REPL, or with
lean --jsonand atrace_stateor failingdoneat the point of interest. See tooling. - Never invent a lemma name. Invented names are the most common failure in
published evaluations of LLM-written Lean.
#checka name before using it, and letexact?,simp?andrw?find candidates. Commit the term they print, not the search call. - Try the automation first:
omegafor linearNat/Intarithmetic,simpfor rewriting,grindfor mixed reasoning,decidefor small finite facts. Structure the proof byinductionon the data, or by functional induction (fun_induction f x) when the recursion is not structural. - A
simpthat closes a goal is fine. Asimpfollowed by more tactics should be thesimp only [...]thatsimp?prints, because the default simp set changes between releases. - Remove every
sorry, run#print axiomson the headline theorems, and mutation-check one: break the implementation and watch the proof fail.
Examples show each step on lists, arrays, sorting, termination and imperative loops.
Traps that compile
These elaborate and run, then mislead. The full table, with fixes, is in traps and changes.
Natsubtraction truncates (3 - 5 = 0);n / 0 = 0;UInt8and other fixed-width types wrap around.a[i]!panics at runtime and yieldsdefaultin proofs. Usea[i]with a bound proof, ora[i]?.partial,unsafe,@[implemented_by]and@[extern]make the running code differ from, or be invisible to, what you prove.- Well-founded recursive definitions do not unfold by
rflordecide; use their equation lemmas (simp [f]) or functional induction. Floathas no useful proof theory.- Tabs are a hard error: indent Lean with spaces even in tab-indented repos.
main (args : List String)cannot receive invalid UTF-8 unchanged; take raw argument bytes through a thin native launcher when they matter.
Work red to green
Before implementation, add at least one failing executable control derived from the external contract and one theorem statement that cannot yet elaborate or close. Then:
- use external official vectors or an independent implementation for exact differential checks;
- use Lean theorems for invariants and refinement properties;
- use exhaustive finite checks, metamorphic properties, or mutation controls where no external oracle exists;
- keep cross-language tests outside Lean so Lean cannot redefine its own oracle.
Prefer small proved components composed into a larger result. State exactly which boundary remains empirical.
Keep the trusted proof path small
For claims intended to be kernel-checked:
- do not use
sorry,admit, newaxiomdeclarations, orunsafedefinitions in their dependency path; - prefer ordinary proof terms,
simp,omega,grind, and decidable finite reasoning that produces kernel-checkable terms; - treat
native_decide, compiler execution, FFI results, benchmarks, and generated expected values as tests unless their additional trust assumptions are deliberately accepted and reported. Since 4.29, eachnative_decideadds an axiom named liket._native.native_decide.ax_1_1, so#print axiomsshows it; - run
#print axioms TheoremNamefor headline theorems and record every remaining axiom rather than assuming none.propext,Classical.choiceandQuot.soundare Lean's standard axioms; report them accurately instead of calling any nonempty list a failure.sorryAxmeans the proof is incomplete; - compile or elaborate the proof modules with
--trust=0when practical; - for headline results, replay the build with
lean4checker --freshor check it withlake comparator(Lean 4.35); - mutation-check at least one important theorem or external gate by breaking the implementation and observing failure before restoring it.
No checker confirms that a theorem says what you meant. Review the statement and every definition it depends on.
Machine integers need a model
UInt32, UInt64, USize, arrays, and byte buffers are efficient executable
types, but modular arithmetic and bounds can make direct proofs awkward. Choose
one of these deliberately:
- implement in
BitVec n(withbv_decidefor bit-level goals) or a bounded subtype and execute it; - implement in machine integers and prove correspondence to a
Nat/Intmodel under explicit bounds; - retain a slower proved reference implementation and compare an optimized implementation through an external differential gate.
Do not claim overflow safety from test coverage alone. Conversely, do not make the runtime unusably slow solely to maximize the amount proved; measure the tradeoff and preserve both paths when that is useful. Compiled Lean can be an order of magnitude or more slower than tuned Zig or Rust for byte-heavy work, so measure before choosing it for a hot path.
Build and verification floor
Use nix build as the canonical build path, with Lake invoked inside the
derivation. Follow cross-platform-project-build-arch; do not maintain an
optional non-Nix backend. The commands below describe the inner build/check
steps, not a second contributor build recipe. A proof-oriented package should
normally gate:
lake build
lake test
lean --trust=0 <headline proof module>
source scan for sorry/admit/project axioms
#print axioms for headline theorems
external differential and mutation controlsLake cannot fetch dependencies in the sandbox, and Mathlib's
lake exe cache get needs network. Read
tooling and Nix for
buildLakePackage, lean4-nix and the fleet's fixed-output Mathlib pattern.
Use lake shake only as a dependency diagnostic, not as proof. Run the shipped
executable as an installed-package smoke test so an elaborated but unshipped
target cannot create a false green.
Code written in other languages
A proof about code in another language needs one of three bridges, each with a different guarantee: translate the code into Lean (Aeneas, for safe Rust), specify in Lean and differentially test the implementation (the AWS Cedar pattern), or implement in Lean and compile it. No mature translator exists for C++, Go, Python or WebAssembly. An LLM's rewrite of code into Lean proves things about the rewrite, not the original. Read cross-language before promising a proof about non-Lean code.
Worked examples
Each file in examples/ compiles without warnings, and
checks.nix fails CI if any proof uses sorry or an
axiom beyond the standard three.
- Lists.lean: structural induction on hand-written
appendandreverse, then the same facts from core lemmas found with#check,simp?andexact?. - Termination.lean: non-structural recursion with
termination_byanddecreasing_by, proved with functional induction. - InsertionSort.lean: the result is sorted
(
List.Pairwise) and a permutation (List.Perm) of the input. - Arrays.lean: indexing with proofs,
a[i]?, and relating anArrayfold to itsListversion. - Imperative.lean: an
Id.run doloop withlet mut, proved equal to a pure specification twice: with aStd.Doloop invariant throughmvcgen, and withsimpand an induction lemma. - Arithmetic.lean: the
NatandUInt8traps,omega,grindanddecide.
Report the scope of correctness
End proof-oriented work with a short claim matrix:
- theorem proved and its exact statement;
- executable code covered by a refinement theorem;
- behavior checked only by external vectors or differential tests;
- security, probability, numerical-error, parser, I/O, and compiler properties not proved;
- axioms and trusted components;
- build and runtime cost relative to an ordinary implementation;
- recommendation: production implementation, proved reference oracle, selective proof layer, or no Lean adoption.
Never describe cryptographic security, distribution quality, constant-time behavior, absence of compiler bugs, or OS entropy quality as proved merely because the surrounding Lean functions type-check.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

