Verified Spec-Code Mapper
Overview
Establish explicit, evidence-based traceability between formal specifications and verified code components. Generate structured reports that map each specification to its implementation and correctness proofs, supporting verification auditing, documentation, and reproducibility.
Mapping Workflow
Step 1: Identify Specifications
Locate all formal specifications in the codebase:
-
Preconditions:
- Coq:
forall ... -> P -> ...(hypothesis before conclusion) - Dafny:
requiresclauses - Isabelle:
assumesclauses - Look for: Input constraints, safety conditions
- Coq:
-
Postconditions:
- Coq:
... -> Q(conclusion of theorem) - Dafny:
ensuresclauses - Isabelle:
showsclauses - Look for: Output guarantees, correctness properties
- Coq:
-
Loop Invariants:
- Dafny:
invariantclauses in loops - Coq: Lemmas about recursive functions
- Look for: Properties maintained across iterations
- Dafny:
-
Class/Type Invariants:
- Dafny:
predicate Valid()methods - Coq: Well-formedness predicates
- Look for: Structural constraints, consistency properties
- Dafny:
-
Functional Correctness:
- Theorems stating what function computes
- Equivalence to specification
- Look for: Correctness lemmas, specification theorems
Step 2: Map to Code Components
For each specification, identify corresponding code:
-
Direct mapping:
- Specification mentions function/method name
- Theorem statement references definition
- Example:
Lemma factorial_positive : forall n, factorial n >= 1→ Maps tofactorialfunction
-
Contextual mapping:
- Specification in same file/module
- Comments linking spec to code
- Naming conventions (e.g.,
sort_correct→sort)
-
Structural mapping:
- Preconditions → Function parameters
- Postconditions → Return values
- Invariants → Loop bodies or class fields
-
Record locations:
- File path
- Line numbers
- Function/method/class names
Step 3: Identify Verification Evidence
For each specification-code mapping, find proof evidence:
-
Complete proofs:
- Coq: Ends with
Qed - Dafny: Verified by Dafny verifier (no errors)
- Isabelle: Ends with
doneorqed - Status: ✓ Fully Verified
- Coq: Ends with
-
Incomplete proofs:
- Coq: Ends with
Admitted - Isabelle: Contains
sorry - Status: ✗ Unverified
- Coq: Ends with
-
Assumed axioms:
- Coq:
Axiomdeclarations - Isabelle:
axiomatization - Status: ⚠ Assumed (no proof)
- Coq:
-
External verification:
- SMT solver verification
- Automated tool checks
- Status: ✓ Verified (by tool)
-
Record evidence:
- Theorem/lemma name
- Proof location
- Proof method
- Dependencies (lemmas, axioms used)
Step 4: Analyze Coverage
Determine verification completeness:
-
Specification coverage:
Coverage = (Verified Specs / Total Specs) × 100% -
Code coverage:
Coverage = (Verified Functions / Total Functions) × 100% -
Proof completeness:
Completeness = (Complete Proofs / Total Proofs) × 100% -
Categorize status:
- ✓ Fully Verified: All specs proved, no assumptions
- ⚠ Partially Verified: Some specs proved, some assumed
- ✗ Unverified: No formal verification
-
Identify gaps:
- Unverified specifications
- Incomplete proofs
- Missing specifications
- Assumed axioms
Step 5: Generate Report
Produce structured Markdown report:
-
Choose report type:
- Function-centric: One function per section
- Specification-centric: One spec per section
- Module-level: Overview of entire module
-
Include required sections:
- Specification statements (formal)
- Code component references (with locations)
- Verification evidence (proofs, theorems)
- Coverage summary
- Assumptions and gaps
-
Use clear status indicators:
- ✓ Fully Verified
- ⚠ Partially Verified (with assumptions)
- ✗ Unverified
-
Provide traceability:
- Link specs to code
- Link code to proofs
- Show dependency chains
Mapping Patterns
For detailed patterns and templates, see mapping_patterns.md.
Quick Reference
| Specification Type | Typical Location | Verification Evidence |
|---|---|---|
| Precondition | requires, hypothesis | Checked by verifier or proved |
| Postcondition | ensures, conclusion | Theorem proving property |
| Loop Invariant | invariant, lemma | Induction proof |
| Class Invariant | predicate Valid() | Maintained by all methods |
| Functional Correctness | Theorem statement | Complete proof |
Examples
Example 1: Simple Function Mapping (Coq)
Input Code:
Definition divide (n m : nat) : option nat :=
if m =? 0 then None else Some (n / m).
Lemma divide_safe : forall n m : nat,
m <> 0 -> exists q, divide n m = Some q.
Proof.
intros n m Hm.
unfold divide.
destruct (m =? 0) eqn:E.
- apply Nat.eqb_eq in E. contradiction.
- exists (n / m). reflexivity.
Qed.
Generated Report:
# Verification Report: divide
## Function Signature
```coq
Definition divide (n m : nat) : option nat
Specifications
Precondition: Non-zero Divisor
- Specification:
m <> 0 - Location: divide.v:4 (lemma hypothesis)
- Status: ✓ Fully Verified
Postcondition: Returns Quotient
- Specification:
exists q, divide n m = Some q - Location: divide.v:4 (lemma conclusion)
- Status: ✓ Fully Verified
Verification Evidence
Theorem: divide_safe
- Statement:
forall n m, m <> 0 -> exists q, divide n m = Some q - Location: divide.v:4-10
- Proof Status: ✓ Complete (ends with Qed)
- Proof Method: Case analysis on
m =? 0 - Dependencies: None
Coverage Summary
- Specifications: 2 total, 2 verified (100%)
- Status: ✓ Fully Verified
### Example 2: Class with Invariants (Dafny)
**Input Code:**
```dafny
class BankAccount {
var balance: int
predicate Valid()
reads this
{
balance >= 0
}
constructor(initial: int)
requires initial >= 0
ensures Valid()
ensures balance == initial
{
balance := initial;
}
method Deposit(amount: int)
requires Valid()
requires amount >= 0
modifies this
ensures Valid()
ensures balance == old(balance) + amount
{
balance := balance + amount;
}
}
Generated Report:
# Verification Report: BankAccount
## Class Invariant
### Invariant: Non-negative Balance
- **Specification:** `balance >= 0`
- **Location:** BankAccount.dfy:5 (Valid predicate)
- **Status:** ✓ Fully Verified
## Method: constructor
### Specifications
1. **Precondition: Non-negative Initial**
- Specification: `requires initial >= 0`
- Location: BankAccount.dfy:10
- Status: ✓ Verified by Dafny
2. **Postcondition: Establishes Invariant**
- Specification: `ensures Valid()`
- Location: BankAccount.dfy:11
- Status: ✓ Verified by Dafny
3. **Postcondition: Correct Balance**
- Specification: `ensures balance == initial`
- Location: BankAccount.dfy:12
- Status: ✓ Verified by Dafny
## Method: Deposit
### Specifications
1. **Precondition: Valid State**
- Specification: `requires Valid()`
- Location: BankAccount.dfy:18
- Status: ✓ Verified by Dafny
2. **Precondition: Non-negative Amount**
- Specification: `requires amount >= 0`
- Location: BankAccount.dfy:19
- Status: ✓ Verified by Dafny
3. **Postcondition: Maintains Invariant**
- Specification: `ensures Valid()`
- Location: BankAccount.dfy:21
- Status: ✓ Verified by Dafny
4. **Postcondition: Correct Balance Update**
- Specification: `ensures balance == old(balance) + amount`
- Location: BankAccount.dfy:22
- Status: ✓ Verified by Dafny
## Coverage Summary
- Total Specifications: 7
- Fully Verified: