The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V32. Logic, model theory and computability

Mathlib.Computability.Ackermann

52 declarations. Mathlib docs · Source

Ackermann function

In this file, we define the two-argument Ackermann function ack. Despite having a recursive definition, we show that this isn't a primitive recursive function.

Main results

  • exists_lt_ack_of_nat_primrec: any primitive recursive function is pointwise bounded above by ack m for some m.
  • not_primrec₂_ack: the two-argument Ackermann function is not primitive recursive.
  • computable₂_ack: the two-argument Ackermann function is computable.

Proof approach

We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive f : ℕ → ℕ, there exists m such that f n < ack m n for all n. This then implies that fun n => ack n n can't be primitive recursive, and so neither can ack. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions.

The important bounds we show during the main inductive proof (exists_lt_ack_of_nat_primrec) are the following. Assuming ∀ n, f n < ack a n and ∀ n, g n < ack b n, we have:

  • ∀ n, pair (f n) (g n) < ack (max a b + 3) n.
  • ∀ n, g (f n) < ack (max a b + 2) n.
  • ∀ n, Nat.rec (f n.unpair.1) (fun (y IH : ℕ) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n.

The last one is evidently the hardest. Using unpair_add_le, we reduce it to the more manageable

  • ∀ m n, rec (f m) (fun (y IH : ℕ) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n).

We then prove this by induction on n. Our proof crucially depends on ack_pair_lt, which is applied twice, giving us a constant of 4 + 4. The rest of the proof consists of simpler bounds which bump up our constant to 9.

theorem ack.match_1.congr_eq_3 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (n : ℕ) → motive 0 n) (h_2 : (m : ℕ) → motive m.succ 0)
  (h_3 : (m n : ℕ) → motive m.succ n.succ) (m n : ℕ),
  x = m.succ →
    x_1 = n.succ →
      (match x, x_1 with
        | 0, n => h_1 n
        | m.succ, 0 => h_2 m
        | m.succ, n.succ => h_3 m n) ≍
        h_3 m n

theorem Nat.Partrec.Code.pappAck.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ) (n : ℕ),
  x = n.succ →
    (match x with
      | 0 => h_1 ()
      | n.succ => h_2 n) ≍
      h_2 n

theorem ack.induct E1 #

∀ (motive : ℕ → ℕ → Prop),
  (∀ (n : ℕ), motive 0 n) →
    (∀ (m : ℕ), motive m 1 → motive m.succ 0) →
      (∀ (m n : ℕ), motive (m + 1) n → motive m (ack (m + 1) n) → motive m.succ n.succ) → ∀ (a a_1 : ℕ), motive a a_1

theorem ack.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (n : ℕ) → motive 0 n) (h_2 : (m : ℕ) → motive m.succ 0)
  (h_3 : (m n : ℕ) → motive m.succ n.succ) (n : ℕ),
  x = 0 →
    x_1 = n →
      (match x, x_1 with
        | 0, n => h_1 n
        | m.succ, 0 => h_2 m
        | m.succ, n.succ => h_3 m n) ≍
        h_1 n

theorem ack.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (n : ℕ) → motive 0 n) (h_2 : (m : ℕ) → motive m.succ 0)
  (h_3 : (m n : ℕ) → motive m.succ n.succ) (m : ℕ),
  x = m.succ →
    x_1 = 0 →
      (match x, x_1 with
        | 0, n => h_1 n
        | m.succ, 0 => h_2 m
        | m.succ, n.succ => h_3 m n) ≍
        h_2 m

theorem Nat.Partrec.Code.pappAck.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ),
  x = 0 →
    (match x with
      | 0 => h_1 ()
      | n.succ => h_2 n) ≍
      h_1 ()

def ack E3 #

ℕ → ℕ → ℕ

The two-argument Ackermann function, defined so that

  • ack 0 n = n + 1
  • ack (m + 1) 0 = ack m 1
  • ack (m + 1) (n + 1) = ack m (ack (m + 1) n).

This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive.

theorem ack_zero E0 #

∀ (n : ℕ), ack 0 n = n + 1

theorem ack_succ_zero E0 #

∀ (m : ℕ), ack (m + 1) 0 = ack m 1

theorem ack_succ_succ E0 #

∀ (m n : ℕ), ack (m + 1) (n + 1) = ack m (ack (m + 1) n)

theorem ack_one E0 #

∀ (n : ℕ), ack 1 n = n + 2

theorem ack_two E0 #

∀ (n : ℕ), ack 2 n = 2 * n + 3

theorem ack_three E0 #

∀ (n : ℕ), ack 3 n = 2 ^ (n + 3) - 3

theorem ack_pos E1 #

∀ (m n : ℕ), 0 < ack m n

theorem one_lt_ack_succ_left E1 #

∀ (m n : ℕ), 1 < ack (m + 1) n

theorem one_lt_ack_succ_right E1 #

∀ (m n : ℕ), 1 < ack m (n + 1)

theorem ack_strictMono_right E1 #

