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
f✝ f g : ℕ →. ℕ pf : Partrec f pg : Partrec g hf : ∃ c, eval c = f hg : ∃ c, eval c = g ⊢ ∃ c, eval c = fun n => Seq.seq (Nat.pair <$> f n) fun x => g 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg =>
rcases hf with ⟨cf, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro f g : ℕ →. ℕ pg : Partrec g hg : ∃ c, eval c = g cf : Code pf : Partrec (eval cf) ⊢ ∃ c, eval c = fun n => Seq.seq (Nat.pair <$> eval cf n) fun x => g 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩;
rcases hg with ⟨cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩;
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.intro f : ℕ →. ℕ cf : Code pf : Partrec (eval cf) cg : Code pg : Partrec (eval cg) ⊢ ∃ c, eval c = fun n => Seq.seq (Nat.pair <$> eval cf n) fun x => eval cg 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
exact ⟨pair cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case comp f f✝ g✝ : ℕ →. ℕ a✝¹ : Partrec f✝ a✝ : Partrec g✝ a_ih✝¹ : ∃ c, eval c = f✝ a_ih✝ : ∃ c, eval c = g✝ ⊢ ∃ c, eval c = fun n => g✝ n >>= f✝ case prec f f✝ g✝ : ℕ →. ℕ a✝¹ : Partrec f✝ a✝ : Partrec g✝ a_ih✝¹ : ∃ c, eval c = f✝ a_ih✝ : ∃ c, eval c = g✝ ⊢ ∃ c, eval c = unpaired fun a n => Nat.rec (f✝ a) (fun y IH => do let i ← IH g✝ (Nat.pair a (Nat.pair y i))) n case rfind f f✝ : ℕ →. ℕ a✝ : Partrec f✝ a_ih✝ : ∃ c, eval c = f✝ ⊢ ∃ c, eval c = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> f✝ (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩
case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f✝ f g : ℕ →. ℕ pf : Partrec f pg : Partrec g hf : ∃ c, eval c = f hg : ∃ c, eval c = g ⊢ ∃ c, eval c = fun n => g n >>= f
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩
case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f✝ f g : ℕ →. ℕ pf : Partrec f pg : Partrec g hf : ∃ c, eval c = f hg : ∃ c, eval c = g ⊢ ∃ c, eval c = fun n => g n >>= f
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg =>
rcases hf with ⟨cf, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro f g : ℕ →. ℕ pg : Partrec g hg : ∃ c, eval c = g cf : Code pf : Partrec (eval cf) ⊢ ∃ c, eval c = fun n => g n >>= eval cf
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩;
rcases hg with ⟨cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩;
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.intro f : ℕ →. ℕ cf : Code pf : Partrec (eval cf) cg : Code pg : Partrec (eval cg) ⊢ ∃ c, eval c = fun n => eval cg n >>= eval cf
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
exact ⟨comp cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case prec f f✝ g✝ : ℕ →. ℕ a✝¹ : Partrec f✝ a✝ : Partrec g✝ a_ih✝¹ : ∃ c, eval c = f✝ a_ih✝ : ∃ c, eval c = g✝ ⊢ ∃ c, eval c = unpaired fun a n => Nat.rec (f✝ a) (fun y IH => do let i ← IH g✝ (Nat.pair a (Nat.pair y i))) n case rfind f f✝ : ℕ →. ℕ a✝ : Partrec f✝ a_ih✝ : ∃ c, eval c = f✝ ⊢ ∃ c, eval c = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> f✝ (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩
case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f✝ f g : ℕ →. ℕ pf : Partrec f pg : Partrec g hf : ∃ c, eval c = f hg : ∃ c, eval c = g ⊢ ∃ c, eval c = unpaired fun a n => Nat.rec (f a) (fun y IH => do let i ← IH g (Nat.pair a (Nat.pair y i))) 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩
case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f✝ f g : ℕ →. ℕ pf : Partrec f pg : Partrec g hf : ∃ c, eval c = f hg : ∃ c, eval c = g ⊢ ∃ c, eval c = unpaired fun a n => Nat.rec (f a) (fun y IH => do let i ← IH g (Nat.pair a (Nat.pair y i))) 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg =>
rcases hf with ⟨cf, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro f g : ℕ →. ℕ pg : Partrec g hg : ∃ c, eval c = g cf : Code pf : Partrec (eval cf) ⊢ ∃ c, eval c = unpaired fun a n => Nat.rec (eval cf a) (fun y IH => do let i ← IH g (Nat.pair a (Nat.pair y i))) 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩;
rcases hg with ⟨cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩;
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.intro f : ℕ →. ℕ cf : Code pf : Partrec (eval cf) cg : Code pg : Partrec (eval cg) ⊢ ∃ c, eval c = unpaired fun a n => Nat.rec (eval cf a) (fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i))) 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
exact ⟨prec cf cg, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case rfind f f✝ : ℕ →. ℕ a✝ : Partrec f✝ a_ih✝ : ∃ c, eval c = f✝ ⊢ ∃ c, eval c = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> f✝ (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩
case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id']
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f✝ f : ℕ →. ℕ pf : Partrec f hf : ∃ c, eval c = f ⊢ ∃ c, eval c = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> f (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩
case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id']
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f✝ f : ℕ →. ℕ pf : Partrec f hf : ∃ c, eval c = f ⊢ ∃ c, eval c = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> f (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf =>
rcases hf with ⟨cf, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro f : ℕ →. ℕ cf : Code pf : Partrec (eval cf) ⊢ ∃ c, eval c = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> eval cf (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩
refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro f : ℕ →. ℕ cf : Code pf : Partrec (eval cf) ⊢ eval (comp (rfind' cf) (pair Code.id zero)) = fun a => Nat.rfind fun n => (fun m => decide (m = 0)) <$> eval cf (Nat.pair 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 }) 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩
simp [eval, Seq.seq, pure, PFun.pure, Part.map_id']
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
f : ℕ →. ℕ h : ∃ c, eval c = f ⊢ Partrec f
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by
rcases h with ⟨c, rfl⟩
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro c : Code ⊢ Partrec (eval c)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩;
induction c
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩;
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.zero ⊢ Partrec (eval zero) case intro.succ ⊢ Partrec (eval succ) case intro.left ⊢ Partrec (eval left) case intro.right ⊢ Partrec (eval right) case intro.pair a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (pair a✝¹ a✝)) case intro.comp a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (comp a✝¹ a✝)) case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c
case zero => exact Nat.Partrec.zero
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c
case zero => exact Nat.Partrec.zero
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero =>
exact Nat.Partrec.zero
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.succ ⊢ Partrec (eval succ) case intro.left ⊢ Partrec (eval left) case intro.right ⊢ Partrec (eval right) case intro.pair a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (pair a✝¹ a✝)) case intro.comp a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (comp a✝¹ a✝)) case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero
case succ => exact Nat.Partrec.succ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero
case succ => exact Nat.Partrec.succ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ =>
exact Nat.Partrec.succ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.left ⊢ Partrec (eval left) case intro.right ⊢ Partrec (eval right) case intro.pair a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (pair a✝¹ a✝)) case intro.comp a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (comp a✝¹ a✝)) case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ
case left => exact Nat.Partrec.left
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ
case left => exact Nat.Partrec.left
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left =>
exact Nat.Partrec.left
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.right ⊢ Partrec (eval right) case intro.pair a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (pair a✝¹ a✝)) case intro.comp a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (comp a✝¹ a✝)) case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left
case right => exact Nat.Partrec.right
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left
case right => exact Nat.Partrec.right
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
⊢ Partrec (eval 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right =>
exact Nat.Partrec.right
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.pair a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (pair a✝¹ a✝)) case intro.comp a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (comp a✝¹ a✝)) case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right
case pair cf cg pf pg => exact pf.pair pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf cg : Code pf : Partrec (eval cf) pg : Partrec (eval cg) ⊢ Partrec (eval (pair cf cg))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right
case pair cf cg pf pg => exact pf.pair pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf cg : Code pf : Partrec (eval cf) pg : Partrec (eval cg) ⊢ Partrec (eval (pair cf cg))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg =>
exact pf.pair pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.comp a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (comp a✝¹ a✝)) case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg
case comp cf cg pf pg => exact pf.comp pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf cg : Code pf : Partrec (eval cf) pg : Partrec (eval cg) ⊢ Partrec (eval (comp cf cg))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg
case comp cf cg pf pg => exact pf.comp pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf cg : Code pf : Partrec (eval cf) pg : Partrec (eval cg) ⊢ Partrec (eval (comp cf cg))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg =>
exact pf.comp pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.prec a✝¹ a✝ : Code a_ih✝¹ : Partrec (eval a✝¹) a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (prec a✝¹ a✝)) case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg
case prec cf cg pf pg => exact pf.prec pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf cg : Code pf : Partrec (eval cf) pg : Partrec (eval cg) ⊢ Partrec (eval (prec cf cg))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg
case prec cf cg pf pg => exact pf.prec pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf cg : Code pf : Partrec (eval cf) pg : Partrec (eval cg) ⊢ Partrec (eval (prec cf cg))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg =>
exact pf.prec pg
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
case intro.rfind' a✝ : Code a_ih✝ : Partrec (eval a✝) ⊢ Partrec (eval (rfind' 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 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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg
case rfind' cf pf => exact pf.rfind'
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf : Code pf : Partrec (eval cf) ⊢ Partrec (eval (rfind' cf))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg
case rfind' cf pf => exact pf.rfind'
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
cf : Code pf : Partrec (eval cf) ⊢ Partrec (eval (rfind' cf))
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf =>
exact pf.rfind'
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf =>
Mathlib.Computability.PartrecCode.698_0.A3c3Aev6SyIRjCJ
/-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := pair cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := pair cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ sizeOf cf < sizeOf (pair cf cg)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cg } { fst := Nat.succ k, snd := pair cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cg } { fst := Nat.succ k, snd := pair cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ sizeOf cg < sizeOf (pair cf cg)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cg } { fst := Nat.succ k, snd := comp cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cg } { fst := Nat.succ k, snd := comp cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ sizeOf cg < sizeOf (comp cf cg)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := comp cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := comp cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ sizeOf cf < sizeOf (comp cf cg)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := prec cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := prec cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ sizeOf cf < sizeOf (prec cf cg)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k, snd := prec cf cg } { fst := Nat.succ k, snd := prec cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k, snd := prec cf cg } { fst := Nat.succ k, snd := prec cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ k < Nat.succ k
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cg } { fst := Nat.succ k, snd := prec cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf cg : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cg } { fst := Nat.succ k, snd := prec cf cg }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf cg : Code ⊢ sizeOf cg < sizeOf (prec cf cg)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := rfind' cf }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k + 1, snd := cf } { fst := Nat.succ k, snd := rfind' cf }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf : Code ⊢ sizeOf cf < sizeOf (rfind' cf)
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k, snd := rfind' cf } { fst := Nat.succ k, snd := rfind' cf }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
{ decreasing_with simp (config := { arith := true }) [Zero.zero]; done }
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
k : ℕ cf : Code ⊢ (invImage (fun a => PSigma.casesOn a fun k snd => (k, snd)) Prod.instWellFoundedRelationProd).1 { fst := k, snd := rfind' cf } { fst := Nat.succ k, snd := rfind' cf }
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
decreasing_with simp (config := { arith := true }) [Zero.zero]; done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by {
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
case h k : ℕ cf : Code ⊢ k < Nat.succ k
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
simp (config := { arith := true }) [Zero.zero]
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
done
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero];
Mathlib.Computability.PartrecCode.732_0.A3c3Aev6SyIRjCJ
/-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config
Mathlib_Computability_PartrecCode
c : Code n x : ℕ h : x ∈ evaln 0 c n ⊢ n < 0
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by
simp [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
k : ℕ c : Code n x : ℕ h : x ∈ evaln (k + 1) c n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by
suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
k : ℕ c : Code n x : ℕ h : x ∈ evaln (k + 1) c n this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by
cases c
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case zero k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ evaln (k + 1) zero n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case succ k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ evaln (k + 1) succ n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case left k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ evaln (k + 1) left n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case right k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ evaln (k + 1) right n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case pair k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝¹ a✝ : Code h : x ∈ evaln (k + 1) (pair a✝¹ a✝) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case comp k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝¹ a✝ : Code h : x ∈ evaln (k + 1) (comp a✝¹ a✝) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case prec k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝¹ a✝ : Code h : x ∈ evaln (k + 1) (prec a✝¹ a✝) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case rfind' k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝ : Code h : x ∈ evaln (k + 1) (rfind' a✝) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
rw [evaln] at h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case zero k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ (fun n => do guard (n ≤ k) pure 0) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case succ k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ (fun n => do guard (n ≤ k) pure (Nat.succ n)) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case left k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ (fun n => do guard (n ≤ k) pure (unpair n).1) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case right k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 h : x ∈ (fun n => do guard (n ≤ k) pure (unpair n).2) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case pair k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝¹ a✝ : Code h : x ∈ (fun n => do guard (n ≤ k) Seq.seq (Nat.pair <$> evaln (k + 1) a✝¹ n) fun x => evaln (k + 1) a✝ n) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case comp k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝¹ a✝ : Code h : x ∈ (fun n => do guard (n ≤ k) let x ← evaln (k + 1) a✝ n evaln (k + 1) a✝¹ x) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case prec k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝¹ a✝ : Code h : x ∈ (fun n => do guard (n ≤ k) unpaired (fun a n => Nat.casesOn n (evaln (k + 1) a✝¹ a) fun y => do let i ← evaln k (prec a✝¹ a✝) (Nat.pair a y) evaln (k + 1) a✝ (Nat.pair a (Nat.pair y i))) n) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
case rfind' k n x : ℕ this : ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1 a✝ : Code h : x ∈ (fun n => do guard (n ≤ k) unpaired (fun a m => do let x ← evaln (k + 1) a✝ (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' a✝) (Nat.pair a (m + 1))) n) n ⊢ n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
exact this h
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;>
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode
k : ℕ c : Code n x : ℕ h : x ∈ evaln (k + 1) c n ⊢ ∀ {o : Option ℕ}, (x ∈ do guard (n ≤ k) o) → n < k + 1
/- 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))) have hm : m < n + 4 := by simp only [div2_val] exact lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _)) (Nat.succ_le_succ (Nat.le_add_right _ _)) have m1 : m.unpair.1 < n + 4 := lt_of_le_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 end section open Computable /-- Recursion on `Nat.Partrec.Code` is computable. -/ theorem rec_computable {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Computable c) {z : α → σ} (hz : Computable z) {s : α → σ} (hs : Computable s) {l : α → σ} (hl : Computable l) {r : α → σ} (hr : Computable r) {pr : α → Code × Code × σ × σ → σ} (hpr : Computable₂ pr) {co : α → Code × Code × σ × σ → σ} (hco : Computable₂ co) {pc : α → Code × Code × σ × σ → σ} (hpc : Computable₂ pc) {rf : α → Code × σ → σ} (hrf : Computable₂ 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) Computable fun a => F a (c a) := by -- TODO(Mario): less copy-paste from previous proof 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 : Computable G₁ := by refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _ unfold Computable₂ refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) _ unfold Computable₂ refine' option_map ((list_get?.comp (snd.comp fst) (snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _ have a : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) := fst.comp (fst.comp <| fst.comp <| fst.comp fst) have n : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) := fst.comp (snd.comp <| fst.comp <| fst.comp fst) have m : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) := snd.comp (snd.comp <| fst.comp <| fst.comp fst) have m₁ := fst.comp (Computable.unpair.comp m) have m₂ := snd.comp (Computable.unpair.comp m) have s : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) := snd.comp (fst.comp fst) have s₁ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) := snd.comp fst have s₂ : Computable (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) := snd exact (nat_bodd.comp n).cond ((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Computable.ofNat Code).comp m).pair s)) (hpc.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂))) (Computable.cond (nat_bodd.comp <| nat_div2.comp n) (hco.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.ofNat Code).comp m₂).pair <| s₁.pair s₂)) (hpr.comp a (((Computable.ofNat Code).comp m₁).pair <| ((Computable.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 : Computable₂ G := Computable.nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) <| Computable.nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) <| Computable.nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) (this.comp <| ((Computable.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 Computable.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_computable Nat.Partrec.Code.rec_computable end /-- The interpretation of a `Nat.Partrec.Code` as a partial function. * `Nat.Partrec.Code.zero`: The constant zero function. * `Nat.Partrec.Code.succ`: The successor function. * `Nat.Partrec.Code.left`: Left unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.right`: Right unpairing of a pair of ℕ (encoded by `Nat.pair`) * `Nat.Partrec.Code.pair`: Pairs the outputs of argument codes using `Nat.pair`. * `Nat.Partrec.Code.comp`: Composition of two argument codes. * `Nat.Partrec.Code.prec`: Primitive recursion. Given an argument of the form `Nat.pair a n`: * If `n = 0`, returns `eval cf a`. * If `n = succ k`, returns `eval cg (pair a (pair k (eval (prec cf cg) (pair a k))))` * `Nat.Partrec.Code.rfind'`: Minimization. For `f` an argument of the form `Nat.pair a m`, `rfind' f m` returns the least `a` such that `f a m = 0`, if one exists and `f b m` terminates for `b < a` -/ def eval : Code → ℕ →. ℕ | zero => pure 0 | succ => Nat.succ | left => ↑fun n : ℕ => n.unpair.1 | right => ↑fun n : ℕ => n.unpair.2 | pair cf cg => fun n => Nat.pair <$> eval cf n <*> eval cg n | comp cf cg => fun n => eval cg n >>= eval cf | prec cf cg => Nat.unpaired fun a n => n.rec (eval cf a) fun y IH => do let i ← IH eval cg (Nat.pair a (Nat.pair y i)) | rfind' cf => Nat.unpaired fun a m => (Nat.rfind fun n => (fun m => m = 0) <$> eval cf (Nat.pair a (n + m))).map (· + m) #align nat.partrec.code.eval Nat.Partrec.Code.eval /-- Helper lemma for the evaluation of `prec` in the base case. -/ @[simp] theorem eval_prec_zero (cf cg : Code) (a : ℕ) : eval (prec cf cg) (Nat.pair a 0) = eval cf a := by rw [eval, Nat.unpaired, Nat.unpair_pair] simp (config := { Lean.Meta.Simp.neutralConfig with proj := true }) only [] rw [Nat.rec_zero] #align nat.partrec.code.eval_prec_zero Nat.Partrec.Code.eval_prec_zero /-- Helper lemma for the evaluation of `prec` in the recursive case. -/ theorem eval_prec_succ (cf cg : Code) (a k : ℕ) : eval (prec cf cg) (Nat.pair a (Nat.succ k)) = do {let ih ← eval (prec cf cg) (Nat.pair a k); eval cg (Nat.pair a (Nat.pair k ih))} := by rw [eval, Nat.unpaired, Part.bind_eq_bind, Nat.unpair_pair] simp #align nat.partrec.code.eval_prec_succ Nat.Partrec.Code.eval_prec_succ instance : Membership (ℕ →. ℕ) Code := ⟨fun f c => eval c = f⟩ @[simp] theorem eval_const : ∀ n m, eval (Code.const n) m = Part.some n | 0, m => rfl | n + 1, m => by simp! [eval_const n m] #align nat.partrec.code.eval_const Nat.Partrec.Code.eval_const @[simp] theorem eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq] #align nat.partrec.code.eval_id Nat.Partrec.Code.eval_id @[simp] theorem eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq] #align nat.partrec.code.eval_curry Nat.Partrec.Code.eval_curry theorem const_prim : Primrec Code.const := (_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero) (comp_prim.comp (_root_.Primrec.const succ) Primrec.snd).to₂).of_eq fun n => by simp; induction n <;> simp [*, Code.const, Function.iterate_succ', -Function.iterate_succ] #align nat.partrec.code.const_prim Nat.Partrec.Code.const_prim theorem curry_prim : Primrec₂ curry := comp_prim.comp Primrec.fst <| pair_prim.comp (const_prim.comp Primrec.snd) (_root_.Primrec.const Code.id) #align nat.partrec.code.curry_prim Nat.Partrec.Code.curry_prim theorem curry_inj {c₁ c₂ n₁ n₂} (h : curry c₁ n₁ = curry c₂ n₂) : c₁ = c₂ ∧ n₁ = n₂ := ⟨by injection h, by injection h with h₁ h₂ injection h₂ with h₃ h₄ exact const_inj h₃⟩ #align nat.partrec.code.curry_inj Nat.Partrec.Code.curry_inj /-- The $S_n^m$ theorem: There is a computable function, namely `Nat.Partrec.Code.curry`, that takes a program and a ℕ `n`, and returns a new program using `n` as the first argument. -/ theorem smn : ∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) := ⟨curry, Primrec₂.to_comp curry_prim, eval_curry⟩ #align nat.partrec.code.smn Nat.Partrec.Code.smn /-- A function is partial recursive if and only if there is a code implementing it. -/ theorem exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := ⟨fun h => by induction h case zero => exact ⟨zero, rfl⟩ case succ => exact ⟨succ, rfl⟩ case left => exact ⟨left, rfl⟩ case right => exact ⟨right, rfl⟩ case pair f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨pair cf cg, rfl⟩ case comp f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨comp cf cg, rfl⟩ case prec f g pf pg hf hg => rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩ exact ⟨prec cf cg, rfl⟩ case rfind f pf hf => rcases hf with ⟨cf, rfl⟩ refine' ⟨comp (rfind' cf) (pair Code.id zero), _⟩ simp [eval, Seq.seq, pure, PFun.pure, Part.map_id'], fun h => by rcases h with ⟨c, rfl⟩; induction c case zero => exact Nat.Partrec.zero case succ => exact Nat.Partrec.succ case left => exact Nat.Partrec.left case right => exact Nat.Partrec.right case pair cf cg pf pg => exact pf.pair pg case comp cf cg pf pg => exact pf.comp pg case prec cf cg pf pg => exact pf.prec pg case rfind' cf pf => exact pf.rfind'⟩ #align nat.partrec.code.exists_code Nat.Partrec.Code.exists_code -- Porting note: `>>`s in `evaln` are now `>>=` because `>>`s are not elaborated well in Lean4. /-- A modified evaluation for the code which returns an `Option ℕ` instead of a `Part ℕ`. To avoid undecidability, `evaln` takes a parameter `k` and fails if it encounters a number ≥ k in the course of its execution. Other than this, the semantics are the same as in `Nat.Partrec.Code.eval`. -/ def evaln : ℕ → Code → ℕ → Option ℕ | 0, _ => fun _ => Option.none | k + 1, zero => fun n => do guard (n ≤ k) return 0 | k + 1, succ => fun n => do guard (n ≤ k) return (Nat.succ n) | k + 1, left => fun n => do guard (n ≤ k) return n.unpair.1 | k + 1, right => fun n => do guard (n ≤ k) pure n.unpair.2 | k + 1, pair cf cg => fun n => do guard (n ≤ k) Nat.pair <$> evaln (k + 1) cf n <*> evaln (k + 1) cg n | k + 1, comp cf cg => fun n => do guard (n ≤ k) let x ← evaln (k + 1) cg n evaln (k + 1) cf x | k + 1, prec cf cg => fun n => do guard (n ≤ k) n.unpaired fun a n => n.casesOn (evaln (k + 1) cf a) fun y => do let i ← evaln k (prec cf cg) (Nat.pair a y) evaln (k + 1) cg (Nat.pair a (Nat.pair y i)) | k + 1, rfind' cf => fun n => do guard (n ≤ k) n.unpaired fun a m => do let x ← evaln (k + 1) cf (Nat.pair a m) if x = 0 then pure m else evaln k (rfind' cf) (Nat.pair a (m + 1)) termination_by evaln k c => (k, c) decreasing_by { decreasing_with simp (config := { arith := true }) [Zero.zero]; done } #align nat.partrec.code.evaln Nat.Partrec.Code.evaln theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h
simpa [Bind.bind] using Nat.lt_succ_of_le
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h
Mathlib.Computability.PartrecCode.775_0.A3c3Aev6SyIRjCJ
theorem evaln_bound : ∀ {k c n x}, x ∈ evaln k c n → n < k | 0, c, n, x, h => by simp [evaln] at h | k + 1, c, n, x, h => by suffices ∀ {o : Option ℕ}, x ∈ do { guard (n ≤ k); o } → n < k + 1 by cases c <;> rw [evaln] at h <;> exact this h simpa [Bind.bind] using Nat.lt_succ_of_le
Mathlib_Computability_PartrecCode