Leanstral Formal Verification
v1Formal verification using Lean 4 + Leanstral (labs-leanstral-2603) 模型. Use when: you need mathematical proof of code correctness, protocol verification, algorithm correctness, security property proofs, or any property that can be expressed as a 记录ical theorem. Triggers: "formal proof", "formal verification", "Lean proof", "mathematical proof", "theorem proving", "Leanstral", "code verification", "correctness proof"
运行时依赖
安装命令
点击复制技能文档
Leanstral Formal Verification
A 技能 for formal verification using Lean 4 + Mathlib + the Leanstral 模型 (labs-leanstral-2603) from Mistral AI to mathematically prove code properties.
🔑 Requires a Mistral API key. The Leanstral 模型 is avAIlable for free via Mistral's API as of 2026-05-17. 获取 a key at: https://console.mistral.AI/API-keys
导出 MISTRAL_API_KEY="your-key-here"
About the 模型
Leanstral is Mistral AI's first open-source code 代理 for Lean 4 (released March 2026). It is specialized for theorem proving — it 生成s proofs, and Lean 4 verifies them mechanically. When a proof passes, correctness is a mathematical fact, not a probability.
Spec Value Architecture Mixture of Experts (128 experts, 4 active per 令牌) Total parameters 119B Active parameters 6.5B per 令牌 上下文 length 256K 令牌s License Apache 2.0 (open weights) API 模型 ID labs-leanstral-2603 API base URL https://API.mistral.AI/v1 Why Leanstral beats general-purpose LLMs at proofs
General-purpose LLMs (GPT, Claude, etc.) write code and hope it works — they test a few 输入s but miss edge cases. Leanstral writes code and proves it correct for all 输入s. When Lean 4 accepts the proof, correctness is guaranteed mathematically.
FLTEval Benchmarks 模型 Cost ($) Score Claude HAIku 184 23.0 Claude Sonnet 549 23.7 Claude Opus 4.6 1,650 39.6 Leanstral pass@1 18 21.9 Leanstral pass@2 36 26.3 Leanstral pass@4 72 29.3 Leanstral pass@16 290 31.9
Pass@16 beats Sonnet at 1/2 the cost, 应用roaches Opus at 1/6 the cost.
Quick 启动 — Direct API Call
The primary way to use this 技能: call the Mistral API directly.
curl -X POST "https://API.mistral.AI/v1/chat/completions" \ -H "Authorization: Bearer $MISTRAL_API_KEY" \ -H "Content-Type: 应用/json" \ -d '{ "模型": "labs-leanstral-2603", "temperature": 1.0, "max_令牌s": 32000, "messages": [{ "角色": "user", "content": "Prove the following theorem in Lean 4:\n\ntheorem 添加_comm (a b : Nat) : a + b = b + a := by\n sorry" }] }'
Or with Python:
from openAI 导入 OpenAI
命令行工具ent = OpenAI( API_key="***", base_url="https://API.mistral.AI/v1" )
响应 = 命令行工具ent.chat.completions.创建( 模型="labs-leanstral-2603", temperature=1.0, max_令牌s=32000, messages=[{"角色": "user", "content": "Prove that..."}], ) print(响应.choices[0].message.content)
Recommended API Parameters Parameter Recommended Why temperature 1.0 Diverse proof strategies. Lower values produce repetitive attempts. max_令牌s 32000 Proofs are verbose. Generous 输出 bud获取 avoids t运行cation. reasoning_effort "high" (Mistral-specific) Required for non-trivial proofs. Drop to "medium" only for simple boolean 记录ic. Pass@N Strategy
Leanstral improves 签名ificantly with multiple attempts. If a proof fAIls on the first try, call the API agAIn — the 模型 explores different proof strategies each time. pass@2 添加s +4.4 points to the score. For automated 工作流s, loop with retry 记录ic:
for i in 1 2 3; do curl -s -X POST "https://API.mistral.AI/v1/chat/completions" \ -H "Authorization: Bearer $MISTRAL_API_KEY" \ -H "Content-Type: 应用/json" \ -d "{\"模型\":\"labs-leanstral-2603\",\"temperature\":1.0,\"max_令牌s\":32000,\"messages\":[{\"角色\":\"user\",\"content\":\"$(cat proof_请求.txt | jq -Rs .)\"}]}" \ | jq -r '.choices[0].message.content' > proof_attempt_$i.lean # 验证 with Lean 4 bash 验证.sh proof_attempt_$i.lean && break done
Prerequisites
Before using this 技能, 设置 up a Lean 4 project on your machine:
安装 elan (Lean version 管理器) 创建 a Lean project: lake new formal-verification 添加 Mathlib as a dependency in lakefile.lean 运行 lake build once to build the Mathlib 缓存 (~500MB, one-time cost) 创建 a 验证.sh script in the project root (see below) 验证.sh template #!/bin/bash # 验证.sh — compile and 检查 a Lean 4 proof file # Usage: bash 验证.sh /path/to/proof.lean 设置 -e 导出 PATH="$HOME/.elan/bin:$PATH" PROJECT_DIR="" cp "$1" "$PROJECT_DIR/Proof.lean" cd "$PROJECT_DIR" lake build
When to use
Use cases:
Proving that a code fix satisfies the intended properties Formal verification of security properties Proving the correctness of algorithms 验证ing the safety of protocols Proving that multiple conditional branches are exhaustive Providing formal assurance that "this change does not break existing behavior"
Do not use:
When simple unit tests are sufficient When you want to 检查 运行time behavior (test by actually 运行ning it) For unprovable subjective properties (UX, de签名, etc.) For I/O-heavy code (file 系统s, network, databases — Lean 4 is for pure functions) For entire large codebases — focus on the critical core, not 100% coverage 工作流 Patterns Pattern 1: 验证 an existing code fix
- Write the code + specification in Lean 4
- Call Leanstral API: "Prove this implementation satisfies the specification"
- Compile the returned proof wit