# Chapter 20: Axioms and @[simp]

Two mechanisms let you build large proof libraries efficiently: **axioms** for foundational truths that cannot (or need not) be proved from first principles, and **@[simp]** attributes that teach the `simp` tactic new rewriting rules automatically.

---

## Axioms

An `axiom` is a theorem with no proof body. The compiler trusts it without verification.

```first
// An axiom: trusted statement, no proof required
export axiom add_comm(a: Real, b: Real) -> Eq(add(a, b), add(b, a));
```

The compiler treats it exactly like a proved theorem: callers can use it in `exact`, `apply`, and `simp`. The difference is that it emits a compiler warning:

```
warning: axiom 'add_comm' is trusted without proof
```

### When to use axioms

**Use axioms for:**
- Classic mathematical truths from outside First's proof system (the axioms of the real numbers, Zorn's lemma, axiom of choice)
- Propositions that *could* be proved but whose proof would be extremely long and add no insight
- Stubs for theorems you plan to prove later (like `sorry`, but permanent)

**Do not use axioms for:**
- Statements that are false — a false axiom lets you prove `Eq(0, 1)` and from there anything
- Duplicating what `ring` or `omega` can already prove — this wastes an axiom budget and obscures the proof structure

### How axioms work at runtime

An `axiom` compiles to the same code as a proved theorem: a function that returns `i64 1`. The proof is fully erased. No runtime cost.

```first
// What the compiler sees
export axiom real_add_comm(a: Real, b: Real) -> Eq(add(a, b), add(b, a));

// What LLVM emits
define i64 @real_add_comm(...) { ret i64 1 }
```

---

## A realistic axiom set: Real.first

The standard library's `Real.first` is built entirely on axioms — there is no computational definition of `Real` that the compiler could reduce. Here is a subset:

```first
module Real;
export type Real = __MkReal(Float);

// Field axioms
export axiom add_comm(a: Real, b: Real) -> Eq(add(a, b), add(b, a));
export axiom add_assoc(a: Real, b: Real, c: Real) ->
    Eq(add(add(a, b), c), add(a, add(b, c)));
export axiom add_zero(a: Real) -> Eq(add(a, zero()), a);
export axiom mul_comm(a: Real, b: Real) -> Eq(mul(a, b), mul(b, a));
export axiom left_distrib(a: Real, b: Real, c: Real) ->
    Eq(mul(a, add(b, c)), add(mul(a, b), mul(a, c)));

// Order axioms
export axiom le_refl(a: Real) -> Eq(le(a, a), true);
export axiom le_antisymm(a: Real, b: Real,
    h1: Eq(le(a, b), true), h2: Eq(le(b, a), true)) -> Eq(a, b);
```

Each axiom represents a standard mathematical fact. Users of `Real` can apply these by name in their proofs.

---

## @[simp] — teaching the simp tactic

The `@[simp]` attribute marks a theorem (or axiom) as a **simp lemma**. When the `simp` tactic runs, it automatically applies all simp lemmas as rewriting rules.

```first
@[simp]
export theorem add_zero(n: Int) -> Eq(n + 0, n) by { ring; }

@[simp]
export theorem mul_one(n: Int) -> Eq(n * 1, n) by { ring; }
```

With these registered, `simp` can automatically simplify any expression of the form `x + 0` or `y * 1`:

```first
theorem example(n: Int) -> Eq((n + 0) * 1, n) by {
    simp;   -- applies add_zero and mul_one automatically
}
```

### Attribute syntax

```first
// Single attribute
@[simp]
export theorem my_lemma(n: Int) -> Eq(n + 0, n) by { ring; }

// Multiple attributes
@[simp, norm]
export theorem my_lemma2(n: Int) -> Eq(n * 0, 0) by { ring; }
```

Attributes appear on the line immediately before the `export` / `theorem` declaration.

### @[simp] on axioms

You can mark axioms as simp lemmas too:

```first
@[simp]
export axiom real_add_zero(a: Real) -> Eq(add(a, zero()), a);
```

Now `simp` over Real expressions will automatically cancel `+ 0`.

---

## Designing good simp lemmas

A simp lemma should **simplify** the left-hand side — make it smaller or more canonical. The rule of thumb: the left-hand side should not appear on the right-hand side.

**Good simp lemmas:**

```first
@[simp] theorem add_zero(n: Int) -> Eq(n + 0, n) by { ring; }       -- removes +0
@[simp] theorem mul_one(n: Int)  -> Eq(n * 1, n) by { ring; }       -- removes *1
@[simp] theorem not_not(b: Bool) -> Eq(!(!b), b) by { decide; }     -- removes double neg
@[simp] theorem and_self(b: Bool) -> Eq(b && b, b) by { decide; }   -- idempotent
```

**Problematic simp lemmas:**

```first
@[simp] theorem add_comm(a: Int, b: Int) -> Eq(a + b, b + a) by { ring; }
-- BAD: simp will flip a+b to b+a and then flip it back — infinite loop!
```

