# Chapter 17: Basic Tactics

A **tactic** is a proof-building command inside a `by { ... }` block. Each tactic transforms the current **proof goal** — the proposition you need to prove — into zero or more simpler subgoals. When no goals remain, the proof is complete.

This chapter covers Tiers 1–3: the tactics you will use in the vast majority of everyday proofs.

---

## Reading the proof state

When you write a `by { ... }` block, the initial goal is the return type of the theorem:

```first
theorem add_comm_int(a: Int, b: Int) -> Eq(a + b, b + a) by {
    -- goal: Eq(a + b, b + a)
    ring;
    -- no goals remaining — proof complete
}
```

Hypotheses available in scope (parameters) are listed above the goal. The tactic manipulates both.

---

## Tier 1: Trivial tactics

### `refl` / `rfl`

Proves `Eq(a, a)` — equality by reflexivity. Works when both sides are definitionally equal (reduce to the same value).

```first
theorem lit_eq() -> Eq(2 + 3, 5) by { refl; }
theorem str_eq() -> Eq("hi", "hi") by { rfl; }   // rfl is an alias for refl
```

### `decide`

Closes a goal that is a **decidable proposition** — one the compiler can evaluate at compile time by enumeration or computation.

```first
theorem and_comm_bool(a: Bool, b: Bool) -> Eq(a && b, b && a) by {
    decide;
}

theorem not_not(a: Bool) -> Eq(!(!a), a) by {
    decide;
}
```

`decide` works for any proposition over `Bool`, `Nat` with small bounds, `Fin<n>`, and other enumerable types. It enumerates all possible values and checks the equality holds for each.

### `trivial`

Closes any goal that is obviously true — identical to `refl` for equalities, or `True` for the trivial proposition.

```first
theorem true_holds() -> Eq(true, true) by { trivial; }
```

---

## Tier 2: Arithmetic tactics

### `ring`

Proves polynomial ring identities over `Int`, `Real`, `Rat`, or any type with ring operations. Use it for any goal that is an algebraic identity.

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

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

theorem add_zero_right(n: Int) -> Eq(n + 0, n) by { ring; }
theorem mul_comm(a: Int, b: Int) -> Eq(a * b, b * a) by { ring; }
```

`ring` handles: `+`, `-`, `*`, integer constants, variables, and nesting thereof. It does **not** handle division, square roots, or transcendental functions.

### `omega`

Proves linear arithmetic goals over `Int` and `Nat`. It handles `+`, `-`, integer constants, `<=`, `<`, `>=`, `>`, `==`, and `!=`.

```first
theorem n_le_succ(n: Int) -> Eq(n <= n + 1, true) by { omega; }
theorem add_le_cancel(a: Int, b: Int, c: Int, h: Eq(a + c <= b + c, true)) ->
    Eq(a <= b, true) by {
    omega;
}
```

If `ring` doesn't work (the goal is an inequality, not an equality), try `omega` first.

### `linarith`

Like `omega` but over `Real` and `Float` — handles linear arithmetic with real-valued constants. Use when `omega` can't handle the type.

```first
theorem real_pos(x: Real, h: Eq(x > 0.0, true)) -> Eq(x + 1.0 > 0.0, true) by {
    linarith;
}
```

### `norm_num`

Evaluates and simplifies **numeric literals**. Proves goals like `2 * 3 == 6`, `7 / 2 == 3`, `Nat.succ 4 = 5`.

```first
theorem six_is_even() -> Eq(6 % 2, 0) by { norm_num; }
theorem pow_two() -> Eq(2 * 2 * 2, 8) by { norm_num; }
```

### `norm_cast`

Normalises **numeric cast expressions** — pushes coercions outward and cancels redundant casts so that `ring`, `omega`, and `linarith` can close the goal.

```first
theorem cast_add(a: Int, b: Int) -> Eq(toFloat(a + b), toFloat(a) + toFloat(b)) by {
    norm_cast;
}

