Logo von Lean Verify

Lean Verify

Community
hirosassa
lean-verify

Use the Lean 4 theorem prover to find software bugs with counterexamples (integer overflow, bit manipulation, boundary conditions, spec/implementation mismatches) and to prove that an optimization or refactoring is equivalent to the original, so performance work can land safely. Also covers installing Lean and creating a minimal project. Use this skill proactively, even when Lean is not mentioned by name, when the user mentions Lean / lake / elan / formal verification / theorem proving / bv_decide / counterexample search / "is this optimization equivalent?" / "check that it holds for all inputs" / "find bugs tests can't catch" / "safely swap out a bit-twiddling or arithmetic trick", or wants to confirm the correctness of a peephole optimization, strength reduction, bounds-check elimination, or algorithm rewrite. Language-agnostic (Rust, C, Go, TypeScript, ...).

Übersicht

Herausgeberhirosassa
Repositorylean-verify
Skill-Namelean-verify
Sterne
66
Forks
2
Mitgelieferte Dateien
8
LizenzMIT
Links
  • Anweisungen in Markdown

    Eine SKILL.md-Datei, die das Modell bei Bedarf lädt. So werden nur dann Token verbraucht, wenn eine Anfrage tatsächlich dazu passt.

  • Funktioniert mit jedem LLM

    KI-Skills bestehen aus reinem Markdown, nicht aus anbieterspezifischem Code. Daher funktionieren sie mit GPT, Claude, Gemini, Grok oder einem lokalen Modell.

  • Mitgelieferte Dateien: 8

    Skripte, Vorlagen und Referenzmaterial, die das Modell bei der Arbeit lesen kann. Die Dateien sind schreibgeschützt und werden niemals ausgeführt.

  • Open Source

    Von hirosassa auf GitHub veröffentlicht. Prüfen Sie vor der Installation die Quelldateien.

Installation

Installieren Sie den KI-Skill Lean Verify in TypingMind, um ihn mit jedem LLM zu nutzen, oder fügen Sie ihn einem anderen Agenten hinzu, der SKILL.md lesen kann.

1

In TypingMind installieren

TypingMind installiert einen Skill direkt aus seinem GitHub-Ordner. Dabei liest die App SKILL.md, bündelt die Ressourcendateien und speichert das Ergebnis lokal.

  1. Öffnen Sie die App und gehen Sie zu Plugins → Skills.
  2. Wählen Sie „Von GitHub installieren“.
  3. Fügen Sie die unten angegebene URL des Skill-Ordners ein und bestätigen Sie.
    https://github.com/hirosassa/lean-verify/tree/main/skills/lean-verify
  4. Aktivieren Sie den Skill in jedem Chat, in dem er verfügbar sein soll.
Plugins → Skills → Skill hinzufügen → Über GitHub-URL. Fügen Sie dann die Ordner-URL ein und klicken Sie auf „Weiter“.
2

In einem anderen Agenten installieren

Jeder Agent, der das Agent-Skills-Format unterstützt, kann diesen Skill nutzen. Kopieren Sie den Ordner in das Skill-Verzeichnis des jeweiligen Agenten.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/hirosassa/lean-verify.git /tmp/lean-verify
mkdir -p .claude/skills
cp -r /tmp/lean-verify/skills/lean-verify .claude/skills/lean-verify
Starten Sie Claude Code nach dem Kopieren neu, damit der neue Skill erkannt wird.

In TypingMind verwenden

Aktivieren Sie Lean Verify in einem beliebigen Chat in TypingMind. Den Rest übernimmt das Modell. Name und Beschreibung des Skills stehen im System-Prompt. Sobald eine Anfrage passt, lädt das Modell selbstständig die vollständigen Anweisungen — Sie müssen den Skill nie manuell aufrufen, und er verbraucht erst Token, wenn er tatsächlich genutzt wird.

Das Modell lädt Lean Verify automatisch, sobald eine passende Anfrage gestellt wird.

Funktioniert mit jedem KI-Modell

