# Chapter 22: Type Classes in Proofs

Type classes in First let you write **generic theorems** that hold for any type satisfying a given algebraic contract. This chapter covers how the `class`/`instance`/`[Constraint]` system works and how to use `lib/Algebra.first` — the implemented algebraic hierarchy — in your own proofs.

---

## Syntax recap

You declare a type class with method signatures, provide instances for concrete types, and constrain generic functions with `[ClassName<T>]`:

```first
class Show<A> {
    function show(A) -> String;
}

instance Show<Int> {
    function show(x: Int) -> String {
        return intToString(x);
    }
}

instance Show<Bool> {
    function show(x: Bool) -> String {
        return if (x) { "true" } else { "false" };
    }
}

// Constrained generic function
function display<A>[Show<A>](x: A) -> String {
    return show(x);
}
```

Dispatch is resolved at compile time. The instance method `show` for `Show<Int>` compiles to IR name `__inst_Show_Int_show`; the call in `display` is monomorphised to the right mangled name.

---

## Multiple methods and multiple instances

A class can declare several methods; each instance must implement all of them:

```first
class Addable<A> {
    function zero() -> A;
    function add(A, A) -> A;
}

instance Addable<Int> {
    function zero() -> Int { return 0; }
    function add(x: Int, y: Int) -> Int { return x + y; }
}

instance Addable<Float> {
    function zero() -> Float { return 0.0; }
    function add(x: Float, y: Float) -> Float { return x + y; }
}

function sum<A>[Addable<A>](a: A, b: A) -> A {
    return add(a, b);
}
```

---

## Classes with theorem-shaped methods

A class method can be **theorem-typed** — its signature returns a proposition. This lets you encode algebraic laws into the class contract itself:

```first
class AddCommMonoid<T> {
    function zero() -> T;
    function add(T, T) -> T;
    function add_comm(a: T, b: T) -> Eq(add(a, b), add(b, a));
    function add_assoc(a: T, b: T, c: T) ->
        Eq(add(add(a, b), c), add(a, add(b, c)));
}
```

An instance provides concrete proofs for those methods:

```first
instance AddCommMonoid<Int> {
    function zero() -> Int { return 0; }
    function add(a: Int, b: Int) -> Int { return a + b; }
    function add_comm(a: Int, b: Int) -> Eq(a + b, b + a) by { ring; }
    function add_assoc(a: Int, b: Int, c: Int) -> Eq((a+b)+c, a+(b+c)) by { ring; }
}
```

---

## The Algebra.first hierarchy

`lib/Algebra.first` provides a flat algebraic hierarchy. Classes are **independent** — there is no `extends` keyword in First. Each class declares all its methods explicitly, and each instance implements them from scratch. This design keeps the type checker simple while covering all standard algebraic structures.

### Classes

```first
import "Algebra";

// Additive structures
class AddMonoid<T> {
    function zero() -> T;
    function add(T, T) -> T;
}

class AddGroup<T> {
    function zero() -> T;
    function add(T, T) -> T;
    function neg(T) -> T;
}

// Ring and Field
class Ring<T> {
    function zero() -> T;
    function one() -> T;
    function add(T, T) -> T;
    function neg(T) -> T;
    function mul(T, T) -> T;
}

class Field<T> {
    function zero() -> T;
    function one() -> T;
    function add(T, T) -> T;
    function neg(T) -> T;
    function mul(T, T) -> T;
    function inv(T) -> T;
}

// Ordered structures
class Ord<T> {
    function leq(T, T) -> Bool;
}

class OrderedRing<T> {   // Ring methods + leq
    function zero() -> T;
    function one() -> T;
    function add(T, T) -> T;
    function neg(T) -> T;
    function mul(T, T) -> T;
    function leq(T, T) -> Bool;
}
```

### Instances provided

| Class | Int | Float |
|-------|-----|-------|
| `AddMonoid<T>` | ✅ | ✅ |
| `AddGroup<T>` | ✅ | — |
| `Ring<T>` | ✅ | ✅ |
| `Field<T>` | — | ✅ |
| `Ord<T>` | ✅ | ✅ |
| `OrderedRing<T>` | ✅ | — |

### Key axioms (Int)

```first
export axiom ring_add_comm(a: Int, b: Int) -> Eq(add(a, b), add(b, a))
export axiom ring_mul_comm(a: Int, b: Int) -> Eq(mul(a, b), mul(b, a))
export axiom ring_mul_one(a: Int) -> Eq(mul(a, one()), a)
export axiom ring_add_neg(a: Int) -> Eq(add(a, neg(a)), zero())
export axiom ring_left_distrib(a: Int, b: Int, c: Int) ->
    Eq(mul(a, add(b, c)), add(mul(a, b), mul(a, c)))
export axiom field_mul_inv_float(a: Float) -> Eq(mul(a, inv(a)), one())
export axiom ord_refl_int(a: Int) -> Eq(leq(a, a), true)
```

---

## Writing generic theorems with constraints

```first
import "Algebra";

// Generic function: works for Int and Float (both have Ring instances)
function ringDouble<T>[Ring<T>](a: T) -> T {
    return add(a, a);
}

// Generic theorem: add is commutative over any Ring<Int>
theorem int_ring_comm(a: Int, b: Int) -> Eq(add(a, b), add(b, a)) by {
    exact int_add_comm(a, b);
}

// Use the Field instance for Float
function reciprocal(x: Float) -> Float {
    return inv(x);
}
```

---

## What works now (234/234 tests)

| Feature | Status |
|---------|--------|
| `class Name<T> { function sig; }` | ✅ Working |
| `instance Name<ConcreteType> { function impl; }` | ✅ Working |
| `[Constraint<T>]` on generic functions | ✅ Working |
| Theorem-typed class methods with `by { ... }` bodies | ✅ Working |
| Generic theorems with `<T>[Constraint<T>]` syntax | ✅ Parsed and type-checked |
| `lib/Algebra.first` class hierarchy + Int/Float instances | ✅ Working |
| `lib/NumberTheory.first` GCD/primality | ✅ Working |
| `lib/Finset.first` finite integer sets | ✅ Working |
| User-defined tactics via `tactic Name { }` | ✅ Working |
| `lib/Tactics.first` standard tactic library | ✅ Working |

---

## Extending the hierarchy

To add a new algebraic structure, declare a class and provide instances:

```first
// A type with decidable equality
class DecEq<T> {
    function decEq(T, T) -> Bool;
    function decEq_refl(a: T) -> Eq(decEq(a, a), true);
}

instance DecEq<Int> {
    function decEq(a: Int, b: Int) -> Bool { return a == b; }
    function decEq_refl(a: Int) -> Eq(a == a, true) by { decide; }
}
```

Good contributions to the Algebra hierarchy:
- `instance Ring<Rat>` wired to `lib/Rat.first` axioms
- `instance Field<Rat>` wired to rational field laws
- `instance Ring<Real>` wired to `lib/Real.first` axioms
- `class DecEq<T>` with `Int`, `Bool`, `Float` instances
- Generic `GFinset<A>` using `DecEq<A>` for decidable membership

---

## Summary

- `class`/`instance`/`[Constraint<T>]` syntax is fully implemented and tested (234/234)
- Classes are **flat** — no `extends`; each class declares all its methods independently
- Class methods can be theorem-typed; instances provide `by { ... }` proofs
- `lib/Algebra.first` ships Ring/Field/Ord/OrderedRing with Int and Float instances
- Generic theorems constrain types with `<T>[ClassName<T>]` and use class method axioms

Chapter 23 covers contributing lemmas and class instances to **first-mathlib**.