theorem cast_cancel(n: Int) -> Eq(toInt(toFloat(n)), n) by {
    norm_cast;
}
```

Use `norm_cast` as a preprocessing step before an arithmetic tactic when the goal mixes `Int`, `Float`, or `Nat`.

### `field_simp`

Clears **denominators** and simplifies field expressions. Use it whenever the goal contains division or when `simp` leaves fractions that `ring` can't handle directly.

```first
theorem div_cancel(a: Float, h: Eq(a != 0.0, true)) -> Eq(a / a, 1.0) by {
    field_simp;
}

theorem frac_add(a: Float, b: Float, c: Float, h: Eq(c != 0.0, true)) ->
    Eq(a / c + b / c, (a + b) / c) by {
    field_simp;
    ring;
}
```

The typical pattern is `field_simp; ring;` — `field_simp` removes the denominators, then `ring` closes the resulting polynomial identity.

### `tauto`

Decides **propositional tautologies** by exhaustive search over truth values. Closes any goal that is a tautology in classical propositional logic.

```first
theorem excluded_middle(P: Bool) -> Eq(P || !P, true) by {
    tauto;
}

theorem de_morgan(P: Bool, Q: Bool) ->
    Eq(!(P && Q), !P || !Q) by {
    tauto;
}

theorem contrapositive(P: Bool, Q: Bool, h: Eq(P, true) -> Eq(Q, true)) ->
    (Eq(Q, false) -> Eq(P, false)) by {
    tauto;
}
```

`tauto` is strictly more powerful than `decide` for propositional goals — it handles universally quantified `Bool` variables. Use `decide` when the domain is finite and enumerable; use `tauto` when the goal is a logical identity over `Bool` connectives.

---

## Tier 3: Automation tactics

### `simp`

The `simp` tactic applies a database of **simp lemmas** repeatedly until no more apply. By default, it uses all lemmas marked `@[simp]` in imported modules.

```first
import "Int";

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

You can also supply extra lemmas explicitly:

```first
theorem example2(a: Int, h: Eq(a, 5)) -> Eq(a + 1, 6) by {
    simp [h];   -- rewrites a → 5, then 5 + 1 = 6 by norm_num
}
```

`simp` is powerful but can loop on poorly-designed simp sets. If a proof hangs, switch to `ring` or `omega` instead.

### `simp only [lemmas]`

Apply only the named simp lemmas — no defaults. More predictable than bare `simp`.

```first
theorem targeted(a: Int) -> Eq(a + 0, a) by {
    simp only [add_zero];
}
```

### `exact h`

Close the goal with an exact proof term `h`. Use it when you already have the proof value in scope.

```first
theorem apply_hyp(a: Int, b: Int, h: Eq(a + b, b + a)) -> Eq(a + b, b + a) by {
    exact h;
}

theorem use_library(a: Int, b: Int) -> Eq(a + b, b + a) by {
    exact add_comm(a, b);   -- call the imported lemma directly
}
```

### `assumption`

Search the current hypotheses for one that exactly matches the goal.

```first
theorem by_assumption(a: Int, b: Int, h: Eq(a, b)) -> Eq(a, b) by {
    assumption;   -- finds h: Eq(a, b) in context
}
```

### `have`

Introduce a named intermediate fact.

```first
theorem chain(n: Int) -> Eq(n + 2, n + 1 + 1) by {
    have h1: Eq(1 + 1, 2) by { refl; }
    ring;
}
```

The `have` subproof creates `h1` in scope. You can then use `h1` in later tactics.

### `rw [h]`

Rewrite the goal using the equation `h: Eq(a, b)` — replace `a` with `b` everywhere in the goal.

```first
theorem rw_example(n: Int, h: Eq(n, 5)) -> Eq(n * 2, 10) by {
    rw [h];     -- goal becomes Eq(5 * 2, 10)
    norm_num;
}
```

Rewrite in reverse (replace `b` with `a`) using `rw [← h]` or `rw_rev [h]`.

---

## Tier 4: Proof automation

### `positivity`

Proves goals of the form `Eq(expr >= 0, true)` or `Eq(expr > 0, true)` automatically, by structural analysis of the expression — sums of non-negatives are non-negative, products of non-negatives are non-negative, squares are non-negative, etc.