KI-Skills bestehen aus reinen Markdown-Anweisungen statt aus anbieterspezifischem Code. Daher ist Lean Verify nicht an das Modell gebunden, für das der Skill geschrieben wurde. Installieren Sie ihn einmal in TypingMind und nutzen Sie ihn mit GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder einem selbst betriebenen lokalen Modell — alles mit Ihren eigenen API-Schlüsseln.

  • Wird nur bei Bedarf geladen

    Der System-Prompt enthält nur den Namen und die Beschreibung. Die Anweisungen werden bei der ersten passenden Anfrage geladen. Ein ungenutzter Skill verursacht daher keine Kosten.

  • Modell im laufenden Chat wechseln

    Da der Skill aus Anweisungen statt aus Code besteht, funktioniert er auch nach einem Modellwechsel — das nächste Modell liest dieselbe SKILL.md-Datei.

Skill-Anweisungen

Dies ist der Inhalt von SKILL.md, den das Modell lädt. Lesen Sie ihn vor der Installation — ein Skill besteht aus Anweisungen, denen Ihr Modell folgt.

lean-verify: Find bugs and speed up code safely with Lean 4

Tests only guarantee the inputs you tried. Lean can mechanically answer whether a property holds for all inputs, or hand back a counterexample. This skill uses Lean not as a heavyweight verification framework but as a tool for small functions and single rewrite rules. Do not try to prove a whole program: the proof cost explodes and you learn nothing extra.

When to use it

SituationFitMode
Correctness of integer ops, bit ops, shifts, rotations, wrapping arithmeticExcellentA: bv_decide
"Is the optimized version equal to the naive one?" (strength reduction, bit tricks, expression rewrites)ExcellentB: equivalence
Bounds-check elimination, index range validityGoodB + omega
Loop/recursion invariants, algorithm rewritesGood (needs induction)B + induction
Exhaustive search over a small finite domainGooddecide / #eval
I/O, concurrency, networking, code with huge statePoorRecommend ordinary tests / fuzzing

If the target is a poor fit, don't force it into Lean. Say so and propose another approach.

Workflow

  1. Check the environment → run scripts/check_lean.sh. If it prints found (not on PATH) and a hint: export PATH=... line, Lean is in ~/.elan/bin but not on PATH: run that export in the same Bash call as lean / lake. If Lean is missing, follow references/setup.md (ask the user before installing). If plausible looks necessary (searching for properties over structured data such as lists), read the plausible: line of the output: available means use it, not installed means follow references/setup.md §4 (ask the user first). Don't propose it when bv_decide or #eval can do the job (fixed-width integers, small finite domains).
  2. Narrow the target: verify one function or one rewrite rule. Split anything larger.
  3. Model it: translate the target into Lean definitions. Watch for semantic differences (see "Modeling pitfalls" below). Start the file from this minimal template; forgetting the import is the most common first error (error: "to use bv_decide, please include import Std.Tactic.BVDecide"):
    lean
    import Std.Tactic.BVDecide
    
    theorem t (x : BitVec 32) : (x <<< 1) = x * 2 := by bv_decide
  4. State the property: for bug hunting, the property the code should satisfy; for optimization, fast x = slow x.
  5. Look for a counterexample first: before proving, try bv_decide / plausible / #eval. A counterexample saves the proof effort and is directly usable as a bug report.
  6. Prove it: try the tactics in the order given in references/tactics.md (bv_decide → omega → decide +kernel → simp → induction). Once it is proved, run a mutation check: change one constant or operator (e.g. <<< 3 → <<< 2) and confirm Lean now reports a counterexample. If the mutant is still proved, the proof is probably vacuous (details in references/perf-equivalence.md).
  7. Translate the result back: turn a counterexample into concrete inputs in the original language and write a test that reproduces it in the original code (don't take Lean's output on faith; a modeling gap can cause a false positive). Don't call it a bug until it reproduces in the real code (see "potentially spurious counterexample" under Known limitations). A successful proof means "equivalent in the model"; state explicitly that the correspondence between the model and the real code is a human guarantee.

Modes

A. Bug hunting (go get a counterexample)

Write the spec a different way (more naive, or computed in a wider type) and compare it with the implementation. When bv_decide fails it returns a counterexample assignment:

lean
def avgBad (a b : BitVec 32) : BitVec 32 := (a + b) >>> 1          -- a+b overflows 32 bits
def avgSpec (a b : BitVec 32) : BitVec 32 :=
  ((a.zeroExtend 33 + b.zeroExtend 33) >>> 1).truncate 32          -- compute in 33 bits
theorem t (a b : BitVec 32) : avgBad a b = avgSpec a b := by unfold avgBad avgSpec; bv_decide
-- error: The prover found a counterexample: a = 4294967295#32, b = 4294967295#32

If bv_decide can neither prove the property nor produce a counterexample, suspect how the spec is written or a missing precondition (e.g. h : x ≠ 0). Full examples are in assets/examples/ and have been run. Detailed steps are in references/bug-hunting.md.

B. Proving an optimization equivalent

Optimizations risk being "faster but subtly different". Treat the naive implementation as the spec and the optimized one as the implementation, prove they are equal, and the rewrite can be adopted with confidence:

lean
theorem mul10 (x : BitVec 32) : (x <<< 3) + (x <<< 1) = x * 10 := by bv_decide
theorem mod8  (x : BitVec 32) : x % 8 = x &&& 7 := by bv_decide

For optimizations with preconditions (e.g. "only holds when x is a power of two"), state the precondition as a hypothesis (h : ...). Whether callers actually guarantee it must be checked separately. Rewrites of recursion or loops use induction. Details in references/perf-equivalence.md.

Modeling pitfalls (the main source of false positives/negatives)

  • Lean's BitVec is two's-complement wrapping arithmetic. Rust's + panics in debug builds; wrapping_add is what matches Lean. Map to the semantics of the target language and build mode.
  • Shift amounts behave differently. Shifting a BitVec by its width or more gives 0 in Lean, but Rust's <</>> panic at width or more (masked in release), and C is undefined behavior. Reduce the shift amount with % 32 or put it in a precondition so it matches the original language.
  • Signed division/remainder rounding (toward zero vs. floor) differs by language. Check which of BitVec.sdiv / srem / smod corresponds.
  • Floating point is outside bv_decide's scope. Extract only the integer part.
  • Nat / Int have unbounded precision. Use BitVec n to study fixed-width behavior.

