Install
$ agentstack add skill-arabelatso-skills-4-se-cpp-to-dafny-translator ✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.
Security review
✓ PassedNo issues found. Passed automated security review. · v0.1.0 How review works →
- ✓ Prompt-injection patterns
- ✓ Secret / credential exfiltration
- ✓ Dangerous shell & filesystem operations
- ✓ Untrusted network calls
- ✓ Known-malicious package signatures
What it can access
- ✓ Network access No
- ✓ Filesystem access No
- ✓ Shell / process execution No
- ✓ Environment & secrets No
- ✓ Dynamic code execution No
From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.
Verified badge
Passed review? Show it. Paste this badge into your README, it links to the public security report.
Reliability & compatibility
Declared compatibility
Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.
We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.
How agent discovery & health will work →About
C/C++ to Dafny Translator
Translate C/C++ programs into equivalent, verifiable Dafny code while preserving program semantics and ensuring memory safety.
Overview
This skill provides systematic guidance for translating C/C++ code to Dafny, handling memory management, pointer semantics, type conversions, and ensuring well-typed, verifiable output with appropriate specifications.
Translation Workflow
C/C++ Input → Analyze Structure → Map Types & Memory → Translate → Add Specifications → Verify
├─ Identify types, pointers, memory patterns
├─ Map C/C++ constructs to Dafny equivalents
├─ Handle memory safety and ownership
├─ Add preconditions, postconditions, invariants
└─ Validate executability and verification
Core Translation Principles
1. Memory Safety First
Dafny enforces memory safety. Every translation must:
- Replace raw pointers with safe references or arrays
- Make memory bounds explicit
- Ensure no null pointer dereferences
- Handle dynamic memory with sequences or arrays
2. Preserve Semantics
The translated code must maintain the same computational behavior, preserve function contracts, keep algorithmic complexity, and handle all edge cases including error conditions.
3. Enable Verification
Generated Dafny code must include specifications (preconditions, postconditions, invariants), be verifiable by Dafny's verifier, compile and execute correctly, and follow Dafny idioms.
Type Mapping Reference
Basic Types
| C/C++ Type | Dafny Type | Notes | |-----------|-----------|-------| | int, long | int | Unbounded integers in Dafny | | unsigned int | nat | Natural numbers (≥ 0) | | char | char | Single character | | bool | bool | Direct mapping | | float, double | real | Exact rationals in Dafny | | void | () | Unit type | | NULL | Use Option or bounds checks | No null pointers |
Composite Types
| C/C++ Type | Dafny Type | Notes | |-----------|-----------|-------| | int arr[] | array | Fixed-size arrays | | int* ptr | array or seq | Depends on usage | | struct | class or datatype | Mutable vs immutable | | enum | datatype | Algebraic data types | | union | datatype with variants | Tagged unions |
For detailed mappings, see [references/typemappings.md](references/typemappings.md).
Translation Patterns
Functions
Simple C function:
int add(int a, int b) {
return a + b;
}
Dafny:
function add(a: int, b: int): int
{
a + b
}
Function with side effects:
void increment(int* x) {
(*x)++;
}
Dafny (using method):
method increment(x: array, index: nat)
requires index ) returns (sum: int)
ensures sum == arraySum(arr[..])
{
sum := 0;
var i := 0;
while i ): int
{
if |s| == 0 then 0 else s[0] + arraySum(s[1..])
}
Structs and Classes
C struct:
struct Point {
int x;
int y;
};
int distance_squared(struct Point* p) {
return p->x * p->x + p->y * p->y;
}
Dafny:
class Point {
var x: int
var y: int
constructor(x0: int, y0: int)
ensures x == x0 && y == y0
{
x := x0;
y := y0;
}
}
function distanceSquared(p: Point): int
reads p
{
p.x * p.x + p.y * p.y
}
Control Flow
If-else:
int max(int a, int b) {
if (a > b) return a;
else return b;
}
Dafny:
function max(a: int, b: int): int
{
if a > b then a else b
}
Loops with invariants:
int factorial(int n) {
int result = 1;
for (int i = 1; i , target: int) returns (index: int)
ensures index == -1 || (0 , target: int) returns (index: int)
requires forall i, j :: 0 arr[i] arr[i] arr[i] > target
{
var mid := (low + high) / 2;
if arr[mid] target {
high := mid;
} else {
return mid;
}
}
return -1;
}
Translation Process
Step 1: Analyze C/C++ Code
Identify all functions, structs, and global variables. Analyze pointer usage and memory patterns. Identify side effects and state modifications. Note any unsafe operations.
Step 2: Plan Type and Memory Mappings
Map C/C++ types to Dafny types. Decide how to handle pointers (arrays, sequences, or references). Plan struct translations (class vs datatype). Identify what needs specifications.
Step 3: Translate Constructs
Start with data structures (structs → classes/datatypes). Translate pure functions first. Convert functions with side effects to methods. Add memory safety checks. Include necessary specifications.
Step 4: Add Verification Annotations
Add preconditions (requires). Add postconditions (ensures). Add loop invariants. Add frame conditions (reads, modifies). Add termination measures (decreases).
Step 5: Verify and Test
Run Dafny verifier. Fix verification errors. Test with concrete examples. Ensure executability.
Example Translation
C code:
int is_sorted(int* arr, int n) {
for (int i = 0; i arr[i + 1]) {
return 0;
}
}
return 1;
}
void bubble_sort(int* arr, int n) {
for (int i = 0; i arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Dafny:
predicate isSorted(arr: array)
reads arr
{
forall i, j :: 0 arr[i] )
modifies arr
ensures isSorted(arr)
ensures multiset(arr[..]) == multiset(old(arr[..]))
{
var i := 0;
while i arr[k] arr[k] arr[j + 1] {
arr[j], arr[j + 1] := arr[j + 1], arr[j];
}
j := j + 1;
}
i := i + 1;
}
}
Best Practices
- Start with Pure Functions: Translate side-effect-free code first
- Add Specifications Incrementally: Start with simple contracts, refine as needed
- Use Helper Functions: Define pure functions to express properties
- Leverage Dafny's Verifier: Let the verifier guide you to correct specifications
- Test Executability: Use
Mainmethods to test concrete examples - Document Assumptions: Note where C semantics differ from Dafny
- Handle Memory Explicitly: Make all memory bounds and ownership clear
Verification Checklist
Before finalizing translation:
- [ ] All types are correctly mapped
- [ ] Code compiles without errors
- [ ] Dafny verifier succeeds
- [ ] Preconditions capture all assumptions
- [ ] Postconditions specify all guarantees
- [ ] Loop invariants are sufficient for verification
- [ ] Memory safety is ensured (no out-of-bounds access)
- [ ] Termination is proven (decreases clauses if needed)
- [ ] Code is executable and produces correct results
Additional Resources
For complex translations, refer to:
- [Type Mappings](references/type_mappings.md) - Comprehensive C/C++ to Dafny type guide
- [Memory Patterns](references/memory_patterns.md) - Handling pointers, arrays, and dynamic memory
- [Verification Guide](references/verification_guide.md) - Writing effective specifications
Source & license
This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.
- Author: ArabelaTso
- Source: ArabelaTso/Skills-4-SE
- License: Apache-2.0
- Homepage: https://ArabelaTso.github.io/Skills-4-SE/
Install and usage instructions live in the source repository linked above.
Reviews
No reviews yet, be the first.
Write a review
Versions
- v0.1.0 Imported from the upstream source.