数学研究的本地 Lean 4 证明自动化环境配置指南
TuBrief 편집팀
2026년 8월 10일
0
Computing/Software원본 영상을 바탕으로 AI의 도움을 받아 작성했습니다. 원본 영상이 기준입니다.
커뮤니티의 다른 글
댓글 (0)
Log in to leave a comment
아직 작성된 글이 없습니다
원본 영상을 바탕으로 AI의 도움을 받아 작성했습니다. 원본 영상이 기준입니다.
Log in to leave a comment
아직 작성된 글이 없습니다
在大规模数学问题解决系统部署到实验室台式机时,宏大的架构概述没有任何帮助。本文整理了在本地环境中直接配置 Lean 4 管道并联动长期运行的多智能体以完全自动化数学探索任务的实操步骤。
在开发环境中手动安装 Lean 4 并依赖 VS Code 语言服务器协议,会在大规模 AI 验证时引发严重的瓶颈。如果不缓存 Mathlib4 预编译二进制文件,本地 CPU 将直接重新编译数十万个定理,仅环境搭建就需要耗费 180 分钟以上。为了每秒处理数十次以上的代码验证请求,必须转为基于 FastAPI 的 Kimina Lean Server 管道。
将长期运行的数学问题验证任务配置时间从 180 分钟缩短至 20 分钟以内的具体构建步骤如下:
leanprover/lean4:v4.15.0 文本。`lean
import Lake
open Lake DSL
package «proof_automation» {
}
require mathlib from git
"https://github.com/leanprover-community/mathlib4.git" @ "v4.15.0"
require repl from git
"https://github.com/leanprover-community/repl.git" @ "main"
@[default_target]
lean_lib «ProofAutomation» {
}
`
`bash
lake update
lake exe cache get
lake build
`
完成此操作后,将立即接收预编译缓存并在 20 分钟内准备好完整的本地 REPL 运行环境,同时每秒最多可并行验证 50 个证明段落。
如果尝试通过单个大语言模型提示词调用来证明复杂的定理,随着上下文变长,将会丢失子目标或陷入无限循环。必须应用分层有向无环图架构,将角色分离为负责问题分割的根智能体和执行隔离子战术的子智能体。
采用 JSON 格式作为智能体通信规范并隔离作用域来传递提示词,可将 API 令牌消耗量减少 40% 以上。调用子智能体时使用的 JSON 架构如下:
`json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "SubAgentProofTask",
"type": "object",
"properties": {
"task_id": { "type": "string" },
"target_hypothesis": { "type": "string" },
"current_goal_state": { "type": "string" },
"previous_failure_logs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"attempted_tactic": { "type": "string" },
"error_message": { "type": "string" }
}
}
},
"sub_goal_target": { "type": "string" }
},
"required": ["task_id", "target_hypothesis", "current_goal_state", "sub_goal_target"]
}
`
为阻止由于智能体幻觉导致的无限循环并运行稳定的分层结构,分步控制顺序如下:
在持续数小时的证明探索任务中,如果每次请求都将完整的对话历史记录传输到后端 API,令牌使用量将会激增。必须构建基于 LeanExplore 和向量数据库的缓存层,并通过以引理的形式将已验证的中间战术结果摘要反映到内存图中,从而控制输入令牌开销。
在后端服务器中注册 REPL 会话的环境标识符,以移除提示词顶部重复的 import Mathlib 语法。仅此一项即可立即将输入令牌缩减 50% 至 70%。为防止因超出预算限额而导致 API 成本暴增的控制管道实现步骤如下:
.leanflow/cache/ 和 .leanflow/workflow-state/ 路径。`python
import sys
import json
import logging
class CostMonitor:
def init(self, max_budget_usd: float, token_cost_per_1k: float):
self.max_budget = max_budget_usd
self.cost_per_1k = token_cost_per_1k
self.total_tokens_used = 0
self.current_cost = 0.0
def track_usage(self, prompt_tokens: int, completion_tokens: int):
tokens_in_call = prompt_tokens + completion_tokens
self.total_tokens_used += tokens_in_call
self.current_cost += (tokens_in_call / 1000.0) * self.cost_per_1k
logging.info(f"[TELEMETRY] Used Tokens: {tokens_in_call} | Total Cost: ${self.current_cost:.4f}")
if self.current_cost >= self.max_budget:
self.trigger_safe_pause()
def trigger_safe_pause(self):
logging.warning("[WARNING] Target budget threshold reached. Pausing workflow...")
with open(".leanflow/workflow-state/checkpoint.json", "w") as f:
json.dump({"status": "PAUSED_BUDGET_EXCEEDED", "tokens": self.total_tokens_used}, f)
sys.exit(0)
`
在 AI 智能体生成代码后手动审查错误是整个研究过程中的致命瓶颈。必须将 Lean REPL 编译器输出的语法错误、类型不匹配、未解决目标等 JSON 解析数据直接连接到智能体的反馈输入中。
替代人工审查并将研究速度提升 2 倍以上的自动修复控制循环的工作方式如下:
`python
def parse_lean_repl_output(repl_response_json: str):
data = json.loads(repl_response_json)
parsed_diagnostics = {
"has_error": False,
"errors": [],
"open_sorries": []
}
if "messages" in data:
for msg in data["messages"]:
if msg.get("severity") == "error":
parsed_diagnostics["has_error"] = True
parsed_diagnostics["errors"].append({
"line": msg["pos"]["line"],
"column": msg["pos"]["column"],
"data": msg["data"]
})
if "sorries" in data:
for sorry in data["sorries"]:
parsed_diagnostics["open_sorries"].append({
"goal": sorry["goal"],
"proof_state": sorry["proofState"],
"line": sorry["pos"]["line"]
})
return parsed_diagnostics
`
完全验证后的 Clean 代码可通过 doc-gen4 和 paperproof 转换工具,自动生成可立即插入研究论文中的逻辑图可视化以及 LaTeX 上下文。
`bash
lake build LeanAutomation:docs
lean-graph extract --input Main.lean --output proof_dependency.json
`