lean4
Original:🇺🇸 English
Translated
Use when editing .lean files, debugging Lean 4 builds (type mismatch, sorry, failed to synthesize instance, axiom warnings, lake build errors), searching mathlib for lemmas, formalizing mathematics in Lean, or learning Lean 4 concepts. Also trigger when the user asks for help with Lean 4, mathlib, or lakefile. Do NOT trigger for Coq/Rocq, Agda, Isabelle, HOL4, Mizar, Idris, Megalodon, or other non-Lean theorem provers.
4installs
Added on
NPX Install
npx skill4agent add cameronfreer/lean4-skills lean4Tags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Lean 4 Theorem Proving
Use this skill whenever you're editing Lean 4 proofs, debugging Lean builds, formalizing mathematics in Lean, or learning Lean 4 concepts. It prioritizes LSP-based inspection and mathlib search, with scripted primitives for sorry analysis, axiom checking, and error parsing.
Core Principles
Search before prove. Many mathematical facts already exist in mathlib. Search exhaustively before writing tactics.
Build incrementally. Lean's type checker is your test suite—if it compiles with no sorries and standard axioms only, the proof is sound.
Respect scope. Follow the user's preference: fill one sorry, its transitive dependencies, all sorries in a file, or everything. Ask if unclear.
Use 100-character line width for Lean files. Do not wrap lines at 80 characters — Lean and mathlib convention is 100. If a line fits within 100 characters, keep it on one line. See mathlib-style for breaking strategies when lines exceed 100.
Never change statements or add axioms without explicit permission. Theorem/lemma statements, type signatures, and docstrings are off-limits unless the user requests changes. Inline comments may be adjusted; docstrings may not (they're part of the API). Custom axioms require explicit approval—if a proof seems to need one, stop and discuss. Exception: within synthesis wrappers (, ), session-generated declarations may be redrafted under the outer-loop statement-safety rules; see cycle-engine.md.
/lean4:formalize/lean4:autoformalizeCommands
| Command | Purpose |
|---|---|
| Draft Lean declaration skeletons from informal claims |
| Interactive formalization — drafting plus guided proving |
| Autonomous end-to-end formalization from informal sources |
| Guided cycle-by-cycle theorem proving with explicit checkpoints |
| Autonomous multi-cycle theorem proving with explicit stop budgets |
| Save progress with a safe commit checkpoint |
| Read-only code review of Lean proofs |
| Leverage mathlib, extract helpers, simplify proof strategies |
| Improve Lean proofs for directness, clarity, performance, and brevity |
| Interactive teaching and mathlib exploration |
| Diagnostics, cleanup, and migration help |
This plugin ships a host-agnostic parser () that covers the
parser-decidable startup rules of the six parameter-heavy commands (,
, , , , ). A small set of
documented startup rules in these commands depend on runtime context (repo-
level search, interactive prompting) and are applied by the command after
reading the parser's output. The other commands (, ,
, , ) remain model-parsed.
When a host adapter installs the hook, the parser runs
before the model sees a prompt matching one of the six covered
commands, injects a block into context, and rejects
invalid invocations at the hook level; invocations of the other commands pass
through unchanged. Hosts without the hook fall back to model-parsed startup
via the shared command-invocation.md
contract.
Commands always announce resolved inputs, reject invalid startup configs before
doing work, and treat wall-clock budgets like as
best-effort.
lib/command_args/draftlearnformalizeautoformalizeproveautoprovecheckpointreviewrefactorgolfdoctorUserPromptSubmit/lean4:*validated-invocation--max-total-runtimeWhich Command?
| Situation | Command |
|---|---|
| Draft a Lean skeleton (skeleton by default) | |
| Draft + prove interactively | |
| Filling sorries (interactive) | |
| Filling sorries (unattended) | |
| Save point (per-file + project build, best-effort axiom scan, commit) | |
| Quality check (read-only) | |
| Simplify proof strategies (mathlib leverage, helpers) | |
| Optimizing compiled proofs | |
| New to this project / exploring | |
| Navigating mathlib for a topic | |
| Something not working | |
| Formalize + prove end-to-end (unattended) | |
Contributing (lean4-contribute plugin)
If the plugin is installed, you may suggest these commands at natural stopping points. Rules:
lean4-contribute- Suggest first, never invoke unprompted. Offer a one-line question; do not start the command flow.
- Only invoke after explicit user opt-in in the current conversation. Silence, topic change, or implicit frustration do not count as consent.
- At most once per topic per session unless the user engages.
- Never mid-proof. Wait for a natural stopping point.
| Situation | Suggest |
|---|---|
| Problem appears to be in lean4-skills itself (wrong command behavior, contradictory docs, broken lint, bad guardrail, confusing plugin UX) — not ordinary Lean/mathlib/user-proof problems | "This looks like a lean4-skills bug. Want me to draft a bug report?" → |
| User wants a workflow the plugin doesn't support, says a command should behave differently, or you must recommend awkward manual steps due to a missing feature | "This looks like a plugin workflow gap. Want me to draft a feature request?" → |
| Result seems reusable beyond the current task: tactic-selection heuristic, mathlib search pattern, anti-pattern, documentation gap with a clear lesson — not one-off theorem facts or private repo details | "That seems reusable beyond this task. Want me to draft a shareable insight?" → |
If the plugin is not installed and the user clearly hit a lean4-skills bug, workflow gap, or reusable insight (same criteria as above — not ordinary Lean/mathlib issues), you may offer the install hint once:
- At most once per session. Do not repeat if the user declined, ignored it, or moved on.
- Never mid-proof or during an active debugging loop.
- One short line, not a pitch: "If you want, install the plugin and I can draft that report for you here." See the lean4-contribute README for setup.
lean4-contribute
Typical Workflow
┌─ Entry points (pick one) ──────────────────────────────────────────────────────────┐
│ /lean4:draft Skeleton by default (--mode=attempt for shallow proof) │
│ /lean4:formalize Interactive: draft + guided proving │
│ /lean4:autoformalize Autonomous: draft + autonomous proving │
└────────────────────────────────────────────────────────────────────────────────────┘
↓ (if sorries remain)
/lean4:prove / autoprove Proof engines (sorry filling, no header edits)
↓
/lean4:refactor Leverage mathlib, extract helpers (optional)
↓
/lean4:golf Improve proofs (optional)
↓
/lean4:checkpoint Save point (per-file + project build)Use at any point to explore repo structure or navigate mathlib. Three entry points: for skeletons, for interactive synthesis (draft + guided proving), for unattended source-to-proof.
/lean4:learn/lean4:draft/lean4:formalize/lean4:autoformalizeNotes:
- asks before each cycle;
/lean4:proveloops autonomously with explicit stop budgets/lean4:autoprove - Both trigger at configured intervals (
/lean4:review)--review-every - When reviews run (via ), they act as gates: review → replan → continue. In prove, replan requires user approval; in autoprove, replan auto-continues
--review-every - Review supports (default) or
--mode=batch(triage); review is always read-only--mode=stuck - wraps draft+autoprove in a single command (source → claims → skeletons → proofs); replaces
/lean4:autoformalizeautoprove --formalize=auto - Proof engines (/
prove) never modify declaration headers (header fence)autoprove - If you hit environment issues, run to diagnose
/lean4:doctor
LSP Tools (Preferred)
Sub-second feedback and search tools (LeanSearch, Loogle, LeanFinder) via Lean LSP MCP:
lean_goal(file, line) # See exact goal
lean_hover_info(file, line, col) # Understand types
lean_local_search("keyword") # Fast local + mathlib (unlimited)
lean_leanfinder("goal or query") # Semantic, goal-aware (10/30s)
lean_leansearch("natural language") # Semantic search (3/30s)
lean_loogle("?a → ?b → _") # Type-pattern (unlimited if local mode)
lean_hammer_premise(file, line, col) # Premise suggestions for simp/aesop/grind (3/30s)
lean_state_search(file, line, col) # Goal-conditioned lemma search (3/30s)
lean_multi_attempt(file, line, snippets=[...]) # Test multiple tactics
lean_diagnostic_messages(file) # Per-file error/warning check
lean_code_actions(file, line) # Resolve "Try this" suggestions to editslean_run_codelean_goallean_multi_attemptlean_diagnostic_messagesCapabilities
| Capability | Required | Check | Fallback |
|---|---|---|---|
| Lean / Lake | yes | | none — run |
| Python 3 | yes (scripts) | | none for script-dependent operations |
| yes (set by bootstrap) | | run |
| Lean LSP MCP | no | try | scripts + |
| no | try calling it | |
| no | try calling it | manual "Try this" application |
| Subagent dispatch | no | host-dependent | run work in main thread |
| Slash commands | no | host-dependent | follow skill instructions directly |
Operating Profiles
The skill adapts to what's available. Determine your profile by checking capabilities above, then follow the corresponding guidance.
full (all capabilities)
MCP + subagents + commands. Full workflow with live goal inspection, tactic testing, and parallel subagent dispatch (requires disjoint owned-file sets per agent, or separate worktrees). Subagents get pre-collected MCP context per cycle-engine.md § Pre-flight Context. If is unavailable, use scratch files with for isolated experiments.
lean_run_code/tmplake env leanmcp_main_only (MCP available, no subagent dispatch)
MCP works in the main thread. Run all proof work directly — do not delegate to subagents. All cycle-engine phases execute in-thread. If is unavailable, use scratch files with for isolated experiments.
lean_run_code/tmplake env leanscripts_only (no MCP, no subagents)
Use for search and / for validation. Key limitations in this mode:
$LEAN4_SCRIPTSlake env leanlake build- No live goal inspection — is unavailable; you can read the file and check compilation output, but cannot see proof state at a specific line
lean_goal - No tactic testing — is unavailable; edits must be validated by compiling the file (
lean_multi_attempt)lake env lean - No real-time diagnostics — is unavailable; use
lean_diagnostic_messages(from project root) for compilation errors, but feedback is file-level, not line-levellake env lean <file> - Search is script-based — replaces LSP search tools
$LEAN4_SCRIPTS/smart_search.sh
This mode is functional for straightforward proofs but significantly slower and less precise than MCP-backed workflows.
review_only (read-only, no edits)
Read proof state and assess quality. No edits, no commits, no subagent dispatch.
File Handling Rules
Scratch-work ladder (in preference order):
- Live file + MCP tools (,
lean_goal,lean_multi_attempt)lean_diagnostic_messages - for isolated experiments
lean_run_code - scratch files only when
/tmpis unavailable and the experiment must not touch the live filelean_run_code - Never create scratch files in the repo root
File inspection: Use Read and Grep to view source files. Never write Python scripts, temp files, or use pipelines just to read lines from a file you already have access to.
catStaging: Stage only files touched during the current session. Never use or broad glob patterns. Print the exact staged set before committing.
git add -ASee sorry-filling.md for the full scratch-work preference order.
Core Primitives
| Script | Purpose | Output |
|---|---|---|
| Find sorries with context | text (default), json, markdown, summary |
| Best-effort axiom scan (top-level declarations) | text |
| Multi-source mathlib search | text |
| Detect optimization patterns | JSON |
| Find declaration usages | text |
Usage: Invoked by commands automatically. See references/ for details.
Invocation contract: Never run bare script names. Always use:
- Python:
${LEAN4_PYTHON_BIN:-python3} "$LEAN4_SCRIPTS/script.py" ... - Shell:
bash "$LEAN4_SCRIPTS/script.sh" ... - Report-only calls: add to
--report-only,sorry_analyzer.py,check_axioms_inline.sh— suppresses exit 1 on findings; real errors still exit 1. Do not use in gate commands likeunused_declarations.sh./lean4:checkpoint - Keep stderr visible for Lean scripts (no redirection), so real errors are not hidden.
/dev/null
If is unset or missing, run and stay LSP-only until resolved.
$LEAN4_SCRIPTS/lean4:doctorAutomation
/lean4:prove/lean4:autoprove- prove — guided, asks before each cycle. Ideal for interactive sessions.
- autoprove — autonomous, loops with explicit stop budgets. Ideal for unattended runs.
Both share the same cycle engine (plan → work → checkpoint → review → replan → continue/stop) and follow the LSP-first protocol: LSP tools are normative for discovery and search; script fallback only when LSP is unavailable or exhausted. Compiler-guided repair is escalation-only — not the first response to build errors. For complex proofs, they may delegate to internal workflows for deep sorry-filling (with snapshot, rollback, and scope budgets), proof repair, or axiom elimination. You don't invoke these directly.
Skill-Only Behavior
When editing files without invoking a command, the skill runs one bounded pass:
.lean- Read the goal or error via /
lean_goallean_diagnostic_messages - Search mathlib with up to 2 LSP tools (e.g. +
lean_local_search/lean_leanfinder/lean_leansearch)lean_loogle - Try the Automation Tactics cascade
- Validate with (no project-gate
lean_diagnostic_messagesin this mode)lake build - No looping, no deep escalation, no multi-cycle behavior, no commits
- End with suggestions:
Usefor guided cycle-by-cycle help. Use
/lean4:provefor autonomous cycles with stop safeguards./lean4:autoprove
Quality Gate
A proof is complete when:
- passes
lake build - Zero sorries in agreed scope
- Only standard axioms (,
propext,Classical.choice)Quot.sound - No statement changes without permission
Verification ladder: per-edit → file gate (run from project root) → project gate only. See cycle-engine: Build Target Policy.
lean_diagnostic_messages(file)lake env lean <path/to/File.lean>lake buildCommon Fixes
See compilation-errors for error-by-error guidance (type mismatch, unknown identifier, failed to synthesize, timeout, etc.).
Type Class Patterns
lean
-- Local instance for this proof block
haveI : MeasurableSpace Ω := inferInstance
letI : Fintype α := ⟨...⟩
-- Scoped instances (affects current section)
open scoped Topology MeasureTheoryOrder matters: provide outer structures before inner ones.
Automation Tactics
Try in order (stop on first success):
→ → → → → → → → →
rflsimpringlinarithnlinarithomegaexact?apply?grindaesopNote: / query mathlib (slow). and are powerful but may timeout. See grind-tactic for interactive workflows, annotation strategy, and simproc escalation.
exact?apply?grindaesopTroubleshooting
If LSP tools aren't responding, check your operating profile above. In mode, provides search and provides file-level compilation feedback, but live goal inspection, tactic testing, and line-level diagnostics are unavailable. If environment variables (, ) are missing, run to diagnose.
scripts_only$LEAN4_SCRIPTSlake env leanLEAN4_SCRIPTSLEAN4_REFS/lean4:doctorScript environment check:
bash
echo "$LEAN4_SCRIPTS"
ls -l "$LEAN4_SCRIPTS/sorry_analyzer.py"
# One-pass discovery for troubleshooting (human-readable default text):
${LEAN4_PYTHON_BIN:-python3} "$LEAN4_SCRIPTS/sorry_analyzer.py" . --report-only
# Structured output (optional): --format=json
# Counts only (optional): --format=summaryCold start / fresh worktree:
- Fresh worktree or after ? Prime the cache in that worktree before the first real build.
lake clean - Use the project's cache command: on newer Lake, or
lake cache getwhere the project still uses the mathlib cache executable.lake exe cache get - If Lean LSP is cold or timing out on first use, run one to bootstrap the workspace.
lake build - After bootstrap, return to the normal verification ladder:
→
lean_diagnostic_messages(file)(from project root) →lake env lean <path/to/File.lean>only at checkpoint/final gate.lake build - Do not symlink another worktree's ; use Lake cache/artifact mechanisms instead.
.lake/build
References
Cycle Engine: cycle-engine — shared prove/autoprove logic (stuck, deep mode, falsification, safety)
LSP Tools: lean-lsp-server (quick start), lean-lsp-tools-api (full API — grep for tool names)
^##Search: mathlib-guide (read when searching for existing lemmas), lean-phrasebook (math→Lean translations)
Errors: compilation-errors (read first for any build error), instance-pollution (typeclass conflicts — grep for patterns), compiler-guided-repair (escalation-only repair — not first-pass)
## Sub-Tactics: tactics-reference (tactic lookup — grep ), grind-tactic (SMT-style automation — when simp can't close), simp-reference (simp hygiene + custom simprocs), tactic-patterns, calc-patterns
^### TacticNameProof Development: proof-templates, proof-refactoring (28K — grep by topic), proof-simplification (strategy-level: mathlib search, congr lemmas, helper extraction), sorry-filling
Optimization: proof-golfing (includes safety rules, bounded LSP lemma replacement, bulk rewrites, anti-patterns; escalates to axiom-eliminator), proof-golfing-patterns, performance-optimization (grep by symptom), profiling-workflows (diagnose slow builds/proofs)
Domain: domain-patterns (25K — grep ), measure-theory (28K), axiom-elimination
## AreaStyle: mathlib-style, verso-docs (Verso doc comment roles and fixups)
Custom Syntax: lean4-custom-syntax (read when building notations, macros, elaborators, or DSLs), metaprogramming-patterns (MetaM/TacticM API — composable blocks, elaborators), scaffold-dsl (copy-paste DSL template), json-patterns (json% syntax + ToJson)
Quality: linter-authoring (project-specific linter rules), ffi-interop (FFI, , init, symbol linkage)
@&Workflows: agent-workflows, subagent-workflows, command-examples, learn-pathways (intent taxonomy, game tracks, source handling)
Internals: review-hook-schema, compiler-internals (attributes, specialization, pipeline)