# Chapter 23: Contributing to first-mathlib

**first-mathlib** is the community mathematics library for First — analogous to Lean's Mathlib. It is a sibling repository at `github.com/first-lang/first-mathlib`. This chapter explains how to add new lemmas, use `axiom` stubs for work in progress, and submit pull requests.

---

## Repository layout

```
first-mathlib/
  fir.json             # depends on the first compiler; no other deps
  lib/
    Nat.first          # extended Nat lemmas beyond stdlib
    Int.first          # extended Int lemmas
    Real.first         # Real analysis lemmas
    Rat.first          # Rat lemmas
    Complex.first      # Complex analysis lemmas
    Bool.first         # extended Bool lemmas
    Set.first          # set theory
    List.first         # list combinatorics
    Algebra.first      # planned: Ring/Field/Ord class hierarchy
    NumberTheory.first # planned: primes, gcd, Euclid
  test/
    test_nat.first
    test_int.first
    ...
  LEMMAS.md            # tracking sheet: proved / stub / planned
```

---

## Installing first-mathlib in your project

```json
// fir.json
{
  "dependencies": {
    "first-mathlib": {
      "git": "https://github.com/first-lang/first-mathlib",
      "tag": "v0.1.0"
    }
  }
}
```

Then:

```bash
fir install
```

Import a module in your code or proofs:

```first
import "first-mathlib/Int";

theorem my_lemma(a: Int) -> Eq(a + 0, a) by {
    exact Int.add_zero(a);
}
```

---

## Finding a lemma to contribute

**LEMMAS.md** tracks the status of every planned lemma in three columns:

| Status | Meaning |
|--------|---------|
| ✅ proved | A `theorem` with a complete tactic proof |
| 🔧 stub | An `axiom` placeholder — proof needed |
| 📋 planned | Not yet written — up for grabs |

A good first contribution: pick any 🔧 stub and replace the `axiom` with a proved `theorem`.

---

## Adding a new lemma

### Step 1: State the lemma as an axiom

If you are not sure the proof is straightforward, state it as an axiom first:

```first
// In lib/Int.first
export axiom add_left_cancel(a: Int, b: Int, c: Int,
    h: Eq(a + b, a + c)) -> Eq(b, c);
```

This gets the statement into the library and lets others use it immediately (as a trusted fact) while the proof is worked out.

### Step 2: Replace the axiom with a theorem

Once you have a proof:

```first
export theorem add_left_cancel(a: Int, b: Int, c: Int,
    h: Eq(a + b, a + c)) -> Eq(b, c) by {
    have := h;
    omega;
}
```

The change is backwards-compatible: callers using `exact add_left_cancel(...)` see no difference.

### Step 3: Consider adding @[simp]

If your lemma is a normalisation rule (removes `+0`, `*1`, cancels a subtraction), mark it `@[simp]`:

```first
@[simp]
export theorem add_left_cancel(a: Int, b: Int, c: Int,
    h: Eq(a + b, a + c)) -> Eq(b, c) by { omega; }
```

---

## Proof strategies by domain

### Nat lemmas

Most `Nat` lemmas use `induction n`:

```first
theorem add_succ(m: Nat, n: Nat) ->
    Eq(add(m, Succ(n)), Succ(add(m, n))) by {
    induction m {
        case Zero   => { simp [add]; }
        case Succ(k) ih => {
            simp [add, ih];
        }
    }
}
```

### Int lemmas

Most `Int` lemmas close with `ring`, `omega`, or `linarith`:

```first
theorem sq_sub_sq(a: Int, b: Int) ->
    Eq(a*a - b*b, (a + b) * (a - b)) by {
    ring;
}
```

### Real lemmas

`Real` is axiomatic, so proofs reuse the field and order axioms via `exact` and `simp`:

```first
theorem real_sq_nonneg(a: Real) ->
    Eq(le(zero(), mul(a, a)), true) by {
    exact Real.sq_nonneg(a);
}
```

If the axiom doesn't exist yet, add it as an `axiom` stub first.

### Bool lemmas

All `Bool` lemmas use `decide`:

```first
theorem xor_comm(a: Bool, b: Bool) ->
    Eq(a != b, b != a) by {
    decide;
}
```

### List lemmas

Most `List` lemmas use `induction xs`:

```first
theorem filter_length<A>(xs: List<A>, p: A -> Bool) ->
    Eq(length(filter(xs, p)) <= length(xs), true) by {
    induction xs {
        case Nil => { decide; }
        case Cons(h, t) ih => {
            simp [filter, length];
            omega;
        }
    }
}
```

---

## Writing tests

Every new lemma needs a test in `test/`:

```first
// test/test_int.first
import "first-mathlib/Int";

theorem test_add_left_cancel() -> Eq(3, 3) by {
    have h: Eq(2 + 3, 2 + 3) by { refl; }
    exact add_left_cancel(2, 3, 3, h);
}
```

The test just needs to compile without warnings. If it uses `sorry`, it doesn't count.

---

## Submitting a pull request

1. Fork `github.com/first-lang/first-mathlib`
2. Create a branch: `git checkout -b lemma/int-add-left-cancel`
3. Add the lemma and its test; make sure `fir build` passes with no warnings
4. Update `LEMMAS.md`: change 🔧 stub → ✅ proved (or add a new ✅ row)
5. Open a PR with title `[Int] add_left_cancel: proved via omega`

PR checklist:
- [ ] Lemma is in the correct module file
- [ ] `export` on the theorem
- [ ] Test in `test/` that uses the lemma
- [ ] No `sorry` or `axiom` in the proof body
- [ ] `fir build` passes with no warnings
- [ ] LEMMAS.md updated

---

## Lemma count milestones

The goal is to cover the most-used Mathlib lemmas in each domain. Current progress:

| Module | Proved | Stubs | Planned |
|--------|--------|-------|---------|
| Nat | ~15 | 10 | 50 |
| Int | ~20 | 15 | 100 |
| Real | 0 | 25 | 200 |
| Rat | 0 | 11 | 50 |
| Complex | 0 | 20 | 50 |
| Bool | ~35 | 0 | 5 |
| Set | 0 | 30 | 100 |
| List | 0 | 15 | 100 |

The highest-leverage contribution is proving the `axiom` stubs in `Int` and `Real` — those are the building blocks everything else depends on.

---

## Philosophy

first-mathlib follows the same design philosophy as Lean's Mathlib:

- **One module, one topic.** Don't put List lemmas in the Int file.
- **Naming convention:** `type_operation_property` — e.g., `int_add_comm`, `list_length_append`.
- **@[simp] for normal forms.** A lemma that simplifies an expression should be `@[simp]`.
- **Never use sorry in a merged lemma.** Use `axiom` for stubs; `sorry` is a development tool.
- **Small lemmas over large.** A 50-line proof is usually 5 lemmas. Split it.

---

## Summary

- Pick a 🔧 stub in LEMMAS.md, replace `axiom` with `theorem + tactic proof`
- Domain guide: `Nat` → `induction`; `Int` → `ring`/`omega`; `Bool` → `decide`; `List` → `induction`
- Add a test, update LEMMAS.md, open a PR
- `@[simp]` for normalization lemmas; never merge `sorry`

This is the last chapter of Part II. For the full language reference, see `docs/First-Language-Specification.md`. To build your own library, revisit Chapter 15.
