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
|
---|---|---|---|---|---|---|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
|
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ G
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
|
unfold Primrec₂
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p => G p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
|
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n =>
(fun n =>
Nat.casesOn n (some (s p.1)) fun n =>
Nat.casesOn n (some (l p.1)) fun n => Nat.casesOn n (some (r p.1)) fun n => G₁ ((p.1, p.2), n, div2 (div2 n)))
n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
|
unfold Primrec₂
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p =>
(fun p n =>
(fun n =>
Nat.casesOn n (some (s p.1)) fun n =>
Nat.casesOn n (some (l p.1)) fun n =>
Nat.casesOn n (some (r p.1)) fun n => G₁ ((p.1, p.2), n, div2 (div2 n)))
n)
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
|
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n =>
(fun n =>
Nat.casesOn n (some (l p.1.1)) fun n =>
Nat.casesOn n (some (r p.1.1)) fun n => G₁ ((p.1.1, p.1.2), n, div2 (div2 n)))
n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
|
unfold Primrec₂
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p =>
(fun p n =>
(fun n =>
Nat.casesOn n (some (l p.1.1)) fun n =>
Nat.casesOn n (some (r p.1.1)) fun n => G₁ ((p.1.1, p.1.2), n, div2 (div2 n)))
n)
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
|
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n => (fun n => Nat.casesOn n (some (r p.1.1.1)) fun n => G₁ ((p.1.1.1, p.1.1.2), n, div2 (div2 n))) n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
|
unfold Primrec₂
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p =>
(fun p n => (fun n => Nat.casesOn n (some (r p.1.1.1)) fun n => G₁ ((p.1.1.1, p.1.1.2), n, div2 (div2 n))) n) p.1
p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n => (fun n => G₁ ((p.1.1.1.1, p.1.1.1.2), n, div2 (div2 n))) n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
|
unfold Primrec₂
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p => (fun p n => (fun n => G₁ ((p.1.1.1.1, p.1.1.1.2), n, div2 (div2 n))) n) p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ F (id a) (ofNat Code (encode (c a))) = F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by
|
simp
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G (a, List.map ((fun a n => F a (ofNat Code n)) a) (List.range n)).1
(a, List.map ((fun a n => F a (ofNat Code n)) a) (List.range n)).2 =
some ((fun a n => F a (ofNat Code n)) a n)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
|
simp (config := { zeta := false })
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range n)) = some (F a (ofNat Code n))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
|
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range n)) = some (F a (ofNat Code n))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range Nat.zero)) = some (F a (ofNat Code Nat.zero))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a [] = some (F a zero)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ n))) = some (F a (ofNat Code (Nat.succ n)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ Nat.zero))) =
some (F a (ofNat Code (Nat.succ Nat.zero)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNatCode n)) (List.range (Nat.succ 0))) = some (F a succ)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ n)))) =
some (F a (ofNat Code (Nat.succ (Nat.succ n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ Nat.zero)))) =
some (F a (ofNat Code (Nat.succ (Nat.succ Nat.zero))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNatCode n)) (List.range (Nat.succ (Nat.succ 0)))) = some (F a left)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ (Nat.succ n))))) =
some (F a (ofNat Code (Nat.succ (Nat.succ (Nat.succ n)))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ (Nat.succ Nat.zero))))) =
some (F a (ofNat Code (Nat.succ (Nat.succ (Nat.succ Nat.zero)))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNatCode n)) (List.range (Nat.succ (Nat.succ (Nat.succ 0))))) = some (F a right)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n)))))) =
some (F a (ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
|
simp only []
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ Nat.rec (some (z a))
(fun n_1 n_ih =>
Nat.rec (some (s a))
(fun n_2 n_ih =>
Nat.rec (some (l a))
(fun n_3 n_ih =>
Nat.rec (some (r a))
(fun n_4 n_ih =>
Option.bind
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(div2 (div2 n_4)))
fun s_1 =>
Option.bind
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).1)
fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n_4 then
bif bodd (div2 n_4) then rf a (ofNat Code (div2 (div2 n_4)), s_1)
else
pc a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂)
else
bif bodd (div2 n_4) then
co a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂)
else
pr a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂))
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).2))
n_3)
n_2)
n_1)
(List.length
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))) =
some
(rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
|
rw [List.length_map, List.length_range]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ Nat.rec (some (z a))
(fun n_1 n_ih =>
Nat.rec (some (s a))
(fun n_2 n_ih =>
Nat.rec (some (l a))
(fun n_3 n_ih =>
Nat.rec (some (r a))
(fun n_4 n_ih =>
Option.bind
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(div2 (div2 n_4)))
fun s_1 =>
Option.bind
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).1)
fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n_4 then
bif bodd (div2 n_4) then rf a (ofNat Code (div2 (div2 n_4)), s_1)
else
pc a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂)
else
bif bodd (div2 n_4) then
co a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂)
else
pr a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂))
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).2))
n_3)
n_2)
n_1)
(Nat.succ (Nat.succ (Nat.succ (Nat.succ n)))) =
some
(rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
|
let m := n.div2.div2
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
⊢ Nat.rec (some (z a))
(fun n_1 n_ih =>
Nat.rec (some (s a))
(fun n_2 n_ih =>
Nat.rec (some (l a))
(fun n_3 n_ih =>
Nat.rec (some (r a))
(fun n_4 n_ih =>
Option.bind
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(div2 (div2 n_4)))
fun s_1 =>
Option.bind
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).1)
fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n_4 then
bif bodd (div2 n_4) then rf a (ofNat Code (div2 (div2 n_4)), s_1)
else
pc a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂)
else
bif bodd (div2 n_4) then
co a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂)
else
pr a
(ofNat Code (unpair (div2 (div2 n_4))).1, ofNat Code (unpair (div2 (div2 n_4))).2, s₁,
s₂))
(List.get?
(List.map
(fun n =>
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).2))
n_3)
n_2)
n_1)
(Nat.succ (Nat.succ (Nat.succ (Nat.succ n)))) =
some
(rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
|
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
⊢ G₁ ((a, List.map (fun n => F a (ofNat Code n)) (List.range (n + 4))), n, m) = some (F a (ofNat Code (n + 4)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
|
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
⊢ m < n + 4
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
|
simp only [div2_val]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
⊢ n / 2 / 2 < n + 4
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
|
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
⊢ G₁ ((a, List.map (fun n => F a (ofNat Code n)) (List.range (n + 4))), n, m) = some (F a (ofNat Code (n + 4)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
|
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
⊢ G₁ ((a, List.map (fun n => F a (ofNat Code n)) (List.range (n + 4))), n, m) = some (F a (ofNat Code (n + 4)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
|
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ G₁ ((a, List.map (fun n => F a (ofNat Code n)) (List.range (n + 4))), n, m) = some (F a (ofNat Code (n + 4)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
|
simp [List.get?_map, List.get?_range, hm, m1, m2]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif bodd n then
bif bodd (div2 n) then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif bodd (div2 n) then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf)) (ofNat Code (n + 4))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
|
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif bodd n then
bif bodd (div2 n) then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif bodd (div2 n) then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf)) (ofNatCode (n + 4))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
|
simp [ofNatCode]
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif bodd n then
bif bodd (div2 n) then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif bodd (div2 n) then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match bodd n, bodd (div2 n) with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
|
cases n.bodd
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ.false
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif false then
bif bodd (div2 n) then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif bodd (div2 n) then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match false, bodd (div2 n) with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;>
|
cases n.div2.bodd
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;>
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ.true
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif true then
bif bodd (div2 n) then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif bodd (div2 n) then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match true, bodd (div2 n) with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;>
|
cases n.div2.bodd
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;>
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ.false.false
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif false then
bif false then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif false then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match false, false with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ.false.true
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif false then
bif true then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif true then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match false, true with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ.true.false
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif true then
bif false then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif false then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match true, false with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ.true.true
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code × Code × σ × σ → σ
hpr : Primrec₂ pr
co : α → Code × Code × σ × σ → σ
hco : Primrec₂ co
pc : α → Code × Code × σ × σ → σ
hpc : Primrec₂ pc
rf : α → Code × σ → σ
hrf : Primrec₂ rf
PR✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pr a (cf, cg, hf, hg)
CO✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => co a (cf, cg, hf, hg)
PC✝ : α → Code → Code → σ → σ → σ := fun a cf cg hf hg => pc a (cf, cg, hf, hg)
RF✝ : α → Code → σ → σ := fun a cf hf => rf a (cf, hf)
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (PR✝ a) (CO✝ a) (PC✝ a) (RF✝ a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m, s)
else pc a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂)
else pr a (ofNat Code (unpair m).1, ofNat Code (unpair m).2, s₁, s₂))
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
hm : m < n + 4
m1 : (unpair m).1 < n + 4
m2 : (unpair m).2 < n + 4
⊢ (bif true then
bif true then
rf a
(ofNat Code (div2 (div2 n)),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (div2 (div2 n))))
else
pc a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
bif true then
co a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))
else
pr a
(ofNat Code (unpair (div2 (div2 n))).1, ofNat Code (unpair (div2 (div2 n))).2,
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).1),
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg))
(fun cf cg hf hg => co a (cf, cg, hf, hg)) (fun cf cg hf hg => pc a (cf, cg, hf, hg))
(fun cf hf => rf a (cf, hf)) (ofNat Code (unpair (div2 (div2 n))).2))) =
rec (z a) (s a) (l a) (r a) (fun cf cg hf hg => pr a (cf, cg, hf, hg)) (fun cf cg hf hg => co a (cf, cg, hf, hg))
(fun cf cg hf hg => pc a (cf, cg, hf, hg)) (fun cf hf => rf a (cf, hf))
(match true, true with
| false, false => pair (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| false, true => comp (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, false => prec (ofNatCode (unpair (div2 (div2 n))).1) (ofNatCode (unpair (div2 (div2 n))).2)
| true, true => rfind' (ofNatCode (div2 (div2 n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
rfl
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;>
|
Mathlib.Computability.PartrecCode.276_0.A3c3Aev6SyIRjCJ
|
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
⊢ let F := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a);
Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
|
intros F
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
|
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
|
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec G₁
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
|
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec₂ fun p s =>
Option.bind (List.get? p.1.2 (unpair p.2.2).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd p.2.1 then
bif bodd (div2 p.2.1) then rf p.1.1 (ofNat Code p.2.2) s
else pc p.1.1 (ofNat Code (unpair p.2.2).1) (ofNat Code (unpair p.2.2).2) s₁ s₂
else
bif bodd (div2 p.2.1) then co p.1.1 (ofNat Code (unpair p.2.2).1) (ofNat Code (unpair p.2.2).2) s₁ s₂
else pr p.1.1 (ofNat Code (unpair p.2.2).1) (ofNat Code (unpair p.2.2).2) s₁ s₂)
(List.get? p.1.2 (unpair p.2.2).2)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec fun p =>
(fun p s =>
Option.bind (List.get? p.1.2 (unpair p.2.2).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd p.2.1 then
bif bodd (div2 p.2.1) then rf p.1.1 (ofNat Code p.2.2) s
else pc p.1.1 (ofNat Code (unpair p.2.2).1) (ofNat Code (unpair p.2.2).2) s₁ s₂
else
bif bodd (div2 p.2.1) then co p.1.1 (ofNat Code (unpair p.2.2).1) (ofNat Code (unpair p.2.2).2) s₁ s₂
else pr p.1.1 (ofNat Code (unpair p.2.2).1) (ofNat Code (unpair p.2.2).2) s₁ s₂)
(List.get? p.1.2 (unpair p.2.2).2))
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
|
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec₂ fun p s₁ =>
Option.map
(fun s₂ =>
bif bodd p.1.2.1 then
bif bodd (div2 p.1.2.1) then rf p.1.1.1 (ofNat Code p.1.2.2) p.2
else pc p.1.1.1 (ofNat Code (unpair p.1.2.2).1) (ofNat Code (unpair p.1.2.2).2) s₁ s₂
else
bif bodd (div2 p.1.2.1) then co p.1.1.1 (ofNat Code (unpair p.1.2.2).1) (ofNat Code (unpair p.1.2.2).2) s₁ s₂
else pr p.1.1.1 (ofNat Code (unpair p.1.2.2).1) (ofNat Code (unpair p.1.2.2).2) s₁ s₂)
(List.get? p.1.1.2 (unpair p.1.2.2).2)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec fun p =>
(fun p s₁ =>
Option.map
(fun s₂ =>
bif bodd p.1.2.1 then
bif bodd (div2 p.1.2.1) then rf p.1.1.1 (ofNat Code p.1.2.2) p.2
else pc p.1.1.1 (ofNat Code (unpair p.1.2.2).1) (ofNat Code (unpair p.1.2.2).2) s₁ s₂
else
bif bodd (div2 p.1.2.1) then
co p.1.1.1 (ofNat Code (unpair p.1.2.2).1) (ofNat Code (unpair p.1.2.2).2) s₁ s₂
else pr p.1.1.1 (ofNat Code (unpair p.1.2.2).1) (ofNat Code (unpair p.1.2.2).2) s₁ s₂)
(List.get? p.1.1.2 (unpair p.1.2.2).2))
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
|
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
|
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
|
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
|
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
|
have m₁ := fst.comp (Primrec.unpair.comp m)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
|
have m₂ := snd.comp (Primrec.unpair.comp m)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
|
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
|
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
|
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
s₂ : Primrec fun p => p.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
|
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
s₂ : Primrec fun p => p.2
h₁ :
Primrec fun a =>
rf (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).1 (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.1
(a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
|
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
s₂ : Primrec fun p => p.2
h₁ :
Primrec fun a =>
rf (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).1 (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.1
(a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.2
h₂ :
Primrec fun a =>
pc (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
s₂ : Primrec fun p => p.2
h₁ :
Primrec fun a =>
rf (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).1 (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.1
(a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.2
h₂ :
Primrec fun a =>
pc (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
h₃ :
Primrec fun a =>
co (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
s₂ : Primrec fun p => p.2
h₁ :
Primrec fun a =>
rf (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).1 (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.1
(a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.2
h₂ :
Primrec fun a =>
pc (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
h₃ :
Primrec fun a =>
co (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
h₄ :
Primrec fun a =>
pr (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
⊢ Primrec₂ fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s✝ : α → σ
hs : Primrec s✝
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s✝ a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
a : Primrec fun p => p.1.1.1.1.1
n : Primrec fun p => p.1.1.1.2.1
m : Primrec fun p => p.1.1.1.2.2
m₁ : Primrec fun a => (unpair a.1.1.1.2.2).1
m₂ : Primrec fun a => (unpair a.1.1.1.2.2).2
s : Primrec fun p => p.1.1.2
s₁ : Primrec fun p => p.1.2
s₂ : Primrec fun p => p.2
h₁ :
Primrec fun a =>
rf (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).1 (a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.1
(a.1.1.1.1.1, ofNat Code a.1.1.1.2.2, a.1.1.2).2.2
h₂ :
Primrec fun a =>
pc (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
h₃ :
Primrec fun a =>
co (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
h₄ :
Primrec fun a =>
pr (a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.1
(a.1.1.1.1.1, ofNat Code (unpair a.1.1.1.2.2).1, ofNat Code (unpair a.1.1.1.2.2).2, a.1.2, a.2).2.2.2.2
⊢ Primrec fun p =>
(fun p s₂ =>
bif bodd p.1.1.2.1 then
bif bodd (div2 p.1.1.2.1) then rf p.1.1.1.1 (ofNat Code p.1.1.2.2) p.1.2
else pc p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else
bif bodd (div2 p.1.1.2.1) then
co p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂
else pr p.1.1.1.1 (ofNat Code (unpair p.1.1.2.2).1) (ofNat Code (unpair p.1.1.2.2).2) p.2 s₂)
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
|
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
|
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
|
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ G
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p => G p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
|
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n =>
(fun n =>
Nat.casesOn n (some (s p.1)) fun n =>
Nat.casesOn n (some (l p.1)) fun n => Nat.casesOn n (some (r p.1)) fun n => G₁ ((p.1, p.2), n, div2 (div2 n)))
n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p =>
(fun p n =>
(fun n =>
Nat.casesOn n (some (s p.1)) fun n =>
Nat.casesOn n (some (l p.1)) fun n =>
Nat.casesOn n (some (r p.1)) fun n => G₁ ((p.1, p.2), n, div2 (div2 n)))
n)
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
|
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n =>
(fun n =>
Nat.casesOn n (some (l p.1.1)) fun n =>
Nat.casesOn n (some (r p.1.1)) fun n => G₁ ((p.1.1, p.1.2), n, div2 (div2 n)))
n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p =>
(fun p n =>
(fun n =>
Nat.casesOn n (some (l p.1.1)) fun n =>
Nat.casesOn n (some (r p.1.1)) fun n => G₁ ((p.1.1, p.1.2), n, div2 (div2 n)))
n)
p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
|
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n => (fun n => Nat.casesOn n (some (r p.1.1.1)) fun n => G₁ ((p.1.1.1, p.1.1.2), n, div2 (div2 n))) n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p =>
(fun p n => (fun n => Nat.casesOn n (some (r p.1.1.1)) fun n => G₁ ((p.1.1.1, p.1.1.2), n, div2 (div2 n))) n) p.1
p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec₂ fun p n => (fun n => G₁ ((p.1.1.1.1, p.1.1.1.2), n, div2 (div2 n))) n
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
|
unfold Primrec₂
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
⊢ Primrec fun p => (fun p n => (fun n => G₁ ((p.1.1.1.1, p.1.1.1.2), n, div2 (div2 n))) n) p.1 p.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
⊢ Primrec fun a => F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ F (id a) (ofNat Code (encode (c a))) = F a (c a)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by
|
simp
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G (a, List.map ((fun a n => F a (ofNat Code n)) a) (List.range n)).1
(a, List.map ((fun a n => F a (ofNat Code n)) a) (List.range n)).2 =
some ((fun a n => F a (ofNat Code n)) a n)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
|
simp (config := { zeta := false })
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range n)) = some (F a (ofNat Code n))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
|
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range n)) = some (F a (ofNat Code n))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range Nat.zero)) = some (F a (ofNat Code Nat.zero))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a [] = some (F a zero)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ n))) = some (F a (ofNat Code (Nat.succ n)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ Nat.zero))) =
some (F a (ofNat Code (Nat.succ Nat.zero)))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNatCode n)) (List.range (Nat.succ 0))) = some (F a succ)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ n)))) =
some (F a (ofNat Code (Nat.succ (Nat.succ n))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ Nat.zero)))) =
some (F a (ofNat Code (Nat.succ (Nat.succ Nat.zero))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNatCode n)) (List.range (Nat.succ (Nat.succ 0)))) = some (F a left)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ (Nat.succ n))))) =
some (F a (ofNat Code (Nat.succ (Nat.succ (Nat.succ n)))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
cases' n with n
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ (Nat.succ Nat.zero))))) =
some (F a (ofNat Code (Nat.succ (Nat.succ (Nat.succ Nat.zero)))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; ·
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.zero
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
⊢ G a (List.map (fun n => F a (ofNatCode n)) (List.range (Nat.succ (Nat.succ (Nat.succ 0))))) = some (F a right)
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
rfl
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode];
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ G a (List.map (fun n => F a (ofNat Code n)) (List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n)))))) =
some (F a (ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
|
simp only []
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ Nat.rec (some (z a))
(fun n_1 n_ih =>
Nat.rec (some (s a))
(fun n_2 n_ih =>
Nat.rec (some (l a))
(fun n_3 n_ih =>
Nat.rec (some (r a))
(fun n_4 n_ih =>
Option.bind
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(div2 (div2 n_4)))
fun s_1 =>
Option.bind
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).1)
fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n_4 then
bif bodd (div2 n_4) then rf a (ofNat Code (div2 (div2 n_4))) s_1
else
pc a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂
else
bif bodd (div2 n_4) then
co a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂
else
pr a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂)
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).2))
n_3)
n_2)
n_1)
(List.length
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))) =
some
(rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
(ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
|
rw [List.length_map, List.length_range]
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
⊢ Nat.rec (some (z a))
(fun n_1 n_ih =>
Nat.rec (some (s a))
(fun n_2 n_ih =>
Nat.rec (some (l a))
(fun n_3 n_ih =>
Nat.rec (some (r a))
(fun n_4 n_ih =>
Option.bind
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(div2 (div2 n_4)))
fun s_1 =>
Option.bind
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).1)
fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n_4 then
bif bodd (div2 n_4) then rf a (ofNat Code (div2 (div2 n_4))) s_1
else
pc a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂
else
bif bodd (div2 n_4) then
co a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂
else
pr a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂)
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).2))
n_3)
n_2)
n_1)
(Nat.succ (Nat.succ (Nat.succ (Nat.succ n)))) =
some
(rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
(ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
|
let m := n.div2.div2
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
case succ.succ.succ.succ
α : Type u_1
σ : Type u_2
inst✝¹ : Primcodable α
inst✝ : Primcodable σ
c : α → Code
hc : Primrec c
z : α → σ
hz : Primrec z
s : α → σ
hs : Primrec s
l : α → σ
hl : Primrec l
r : α → σ
hr : Primrec r
pr : α → Code → Code → σ → σ → σ
hpr : Primrec fun a => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
co : α → Code → Code → σ → σ → σ
hco : Primrec fun a => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
pc : α → Code → Code → σ → σ → σ
hpc : Primrec fun a => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2
rf : α → Code → σ → σ
hrf : Primrec fun a => rf a.1 a.2.1 a.2.2
F : α → Code → σ := fun a c => Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
G₁ : (α × List σ) × ℕ × ℕ → Option σ :=
fun p =>
let a := p.1.1;
let IH := p.1.2;
let n := p.2.1;
let m := p.2.2;
Option.bind (List.get? IH m) fun s =>
Option.bind (List.get? IH (unpair m).1) fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n then
bif bodd (div2 n) then rf a (ofNat Code m) s
else pc a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else
bif bodd (div2 n) then co a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂
else pr a (ofNat Code (unpair m).1) (ofNat Code (unpair m).2) s₁ s₂)
(List.get? IH (unpair m).2)
this✝ : Primrec G₁
G : α → List σ → Option σ :=
fun a IH =>
Nat.casesOn (List.length IH) (some (z a)) fun n =>
Nat.casesOn n (some (s a)) fun n =>
Nat.casesOn n (some (l a)) fun n => Nat.casesOn n (some (r a)) fun n => G₁ ((a, IH), n, div2 (div2 n))
this : Primrec₂ G
a : α
n : ℕ
m : ℕ := div2 (div2 n)
⊢ Nat.rec (some (z a))
(fun n_1 n_ih =>
Nat.rec (some (s a))
(fun n_2 n_ih =>
Nat.rec (some (l a))
(fun n_3 n_ih =>
Nat.rec (some (r a))
(fun n_4 n_ih =>
Option.bind
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(div2 (div2 n_4)))
fun s_1 =>
Option.bind
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).1)
fun s₁ =>
Option.map
(fun s₂ =>
bif bodd n_4 then
bif bodd (div2 n_4) then rf a (ofNat Code (div2 (div2 n_4))) s_1
else
pc a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂
else
bif bodd (div2 n_4) then
co a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂
else
pr a (ofNat Code (unpair (div2 (div2 n_4))).1) (ofNat Code (unpair (div2 (div2 n_4))).2)
s₁ s₂)
(List.get?
(List.map (fun n => rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a) (ofNat Code n))
(List.range (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
(unpair (div2 (div2 n_4))).2))
n_3)
n_2)
n_1)
(Nat.succ (Nat.succ (Nat.succ (Nat.succ n)))) =
some
(rec (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
(ofNat Code (Nat.succ (Nat.succ (Nat.succ (Nat.succ n))))))
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
|
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
|
Mathlib.Computability.PartrecCode.385_0.A3c3Aev6SyIRjCJ
|
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ
|
Mathlib_Computability_PartrecCode
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.