writing-lean-proofs
它能做什么
该技能指导用 Lean 4 语言编写形式化证明,并把 Lean 代码组织成库。适用于形式化定理或规格的数学家和工程师。
适用场景
- ✓你在 Lean 4 中证明定理,并希望证明有清晰结构的时候。
- ✓冗长的 tactic 证明难以阅读,需要拆分为更小引理的时候。
- ✓你在审查 Lean 代码的可读性,或准备将其提交给 Mathlib 的时候。
技能链接
https://github.com/trailofbits/skills/blob/main/plugins/writing-lean-proofs/skills/writing-lean-proofs/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我在 Lean 4 中证明这个定理,并把冗长的证明拆分为更小的引理。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能采用自顶向下的方法。你先设计定义和陈述,然后逐步补全证明,在每个证明完成前使用占位符。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Writing Lean Proofs
Contents
- When to Use
- When NOT to Use
- The workflow
- The extraction ladder
- Quick reference
- Rationalizations to reject
- References
Structured Lean 4 proof writing and library design, distilled from Mathlib's style and review conventions and from the methodology of large formalization projects (Liquid Tensor Experiment, PFR, Fermat's Last Theorem).
Core principle: design top-down, prove bottom-up. Lean propositions are
proof-irrelevant — only a theorem's statement can affect later declarations.
Statements are the stable interface; proofs are disposable and freely
replaceable. Put design effort into definitions and statements, then fill in
proofs against skeletons that already compile (modulo sorry).
When to Use
- Proving theorems in Lean 4, from single lemmas to multi-file developments
- Formalizing mathematics, protocols, or software specifications in Lean
- Defining new types, structures, or functions in a Lean library
- Reviewing Lean code for readability, maintainability, or Mathlib readiness
- Refactoring a long or fragile tactic proof into lemmas
- Setting up a formalization project that several people or agents will contribute to in parallel
- Setting up CI, linters, or verification gates for a Lean project — do this at project start, before patterns propagate
- Diagnosing slow proofs,
maxHeartbeatstimeouts, or expensive reduction - Writing custom tactics, macros, or project-specific linters
When NOT to Use
- Lean 4 as a general-purpose programming language (no proofs involved) — most of this skill targets proof and API structure
- Coq, Isabelle, Agda, or Lean 3 — conventions and tactic names differ;
Lean 3 idioms (
ge_or_gtlinting,discrete_field) are obsolete - Verified-software Lean projects with their own house style (e.g. spec-traceability-first codebases): Mathlib conventions are the community default, but check the project's CONTRIBUTING first and defer to it
The workflow
1. Design definitions and their API first
Definitions carry the design weight. Before proving anything about a new concept:
- Prefer total functions with junk values over subtypes or
Optionin signatures (Mathlib:(0 : ℝ)⁻¹ = 0). Side conditions then appear only on the lemmas that need them, not at every use site. - Bundle: new morphism kinds are structures with a
FunLikeinstance; new subobject kinds useSetLike; carry property proofs as structure fields, not separateIsHom-style predicates. - Pick the canonical spelling (simp-normal form) for every concept with multiple equivalent forms, and state all API lemmas for that form only.
- Write the API in the same file, immediately:
ext,@[simp], coercion, and injectivity lemmas — before the definition is used anywhere. Downstream proofs use the API, neverunfold/show ... from rfl.
See library-design.md for the full set of design rules with rationale.
2. Build a sorry skeleton
State everything before proving anything, at every scale:
- Project scale: state the target theorem and the lemmas it needs, all
with
:= sorry, and make the file compile. Eachsorryis now an independent work unit — a contributor (human or LLM) can discharge one without understanding the rest. This is how LTE, PFR, and FLT scale to dozens of parallel contributors. - Proof scale: inside a proof, lay out the
have/suffices/calcskeleton withsorryjustifications, get Lean to accept the structure, then fill each step. Keeping the structure intact is what produces useful error messages while you work.
example (a b c d : ℝ) (h : c = d * a + b) (h' : b = a * d) : c = 2 * a * d := by
calc
c = d * a + b := sorry
_ = d * a + a * d := sorry
_ = 2 * a * d := sorry
3. Fill goals, one focused goal at a time
- Every new subgoal gets a focusing dot
·with an indented block — never leave several goals active in unfocused sequence (Mathlib'smultiGoallinter enforces this). This is what kills fragile goal-ordering dependence. - Open each block with a redundant
showstating its goal. The proof works without it; reviewers and future editors need it. Ifshowwould change the goal, usechangeinstead — keep stated goals honest. - Ch
常见问题
核心思路是什么?
先设计陈述,因为它们是稳定的部分。证明之后可以替换,而不会破坏其他代码。
它涵盖项目设置吗?
涵盖。对于多人或多个智能体共同工作的项目,它提到了 CI、代码检查器和验证设置。
它遵循某种特定风格吗?
它遵循 Mathlib 的风格和审查惯例,并借鉴了大型形式化项目的经验。
详情
作者的原始描述
Writes and reviews structured Lean 4 proofs and designs Lean libraries following Mathlib conventions. Use when proving theorems in Lean, formalizing mathematics or specifications in Lean 4, defining new types or definitions in a Lean library, reviewing Lean proofs for readability and maintainability, refactoring long tactic proofs into lemmas, filling in sorry placeholders in a Lean development, setting up CI or linters for a Lean project, diagnosing slow proofs or maxHeartbeats timeouts, or writing custom tactics, macros, or linters.
repo: trailofbits/skills
热度
- GitHub 星标
- 8.0k
- 排名
- #12741
- 许可证
- CC-BY-SA-4.0
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
audit-context-building
8.0k帮你的智能体在安全审查之前熟悉一个陌生的代码库,梳理每个函数的假设、保证和依赖关系。
algorand-vulnerability-scanner
8.0k帮你的智能体扫描 TEAL 或 PyTeal 编写的 Algorand 智能合约,检查 11 类常见安全问题,如重新密钥攻击和未校验费用。
audit-prep-assistant
8.0k帮你的智能体为外部安全审查做好代码准备,包括静态分析、测试覆盖、死代码清理和清晰的文档。
cairo-vulnerability-scanner
8.0k帮你的智能体扫描 StarkNet 上的 Cairo 智能合约,检查 6 类严重问题,包括算术溢出、有风险的跨层消息和签名重放。