∀ (m : ℕ), StrictMono (ack m)

theorem ack_mono_right E1 #

∀ (m : ℕ), Monotone (ack m)

theorem ack_injective_right E1 #

∀ (m : ℕ), Function.Injective (ack m)

theorem ack_lt_iff_right E0 #

∀ {m n₁ n₂ : ℕ}, ack m n₁ < ack m n₂ ↔ n₁ < n₂

theorem ack_le_iff_right E0 #

∀ {m n₁ n₂ : ℕ}, ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂

theorem ack_inj_right E0 #

∀ {m n₁ n₂ : ℕ}, ack m n₁ = ack m n₂ ↔ n₁ = n₂

theorem max_ack_right E1 #

∀ (m n₁ n₂ : ℕ), ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂)

theorem add_lt_ack E1 #

∀ (m n : ℕ), m + n < ack m n

theorem add_add_one_le_ack E1 #

∀ (m n : ℕ), m + n + 1 ≤ ack m n

theorem lt_ack_left E1 #

∀ (m n : ℕ), m < ack m n

theorem lt_ack_right E1 #

∀ (m n : ℕ), n < ack m n

theorem ack_strictMono_left E1 #

∀ (n : ℕ), StrictMono fun m ↦ ack m n

theorem ack_mono_left E1 #

∀ (n : ℕ), Monotone fun m ↦ ack m n

theorem ack_injective_left E1 #

∀ (n : ℕ), Function.Injective fun m ↦ ack m n

theorem ack_lt_iff_left E0 #

∀ {m₁ m₂ n : ℕ}, ack m₁ n < ack m₂ n ↔ m₁ < m₂

theorem ack_le_iff_left E0 #

∀ {m₁ m₂ n : ℕ}, ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂

theorem ack_inj_left E0 #

∀ {m₁ m₂ n : ℕ}, ack m₁ n = ack m₂ n ↔ m₁ = m₂

theorem max_ack_left E1 #

∀ (m₁ m₂ n : ℕ), ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n)

theorem ack_le_ack E1 #

∀ {m₁ m₂ n₁ n₂ : ℕ}, m₁ ≤ m₂ → n₁ ≤ n₂ → ack m₁ n₁ ≤ ack m₂ n₂

theorem ack_succ_right_le_ack_succ_left E1 #

∀ (m n : ℕ), ack m (n + 1) ≤ ack (m + 1) n

theorem ack_add_one_sq_lt_ack_add_three E1 #

∀ (m n : ℕ), (ack m n + 1) ^ 2 ≤ ack (m + 3) n

theorem ack_ack_lt_ack_max_add_two E1 #

∀ (m n k : ℕ), ack m (ack n k) < ack (max m n + 2) k

theorem ack_add_one_sq_lt_ack_add_four E1 #

∀ (m n : ℕ), ack m ((n + 1) ^ 2) < ack (m + 4) n

theorem ack_pair_lt E1 #

∀ (m n k : ℕ), ack m (Nat.pair n k) < ack (m + 4) (max n k)

theorem exists_lt_ack_of_nat_primrec E3 #

∀ {f : ℕ → ℕ}, Nat.Primrec f → ∃ m, ∀ (n : ℕ), f n < ack m n

If f is primitive recursive, there exists m such that f n < ack m n for all n.

theorem not_nat_primrec_ack_self E1 #

¬Nat.Primrec fun n ↦ ack n n

theorem not_primrec_ack_self E1 #

¬Primrec fun n ↦ ack n n

theorem not_primrec₂_ack E3 #

¬Primrec₂ ack

The Ackermann function is not primitive recursive.

def Nat.Partrec.Code.pappAck E1 #

ℕ → Nat.Partrec.Code

The code for the partially applied Ackermann function. This is used to prove that the Ackermann function is computable.

def Nat.Partrec.Code.pappAck.step E1 #

Nat.Partrec.Code → Nat.Partrec.Code

Yields single recursion step on pappAck.

theorem Nat.Partrec.Code.primrec_pappAck_step E1 #

Primrec Nat.Partrec.Code.pappAck.step

theorem Nat.Partrec.Code.eval_pappAck_step_zero E0 #

∀ (c : Nat.Partrec.Code), (Nat.Partrec.Code.pappAck.step c).eval 0 = c.eval 1

theorem Nat.Partrec.Code.eval_pappAck_step_succ E0 #

∀ (c : Nat.Partrec.Code) (n : ℕ),
  (Nat.Partrec.Code.pappAck.step c).eval (n + 1) = ((Nat.Partrec.Code.pappAck.step c).eval n).bind c.eval

theorem Nat.Partrec.Code.primrec_pappAck E1 #

Primrec Nat.Partrec.Code.pappAck

theorem Nat.Partrec.Code.eval_pappAck E0 #

∀ (m n : ℕ), (Nat.Partrec.Code.pappAck m).eval n = Part.some (ack m n)

theorem computable₂_ack E3 #

Computable₂ ack

The Ackermann function is computable.