writing-lean-proofs
できること
Lean 4言語で形式的な証明を書き、Leanのコードをライブラリとして整理する方法を案内します。定理や仕様を形式化する数学者とエンジニア向けです。
使う場面
- ✓Lean 4で定理を証明していて、証明の明確な構造が必要なとき。
- ✓長い戦術の証明が読みにくく、より小さな補題に分ける必要があるとき。
- ✓Leanのコードを読みやすさのためにレビューしたり、Mathlibへの提出の準備をしたりしているとき。
スキルのリンク
https://github.com/trailofbits/skills/blob/main/plugins/writing-lean-proofs/skills/writing-lean-proofs/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。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つの深刻な問題について検査できるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。