# Formal Verification Techniques for Equivalence Checking

> Formal verification provides mathematical guarantees about semantic equivalence through rigorous proof techniques. Use these methods when high confidence is required or when testing alone is insufficient.

- Skill: `tools-only/formal-verification-techniques-for-equivalence-checking` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add tools-only/formal-verification-techniques-for-equivalence-checking`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tools-only/formal-verification-techniques-for-equivalence-checking/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tools-only (https://skillmd.com/u/tools-only)
- Updated: 2026-09-29
- Page: https://skillmd.com/skills/tools-only/formal-verification-techniques-for-equivalence-checking

---

# Formal Verification Techniques for Equivalence Checking

## Overview

Formal verification provides mathematical guarantees about semantic equivalence through rigorous proof techniques. Use these methods when high confidence is required or when testing alone is insufficient.

## Proof-Based Approaches

### Hoare Logic

Use pre/postconditions to prove equivalence:

1. Define precondition P (input constraints)
2. Define postcondition Q (expected output relation)
3. Prove {P} Artifact_A {Q} and {P} Artifact_B {Q}
4. If both hold, artifacts are equivalent under P

**Example:**
```
Precondition: n >= 0
Postcondition: result = sum of squares from 1 to n

Prove both implementations satisfy this specification
```

### Weakest Precondition Calculus

Work backwards from postconditions:
- Compute wp(Artifact_A, Q) and wp(Artifact_B, Q)
- If weakest preconditions are logically equivalent, artifacts are equivalent

### Invariant-Based Reasoning

For loops and recursive functions:
1. Identify loop/recursion invariants
2. Prove invariants hold for both artifacts
3. Show invariants imply equivalence

## SMT-Based Verification

### Using Z3 Solver

Encode programs as logical formulas:

```python
from z3 import *

# Define symbolic variables
x = Int('x')
n = Int('n')

# Encode Artifact A
result_A = Sum([i*i for i in range(1, n+1)])

# Encode Artifact B
result_B = n * (n + 1) * (2*n + 1) / 6

# Check equivalence
solver = Solver()
solver.add(result_A != result_B)
solver.add(n >= 0)

if solver.check() == unsat:
    print("Equivalent for n >= 0")
else:
    print("Counterexample:", solver.model())
```

### Bounded Model Checking

For finite state spaces:
- Unroll loops up to bound k
- Check equivalence for all paths up to depth k
- Increase k until confidence threshold met

## Translation Validation

Verify compiler optimizations and transformations:

1. **Source-to-source**: Compare original and refactored code
2. **Compiler correctness**: Verify compiled code matches source semantics
3. **Optimization validation**: Prove optimizations preserve behavior

**Workflow:**
- Generate verification conditions (VCs) from both artifacts
- Use automated theorem provers to discharge VCs
- Report any VCs that cannot be proven

## Denotational Semantics

Define mathematical meaning of programs:

1. Map each construct to mathematical function
2. Compute denotation [[Artifact_A]] and [[Artifact_B]]
3. Prove [[Artifact_A]] = [[Artifact_B]]

**Useful for:**
- Functional programs
- Expression-level equivalence
- Mathematical transformations

## Relational Verification

Prove properties relating two programs:

**Product Programs:**
- Construct product program executing both artifacts in lockstep
- Prove output relation holds at end
- More efficient than separate verification

**Coupling Invariants:**
- Define relation between states of both artifacts
- Prove relation maintained throughout execution
- Show relation implies output equivalence

## Proof Assistants

### Coq

Interactive theorem proving:
```coq
Theorem equivalence : forall n,
  sum_squares_A n = sum_squares_B n.
Proof.
  induction n.
  - reflexivity.
  - simpl. rewrite IHn. ring.
Qed.
```

### Isabelle/HOL

Higher-order logic verification:
```isabelle
lemma "sum_squares_A n = sum_squares_B n"
  apply (induction n)
  apply auto
  done
```

## Practical Guidelines

**When to use formal verification:**
- Safety-critical systems (medical, aerospace)
- Security-sensitive code (cryptography, authentication)
- Complex algorithms with subtle correctness properties
- Refactoring with high risk of introducing bugs

**Limitations:**
- Requires formal specifications
- Can be time-intensive
- May need expert knowledge
- Not all properties are decidable

**Best practices:**
- Start with automated tools (SMT solvers)
- Use interactive provers for complex cases
- Combine with testing for comprehensive validation
- Document assumptions and proof obligations

