santosomar/general-secure-coding-agent-skills/c-cpp-to-lean4-translator
- community
Checking that the SKILL.md file is still in the repository.
Translates C/C++ into Lean 4 for interactive theorem proving — deep verification where automated tools fail. Use when Dafny's automation isn't enough, when proving mathematical properties of an algorithm, or when building a machine-checked proof for publication or certification.
Install
npx vskill@latest install santosomar/general-secure-coding-agent-skills/c-cpp-to-lean4-translatorSKILL.md
C/C++ → Lean 4 Translator
Lean is for when you need a proof, not a check. Dafny asks an SMT solver "is this true?"; Lean asks you to construct the proof term. More work, more control, no timeouts.
When Lean over Dafny
| Situation | Why Lean | |
|---|---|---|
SMT solver times out or returns unknown | You write the proof steps; no solver to time out | |
| Proving mathematical properties (complexity bounds, algebraic laws) | Lean has Mathlib; Dafny has arithmetic | |
| The proof itself is the artifact (paper, certification) | Lean proofs are inspectable; Dafny's are solver-internal | |
| You need custom proof tactics | Lean is a tactic language; Dafny's hints are fixed |
The functional lift
Lean is purely functional. The translation path is: imperative C → functional model → proof about the model.
| C construct | Lean model | |
|---|---|---|
int32_t | Int32 (modular) or Int (unbounded) — same choice as Dafny | |
| Loop with accumulator | Fold: xs.foldl f init — or structural recursion | |
| Loop with early exit | Recursion with a sum type result: Option α or Except ε α | |
| Mutable local var | Let-bind a new name each "mutation": let x₁ := ...; let x₂ := f x₁ | |
Array a[i] | a.get i with (h : i < a.size) proof, or a.get? i : Option α | |
| Pointer into array | (a : Array α) × (i : Fin a.size) — a dependent pair | |
struct | structure — direct | |
malloc/pointer to object | Just the object. No heap in the pure fragment. |
The state monad escape hatch
If the imperative structure is too tangled to lift functionally, use StateM:
But prefer the functional lift — proofs over folds are easier than proofs over monadic runs.
Worked example
C:
Lean — functional lift:
Notes:
Listinstead ofArray— easier induction. If you needArrayfor the performance story, prove onListand transport viaArray.toList.- The theorem connects our
countto Mathlib'scountP— once connected, all of Mathlib's lemmas aboutcountPapply to us for free. size_t→Nat(unbounded). If we cared aboutSIZE_MAX, we'd add a separate bound theorem.
Proof patterns you'll need
| Goal shape | Tactic | |
|---|---|---|
| Property of a fold | induction xs + unfold the fold one step | |
Property of recursion on Nat | induction n or Nat.rec | |
| Array index in bounds | omega / simp_arith — or carry Fin n so it's by-type | |
| Decidable equality case split | split after if / by_cases h : P | |
| "Obvious" arithmetic | omega, ring, simp_arith | |
| Reuse a library lemma | exact? / apply? — let Lean search Mathlib |
Do not
- Do not translate to Lean when Dafny would verify automatically. Lean proofs are manual labor; only pay for what you need.
- Do not state the theorem as "equals the C code." State it as "satisfies this mathematical property." The C is an implementation; the theorem is about the spec.
- Do not fight Lean's termination checker with
partial def.partialmeans "trust me" — you lose proofs-by-induction. Find the structural argument. - Do not reinvent Mathlib.
List.countP,List.Sorted,Nat.gcd— they exist, with lemma libraries. Connect to them.
Output format
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/verification/c-cpp-to-lean4-translator/SKILL.md
Popularity
- 7-day trend score
- 0
Refreshed 18 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/santosomar/general-secure-coding-agent-skills/c-cpp-to-lean4-translator)