Known limitations (hit in practice)

  • Plausible (property-based testing) is not part of core. If check_lean.sh reports plausible: not installed and you need it, ask the user for permission before adding it (it edits lakefile.toml and downloads from the network; see references/setup.md §4). Never add it unasked. If the user declines, or for a single file, an exhaustive #eval search can stand in.

  • ring_nf / nlinarith / linarith and friends are Mathlib-only. To stay within core, use simp only [Nat.add_mul, ...] + omega. Mathlib's first build is heavy, so don't add it until it is needed.

  • Using decide for an exhaustive search as small as Fin 256 can hit the recursion limit. Use decide +kernel or set_option maxRecDepth N in decide.

  • The first lean run downloads the toolchain and can take minutes, and concurrent elan invocations wait on a lock. Watch for timeouts and use background execution.

  • bv_decide uses a SAT solver (cadical); 64-bit expressions with many multiplications or divisions can be slow. If so, shrink the width to 8/16 bits and check first whether a counterexample exists (the structure of a bug often doesn't depend on width).

  • "potentially spurious counterexample" from bv_decide means the counterexample may not be real. It appears when the goal contains something the solver treats as opaque: an opaque / abstracted variable, an expression you abstracted away (e.g. a hash or call replaced by a free variable), or floating-point values. Treat it as a lead only: reproduce it in the real code (or model the abstracted part / add the precondition) before reporting a bug. Steps in references/bug-hunting.md.

Report format

Return results to the user in this form:

  1. What was verified, and the assumptions made while modeling
  2. Result: counterexample found (input values and how to reproduce in the original code) / proved (under which preconditions) / undecided (what blocked it)
  3. Next action (fix proposal, tests to add, whether to adopt the optimization)

Do not rephrase "proved" as "the original code is correct". The guarantee only extends to the model.

Mitgelieferte Dateien

Das Modell liest diese Dateien bei Bedarf, während der Skill geladen ist. Sie werden als lesbare Dateien bereitgestellt und niemals ausgeführt.

Häufig gestellte Fragen

Was macht der KI-Skill Lean Verify?

Use the Lean 4 theorem prover to find software bugs with counterexamples (integer overflow, bit manipulation, boundary conditions, spec/implementation mismatches) and to prove that an optimization or refactoring is equivalent to the original, so performance work can land safely. Also covers installing Lean and creating a minimal project. Use this skill proactively, even when Lean is not mentioned by name, when the user mentions Lean / lake / elan / formal verification / theorem proving / bv_decide / counterexample search / "is this optimization equivalent?" / "check that it holds for all in...

Warum Lean Verify in TypingMind nutzen?

Weil Sie den Skill einmal installieren und mit jedem Modell verwenden können. Lean Verify besteht aus reinem Markdown statt aus anbieterspezifischem Code. So funktioniert derselbe Skill mit GPT-5, Claude, Gemini, Grok oder einem lokalen Modell – und Sie können das Modell mitten im Chat wechseln, ohne dass der Skill seine Funktion verliert. TypingMind verwendet Ihre eigenen API-Schlüssel. Sie bezahlen die Anbieter daher direkt statt über ein Abonnement pro Nutzerplatz, und Ihre Skills und Chats bleiben in Ihrem eigenen Speicher.

Wie installiere ich Lean Verify in TypingMind?

Öffnen Sie in TypingMind Plugins → Skills → Von GitHub installieren und fügen Sie https://github.com/hirosassa/lean-verify/tree/main/skills/lean-verify ein. TypingMind liest die Datei SKILL.md, bündelt die zugehörigen Dateien und installiert alles als Skill, den Sie für jeden Chat einzeln aktivieren können.

Welche KI-Modelle können Lean Verify nutzen?

Jedes Modell, das Sie mit TypingMind verbinden. KI-Skills bestehen aus reinen Markdown-Anweisungen statt aus anbieterspezifischem Code. Daher können GPT, Claude, Gemini, Grok und lokale Modelle diesen Skill laden, wenn eine Anfrage dazu passt.

Wie viele KI-Modelle kann ich mit Lean Verify verwenden?

So viele Sie möchten. In TypingMind können Sie Lean Verify mit jedem Modell verwenden, das Skills unterstützt, etwa mit GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama. Dabei nutzen Sie Ihre eigenen API-Schlüssel.

Ist der KI-Skill Lean Verify kostenlos?

Ja. Der Skill wird von hirosassa auf GitHub unter der Lizenz MIT veröffentlicht. Sie bezahlen nur Ihren eigenen KI-Anbieter für die verwendeten Token.

Was sind KI-Skills?

Ein KI-Skill ist ein wiederverwendbares Anweisungspaket, das einem KI-Modell vermittelt, wie es eine bestimmte Aufgabe erledigt. Es folgt dem offenen Format Agent Skills: Die Datei SKILL.md enthält den Namen und die Beschreibung des Skills. Hinzu kommen bei Bedarf Skripte, Vorlagen oder Referenzdateien für das Modell. Das Modell liest die Anweisungen nur, wenn Ihre Anfrage zum Skill passt. Ein installierter Skill verursacht daher erst Kosten, wenn er verwendet wird.

Wie unterscheiden sich KI-Skills von Plugins oder MCP-Servern?

Ein Plugin oder MCP-Server stellt einem Modell neue aufrufbare Tools bereit – Code, der auf einem System ausgeführt wird und ein Ergebnis zurückgibt. Ein KI-Skill vermittelt dem Modell stattdessen Wissen und Vorgehensweisen: wie es eine Aufgabe angeht, welche Schritte es befolgt und wie eine gute Ausgabe aussieht. Skills bestehen aus reinem Markdown und kommen daher ohne Server, API-Schlüssel und Laufzeitumgebung aus. Sie funktionieren mit jedem Modell.

Alle anzeigen

Jetzt eigenen KI-Arbeitsbereich einrichten

Abonnieren Sie unseren Newsletter und erfahren Sie mehr über neue Funktionen und kommende Gewinnspiele 👇