Agent skill

C Cpp To Lean4 Translator

by ArabelaTso in ArabelaTso/Skills-4-SE

Translate C or C++ programs into equivalent Lean4 code, preserving program semantics and ensuring the generated code is well-typed, executable, and can run successfully.

Apache-2.0Auto-check passedWriting & Content

Install C Cpp To Lean4 Translator

skills CLI
$ npx skills add ArabelaTso/Skills-4-SE --skill c-cpp-to-lean4-translator -a claude-code

Project install by default; add -g for ~/.claude/skills/.

GitHub CLI
$ gh skill install ArabelaTso/Skills-4-SE c-cpp-to-lean4-translator --agent claude-code

Project scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).

Manual copy
$ git clone --depth 1 https://github.com/ArabelaTso/Skills-4-SE.git skills-src && mkdir -p .claude/skills && cp -r skills-src/skills/c-cpp-to-lean4-translator .claude/skills/c-cpp-to-lean4-translator && rm -rf skills-src

Use ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.

Claude Code skills documentation · loads skills from .claude/skills/

Facts

Skill name
c-cpp-to-lean4-translator
GitHub stars
253
Token cost
~2.5k tokens
SKILL.md length
747 words
Files
2 (incl. references)
Skills in repo
150
Repo updated
First seen
Licence
Apache-2.0

At a glance

Translate C or C++ programs into equivalent Lean4 code, preserving program semantics and ensuring the generated code is well-typed, executable, and can run successfully.

  • Works in 6 steps: Analyze Input Code → Design Lean4 Structure → Translate Code → …
  • The user asks to convert C/C++ code to Lean4
  • SKILL.md covers Overview, Translation Workflow, Common Translation Patterns and Examples, plus 3 more sections
  • Instructions only: no scripts, shell commands, URLs or credentials in SKILL.md

What it does

C Cpp To Lean4 Translator is an agent skill from ArabelaTso/Skills-4-SE. Translate C or C++ programs into equivalent Lean4 code, preserving program semantics and ensuring the generated code is well-typed, executable, and can run successfully. Use when the user asks to convert C/C++ code to Lean4, port C/C++ programs to Lean4, translate imperative code to functional Lean4, or create Lean4 versions of C/C++ algorithms.

Its SKILL.md is about 2.5k tokens, which your agent loads only when the skill is triggered. The skill folder holds 2 other files, including reference files (for example `references/translation_patterns.md`).

It sits in Writing & Content, covering Translation. It works with C++. The repository describes itself as: A curated list of 180+ useful Claude Skills for Software Engineering and resources for customizing AI for SE workflows. The licence is Apache-2.0.

When your agent uses it

  • The user asks to convert C/C++ code to Lean4
  • Port C/C++ programs to Lean4
  • Translate imperative code to functional Lean4
  • Create Lean4 versions of C/C++ algorithms

Example prompts

  • “/c-cpp-to-lean4-translator”

Workflow steps

6 steps, taken from the step headings in SKILL.md.

  1. Analyze Input Code
  2. Design Lean4 Structure
  3. Translate Code
  4. Ensure Type Correctness
  5. Test and Verify
  6. Optimize and Refine

What it can do on your machine

Read from SKILL.md and the folder at commit 4f38503. It shows what the files ask for, not the result of running them.

  • Tool permissions

    Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.

    From allowed-tools in the SKILL.md frontmatter.

  • Runs code

    No scripts in the folder and no shell commands in SKILL.md (its code samples are lean, c, bash and cpp).

    From the folder's file list and the shell code blocks in SKILL.md.

  • Network

    Links to these hosts (documentation or services it may open):

    • lean-lang.org
    • github.com

    From URLs in SKILL.md, links to its own repository left out.

  • Credentials

    Names no API keys, tokens, secrets or passwords.

    From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.

Context cost

C Cpp To Lean4 Translator loads about 2.5k tokens when it runs, and up to ~4.4k if it reads all its reference files. Until then it costs about 93 tokens; SKILL.md has 747 words of instructions outside code blocks.

Always · name and description, kept in context so the agent knows when to use it
~93
When it runs · the whole SKILL.md, loaded when a task matches
~2.5k
With references · SKILL.md plus every file in references/, read only if the agent opens them
~4.4k

Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.

Safety

Auto-check passed

The automated check found no risky patterns in SKILL.md.

Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.

