state
stringlengths 0
159k
| srcUpToTactic
stringlengths 387
167k
| nextTactic
stringlengths 3
9k
| declUpToTactic
stringlengths 22
11.5k
| declId
stringlengths 38
95
| decl
stringlengths 16
1.89k
| file_tag
stringlengths 17
73
|
---|---|---|---|---|---|---|
m n : ℕ
⊢ 1 < ack (m + 1 + 1) (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
| rw [ack_succ_succ] | theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
| Mathlib.Computability.Ackermann.117_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ 1 < ack (m + 1) (ack (m + 1 + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
| apply one_lt_ack_succ_left | theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
| Mathlib.Computability.Ackermann.117_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left | Mathlib_Computability_Ackermann |
n : ℕ
⊢ 1 < ack 0 (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by | simp | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by | Mathlib.Computability.Ackermann.127_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ 1 < ack (m + 1) (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
| rw [ack_succ_succ] | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
| Mathlib.Computability.Ackermann.127_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ 1 < ack m (ack (m + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
| cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
| Mathlib.Computability.Ackermann.127_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right | Mathlib_Computability_Ackermann |
case intro
m n h✝ : ℕ
h : ack (m + 1) n = succ h✝
⊢ 1 < ack m (ack (m + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
| rw [h] | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
| Mathlib.Computability.Ackermann.127_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right | Mathlib_Computability_Ackermann |
case intro
m n h✝ : ℕ
h : ack (m + 1) n = succ h✝
⊢ 1 < ack m (succ h✝) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
| apply one_lt_ack_succ_right | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
| Mathlib.Computability.Ackermann.127_0.nk1BuTevlQj1gCN | theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right | Mathlib_Computability_Ackermann |
n₁ n₂ : ℕ
h : n₁ < n₂
⊢ ack 0 n₁ < ack 0 n₂ | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by | simpa using h | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by | Mathlib.Computability.Ackermann.136_0.nk1BuTevlQj1gCN | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x) | Mathlib_Computability_Ackermann |
m n : ℕ
_h : 0 < n + 1
⊢ ack (m + 1) 0 < ack (m + 1) (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
| rw [ack_succ_zero, ack_succ_succ] | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
| Mathlib.Computability.Ackermann.136_0.nk1BuTevlQj1gCN | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x) | Mathlib_Computability_Ackermann |
m n : ℕ
_h : 0 < n + 1
⊢ ack m 1 < ack m (ack (m + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
| exact ack_strictMono_right _ (one_lt_ack_succ_left m n) | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
| Mathlib.Computability.Ackermann.136_0.nk1BuTevlQj1gCN | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x) | Mathlib_Computability_Ackermann |
m n₁ n₂ : ℕ
h : n₁ + 1 < n₂ + 1
⊢ ack (m + 1) (n₁ + 1) < ack (m + 1) (n₂ + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
| rw [ack_succ_succ, ack_succ_succ] | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
| Mathlib.Computability.Ackermann.136_0.nk1BuTevlQj1gCN | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x) | Mathlib_Computability_Ackermann |
m n₁ n₂ : ℕ
h : n₁ + 1 < n₂ + 1
⊢ ack m (ack (m + 1) n₁) < ack m (ack (m + 1) n₂) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
| apply ack_strictMono_right _ (ack_strictMono_right _ _) | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
| Mathlib.Computability.Ackermann.136_0.nk1BuTevlQj1gCN | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x) | Mathlib_Computability_Ackermann |
m n₁ n₂ : ℕ
h : n₁ + 1 < n₂ + 1
⊢ n₁ < n₂ | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
| rwa [add_lt_add_iff_right] at h | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
| Mathlib.Computability.Ackermann.136_0.nk1BuTevlQj1gCN | theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x) | Mathlib_Computability_Ackermann |
n : ℕ
⊢ 0 + n < ack 0 n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by | simp | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by | Mathlib.Computability.Ackermann.175_0.nk1BuTevlQj1gCN | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) | Mathlib_Computability_Ackermann |
m : ℕ
⊢ m + 1 + 0 < ack (m + 1) 0 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by | simpa using add_lt_ack m 1 | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by | Mathlib.Computability.Ackermann.175_0.nk1BuTevlQj1gCN | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ m + 1 + n + 1 ≤ m + (m + n + 2) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by | linarith | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by | Mathlib.Computability.Ackermann.175_0.nk1BuTevlQj1gCN | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ m + n + 2 = succ (m + 1 + n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by | rw [succ_eq_add_one] | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by | Mathlib.Computability.Ackermann.175_0.nk1BuTevlQj1gCN | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ m + n + 2 = m + 1 + n + 1 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; | ring_nf | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; | Mathlib.Computability.Ackermann.175_0.nk1BuTevlQj1gCN | theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) | Mathlib_Computability_Ackermann |
m : ℕ
_h : 0 < m + 1
⊢ ack 0 0 < ack (m + 1) 0 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by | simpa using one_lt_ack_succ_right m 0 | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by | Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m n : ℕ
h : 0 < m + 1
⊢ ack 0 (n + 1) < ack (m + 1) (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
| rw [ack_zero, ack_succ_succ] | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
| Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m n : ℕ
h : 0 < m + 1
⊢ n + 1 + 1 < ack m (ack (m + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
| apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _) | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
| Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m n : ℕ
h : 0 < m + 1
⊢ n + 1 + 1 ≤ m + (m + 1 + n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
| linarith | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
| Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m₁ m₂ : ℕ
h : m₁ + 1 < m₂ + 1
⊢ ack (m₁ + 1) 0 < ack (m₂ + 1) 0 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
| simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h) | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
| Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m₁ m₂ n : ℕ
h : m₁ + 1 < m₂ + 1
⊢ ack (m₁ + 1) (n + 1) < ack (m₂ + 1) (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
| rw [ack_succ_succ, ack_succ_succ] | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
| Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m₁ m₂ n : ℕ
h : m₁ + 1 < m₂ + 1
⊢ ack m₁ (ack (m₁ + 1) n) < ack m₂ (ack (m₂ + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
| exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h) | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
| Mathlib.Computability.Ackermann.202_0.nk1BuTevlQj1gCN | private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y) | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ ack m (n + 1) ≤ ack (m + 1) n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
| cases' n with n n | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
| Mathlib.Computability.Ackermann.253_0.nk1BuTevlQj1gCN | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n | Mathlib_Computability_Ackermann |
case zero
m : ℕ
⊢ ack m (zero + 1) ≤ ack (m + 1) zero | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· | simp | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· | Mathlib.Computability.Ackermann.253_0.nk1BuTevlQj1gCN | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n | Mathlib_Computability_Ackermann |
case succ
m n : ℕ
⊢ ack m (succ n + 1) ≤ ack (m + 1) (succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· | rw [ack_succ_succ, succ_eq_add_one] | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· | Mathlib.Computability.Ackermann.253_0.nk1BuTevlQj1gCN | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n | Mathlib_Computability_Ackermann |
case succ
m n : ℕ
⊢ ack m (n + 1 + 1) ≤ ack m (ack (m + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
| apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n) | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
| Mathlib.Computability.Ackermann.253_0.nk1BuTevlQj1gCN | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ n + 1 + 1 ≤ m + 1 + n + 1 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
| linarith | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
| Mathlib.Computability.Ackermann.253_0.nk1BuTevlQj1gCN | theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n | Mathlib_Computability_Ackermann |
n : ℕ
⊢ n ^ 2 ≤ 2 ^ (n + 1) - 3 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
| induction' n with k hk | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
| Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case zero
⊢ zero ^ 2 ≤ 2 ^ (zero + 1) - 3 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· | norm_num | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· | Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ
k : ℕ
hk : k ^ 2 ≤ 2 ^ (k + 1) - 3
⊢ succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· | cases' k with k k | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· | Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.zero
hk : zero ^ 2 ≤ 2 ^ (zero + 1) - 3
⊢ succ zero ^ 2 ≤ 2 ^ (succ zero + 1) - 3 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· | norm_num | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· | Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ succ (succ k) ^ 2 ≤ 2 ^ (succ (succ k) + 1) - 3 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· | rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc] | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· | Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ (k + 1) ^ 2 + (2 * (k + 1) * 1 + 1 ^ 2) ≤ 2 ^ (k + 2) - 3 + 2 ^ (k + 2) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· | apply Nat.add_le_add hk | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· | Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ 2 * (k + 1) * 1 + 1 ^ 2 ≤ 2 ^ (k + 2) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
| norm_num | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
| Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ 2 * (k + 1) + 1 ≤ 2 ^ (k + 2) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
| apply succ_le_of_lt | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
| Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ.h
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ 2 * (k + 1) < 2 ^ (k + 2) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
| rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)] | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
| Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ.h
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ k + 1 < 2 ^ (k + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
| apply lt_two_pow | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
| Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ.h
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ 3 ≤ 2 ^ (k + 2) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· | rw [Nat.pow_succ, Nat.pow_succ] | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· | Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
case succ.succ.h
k : ℕ
hk : succ k ^ 2 ≤ 2 ^ (succ k + 1) - 3
⊢ 3 ≤ 2 ^ k * 2 * 2 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
| linarith [one_le_pow k 2 zero_lt_two] | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
| Mathlib.Computability.Ackermann.262_0.nk1BuTevlQj1gCN | private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 | Mathlib_Computability_Ackermann |
n : ℕ
⊢ (ack 0 n + 1) ^ 2 ≤ ack (0 + 3) n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by | simpa using sq_le_two_pow_add_one_minus_three (n + 2) | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by | Mathlib.Computability.Ackermann.277_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith | Mathlib_Computability_Ackermann |
m : ℕ
⊢ (ack (m + 1) 0 + 1) ^ 2 ≤ ack (m + 1 + 3) 0 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
| rw [ack_succ_zero, ack_succ_zero] | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
| Mathlib.Computability.Ackermann.277_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith | Mathlib_Computability_Ackermann |
m : ℕ
⊢ (ack m 1 + 1) ^ 2 ≤ ack (m + 3) 1 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
| apply ack_add_one_sq_lt_ack_add_three | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
| Mathlib.Computability.Ackermann.277_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ (ack (m + 1) (n + 1) + 1) ^ 2 ≤ ack (m + 1 + 3) (n + 1) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
| rw [ack_succ_succ, ack_succ_succ] | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
| Mathlib.Computability.Ackermann.277_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ (ack m (ack (m + 1) n) + 1) ^ 2 ≤ ack (m + 3) (ack (m + 3 + 1) n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
| apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _) | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
| Mathlib.Computability.Ackermann.277_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ m + 1 ≤ m + 3 + 1 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
| linarith | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
| Mathlib.Computability.Ackermann.277_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith | Mathlib_Computability_Ackermann |
m n : ℕ
⊢ m ≤ m + 2 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by | linarith | theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by | Mathlib.Computability.Ackermann.297_0.nk1BuTevlQj1gCN | theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n | Mathlib_Computability_Ackermann |
f : ℕ → ℕ
hf : Nat.Primrec f
⊢ ∃ m, ∀ (n : ℕ), f n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
| induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case zero
f : ℕ → ℕ
⊢ ∃ m, ∀ (n : ℕ), (fun x => 0) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· | exact ⟨0, ack_pos 0⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case succ
f : ℕ → ℕ
⊢ ∃ m, ∀ (n : ℕ), succ n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· | refine' ⟨1, fun n => _⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case succ
f : ℕ → ℕ
n : ℕ
⊢ succ n < ack 1 n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
| rw [succ_eq_one_add] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case succ
f : ℕ → ℕ
n : ℕ
⊢ 1 + n < ack 1 n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
| apply add_lt_ack | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case left
f : ℕ → ℕ
⊢ ∃ m, ∀ (n : ℕ), (fun n => (unpair n).1) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· | refine' ⟨0, fun n => _⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case left
f : ℕ → ℕ
n : ℕ
⊢ (fun n => (unpair n).1) n < ack 0 n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
| rw [ack_zero, lt_succ_iff] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case left
f : ℕ → ℕ
n : ℕ
⊢ (fun n => (unpair n).1) n ≤ n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
| exact unpair_left_le n | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case right
f : ℕ → ℕ
⊢ ∃ m, ∀ (n : ℕ), (fun n => (unpair n).2) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· | refine' ⟨0, fun n => _⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case right
f : ℕ → ℕ
n : ℕ
⊢ (fun n => (unpair n).2) n < ack 0 n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
| rw [ack_zero, lt_succ_iff] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case right
f : ℕ → ℕ
n : ℕ
⊢ (fun n => (unpair n).2) n ≤ n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
| exact unpair_right_le n | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case pair
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHf : ∃ m, ∀ (n : ℕ), f n < ack m n
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
⊢ ∃ m, ∀ (n : ℕ), (fun n => pair (f n) (g n)) n < ack m n
case comp
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHf : ∃ m, ∀ (n : ℕ), f n < ack m n
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
⊢ ∃ m, ∀ (n : ℕ), (fun n => f (g n)) n < ack m n
case prec
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHf : ∃ m, ∀ (n : ℕ), f n < ack m n
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
⊢ ∃ m, ∀ (n : ℕ), unpaired (fun z n => rec (f z) (fun y IH => g (pair z (pair y IH))) n) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
| all_goals cases' IHf with a ha; cases' IHg with b hb | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case pair
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHf : ∃ m, ∀ (n : ℕ), f n < ack m n
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
⊢ ∃ m, ∀ (n : ℕ), (fun n => pair (f n) (g n)) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals | cases' IHf with a ha | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case pair.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
⊢ ∃ m, ∀ (n : ℕ), (fun n => pair (f n) (g n)) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; | cases' IHg with b hb | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case comp
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHf : ∃ m, ∀ (n : ℕ), f n < ack m n
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
⊢ ∃ m, ∀ (n : ℕ), (fun n => f (g n)) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals | cases' IHf with a ha | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case comp.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
⊢ ∃ m, ∀ (n : ℕ), (fun n => f (g n)) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; | cases' IHg with b hb | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case prec
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHf : ∃ m, ∀ (n : ℕ), f n < ack m n
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
⊢ ∃ m, ∀ (n : ℕ), unpaired (fun z n => rec (f z) (fun y IH => g (pair z (pair y IH))) n) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals | cases' IHf with a ha | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case prec.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
IHg : ∃ m, ∀ (n : ℕ), g n < ack m n
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
⊢ ∃ m, ∀ (n : ℕ), unpaired (fun z n => rec (f z) (fun y IH => g (pair z (pair y IH))) n) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; | cases' IHg with b hb | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case pair.intro.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
⊢ ∃ m, ∀ (n : ℕ), (fun n => pair (f n) (g n)) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· | refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case pair.intro.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
n : ℕ
⊢ max (f n) (g n) ≤ ack (max a b) n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
| rw [max_ack_left] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case pair.intro.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
n : ℕ
⊢ max (f n) (g n) ≤ max (ack a n) (ack b n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
| exact max_le_max (ha n).le (hb n).le | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case comp.intro.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
⊢ ∃ m, ∀ (n : ℕ), (fun n => f (g n)) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· | exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case prec.intro.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
⊢ ∃ m, ∀ (n : ℕ), unpaired (fun z n => rec (f z) (fun y IH => g (pair z (pair y IH))) n) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
| have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl) | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
⊢ ∀ {m n : ℕ}, rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
| intro m n | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
⊢ rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
| induction' n with n IH | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case zero
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m : ℕ
⊢ rec (f m) (fun y IH => g (pair m (pair y IH))) zero < ack (max a b + 9) (m + zero) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· | apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _) | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m : ℕ
⊢ max a b < max a b + 9 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
| linarith | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case succ
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
⊢ rec (f m) (fun y IH => g (pair m (pair y IH))) (succ n) < ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
| simp only [ge_iff_le] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case succ
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
⊢ g (pair m (pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n))) < ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
| apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _) | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
⊢ ack (b + 4) (max m (pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n))) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
| cases' lt_or_le _ m with h₁ h₁ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inl
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : ?m.147498 < m
⊢ ack (b + 4) (max m (pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n))) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· | rw [max_eq_left h₁.le] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inl
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n) < m
⊢ ack (b + 4) m ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
| exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _) | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n) < m
⊢ 4 ≤ 9 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by | norm_num | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
⊢ ack (b + 4) (max m (pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n))) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
| rw [max_eq_right h₁] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
⊢ ack (b + 4) (pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
| apply (ack_pair_lt _ _ _).le.trans | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
⊢ ack (b + 4 + 4) (max n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
| cases' lt_or_le _ n with h₂ h₂ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr.inl
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : ?m.147966 < n
⊢ ack (b + 4 + 4) (max n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· | rw [max_eq_left h₂.le, add_assoc] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr.inl
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : rec (f m) (fun y IH => g (pair m (pair y IH))) n < n
⊢ ack (b + (4 + 4)) n ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
| exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _) | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : rec (f m) (fun y IH => g (pair m (pair y IH))) n < n
⊢ 4 + 4 ≤ 9 | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by | norm_num | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by | Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr.inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : n ≤ rec (f m) (fun y IH => g (pair m (pair y IH))) n
⊢ ack (b + 4 + 4) (max n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
| rw [max_eq_right h₂] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr.inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : n ≤ rec (f m) (fun y IH => g (pair m (pair y IH))) n
⊢ ack (b + 4 + 4) (rec (f m) (fun y IH => g (pair m (pair y IH))) n) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
| apply (ack_strictMono_right _ IH).le.trans | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr.inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : n ≤ rec (f m) (fun y IH => g (pair m (pair y IH))) n
⊢ ack (b + 4 + 4) (ack (max a b + 9) (m + n)) ≤ ack (max a b + 9) (m + succ n) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
| rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc] | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case inr.inr
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
m n : ℕ
IH : rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
h₁ : m ≤ pair n (rec (f m) (fun y IH => g (pair m (pair y IH))) n)
h₂ : n ≤ rec (f m) (fun y IH => g (pair m (pair y IH))) n
⊢ ack (b + (4 + 4)) (ack (max a b + 8 + 1) (m + n)) ≤ ack (max a b + 8) (ack (max a b + 8 + 1) (m + n)) | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
| exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl) | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
case prec.intro.intro
f✝ f g : ℕ → ℕ
hf : Nat.Primrec f
hg : Nat.Primrec g
a : ℕ
ha : ∀ (n : ℕ), f n < ack a n
b : ℕ
hb : ∀ (n : ℕ), g n < ack b n
this : ∀ {m n : ℕ}, rec (f m) (fun y IH => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)
⊢ ∃ m, ∀ (n : ℕ), unpaired (fun z n => rec (f z) (fun y IH => g (pair z (pair y IH))) n) n < ack m n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl)
-- The proof is now simple.
| exact ⟨max a b + 9, fun n => this.trans_le <| ack_mono_right _ <| unpair_add_le n⟩ | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl)
-- The proof is now simple.
| Mathlib.Computability.Ackermann.312_0.nk1BuTevlQj1gCN | /-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n | Mathlib_Computability_Ackermann |
h : Nat.Primrec fun n => ack n n
⊢ False | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl)
-- The proof is now simple.
exact ⟨max a b + 9, fun n => this.trans_le <| ack_mono_right _ <| unpair_add_le n⟩
#align exists_lt_ack_of_nat_primrec exists_lt_ack_of_nat_primrec
theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n := fun h => by
| cases' exists_lt_ack_of_nat_primrec h with m hm | theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n := fun h => by
| Mathlib.Computability.Ackermann.381_0.nk1BuTevlQj1gCN | theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n | Mathlib_Computability_Ackermann |
case intro
h : Nat.Primrec fun n => ack n n
m : ℕ
hm : ∀ (n : ℕ), ack n n < ack m n
⊢ False | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl)
-- The proof is now simple.
exact ⟨max a b + 9, fun n => this.trans_le <| ack_mono_right _ <| unpair_add_le n⟩
#align exists_lt_ack_of_nat_primrec exists_lt_ack_of_nat_primrec
theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n := fun h => by
cases' exists_lt_ack_of_nat_primrec h with m hm
| exact (hm m).false | theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n := fun h => by
cases' exists_lt_ack_of_nat_primrec h with m hm
| Mathlib.Computability.Ackermann.381_0.nk1BuTevlQj1gCN | theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n | Mathlib_Computability_Ackermann |
⊢ ¬Primrec fun n => ack n n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl)
-- The proof is now simple.
exact ⟨max a b + 9, fun n => this.trans_le <| ack_mono_right _ <| unpair_add_le n⟩
#align exists_lt_ack_of_nat_primrec exists_lt_ack_of_nat_primrec
theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n := fun h => by
cases' exists_lt_ack_of_nat_primrec h with m hm
exact (hm m).false
#align not_nat_primrec_ack_self not_nat_primrec_ack_self
theorem not_primrec_ack_self : ¬Primrec fun n => ack n n := by
| rw [Primrec.nat_iff] | theorem not_primrec_ack_self : ¬Primrec fun n => ack n n := by
| Mathlib.Computability.Ackermann.386_0.nk1BuTevlQj1gCN | theorem not_primrec_ack_self : ¬Primrec fun n => ack n n | Mathlib_Computability_Ackermann |
⊢ ¬Nat.Primrec fun n => ack n n | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"
/-!
# 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.
## 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`.
-/
open Nat
/-- 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. -/
def ack : ℕ → ℕ → ℕ
| 0, n => n + 1
| m + 1, 0 => ack m 1
| m + 1, n + 1 => ack m (ack (m + 1) n)
termination_by ack m n => (m, n)
#align ack ack
@[simp]
theorem ack_zero (n : ℕ) : ack 0 n = n + 1 := by rw [ack]
#align ack_zero ack_zero
@[simp]
theorem ack_succ_zero (m : ℕ) : ack (m + 1) 0 = ack m 1 := by rw [ack]
#align ack_succ_zero ack_succ_zero
@[simp]
theorem ack_succ_succ (m n : ℕ) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack]
#align ack_succ_succ ack_succ_succ
@[simp]
theorem ack_one (n : ℕ) : ack 1 n = n + 2 := by
induction' n with n IH
· rfl
· simp [IH]
#align ack_one ack_one
@[simp]
theorem ack_two (n : ℕ) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
· rfl
· simpa [mul_succ]
#align ack_two ack_two
-- Porting note: re-written to get rid of ack_three_aux
@[simp]
theorem ack_three (n : ℕ) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
· rfl
· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2,
Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≤ 2 * 2 ^ 3 := by norm_num
apply H.trans
simp [pow_le_pow_right (show 1 ≤ 2 by norm_num)]
#align ack_three ack_three
theorem ack_pos : ∀ m n, 0 < ack m n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply ack_pos
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply ack_pos
#align ack_pos ack_pos
theorem one_lt_ack_succ_left : ∀ m n, 1 < ack (m + 1) n
| 0, n => by simp
| m + 1, 0 => by
rw [ack_succ_zero]
apply one_lt_ack_succ_left
| m + 1, n + 1 => by
rw [ack_succ_succ]
apply one_lt_ack_succ_left
#align one_lt_ack_succ_left one_lt_ack_succ_left
theorem one_lt_ack_succ_right : ∀ m n, 1 < ack m (n + 1)
| 0, n => by simp
| m + 1, n => by
rw [ack_succ_succ]
cases' exists_eq_succ_of_ne_zero (ack_pos (m + 1) n).ne' with h h
rw [h]
apply one_lt_ack_succ_right
#align one_lt_ack_succ_right one_lt_ack_succ_right
theorem ack_strictMono_right : ∀ m, StrictMono (ack m)
| 0, n₁, n₂, h => by simpa using h
| m + 1, 0, n + 1, _h => by
rw [ack_succ_zero, ack_succ_succ]
exact ack_strictMono_right _ (one_lt_ack_succ_left m n)
| m + 1, n₁ + 1, n₂ + 1, h => by
rw [ack_succ_succ, ack_succ_succ]
apply ack_strictMono_right _ (ack_strictMono_right _ _)
rwa [add_lt_add_iff_right] at h
termination_by ack_strictMono_right m x y h => (m, x)
#align ack_strict_mono_right ack_strictMono_right
theorem ack_mono_right (m : ℕ) : Monotone (ack m) :=
(ack_strictMono_right m).monotone
#align ack_mono_right ack_mono_right
theorem ack_injective_right (m : ℕ) : Function.Injective (ack m) :=
(ack_strictMono_right m).injective
#align ack_injective_right ack_injective_right
@[simp]
theorem ack_lt_iff_right {m n₁ n₂ : ℕ} : ack m n₁ < ack m n₂ ↔ n₁ < n₂ :=
(ack_strictMono_right m).lt_iff_lt
#align ack_lt_iff_right ack_lt_iff_right
@[simp]
theorem ack_le_iff_right {m n₁ n₂ : ℕ} : ack m n₁ ≤ ack m n₂ ↔ n₁ ≤ n₂ :=
(ack_strictMono_right m).le_iff_le
#align ack_le_iff_right ack_le_iff_right
@[simp]
theorem ack_inj_right {m n₁ n₂ : ℕ} : ack m n₁ = ack m n₂ ↔ n₁ = n₂ :=
(ack_injective_right m).eq_iff
#align ack_inj_right ack_inj_right
theorem max_ack_right (m n₁ n₂ : ℕ) : ack m (max n₁ n₂) = max (ack m n₁) (ack m n₂) :=
(ack_mono_right m).map_max
#align max_ack_right max_ack_right
theorem add_lt_ack : ∀ m n, m + n < ack m n
| 0, n => by simp
| m + 1, 0 => by simpa using add_lt_ack m 1
| m + 1, n + 1 =>
calc
m + 1 + n + 1 ≤ m + (m + n + 2) := by linarith
_ < ack m (m + n + 2) := add_lt_ack _ _
_ ≤ ack m (ack (m + 1) n) :=
ack_mono_right m <| le_of_eq_of_le (by rw [succ_eq_add_one]; ring_nf)
<| succ_le_of_lt <| add_lt_ack (m + 1) n
_ = ack (m + 1) (n + 1) := (ack_succ_succ m n).symm
termination_by add_lt_ack m n => (m, n)
#align add_lt_ack add_lt_ack
theorem add_add_one_le_ack (m n : ℕ) : m + n + 1 ≤ ack m n :=
succ_le_of_lt (add_lt_ack m n)
#align add_add_one_le_ack add_add_one_le_ack
theorem lt_ack_left (m n : ℕ) : m < ack m n :=
(self_le_add_right m n).trans_lt <| add_lt_ack m n
#align lt_ack_left lt_ack_left
theorem lt_ack_right (m n : ℕ) : n < ack m n :=
(self_le_add_left n m).trans_lt <| add_lt_ack m n
#align lt_ack_right lt_ack_right
-- we reorder the arguments to appease the equation compiler
private theorem ack_strict_mono_left' : ∀ {m₁ m₂} (n), m₁ < m₂ → ack m₁ n < ack m₂ n
| m, 0, n => fun h => (not_lt_zero m h).elim
| 0, m + 1, 0 => fun _h => by simpa using one_lt_ack_succ_right m 0
| 0, m + 1, n + 1 => fun h => by
rw [ack_zero, ack_succ_succ]
apply lt_of_le_of_lt (le_trans _ <| add_le_add_left (add_add_one_le_ack _ _) m) (add_lt_ack _ _)
linarith
| m₁ + 1, m₂ + 1, 0 => fun h => by
simpa using ack_strict_mono_left' 1 ((add_lt_add_iff_right 1).1 h)
| m₁ + 1, m₂ + 1, n + 1 => fun h => by
rw [ack_succ_succ, ack_succ_succ]
exact
(ack_strict_mono_left' _ <| (add_lt_add_iff_right 1).1 h).trans
(ack_strictMono_right _ <| ack_strict_mono_left' n h)
termination_by ack_strict_mono_left' x y => (x, y)
theorem ack_strictMono_left (n : ℕ) : StrictMono fun m => ack m n := fun _m₁ _m₂ =>
ack_strict_mono_left' n
#align ack_strict_mono_left ack_strictMono_left
theorem ack_mono_left (n : ℕ) : Monotone fun m => ack m n :=
(ack_strictMono_left n).monotone
#align ack_mono_left ack_mono_left
theorem ack_injective_left (n : ℕ) : Function.Injective fun m => ack m n :=
(ack_strictMono_left n).injective
#align ack_injective_left ack_injective_left
@[simp]
theorem ack_lt_iff_left {m₁ m₂ n : ℕ} : ack m₁ n < ack m₂ n ↔ m₁ < m₂ :=
(ack_strictMono_left n).lt_iff_lt
#align ack_lt_iff_left ack_lt_iff_left
@[simp]
theorem ack_le_iff_left {m₁ m₂ n : ℕ} : ack m₁ n ≤ ack m₂ n ↔ m₁ ≤ m₂ :=
(ack_strictMono_left n).le_iff_le
#align ack_le_iff_left ack_le_iff_left
@[simp]
theorem ack_inj_left {m₁ m₂ n : ℕ} : ack m₁ n = ack m₂ n ↔ m₁ = m₂ :=
(ack_injective_left n).eq_iff
#align ack_inj_left ack_inj_left
theorem max_ack_left (m₁ m₂ n : ℕ) : ack (max m₁ m₂) n = max (ack m₁ n) (ack m₂ n) :=
(ack_mono_left n).map_max
#align max_ack_left max_ack_left
theorem ack_le_ack {m₁ m₂ n₁ n₂ : ℕ} (hm : m₁ ≤ m₂) (hn : n₁ ≤ n₂) : ack m₁ n₁ ≤ ack m₂ n₂ :=
(ack_mono_left n₁ hm).trans <| ack_mono_right m₂ hn
#align ack_le_ack ack_le_ack
theorem ack_succ_right_le_ack_succ_left (m n : ℕ) : ack m (n + 1) ≤ ack (m + 1) n := by
cases' n with n n
· simp
· rw [ack_succ_succ, succ_eq_add_one]
apply ack_mono_right m (le_trans _ <| add_add_one_le_ack _ n)
linarith
#align ack_succ_right_le_ack_succ_left ack_succ_right_le_ack_succ_left
-- All the inequalities from this point onwards are specific to the main proof.
private theorem sq_le_two_pow_add_one_minus_three (n : ℕ) : n ^ 2 ≤ 2 ^ (n + 1) - 3 := by
induction' n with k hk
· norm_num
· cases' k with k k
· norm_num
· rw [succ_eq_add_one, add_sq, Nat.pow_succ 2, mul_comm _ 2, two_mul (2 ^ _),
add_tsub_assoc_of_le, add_comm (2 ^ _), add_assoc]
· apply Nat.add_le_add hk
norm_num
apply succ_le_of_lt
rw [Nat.pow_succ, mul_comm _ 2, mul_lt_mul_left (zero_lt_two' ℕ)]
apply lt_two_pow
· rw [Nat.pow_succ, Nat.pow_succ]
linarith [one_le_pow k 2 zero_lt_two]
theorem ack_add_one_sq_lt_ack_add_three : ∀ m n, (ack m n + 1) ^ 2 ≤ ack (m + 3) n
| 0, n => by simpa using sq_le_two_pow_add_one_minus_three (n + 2)
| m + 1, 0 => by
rw [ack_succ_zero, ack_succ_zero]
apply ack_add_one_sq_lt_ack_add_three
| m + 1, n + 1 => by
rw [ack_succ_succ, ack_succ_succ]
apply (ack_add_one_sq_lt_ack_add_three _ _).trans (ack_mono_right _ <| ack_mono_left _ _)
linarith
#align ack_add_one_sq_lt_ack_add_three ack_add_one_sq_lt_ack_add_three
theorem ack_ack_lt_ack_max_add_two (m n k : ℕ) : ack m (ack n k) < ack (max m n + 2) k :=
calc
ack m (ack n k) ≤ ack (max m n) (ack n k) := ack_mono_left _ (le_max_left _ _)
_ < ack (max m n) (ack (max m n + 1) k) :=
ack_strictMono_right _ <| ack_strictMono_left k <| lt_succ_of_le <| le_max_right m n
_ = ack (max m n + 1) (k + 1) := (ack_succ_succ _ _).symm
_ ≤ ack (max m n + 2) k := ack_succ_right_le_ack_succ_left _ _
#align ack_ack_lt_ack_max_add_two ack_ack_lt_ack_max_add_two
theorem ack_add_one_sq_lt_ack_add_four (m n : ℕ) : ack m ((n + 1) ^ 2) < ack (m + 4) n :=
calc
ack m ((n + 1) ^ 2) < ack m ((ack m n + 1) ^ 2) :=
ack_strictMono_right m <| Nat.pow_lt_pow_left (succ_lt_succ <| lt_ack_right m n) two_ne_zero
_ ≤ ack m (ack (m + 3) n) := ack_mono_right m <| ack_add_one_sq_lt_ack_add_three m n
_ ≤ ack (m + 2) (ack (m + 3) n) := ack_mono_left _ <| by linarith
_ = ack (m + 3) (n + 1) := (ack_succ_succ _ n).symm
_ ≤ ack (m + 4) n := ack_succ_right_le_ack_succ_left _ n
#align ack_add_one_sq_lt_ack_add_four ack_add_one_sq_lt_ack_add_four
theorem ack_pair_lt (m n k : ℕ) : ack m (pair n k) < ack (m + 4) (max n k) :=
(ack_strictMono_right m <| pair_lt_max_add_one_sq n k).trans <|
ack_add_one_sq_lt_ack_add_four _ _
#align ack_mkpair_lt ack_pair_lt
/-- If `f` is primitive recursive, there exists `m` such that `f n < ack m n` for all `n`. -/
theorem exists_lt_ack_of_nat_primrec {f : ℕ → ℕ} (hf : Nat.Primrec f) :
∃ m, ∀ n, f n < ack m n := by
induction' hf with f g hf hg IHf IHg f g hf hg IHf IHg f g hf hg IHf IHg
-- Zero function:
· exact ⟨0, ack_pos 0⟩
-- Successor function:
· refine' ⟨1, fun n => _⟩
rw [succ_eq_one_add]
apply add_lt_ack
-- Left projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_left_le n
-- Right projection:
· refine' ⟨0, fun n => _⟩
rw [ack_zero, lt_succ_iff]
exact unpair_right_le n
all_goals cases' IHf with a ha; cases' IHg with b hb
-- Pairing:
· refine'
⟨max a b + 3, fun n =>
(pair_lt_max_add_one_sq _ _).trans_le <|
(Nat.pow_le_pow_left (add_le_add_right _ _) 2).trans <|
ack_add_one_sq_lt_ack_add_three _ _⟩
rw [max_ack_left]
exact max_le_max (ha n).le (hb n).le
-- Composition:
· exact
⟨max a b + 2, fun n =>
(ha _).trans <| (ack_strictMono_right a <| hb n).trans <| ack_ack_lt_ack_max_add_two a b n⟩
-- Primitive recursion operator:
· -- We prove this simpler inequality first.
have :
∀ {m n},
rec (f m) (fun y IH => g <| pair m <| pair y IH) n < ack (max a b + 9) (m + n) := by
intro m n
-- We induct on n.
induction' n with n IH
-- The base case is easy.
· apply (ha m).trans (ack_strictMono_left m <| (le_max_left a b).trans_lt _)
linarith
· -- We get rid of the first `pair`.
simp only [ge_iff_le]
apply (hb _).trans ((ack_pair_lt _ _ _).trans_le _)
-- If m is the maximum, we get a very weak inequality.
cases' lt_or_le _ m with h₁ h₁
· rw [max_eq_left h₁.le]
exact ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
(self_le_add_right m _)
rw [max_eq_right h₁]
-- We get rid of the second `pair`.
apply (ack_pair_lt _ _ _).le.trans
-- If n is the maximum, we get a very weak inequality.
cases' lt_or_le _ n with h₂ h₂
· rw [max_eq_left h₂.le, add_assoc]
exact
ack_le_ack (Nat.add_le_add (le_max_right a b) <| by norm_num)
((le_succ n).trans <| self_le_add_left _ _)
rw [max_eq_right h₂]
-- We now use the inductive hypothesis, and some simple algebraic manipulation.
apply (ack_strictMono_right _ IH).le.trans
rw [add_succ m, add_succ _ 8, succ_eq_add_one, succ_eq_add_one,
ack_succ_succ (_ + 8), add_assoc]
exact ack_mono_left _ (Nat.add_le_add (le_max_right a b) le_rfl)
-- The proof is now simple.
exact ⟨max a b + 9, fun n => this.trans_le <| ack_mono_right _ <| unpair_add_le n⟩
#align exists_lt_ack_of_nat_primrec exists_lt_ack_of_nat_primrec
theorem not_nat_primrec_ack_self : ¬Nat.Primrec fun n => ack n n := fun h => by
cases' exists_lt_ack_of_nat_primrec h with m hm
exact (hm m).false
#align not_nat_primrec_ack_self not_nat_primrec_ack_self
theorem not_primrec_ack_self : ¬Primrec fun n => ack n n := by
rw [Primrec.nat_iff]
| exact not_nat_primrec_ack_self | theorem not_primrec_ack_self : ¬Primrec fun n => ack n n := by
rw [Primrec.nat_iff]
| Mathlib.Computability.Ackermann.386_0.nk1BuTevlQj1gCN | theorem not_primrec_ack_self : ¬Primrec fun n => ack n n | Mathlib_Computability_Ackermann |
α : Type u
e : Encoding α
⊢ Function.Injective e.encode | /-
Copyright (c) 2020 Pim Spelier, Daan van Gent. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Pim Spelier, Daan van Gent
-/
import Mathlib.Data.Fintype.Basic
import Mathlib.Data.Num.Lemmas
import Mathlib.SetTheory.Cardinal.Ordinal
#align_import computability.encoding from "leanprover-community/mathlib"@"b6395b3a5acd655b16385fa0cdbf1961d6c34b3e"
/-!
# Encodings
This file contains the definition of a (finite) encoding, a map from a type to
strings in an alphabet, used in defining computability by Turing machines.
It also contains several examples:
## Examples
- `finEncodingNatBool` : a binary encoding of ℕ in a simple alphabet.
- `finEncodingNatΓ'` : a binary encoding of ℕ in the alphabet used for TM's.
- `unaryFinEncodingNat` : a unary encoding of ℕ
- `finEncodingBoolBool` : an encoding of bool.
-/
universe u v
open Cardinal
namespace Computability
/-- An encoding of a type in a certain alphabet, together with a decoding. -/
structure Encoding (α : Type u) where
Γ : Type v
encode : α → List Γ
decode : List Γ → Option α
decode_encode : ∀ x, decode (encode x) = some x
#align computability.encoding Computability.Encoding
theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode := by
| refine' fun _ _ h => Option.some_injective _ _ | theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode := by
| Mathlib.Computability.Encoding.42_0.rxHMk1LMmv2SbJ8 | theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode | Mathlib_Computability_Encoding |
α : Type u
e : Encoding α
x✝¹ x✝ : α
h : encode e x✝¹ = encode e x✝
⊢ some x✝¹ = some x✝ | /-
Copyright (c) 2020 Pim Spelier, Daan van Gent. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Pim Spelier, Daan van Gent
-/
import Mathlib.Data.Fintype.Basic
import Mathlib.Data.Num.Lemmas
import Mathlib.SetTheory.Cardinal.Ordinal
#align_import computability.encoding from "leanprover-community/mathlib"@"b6395b3a5acd655b16385fa0cdbf1961d6c34b3e"
/-!
# Encodings
This file contains the definition of a (finite) encoding, a map from a type to
strings in an alphabet, used in defining computability by Turing machines.
It also contains several examples:
## Examples
- `finEncodingNatBool` : a binary encoding of ℕ in a simple alphabet.
- `finEncodingNatΓ'` : a binary encoding of ℕ in the alphabet used for TM's.
- `unaryFinEncodingNat` : a unary encoding of ℕ
- `finEncodingBoolBool` : an encoding of bool.
-/
universe u v
open Cardinal
namespace Computability
/-- An encoding of a type in a certain alphabet, together with a decoding. -/
structure Encoding (α : Type u) where
Γ : Type v
encode : α → List Γ
decode : List Γ → Option α
decode_encode : ∀ x, decode (encode x) = some x
#align computability.encoding Computability.Encoding
theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode := by
refine' fun _ _ h => Option.some_injective _ _
| rw [← e.decode_encode, ← e.decode_encode, h] | theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode := by
refine' fun _ _ h => Option.some_injective _ _
| Mathlib.Computability.Encoding.42_0.rxHMk1LMmv2SbJ8 | theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode | Mathlib_Computability_Encoding |
⊢ Multiset.Nodup {blank, bit true, bit false, bra, ket, comma} | /-
Copyright (c) 2020 Pim Spelier, Daan van Gent. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Pim Spelier, Daan van Gent
-/
import Mathlib.Data.Fintype.Basic
import Mathlib.Data.Num.Lemmas
import Mathlib.SetTheory.Cardinal.Ordinal
#align_import computability.encoding from "leanprover-community/mathlib"@"b6395b3a5acd655b16385fa0cdbf1961d6c34b3e"
/-!
# Encodings
This file contains the definition of a (finite) encoding, a map from a type to
strings in an alphabet, used in defining computability by Turing machines.
It also contains several examples:
## Examples
- `finEncodingNatBool` : a binary encoding of ℕ in a simple alphabet.
- `finEncodingNatΓ'` : a binary encoding of ℕ in the alphabet used for TM's.
- `unaryFinEncodingNat` : a unary encoding of ℕ
- `finEncodingBoolBool` : an encoding of bool.
-/
universe u v
open Cardinal
namespace Computability
/-- An encoding of a type in a certain alphabet, together with a decoding. -/
structure Encoding (α : Type u) where
Γ : Type v
encode : α → List Γ
decode : List Γ → Option α
decode_encode : ∀ x, decode (encode x) = some x
#align computability.encoding Computability.Encoding
theorem Encoding.encode_injective {α : Type u} (e : Encoding α) : Function.Injective e.encode := by
refine' fun _ _ h => Option.some_injective _ _
rw [← e.decode_encode, ← e.decode_encode, h]
#align computability.encoding.encode_injective Computability.Encoding.encode_injective
/-- An encoding plus a guarantee of finiteness of the alphabet. -/
structure FinEncoding (α : Type u) extends Encoding.{u, 0} α where
ΓFin : Fintype Γ
#align computability.fin_encoding Computability.FinEncoding
instance Γ.fintype {α : Type u} (e : FinEncoding α) : Fintype e.toEncoding.Γ :=
e.ΓFin
#align computability.Γ.fintype Computability.Γ.fintype
/-- A standard Turing machine alphabet, consisting of blank,bit0,bit1,bra,ket,comma. -/
inductive Γ'
| blank
| bit (b : Bool)
| bra
| ket
| comma
deriving DecidableEq
#align computability.Γ' Computability.Γ'
-- Porting note: A handler for `Fintype` had not been implemented yet.
instance Γ'.fintype : Fintype Γ' :=
⟨⟨{.blank, .bit true, .bit false, .bra, .ket, .comma}, by | decide | instance Γ'.fintype : Fintype Γ' :=
⟨⟨{.blank, .bit true, .bit false, .bra, .ket, .comma}, by | Mathlib.Computability.Encoding.67_0.rxHMk1LMmv2SbJ8 | instance Γ'.fintype : Fintype Γ' | Mathlib_Computability_Encoding |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.