智慧分配 Lean 4 定理證明工作給 Codex。 我(Claude)先分析 sorry/stub,判斷 Codex 適合做什麼, 只把「Codex 擅長」的部分派給他,其餘自己處理。 Use when: 有 sorry 或 True := trivial 需要處理、想利用 Codex 的暴力搜索能力。
From lean-provernpx claudepluginhub psychquant/psychquant-claude-plugins --plugin lean-proverThis skill is limited to using the following tools:
我先分析,判斷什麼該派給 Codex、什麼該自己做,然後協調執行。
我是 orchestrator,Codex 是 worker。 我負責架構決策、bridge 設計、axiom 紀律;Codex 負責 tactic 暴力搜索和 statement 生成。
| 任務類型 | 誰做 | 原因 |
|---|---|---|
True := trivial → 正式 theorem statement | Codex | 擅長從書的描述生成 Lean 4 類型 |
純 tactic 證明(simp/linarith/omega 組合) | Codex | xhigh 深度推理 + 暴力搜索 |
| CDF set equality 類型的代數證明 | Codex | 機械性 ring/field_simp 操作 |
| 需要新 Ch0 bridge 的證明 | 我 | 需要架構判斷 |
涉及 IsProbabilityMeasure instance 的 | 我 | Codex 不理解 Lean 4 type class |
Nat.cast / coercion 問題 | 我 | Lean 4 特有的 elaboration 問題 |
| axiom 設計和放置 | 我 | axiom 只能在 Ch0 |
| 跨章節依賴分析 | 我 | 需要全局視野 |
axiom 只能放在 Ch0_Foundations.lean。 Codex 的 prompt 必須明確禁止在其他檔案新增 axiom。
axiom(在非 Ch0 檔案)→ 立即拒絕1. 讀取目標檔案
2. 找出所有 sorry 和 True := trivial
3. 對每個目標分類:
- STUB: True := trivial → 需要生成 theorem statement
- TACTIC: 有完整 signature,缺 proof body → 需要 tactic 證明
- BRIDGE: 需要新 Ch0 infrastructure → 我先處理
4. 輸出分類表給用戶確認
適用:True := trivial 需要變成正式 Lean 4 theorem。
Prompt 構成:
/-- ... -/ 提取)OUTPUT_FILE=$(mktemp /tmp/codex_stmt_XXXXX)
codex exec \
-c 'model="gpt-5.4"' \
-c 'model_reasoning_effort="xhigh"' \
-c 'service_tier="fast"' \
-s read-only \
-o "$OUTPUT_FILE" \
--ephemeral \
"$(cat $CONTEXT_FILE)"
驗證:lake env lean $FILE 確認 signature 類型正確(sorry 警告 OK)。
對每個 TACTIC 目標:
.claude/rules/mathlib-api.md 是否有對應 APIPrompt 構成:
=== TASK ===
Replace the 'sorry' with a complete proof.
The proof MUST compile with Lean 4 v4.28.0 + Mathlib v4.28.0.
Return ONLY the proof body (after ':= by'), no markdown, no explanation.
CRITICAL RULES:
- Do NOT add axiom, sorry, or admit anywhere
- Do NOT modify the theorem signature
- Use ONLY the API names listed in the reference below
- If you cannot prove it, return exactly: CANNOT_PROVE
=== THEOREM ===
{theorem with sorry}
=== AVAILABLE BRIDGES (Ch0) ===
{bridge lemma list from Ch0}
=== MATHLIB API REFERENCE ===
{from mathlib-api.md}
=== PRECEDING DECLARATIONS ===
{last 10 declarations before the theorem}
=== FILE HEADER ===
{first 30 lines: imports, namespace, variables}
如果是 loop 模式,加上:
=== PREVIOUS ATTEMPT FAILED ===
{build errors from last round}
Common Lean 4 / Mathlib v4.28.0 issues:
- ₀ suffix: div_le_iff₀, pow_le_pow_left₀
- Nat.cast: use ((n + 1 : ℕ) : ℝ) not ↑(n + 1)
- field_simp needs non-zero hypotheses
- abs_neg : |-(x)| = |x|
- nlinarith can't handle division — use explicit mul_div_cancel₀
FOR round = 1 to MAX_ROUNDS (default 3):
1. Codex 執行 → 取得 proof
2. 檢查 proof 是否含 axiom/sorry/admit → 拒絕
3. 檢查 proof 是否為 CANNOT_PROVE → 標記為 MANUAL,跳過
4. 插入到檔案(先 git stash 備份)
5. lake env lean $FILE 驗證
IF 通過且無 sorry:
→ 成功!
→ BREAK
IF 失敗:
→ 提取 build errors
→ 我先看錯誤:
- Nat.cast 問題 → 我自己修
- IsProbabilityMeasure 問題 → 我自己修
- API 名字錯 → 附錯誤給 Codex 再試
- 邏輯錯誤 → 附錯誤給 Codex 再試
→ 還原檔案
→ 繼續下一輪
FOR each MANUAL target:
→ 我自己嘗試證明
→ 如果需要新 bridge,加到 Ch0
→ 如果證不出,留 sorry + 報告缺什麼
最後輸出 summary table
## codex-prove-assist: {file}
### Scan Results
| # | Theorem | Type | Assignment | Status |
|---|---------|------|------------|--------|
| 1 | thm_xyz | STUB | Codex | ✅ statement generated |
| 2 | lemma_abc | TACTIC | Codex | ✅ proved (round 2) |
| 3 | thm_def | BRIDGE | Me+Codex | ✅ added Ch0 bridge, Codex proved |
| 4 | thm_ghi | MANUAL | Me | ⏳ needs bridge_xxx |
### Ch0 Changes
- Added: bridge_foo, bridge_bar
### Sorry Remaining
{count and list}
### Codex Stats
- Statements generated: N
- Proofs attempted: M
- Proofs succeeded: K (round avg: X.Y)
- CANNOT_PROVE: J
# 非互動模式(用於 STUB 和 TACTIC)
codex exec \
-c 'model="gpt-5.4"' \
-c 'model_reasoning_effort="xhigh"' \
-c 'service_tier="fast"' \
-s read-only \
-o "$OUTPUT_FILE" \
--ephemeral \
"prompt text here"
-c 值是 TOML 格式:字串要 "quoted"-s read-only 只讀沙盒-o 將回覆寫入檔案--ephemeral 不保存 session-s workspace-writegit stash 或 git checkout -- $FILEgrep -c "axiom" 過濾lake env lean 是唯一的成功標準