SKILL.md

The full file from ArabelaTso/Skills-4-SE at commit 4f38503, republished under its Apache-2.0 licence (© ArabelaTso). 747 words, ~2,478 tokens.

Download SKILL.mdSave it as .claude/skills/c-cpp-to-lean4-translator/SKILL.md (or your agent's skills folder). This skill also uses 1 other file; get the full folder from GitHub.
name
c-cpp-to-lean4-translator
description
Translate C or C++ programs into equivalent Lean4 code, preserving program semantics and ensuring the generated code is well-typed, executable, and can run successfully. Use when the user asks to convert C/C++ code to Lean4, port C/C++ programs to Lean4, translate imperative code to functional Lean4, or create Lean4 versions of C/C++ algorithms.

C/C++ to Lean4 Translator

Overview

Transform C or C++ programs into equivalent Lean4 code that preserves the original semantics while leveraging Lean4's functional programming paradigm, strong type system, and proof capabilities.

Translation Workflow

Step 1: Analyze Input Code

Understand the C/C++ program structure and semantics:

  1. Identify program components:

    • Functions and their signatures
    • Data structures (structs, classes, arrays)
    • Control flow patterns (loops, conditionals)
    • Memory management (allocation, pointers)
    • I/O operations
    • Dependencies and includes
  2. Understand semantics:

    • What does the program compute?
    • What are the inputs and outputs?
    • Are there side effects?
    • What are the invariants and preconditions?
  3. Note translation challenges:

    • Pointer arithmetic
    • Mutable state
    • Imperative loops
    • Manual memory management
    • Undefined behavior
Step 2: Design Lean4 Structure

Plan the Lean4 equivalent before writing code:

  1. Choose appropriate types:

    • Int for signed integers
    • Nat for unsigned integers and array indices
    • Float for floating-point numbers
    • Array for dynamic arrays
    • List for linked lists
    • Custom structure types for structs/classes
  2. Determine purity:

    • Pure functions: return values directly
    • Side effects: use IO monad
    • Mutable state: use IO.Ref or ST monad
  3. Plan control flow translation:

    • Loops → Recursive functions
    • Mutable variables → Function parameters
    • Early returns → Conditional expressions
  4. Handle memory:

    • Stack allocation → Direct values
    • Heap allocation → Automatic memory management
    • Pointers → Direct values or references
Step 3: Translate Code

Follow these translation principles:

Functions

Pattern: Pure function

c
// C/C++
int add(int a, int b) {
    return a + b;
}
lean
-- Lean4
def add (a b : Int) : Int :=
  a + b

Pattern: Function with side effects

c
// C/C++
void printSum(int a, int b) {
    printf("%d\n", a + b);
}
lean
-- Lean4
def printSum (a b : Int) : IO Unit :=
  IO.println (a + b)
Control Flow

Pattern: If-else

c
// C/C++
int max(int a, int b) {
    if (a > b) return a;
    else return b;
}
lean
-- Lean4
def max (a b : Int) : Int :=
  if a > b then a else b

Pattern: For loop → Tail recursion

c
// C/C++
int sum(int n) {
    int result = 0;
    for (int i = 0; i < n; i++) {
        result += i;
    }
    return result;
}
lean
-- Lean4
def sum (n : Nat) : Nat :=
  let rec loop (i acc : Nat) : Nat :=
    if i >= n then acc
    else loop (i + 1) (acc + i)
  loop 0 0

Pattern: While loop → Recursion

c
// C/C++
int factorial(int n) {
    int result = 1;
    while (n > 1) {
        result *= n;
        n--;
    }
    return result;
}
lean
-- Lean4
def factorial (n : Nat) : Nat :=
  let rec loop (n acc : Nat) : Nat :=
    if n <= 1 then acc
    else loop (n - 1) (acc * n)
  loop n 1
Data Structures

Pattern: Struct

c
// C/C++
struct Point {
    int x;
    int y;
};
lean
-- Lean4
structure Point where
  x : Int
  y : Int
  deriving Repr

Pattern: Array

c
// C/C++
int arr[5] = {1, 2, 3, 4, 5};
lean
-- Lean4
def arr : Array Int := #[1, 2, 3, 4, 5]
Pointers and References

Key principle: Lean4 doesn't have raw pointers. Translate based on usage:

  • Read-only pointers: Pass by value
  • Output parameters: Return values (use tuples for multiple returns)
  • Mutable references: Use IO.Ref or return new values
c
// C/C++ - Output parameter
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
lean
-- Lean4 - Return tuple
def swap (a b : Int) : Int × Int :=
  (b, a)
Step 4: Ensure Type Correctness

Lean4's type system is strict. Address common type issues:

  1. Integer types:

    • Use Nat for non-negative values (array indices, counts)
    • Use Int for potentially negative values
    • Convert explicitly: n.toNat, n.toInt
  2. Array bounds:

    • Lean4 requires proof of valid indices
    • Use safe accessors: arr.get?, arr[i]?
    • Or use arr[i]! with runtime check
  3. Division:

    • Natural number division: n / m (rounds down)
    • Integer division: use Int.div
    • Handle division by zero explicitly
  4. Type annotations:

    • Add explicit types when inference fails
    • Use : Type for clarity
Step 5: Test and Verify

Ensure the translated code works correctly:

  1. Compile check:

    bash
    lake build
  2. Create test cases:

    lean
    #eval add 2 3        -- Should output 5
    #eval factorial 5    -- Should output 120
  3. Compare outputs:

    • Run original C/C++ program
    • Run translated Lean4 program
    • Verify outputs match for same inputs
  4. Handle edge cases:

    • Empty arrays
    • Zero values
    • Negative numbers
    • Boundary conditions
Step 6: Optimize and Refine

Improve the translated code:

  1. Use Lean4 idioms:

    • Replace manual recursion with List.foldl, Array.foldl
    • Use pattern matching instead of nested if-else
    • Leverage standard library functions
  2. Add documentation:

    lean
    /-- Calculate the sum of first n natural numbers -/
    def sum (n : Nat) : Nat :=
      n * (n + 1) / 2
  3. Consider performance:

    • Use tail recursion for loops
    • Prefer Array over List for random access
    • Use @[inline] for small functions
Show full SKILL.md (281 more words)Show less

Common Translation Patterns

For detailed patterns, see translation_patterns.md.

Quick Reference
C/C++Lean4
int xdef x : Int
unsigned int xdef x : Nat
float xdef x : Float
bool xdef x : Bool
char* strdef str : String
int arr[]def arr : Array Int
struct Sstructure S where
for (...)let rec loop ...
while (...)let rec loop ...
if (...) {...}if ... then ... else ...
switch (...)match ... with
return xx (last expression)
void f()def f : IO Unit
printf(...)IO.println ...

Examples

Example 1: Simple Algorithm

C/C++ Input:

c
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

Lean4 Output:

lean
def gcd (a b : Nat) : Nat :=
  if b = 0 then a
  else gcd b (a % b)
Example 2: Array Processing

C/C++ Input:

c
int findMax(int arr[], int size) {
    int max = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

Lean4 Output:

lean
def findMax (arr : Array Int) : Option Int :=
  if arr.isEmpty then
    none
  else
    some (arr.foldl max arr[0]!)
Example 3: Struct with Methods

C/C++ Input:

cpp
struct Rectangle {
    int width;
    int height;

    int area() {
        return width * height;
    }

    int perimeter() {
        return 2 * (width + height);
    }
};

Lean4 Output:

lean
structure Rectangle where
  width : Nat
  height : Nat
  deriving Repr

def Rectangle.area (r : Rectangle) : Nat :=
  r.width * r.height

def Rectangle.perimeter (r : Rectangle) : Nat :=
  2 * (r.width + r.height)
Example 4: I/O Program

C/C++ Input:

c
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial: %d\n", factorial(n));
    return 0;
}

Lean4 Output:

lean
def factorial (n : Nat) : Nat :=
  if n <= 1 then 1
  else n * factorial (n - 1)

def main : IO Unit := do
  IO.print "Enter a number: "
  let input ← IO.getStdIn >>= (·.getLine)
  match input.trim.toNat? with
  | some n =>
    IO.println s!"Factorial: {factorial n}"
  | none =>
    IO.println "Invalid input"

Best Practices

  1. Start simple: Translate basic functions first, then build up complexity
  2. Preserve semantics: Ensure the Lean4 code computes the same results
  3. Use types wisely: Leverage Lean4's type system for correctness
  4. Embrace immutability: Prefer pure functions over mutable state
  5. Test thoroughly: Verify outputs match for various inputs
  6. Document assumptions: Note any semantic differences or limitations
  7. Leverage standard library: Use built-in functions when available
  8. Handle errors gracefully: Use Option or Except for error cases

Limitations and Considerations

  1. Undefined behavior: C/C++ undefined behavior must be handled explicitly in Lean4
  2. Performance: Functional code may have different performance characteristics
  3. Concurrency: C/C++ threading requires different approaches in Lean4
  4. Low-level operations: Bit manipulation and pointer arithmetic need careful translation
  5. External libraries: C/C++ library calls may not have direct Lean4 equivalents
  6. Macros: C preprocessor macros need manual translation
  7. Templates: C++ templates translate to Lean4 generics differently

Resources

© ArabelaTso, Apache-2.0. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file

Files

SKILL.md and 1 other file (references) in skills/c-cpp-to-lean4-translator of ArabelaTso/Skills-4-SE.

  • SKILL.md
  • references/translation_patterns.md

Open the folder on GitHubat commit 4f38503

Compare with similar skills

C Cpp To Lean4 Translator next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.

C Cpp To Lean4 Translator compared with similar skills
SkillStarsUsed inTokensAuto-checkLicenceRepo updated
C Cpp To Lean4 Translator this skillArabelaTso/Skills-4-SE253—~2.5kAutomated safety check: PassApache-2.0
Translationdoxygen/doxygen6.6k—~5.2kAutomated safety check: PassGPL-2.0
Smooth Translationopenforecast-org/smooth107—~2.2kAutomated safety check: PassLGPL-2.1
Msvc Clmohitmishra786/low-level-dev-skills253—~1.3kAutomated safety check: PassMIT
D2mcpp Authoringmcpp-community/d2mcpp1.8k—~2.7kAutomated safety check: PassCustom licence
Mcpp Docs Stylemcpp-community/mcpp156—~3.7kAutomated safety check: PassApache-2.0

Similar skills

  • Translation

    doxygen/doxygen

    Keeps all Doxygen and Doxywizard translations up to date across three mechanisms: translator C++ classes (src/translatorxx.h), Qt .ts locale files for the Doxywizard GUI (addon/doxywizard/i18n/)…

    6.6k GitHub stars~5.2k tokensUpdated 7 days ago
    Writing & ContentAuto-check passed
  • Smooth Translation

    openforecast-org/smooth

    Port a feature from the R smooth package to the Python port, or check how an R name maps to Python.

    107 GitHub stars~2.2k tokensUpdated today
    Writing & ContentAuto-check passed
  • Msvc Cl

    mohitmishra786/low-level-dev-skills

    MSVC cl.exe and clang-cl skill for Windows C/C++ projects. An agent skill from mohitmishra786/low-level-dev-skills.

    253 GitHub stars~1.3k tokensUpdated 3 mo ago
    Writing & ContentAuto-check passed
  • D2mcpp Authoring

    mcpp-community/d2mcpp

    Authoring conventions, design principles, and file formats for the d2mcpp (D2X) Modern C++ tutorial project.

    1.8k GitHub stars~2.7k tokensUpdated 2 mo ago
    DevelopmentAuto-check passed
  • Mcpp Docs Style

    mcpp-community/mcpp

    A skill your agent uses when writing or editing anything under docs/ (English or 简体中文), docs/specs/, README files, or the design records under .agents/docs/ — states which tree a document belongs…

    156 GitHub stars~3.7k tokensUpdated today
    DevelopmentAuto-check passed
  • Translation Diff Export

    Devolutions/UniGetUI

    Compares UniGetUI JSON locale files against English, identifies untranslated or source-changed keys, and generates patch, reference, and handoff files for a target language.

    26k GitHub stars~1.1k tokensUpdated today
    Writing & ContentAuto-check passed

More from ArabelaTso/Skills-4-SE

All 150 skills in this repo
  • Framework Migration Assistant

    ArabelaTso/Skills-4-SE

    Automatically migrate Python web applications between frameworks (Flask → FastAPI, Django → FastAPI).

    253 GitHub stars~1.9k tokensUpdated 1 mo ago
    Auto-check passed
  • Metamorphic Test Generator

    ArabelaTso/Skills-4-SE

    Generate test cases using metamorphic testing by applying transformations based on metamorphic properties.

    253 GitHub stars~798 tokensUpdated 1 mo ago
    Auto-check passed
  • Reproduction Trace Instrumenter

    ArabelaTso/Skills-4-SE

    Instruments programs to capture execution traces specifically for reproducing reported bugs, enabling consistent replay and diagnosis of failures.

    253 GitHub stars~2.4k tokensUpdated 1 mo ago
    Auto-check passed
  • Spring Mvc To Boot Migrator

    ArabelaTso/Skills-4-SE

    Automatically migrate Spring MVC applications to Spring Boot.

    253 GitHub stars~2.2k tokensUpdated 1 mo ago
    Auto-check passed
  • State Snapshot Instrumenter

    ArabelaTso/Skills-4-SE

    Instrument programs (Python, C/C++, Java) to capture snapshots of key program states at runtime, including variables, memory, and call stacks.

    253 GitHub stars~2.2k tokensUpdated 1 mo ago
    Auto-check passed

Works with

Questions about C Cpp To Lean4 Translator

What does C Cpp To Lean4 Translator do?

Translate C or C++ programs into equivalent Lean4 code, preserving program semantics and ensuring the generated code is well-typed, executable, and can run successfully. C Cpp To Lean4 Translator is an agent skill from ArabelaTso/Skills-4-SE. Translate C or C++ programs into equivalent Lean4 code, preserving program semantics and ensuring the generated code is well-typed, executable, and can run successfully.

When should I use C Cpp To Lean4 Translator?

C Cpp To Lean4 Translator fits situations like: the user asks to convert C/C++ code to Lean4; port C/C++ programs to Lean4; translate imperative code to functional Lean4; create Lean4 versions of C/C++ algorithms.

How do I install C Cpp To Lean4 Translator in Claude Code?

Run `npx skills add ArabelaTso/Skills-4-SE --skill c-cpp-to-lean4-translator -a claude-code`. Or copy the skill folder (skills/c-cpp-to-lean4-translator in ArabelaTso/Skills-4-SE) into .claude/skills/c-cpp-to-lean4-translator in your project. Claude Code loads it when a task matches its description.

How do I install C Cpp To Lean4 Translator in Codex?

Run `npx skills add ArabelaTso/Skills-4-SE --skill c-cpp-to-lean4-translator -a codex`. Or copy the skill folder (skills/c-cpp-to-lean4-translator in ArabelaTso/Skills-4-SE) into .agents/skills/c-cpp-to-lean4-translator in your project. Codex loads it when a task matches its description.

Can I use C Cpp To Lean4 Translator in Cursor, Gemini CLI or GitHub Copilot?

Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add ArabelaTso/Skills-4-SE --skill c-cpp-to-lean4-translator -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/c-cpp-to-lean4-translator, .gemini/skills/c-cpp-to-lean4-translator, .github/skills/c-cpp-to-lean4-translator and .opencode/skills/c-cpp-to-lean4-translator in your project.

What does C Cpp To Lean4 Translator need to run?

SKILL.md names no scripts, command-line tools or credentials: C Cpp To Lean4 Translator is instructions for the agent only.

Does C Cpp To Lean4 Translator access the network?

SKILL.md names 2 domains. As links in the text: lean-lang.org and github.com. This is read from the text; nothing was executed.

Is C Cpp To Lean4 Translator safe to install?

Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.

What licence does C Cpp To Lean4 Translator use?

C Cpp To Lean4 Translator is published under the Apache-2.0 licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.

How many tokens does C Cpp To Lean4 Translator use?

About 2.5k tokens (SKILL.md is roughly 9.9k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full. Its references folder adds about 1.9k tokens, read only when the agent opens those files.

What are the alternatives to C Cpp To Lean4 Translator?

Skills that share tags, products or a category with C Cpp To Lean4 Translator: Translation (doxygen/doxygen, 6.6k stars), Smooth Translation (openforecast-org/smooth, 107 stars), Msvc Cl (mohitmishra786/low-level-dev-skills, 253 stars) and D2mcpp Authoring (mcpp-community/d2mcpp, 1.8k stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.

Who maintains C Cpp To Lean4 Translator?

ArabelaTso (a GitHub user) maintains it in ArabelaTso/Skills-4-SE, which has 253 GitHub stars. The repository holds 150 skills in this directory. The repository was last updated on August 21, 2026.

Source: ArabelaTso/Skills-4-SE on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.