```first
theorem sq_nonneg(a: Int) -> Eq(a * a >= 0, true) by {
    positivity;
}

theorem sum_sq_nonneg(a: Int, b: Int) -> Eq(a * a + b * b >= 0, true) by {
    positivity;
}

theorem abs_nonneg(a: Int) -> Eq(a * a >= 0, true) by {
    positivity;
}
```

`positivity` is deterministic and fast. It fails if it cannot syntactically determine non-negativity; fall back to `linarith` with hypotheses in that case.

### `aesop`

A general-purpose **proof search** tactic. It tries a battery of tactics (`ring`, `omega`, `simp`, `decide`, `refl`, `assumption`) in sequence and closes the goal if any of them succeed.

```first
theorem simple_eq(a: Int) -> Eq(a + 0, a) by { aesop; }
theorem bool_taut(b: Bool) -> Eq(b || !b, true) by { aesop; }
theorem refl_goal() -> Eq(42, 42) by { aesop; }
```

`aesop` is a good "try everything" tactic for goals you expect to be easy. For large proofs, prefer specific tactics — `aesop`'s search can be slow on complex goals. If it fails, the error message tells you nothing about *why*, so switch to targeted tactics immediately.

---

## Combining tactics

Most theorems need a small sequence of tactics:

```first
theorem example(a: Int, b: Int, h: Eq(a, 3)) -> Eq((a + b) * (a - b), 9 - b * b) by {
    rw [h];        -- replace a with 3: goal becomes Eq((3 + b) * (3 - b), 9 - b * b)
    ring;          -- ring closes it
}
```

```first
theorem triangle(a: Int, b: Int, c: Int,
    hab: Eq(a <= b, true), hbc: Eq(b <= c, true)) ->
    Eq(a <= c, true) by {
    have hac: Eq(a <= c, true) by { omega; }
    exact hac;
}
```

---

## Quick reference

| Tactic | Proves | Notes |
|--------|--------|-------|
| `refl` / `rfl` | `Eq(a, a)` after reduction | Definitional equality |
| `decide` | Any decidable prop | Enumerates finite types |
| `trivial` | Trivially true props | Alias for `refl` on simple goals |
| `ring` | Polynomial ring identities | +, -, * over Int/Real/Rat |
| `omega` | Linear Int/Nat arithmetic | Handles inequalities |
| `linarith` | Linear Real arithmetic | Like omega for Float/Real |
| `norm_num` | Numeric literal goals | Evaluates constants |
| `norm_cast` | Goals mixing numeric casts | Normalises Int/Float/Nat coercions |
| `field_simp` | Goals with division/fractions | Clears denominators; follow with `ring` |
| `tauto` | Propositional tautologies | Classical logic over Bool |
| `simp` | Rewriting with simp database | Powerful but can loop |
| `simp only [h]` | Rewriting with named lemmas | Predictable, no loops |
| `exact h` | Exact match to a proof term | Use imported theorems |
| `assumption` | Match a hypothesis to goal | Search local context |
| `have x : P by { ... }` | Introduce intermediate step | Named lemma in context |
| `rw [h]` | Rewrite goal using `h: Eq(a,b)` | Replaces `a` → `b` |
| `positivity` | `expr >= 0` or `expr > 0` | Structural non-negativity analysis |
| `aesop` | Easy goals via proof search | Tries ring/omega/simp/decide in sequence |

---

## Summary

- Tier 1 (`refl`, `decide`, `trivial`) — close easy goals by computation
- Tier 2 (`ring`, `omega`, `linarith`, `norm_num`, `norm_cast`, `field_simp`, `tauto`) — close arithmetic and logical goals
- Tier 3 (`simp`, `exact`, `have`, `rw`) — structured reasoning
- Tier 4 (`positivity`, `aesop`) — automated proof search

Chapter 18 covers structural tactics (`induction`, `cases`, `calc`), classical-logic tactics, and tactic combinators (`try`, `repeat`, `first`).