Commutativity is never a good simp lemma. Use it only via `rw` or `ring`.

---

## @[simp] scope

Simp lemmas registered in an imported module become available in the simp database when you `import` that module. They do not leak into other unrelated modules.

```first
import "Int";   -- imports Int's @[simp] lemmas into this module's simp db

theorem using_int_simp(n: Int) -> Eq(n + 0, n) by {
    simp;   -- uses Int's add_zero simp lemma
}
```

---

## sorry — development scaffolding

`sorry` closes any goal unconditionally. It is valuable during development but must never appear in finished proofs:

```first
// Valid at compile time, but emits:
// warning: proof uses sorry — theorem 'hard_theorem' is unverified
theorem hard_theorem(n: Int) -> Eq(n * n + 1 >= 1, true) by {
    sorry;
}
```

### sorry vs axiom

| | `sorry` | `axiom` |
|-|---------|---------|
| Has proof body | Yes (`by { sorry; }`) | No |
| Proof checked | No (sorry bypasses it) | N/A |
| Caller use | Like any proved theorem | Like any proved theorem |
| Warning | "proof uses sorry" | "is trusted without proof" |
| Semantic intent | Placeholder (fix later) | Permanent trusted fact |

Use `sorry` while drafting a proof. Replace it with a real tactic proof or promote to `axiom` once you've decided the statement is a trusted foundation.

---

## Combining axioms, simp, and sorry

Here is a workflow for a new library module:

**Step 1** — Sketch the interface with axioms:

```first
module MyField;
export type F = ...;

export axiom add_comm(a: F, b: F) -> Eq(a + b, b + a);
export axiom mul_zero(a: F) -> Eq(a * zero(), zero());
```

**Step 2** — Add `@[simp]` to normalization lemmas:

```first
@[simp]
export axiom add_zero(a: F) -> Eq(a + zero(), a);
```

**Step 3** — Prove higher-level theorems using the axioms:

```first
export theorem sub_self(a: F) -> Eq(a - a, zero()) by {
    simp [sub_def, add_neg];
}
```

**Step 4** — Use `sorry` for theorems not yet proved:

```first
export theorem div_self(a: F, h: Eq(a != zero(), true)) ->
    Eq(a / a, one()) by {
    sorry;
}
```

---

---

## User-defined tactics

The `tactic` declaration lets you give a name to a reusable tactic sequence. This is how you extend the tactic language without modifying the compiler.

### Declaring a tactic

```first
tactic auto_arith {
    first { ring; } | { first { omega; } | { linarith; }; };
}
```

Use it exactly like a built-in tactic:

```first
theorem example(a: Int, b: Int) -> Eq(a * b + a, a * (b + 1)) by {
    auto_arith;
}
```

### Exporting tactics

Add `export` to make a tactic available to importers of the module:

```first
module MyTactics;

export tactic norm_close {
    first { norm_num; } | { first { ring; } | { omega; }; };
}

export tactic simp_ring {
    simp;
    ring;
}
```

Importers use them like any other tactic:

```first
import "MyTactics";

theorem sum_eq(a: Int) -> Eq(a + 1 - 1, a) by {
    simp_ring;
}
```

### lib/Tactics.first — standard tactic library

The `lib/Tactics.first` module ships with the compiler and provides ready-made automation tactics:

| Tactic | What it tries |
|--------|---------------|
| `auto_arith` | `ring` → `omega` → `linarith` |
| `auto` | `ring` → `omega` → `simp` → `decide` → `refl` |
| `norm_close` | `norm_num` → `ring` → `omega` |
| `simp_ring` | `simp` then `ring` |
| `prop_auto` | `decide` → `tauto` → `refl` |
| `full_auto` | `ring` → `omega` → `simp` → `decide` → `positivity` → `refl` → `tauto` |

```first
import "Tactics";

theorem example1(a: Int, b: Int) -> Eq(a + b, b + a) by { auto; }
theorem example2(a: Int) -> Eq(a * a >= 0, true) by { full_auto; }
theorem example3(P: Bool) -> Eq(P || !P, true) by { prop_auto; }
```

---

## Summary

- `axiom` — trusted proposition without proof; emits a compiler warning; compiles to `i64 1`
- Use axioms for mathematical foundations (field axioms, order axioms) or permanent trusted stubs
- `@[simp]` — registers a theorem/axiom as a simp lemma; `simp` applies it automatically
- Good simp lemmas simplify left → right; never mark commutativity as `@[simp]`
- `sorry` — placeholder proof; emits warning; use during development, not in finished proofs
- `tactic Name { body; }` — define a reusable tactic sequence; `export` to share via modules
- `lib/Tactics.first` — standard automation tactics (`auto`, `full_auto`, `simp_ring`, etc.)

Chapter 21 tours the First standard mathematics library — the axioms and lemmas that ship with the compiler.
