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
|
---|---|---|---|---|---|---|
case succ.prec.intro.succ
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf cg : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (prec cf cg)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (prec cf cg)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (prec cf cg)
lg : encode cg < encode (prec cf cg)
n✝ : ℕ
⊢ (Option.bind
(Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (prec cf cg)))))
(k', prec cf cg) (Nat.pair (unpair n).1 n✝))
fun i =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (prec cf cg)))))
(k' + 1, cg) (Nat.pair (unpair n).1 (Nat.pair n✝ i))) =
Option.bind (evaln k' (prec cf cg) (Nat.pair (unpair n).1 n✝)) fun i =>
evaln (k' + 1) cg (Nat.pair (unpair n).1 (Nat.pair n✝ i))
|
/-
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
|
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.prec.intro.succ
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf cg : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (prec cf cg)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (prec cf cg)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (prec cf cg)
lg : encode cg < encode (prec cf cg)
n✝ : ℕ
⊢ (Option.bind (evaln k' (prec cf cg) (Nat.pair (unpair n).1 n✝)) fun i =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (prec cf cg)))))
(k' + 1, cg) (Nat.pair (unpair n).1 (Nat.pair n✝ i))) =
Option.bind (evaln k' (prec cf cg) (Nat.pair (unpair n).1 n✝)) fun i =>
evaln (k' + 1) cg (Nat.pair (unpair n).1 (Nat.pair n✝ i))
|
/-
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
|
cases evaln k' _ _
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.prec.intro.succ.none
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf cg : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (prec cf cg)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (prec cf cg)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (prec cf cg)
lg : encode cg < encode (prec cf cg)
n✝ : ℕ
⊢ (Option.bind Option.none fun i =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (prec cf cg)))))
(k' + 1, cg) (Nat.pair (unpair n).1 (Nat.pair n✝ i))) =
Option.bind Option.none fun i => evaln (k' + 1) cg (Nat.pair (unpair n).1 (Nat.pair n✝ i))
|
/-
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
·
|
rfl
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
·
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.prec.intro.succ.some
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf cg : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (prec cf cg)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (prec cf cg)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (prec cf cg)
lg : encode cg < encode (prec cf cg)
n✝ val✝ : ℕ
⊢ (Option.bind (some val✝) fun i =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (prec cf cg)))))
(k' + 1, cg) (Nat.pair (unpair n).1 (Nat.pair n✝ i))) =
Option.bind (some val✝) fun i => evaln (k' + 1) cg (Nat.pair (unpair n).1 (Nat.pair n✝ i))
|
/-
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
|
simp [hg (Nat.pair_lt_pair_right _ lg)]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
⊢ (Option.bind
(Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k' + 1, cf) n)
fun x =>
Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
x) =
Option.bind (evaln (k' + 1) cf n) fun x =>
if x = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
·
|
have lf := encode_lt_rfind' cf
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
·
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
⊢ (Option.bind
(Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k' + 1, cf) n)
fun x =>
Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
x) =
Option.bind (evaln (k' + 1) cf n) fun x =>
if x = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
|
rw [hg (Nat.pair_lt_pair_right _ lf)]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
⊢ (Option.bind (evaln k cf n) fun x =>
Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
x) =
Option.bind (evaln (k' + 1) cf n) fun x =>
if x = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
|
cases' evaln k cf n with x
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'.none
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
⊢ (Option.bind Option.none fun x =>
Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
x) =
Option.bind Option.none fun x =>
if x = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
·
|
rfl
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
·
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'.some
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
x : ℕ
⊢ (Option.bind (some x) fun x =>
Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
x) =
Option.bind (some x) fun x =>
if x = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
|
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'.some
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
x : ℕ
⊢ Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
x =
if x = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
|
cases x
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'.some.zero
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
⊢ Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
Nat.zero =
if Nat.zero = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;>
|
simp [Nat.succ_ne_zero]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;>
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'.some.succ
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
n✝ : ℕ
⊢ Nat.rec (some (unpair n).2)
(fun n_1 n_ih =>
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)))
(Nat.succ n✝) =
if Nat.succ n✝ = 0 then some (unpair n).2 else evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;>
|
simp [Nat.succ_ne_zero]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;>
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
case succ.rfind'.some.succ
x✝ : Unit
p n : ℕ
this : List.range p = List.range (Nat.pair (unpair p).1 (encode (ofNat Code (unpair p).2)))
k' : ℕ
k : ℕ := k' + 1
nk : n ≤ k'
cf : Code
hg :
∀ {k' : ℕ} {c' : Code} {n : ℕ},
Nat.pair k' (encode c') < Nat.pair k (encode (rfind' cf)) →
Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair k (encode (rfind' cf)))))
(k', c') n =
evaln k' c' n
lf : encode cf < encode (rfind' cf)
n✝ : ℕ
⊢ Nat.Partrec.Code.lup
(List.map (fun n => List.map (evaln (unpair n).1 (ofNat Code (unpair n).2)) (List.range (unpair n).1))
(List.range (Nat.pair (k' + 1) (encode (rfind' cf)))))
(k', rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 1)) =
evaln k' (rfind' cf) (Nat.pair (unpair n).1 ((unpair n).2 + 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
|
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
this :
Primrec₂ fun x n =>
let a := ofNat (ℕ × Code) n;
List.map (evaln a.1 a.2) (List.range a.1)
x✝ : (ℕ × Code) × ℕ
k : ℕ
c : Code
n : ℕ
⊢ (Option.bind
(List.get?
(let a := ofNat (ℕ × Code) (encode ((k, c), n).1);
List.map (evaln a.1 a.2) (List.range a.1))
((k, c), n).2)
fun b => (((k, c), n), b).2) =
evaln ((k, c), n).1.1 ((k, c), n).1.2 ((k, c), n).2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by
|
simp [evaln_map]
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by
|
Mathlib.Computability.PartrecCode.1088_0.A3c3Aev6SyIRjCJ
|
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2
|
Mathlib_Computability_PartrecCode
|
c : Code
n x : ℕ
⊢ x ∈ eval c n ↔ x ∈ rfindOpt fun k => evaln k c 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'],
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
|
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
|
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
|
Mathlib.Computability.PartrecCode.1157_0.A3c3Aev6SyIRjCJ
|
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n
|
Mathlib_Computability_PartrecCode
|
c : Code
n x : ℕ
⊢ ∀ {a m n_1 : ℕ}, m ≤ n_1 → a ∈ evaln m c n → a ∈ evaln n_1 c 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'],
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
|
intro a m n hl
|
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
|
Mathlib.Computability.PartrecCode.1157_0.A3c3Aev6SyIRjCJ
|
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n
|
Mathlib_Computability_PartrecCode
|
c : Code
n✝ x a m n : ℕ
hl : m ≤ n
⊢ a ∈ evaln m c n✝ → a ∈ evaln n c 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'],
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl;
|
apply evaln_mono hl
|
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl;
|
Mathlib.Computability.PartrecCode.1157_0.A3c3Aev6SyIRjCJ
|
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n
|
Mathlib_Computability_PartrecCode
|
a : Code × ℕ
⊢ (rfindOpt fun b =>
evaln (((a, b).2, (a, b).1.1), (a, b).1.2).1.1 (((a, b).2, (a, b).1.1), (a, b).1.2).1.2
(((a, b).2, (a, b).1.1), (a, b).1.2).2) =
eval a.1 a.2
|
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Computability.Partrec
#align_import computability.partrec_code from "leanprover-community/mathlib"@"6155d4351090a6fad236e3d2e4e0e4e7342668e8"
/-!
# Gödel Numbering for Partial Recursive Functions.
This file defines `Nat.Partrec.Code`, an inductive datatype describing code for partial
recursive functions on ℕ. It defines an encoding for these codes, and proves that the constructors
are primitive recursive with respect to the encoding.
It also defines the evaluation of these codes as partial functions using `PFun`, and proves that a
function is partially recursive (as defined by `Nat.Partrec`) if and only if it is the evaluation
of some code.
## Main Definitions
* `Nat.Partrec.Code`: Inductive datatype for partial recursive codes.
* `Nat.Partrec.Code.encodeCode`: A (computable) encoding of codes as natural numbers.
* `Nat.Partrec.Code.ofNatCode`: The inverse of this encoding.
* `Nat.Partrec.Code.eval`: The interpretation of a `Nat.Partrec.Code` as a partial function.
## Main Results
* `Nat.Partrec.Code.rec_prim`: Recursion on `Nat.Partrec.Code` is primitive recursive.
* `Nat.Partrec.Code.rec_computable`: Recursion on `Nat.Partrec.Code` is computable.
* `Nat.Partrec.Code.smn`: The $S_n^m$ theorem.
* `Nat.Partrec.Code.exists_code`: Partial recursiveness is equivalent to being the eval of a code.
* `Nat.Partrec.Code.evaln_prim`: `evaln` is primitive recursive.
* `Nat.Partrec.Code.fixed_point`: Roger's fixed point theorem.
## References
* [Mario Carneiro, *Formalizing computability theory via partial recursive functions*][carneiro2019]
-/
open Encodable Denumerable Primrec
namespace Nat.Partrec
open Nat (pair)
theorem rfind' {f} (hf : Nat.Partrec f) :
Nat.Partrec
(Nat.unpaired fun a m =>
(Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + m))).map (· + m)) :=
Partrec₂.unpaired'.2 <| by
refine'
Partrec.map
((@Partrec₂.unpaired' fun a b : ℕ =>
Nat.rfind fun n => (fun m => m = 0) <$> f (Nat.pair a (n + b))).1
_)
(Primrec.nat_add.comp Primrec.snd <| Primrec.snd.comp Primrec.fst).to_comp.to₂
have : Nat.Partrec (fun a => Nat.rfind (fun n => (fun m => decide (m = 0)) <$>
Nat.unpaired (fun a b => f (Nat.pair (Nat.unpair a).1 (b + (Nat.unpair a).2)))
(Nat.pair a n))) :=
rfind
(Partrec₂.unpaired'.2
((Partrec.nat_iff.2 hf).comp
(Primrec₂.pair.comp (Primrec.fst.comp <| Primrec.unpair.comp Primrec.fst)
(Primrec.nat_add.comp Primrec.snd
(Primrec.snd.comp <| Primrec.unpair.comp Primrec.fst))).to_comp))
simp at this; exact this
#align nat.partrec.rfind' Nat.Partrec.rfind'
/-- Code for partial recursive functions from ℕ to ℕ.
See `Nat.Partrec.Code.eval` for the interpretation of these constructors.
-/
inductive Code : Type
| zero : Code
| succ : Code
| left : Code
| right : Code
| pair : Code → Code → Code
| comp : Code → Code → Code
| prec : Code → Code → Code
| rfind' : Code → Code
#align nat.partrec.code Nat.Partrec.Code
-- Porting note: `Nat.Partrec.Code.recOn` is noncomputable in Lean4, so we make it computable.
compile_inductive% Code
end Nat.Partrec
namespace Nat.Partrec.Code
open Nat (pair unpair)
open Nat.Partrec (Code)
instance instInhabited : Inhabited Code :=
⟨zero⟩
#align nat.partrec.code.inhabited Nat.Partrec.Code.instInhabited
/-- Returns a code for the constant function outputting a particular natural. -/
protected def const : ℕ → Code
| 0 => zero
| n + 1 => comp succ (Code.const n)
#align nat.partrec.code.const Nat.Partrec.Code.const
theorem const_inj : ∀ {n₁ n₂}, Nat.Partrec.Code.const n₁ = Nat.Partrec.Code.const n₂ → n₁ = n₂
| 0, 0, _ => by simp
| n₁ + 1, n₂ + 1, h => by
dsimp [Nat.add_one, Nat.Partrec.Code.const] at h
injection h with h₁ h₂
simp only [const_inj h₂]
#align nat.partrec.code.const_inj Nat.Partrec.Code.const_inj
/-- A code for the identity function. -/
protected def id : Code :=
pair left right
#align nat.partrec.code.id Nat.Partrec.Code.id
/-- Given a code `c` taking a pair as input, returns a code using `n` as the first argument to `c`.
-/
def curry (c : Code) (n : ℕ) : Code :=
comp c (pair (Code.const n) Code.id)
#align nat.partrec.code.curry Nat.Partrec.Code.curry
-- Porting note: `bit0` and `bit1` are deprecated.
/-- An encoding of a `Nat.Partrec.Code` as a ℕ. -/
def encodeCode : Code → ℕ
| zero => 0
| succ => 1
| left => 2
| right => 3
| pair cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 4
| comp cf cg => 2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg) + 1) + 4
| prec cf cg => (2 * (2 * Nat.pair (encodeCode cf) (encodeCode cg)) + 1) + 4
| rfind' cf => (2 * (2 * encodeCode cf + 1) + 1) + 4
#align nat.partrec.code.encode_code Nat.Partrec.Code.encodeCode
/--
A decoder for `Nat.Partrec.Code.encodeCode`, taking any ℕ to the `Nat.Partrec.Code` it represents.
-/
def ofNatCode : ℕ → Code
| 0 => zero
| 1 => succ
| 2 => left
| 3 => right
| n + 4 =>
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
match n.bodd, n.div2.bodd with
| false, false => pair (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| false, true => comp (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , false => prec (ofNatCode m.unpair.1) (ofNatCode m.unpair.2)
| true , true => rfind' (ofNatCode m)
#align nat.partrec.code.of_nat_code Nat.Partrec.Code.ofNatCode
/-- Proof that `Nat.Partrec.Code.ofNatCode` is the inverse of `Nat.Partrec.Code.encodeCode`-/
private theorem encode_ofNatCode : ∀ n, encodeCode (ofNatCode n) = n
| 0 => by simp [ofNatCode, encodeCode]
| 1 => by simp [ofNatCode, encodeCode]
| 2 => by simp [ofNatCode, encodeCode]
| 3 => by simp [ofNatCode, encodeCode]
| n + 4 => by
let m := n.div2.div2
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have _m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have _m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
have IH := encode_ofNatCode m
have IH1 := encode_ofNatCode m.unpair.1
have IH2 := encode_ofNatCode m.unpair.2
conv_rhs => rw [← Nat.bit_decomp n, ← Nat.bit_decomp n.div2]
simp only [ofNatCode._eq_5]
cases n.bodd <;> cases n.div2.bodd <;>
simp [encodeCode, ofNatCode, IH, IH1, IH2, Nat.bit_val]
instance instDenumerable : Denumerable Code :=
mk'
⟨encodeCode, ofNatCode, fun c => by
induction c <;> try {rfl} <;> simp [encodeCode, ofNatCode, Nat.div2_val, *],
encode_ofNatCode⟩
#align nat.partrec.code.denumerable Nat.Partrec.Code.instDenumerable
theorem encodeCode_eq : encode = encodeCode :=
rfl
#align nat.partrec.code.encode_code_eq Nat.Partrec.Code.encodeCode_eq
theorem ofNatCode_eq : ofNat Code = ofNatCode :=
rfl
#align nat.partrec.code.of_nat_code_eq Nat.Partrec.Code.ofNatCode_eq
theorem encode_lt_pair (cf cg) :
encode cf < encode (pair cf cg) ∧ encode cg < encode (pair cf cg) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right (Nat.pair cf.encodeCode cg.encodeCode) (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
have := lt_of_le_of_lt this (lt_add_of_pos_right _ (by decide : 0 < 4))
exact ⟨lt_of_le_of_lt (Nat.left_le_pair _ _) this, lt_of_le_of_lt (Nat.right_le_pair _ _) this⟩
#align nat.partrec.code.encode_lt_pair Nat.Partrec.Code.encode_lt_pair
theorem encode_lt_comp (cf cg) :
encode cf < encode (comp cf cg) ∧ encode cg < encode (comp cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_comp Nat.Partrec.Code.encode_lt_comp
theorem encode_lt_prec (cf cg) :
encode cf < encode (prec cf cg) ∧ encode cg < encode (prec cf cg) := by
suffices; exact (encode_lt_pair cf cg).imp (fun h => lt_trans h this) fun h => lt_trans h this
change _; simp [encodeCode_eq, encodeCode]
#align nat.partrec.code.encode_lt_prec Nat.Partrec.Code.encode_lt_prec
theorem encode_lt_rfind' (cf) : encode cf < encode (rfind' cf) := by
simp only [encodeCode_eq, encodeCode]
have := Nat.mul_le_mul_right cf.encodeCode (by decide : 1 ≤ 2 * 2)
rw [one_mul, mul_assoc] at this
refine' lt_of_le_of_lt (le_trans this _) (lt_add_of_pos_right _ (by decide : 0 < 4))
exact le_of_lt (Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_of_lt <|
Nat.lt_succ_of_le <| Nat.mul_le_mul_left _ <| le_rfl)
#align nat.partrec.code.encode_lt_rfind' Nat.Partrec.Code.encode_lt_rfind'
section
theorem pair_prim : Primrec₂ pair :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.pair_prim Nat.Partrec.Code.pair_prim
theorem comp_prim : Primrec₂ comp :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double.comp <|
nat_double_succ.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.comp_prim Nat.Partrec.Code.comp_prim
theorem prec_prim : Primrec₂ prec :=
Primrec₂.ofNat_iff.2 <|
Primrec₂.encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <|
nat_double.comp <|
Primrec₂.natPair.comp (encode_iff.2 <| (Primrec.ofNat Code).comp fst)
(encode_iff.2 <| (Primrec.ofNat Code).comp snd))
(Primrec₂.const 4)
#align nat.partrec.code.prec_prim Nat.Partrec.Code.prec_prim
theorem rfind_prim : Primrec rfind' :=
ofNat_iff.2 <|
encode_iff.1 <|
nat_add.comp
(nat_double_succ.comp <| nat_double_succ.comp <|
encode_iff.2 <| Primrec.ofNat Code)
(const 4)
#align nat.partrec.code.rfind_prim Nat.Partrec.Code.rfind_prim
theorem rec_prim' {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intros _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine'
option_bind
((list_get?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) _
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond (hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a
(((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn
(list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show ofNat Code (n + 4) = ofNatCode (n + 4) from rfl]
simp [ofNatCode]
cases n.bodd <;> cases n.div2.bodd <;> rfl
#align nat.partrec.code.rec_prim' Nat.Partrec.Code.rec_prim'
/-- Recursion on `Nat.Partrec.Code` is primitive recursive. -/
theorem rec_prim {α σ} [Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{rf : α → Code → σ → σ} (hrf : Primrec fun a : α × Code × σ => rf a.1 a.2.1 a.2.2) :
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (pr a) (co a) (pc a) (rf a)
Primrec fun a => F a (c a) := by
intros F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
let a := p.1.1
let IH := p.1.2
let n := p.2.1
let m := p.2.2
(IH.get? m).bind fun s =>
(IH.get? m.unpair.1).bind fun s₁ =>
(IH.get? m.unpair.2).map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m) s)
(pc a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂)
(pr a (ofNat Code m.unpair.1) (ofNat Code m.unpair.2) s₁ s₂))
have : Primrec G₁ := by
refine' option_bind (list_get?.comp (snd.comp fst) (snd.comp snd)) _
unfold Primrec₂
refine' option_bind ((list_get?.comp (snd.comp fst) (fst.comp <| Primrec.unpair.comp
(snd.comp snd))).comp fst) _
unfold Primrec₂
refine'
option_map
((list_get?.comp (snd.comp fst) (snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <|
fst.comp fst)
_
have a : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.1.1) :=
fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.1) :=
fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.1.2.2) :=
snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.1.2) :=
snd.comp (fst.comp fst)
have s₁ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.1.2) :=
snd.comp fst
have s₂ : Primrec (fun p : ((((α × List σ) × ℕ × ℕ) × σ) × σ) × σ => p.2) :=
snd
have h₁ := hrf.comp <| a.pair (((Primrec.ofNat Code).comp m).pair s)
have h₂ := hpc.comp <| a.pair (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₃ := hco.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
have h₄ := hpr.comp <| a.pair
(((Primrec.ofNat Code).comp m₁).pair <| ((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)
unfold Primrec₂
exact
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond h₁ h₂)
(cond (nat_bodd.comp <| nat_div2.comp n) h₃ h₄)
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
n.casesOn (some (l a)) fun n =>
n.casesOn (some (r a)) fun n => G₁ ((a, IH), n, n.div2.div2)
have : Primrec₂ G := by
unfold Primrec₂
refine nat_casesOn (list_length.comp snd) (option_some_iff.2 (hz.comp fst)) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hs.comp (fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hl.comp (fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
refine nat_casesOn snd (option_some_iff.2 (hr.comp (fst.comp <| fst.comp <| fst.comp fst))) ?_
unfold Primrec₂
exact this.comp <|
((fst.pair snd).comp <| fst.comp <| fst.comp <| fst.comp <| fst).pair <|
snd.pair <| nat_div2.comp <| nat_div2.comp snd
refine'
((nat_strong_rec (fun a n => F a (ofNat Code n)) this.to₂ fun a n => _).comp
_root_.Primrec.id <| encode_iff.2 hc).of_eq
fun a => by simp
simp (config := { zeta := false })
iterate 4 cases' n with n; · simp (config := { zeta := false }) [ofNatCode_eq, ofNatCode]; rfl
simp only []
rw [List.length_map, List.length_range]
let m := n.div2.div2
show
G₁ ((a, (List.range (n + 4)).map fun n => F a (ofNat Code n)), n, m) =
some (F a (ofNat Code (n + 4)))
have hm : m < n + 4 := by
simp only [div2_val]
exact
lt_of_le_of_lt (le_trans (Nat.div_le_self _ _) (Nat.div_le_self _ _))
(Nat.succ_le_succ (Nat.le_add_right _ _))
have m1 : m.unpair.1 < n + 4 := lt_of_le_of_lt m.unpair_left_le hm
have m2 : m.unpair.2 < n + 4 := lt_of_le_of_lt m.unpair_right_le hm
simp [List.get?_map, List.get?_range, hm, m1, m2]
rw [show 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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl; apply evaln_mono hl
#align nat.partrec.code.eval_eq_rfind_opt Nat.Partrec.Code.eval_eq_rfindOpt
theorem eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(evaln_prim.to_comp.comp ((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by
|
simp [eval_eq_rfindOpt]
|
theorem eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(evaln_prim.to_comp.comp ((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by
|
Mathlib.Computability.PartrecCode.1163_0.A3c3Aev6SyIRjCJ
|
theorem eval_part : Partrec₂ eval
|
Mathlib_Computability_PartrecCode
|
f : Code → Code
hf : Computable f
g : ℕ → ℕ → Part ℕ :=
fun x y => do
let b ← eval (ofNat Code x) x
eval (ofNat Code b) y
this : Partrec₂ g
cg : Code
eg : eval cg = fun n => Part.bind ↑(decode n) fun a => Part.map encode ((fun p => g p.1 p.2) a)
⊢ ∀ (a n : ℕ), eval cg (Nat.pair a n) = Part.map encode (g 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'],
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl; apply evaln_mono hl
#align nat.partrec.code.eval_eq_rfind_opt Nat.Partrec.Code.eval_eq_rfindOpt
theorem eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(evaln_prim.to_comp.comp ((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by simp [eval_eq_rfindOpt]
#align nat.partrec.code.eval_part Nat.Partrec.Code.eval_part
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by
|
simp [eg]
|
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by
|
Mathlib.Computability.PartrecCode.1169_0.A3c3Aev6SyIRjCJ
|
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c
|
Mathlib_Computability_PartrecCode
|
f : Code → Code
hf : Computable f
g : ℕ → ℕ → Part ℕ :=
fun x y => do
let b ← eval (ofNat Code x) x
eval (ofNat Code b) y
this✝ : Partrec₂ g
cg : Code
eg : eval cg = fun n => Part.bind ↑(decode n) fun a => Part.map encode ((fun p => g p.1 p.2) a)
eg' : ∀ (a n : ℕ), eval cg (Nat.pair a n) = Part.map encode (g a n)
F : ℕ → Code := fun x => f (curry cg x)
this : Computable F
cF : Code
eF : eval cF = fun n => Part.bind ↑(decode n) fun a => Part.map encode (↑F a)
⊢ eval cF (encode cF) = Part.some (encode (F (encode 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 }
#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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl; apply evaln_mono hl
#align nat.partrec.code.eval_eq_rfind_opt Nat.Partrec.Code.eval_eq_rfindOpt
theorem eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(evaln_prim.to_comp.comp ((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by simp [eval_eq_rfindOpt]
#align nat.partrec.code.eval_part Nat.Partrec.Code.eval_part
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by simp [eg]
let F (x : ℕ) : Code := f (curry cg x)
have : Computable F :=
hf.comp (curry_prim.comp (_root_.Primrec.const cg) _root_.Primrec.id).to_comp
let ⟨cF, eF⟩ := exists_code.1 this
have eF' : eval cF (encode cF) = Part.some (encode (F (encode cF))) := by
|
simp [eF]
|
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by simp [eg]
let F (x : ℕ) : Code := f (curry cg x)
have : Computable F :=
hf.comp (curry_prim.comp (_root_.Primrec.const cg) _root_.Primrec.id).to_comp
let ⟨cF, eF⟩ := exists_code.1 this
have eF' : eval cF (encode cF) = Part.some (encode (F (encode cF))) := by
|
Mathlib.Computability.PartrecCode.1169_0.A3c3Aev6SyIRjCJ
|
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c
|
Mathlib_Computability_PartrecCode
|
f : Code → Code
hf : Computable f
g : ℕ → ℕ → Part ℕ :=
fun x y => do
let b ← eval (ofNat Code x) x
eval (ofNat Code b) y
this✝ : Partrec₂ g
cg : Code
eg : eval cg = fun n => Part.bind ↑(decode n) fun a => Part.map encode ((fun p => g p.1 p.2) a)
eg' : ∀ (a n : ℕ), eval cg (Nat.pair a n) = Part.map encode (g a n)
F : ℕ → Code := fun x => f (curry cg x)
this : Computable F
cF : Code
eF : eval cF = fun n => Part.bind ↑(decode n) fun a => Part.map encode (↑F a)
eF' : eval cF (encode cF) = Part.some (encode (F (encode cF)))
n : ℕ
⊢ eval (f (curry cg (encode cF))) n = eval (curry cg (encode cF)) 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'],
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl; apply evaln_mono hl
#align nat.partrec.code.eval_eq_rfind_opt Nat.Partrec.Code.eval_eq_rfindOpt
theorem eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(evaln_prim.to_comp.comp ((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by simp [eval_eq_rfindOpt]
#align nat.partrec.code.eval_part Nat.Partrec.Code.eval_part
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by simp [eg]
let F (x : ℕ) : Code := f (curry cg x)
have : Computable F :=
hf.comp (curry_prim.comp (_root_.Primrec.const cg) _root_.Primrec.id).to_comp
let ⟨cF, eF⟩ := exists_code.1 this
have eF' : eval cF (encode cF) = Part.some (encode (F (encode cF))) := by simp [eF]
⟨curry cg (encode cF),
funext fun n =>
show eval (f (curry cg (encode cF))) n = eval (curry cg (encode cF)) n by
|
simp [eg', eF', Part.map_id']
|
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by simp [eg]
let F (x : ℕ) : Code := f (curry cg x)
have : Computable F :=
hf.comp (curry_prim.comp (_root_.Primrec.const cg) _root_.Primrec.id).to_comp
let ⟨cF, eF⟩ := exists_code.1 this
have eF' : eval cF (encode cF) = Part.some (encode (F (encode cF))) := by simp [eF]
⟨curry cg (encode cF),
funext fun n =>
show eval (f (curry cg (encode cF))) n = eval (curry cg (encode cF)) n by
|
Mathlib.Computability.PartrecCode.1169_0.A3c3Aev6SyIRjCJ
|
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c
|
Mathlib_Computability_PartrecCode
|
f : Code → ℕ →. ℕ
hf : Partrec₂ f
cf : Code
ef : eval cf = fun n => Part.bind ↑(decode n) fun a => Part.map encode ((fun p => f p.1 p.2) a)
c : Code
e : eval (curry cf (encode c)) = eval c
n : ℕ
⊢ eval c n = f c 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'],
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
#align nat.partrec.code.evaln_bound Nat.Partrec.Code.evaln_bound
theorem evaln_mono : ∀ {k₁ k₂ c n x}, k₁ ≤ k₂ → x ∈ evaln k₁ c n → x ∈ evaln k₂ c n
| 0, k₂, c, n, x, _, h => by simp [evaln] at h
| k + 1, k₂ + 1, c, n, x, hl, h => by
have hl' := Nat.le_of_succ_le_succ hl
have :
∀ {k k₂ n x : ℕ} {o₁ o₂ : Option ℕ},
k ≤ k₂ → (x ∈ o₁ → x ∈ o₂) →
x ∈ do { guard (n ≤ k); o₁ } → x ∈ do { guard (n ≤ k₂); o₂ } := by
simp only [Option.mem_def, bind, Option.bind_eq_some, Option.guard_eq_some', exists_and_left,
exists_const, and_imp]
introv h h₁ h₂ h₃
exact ⟨le_trans h₂ h, h₁ h₃⟩
simp? at h ⊢ says simp only [Option.mem_def] at h ⊢
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
rw [evaln] at h ⊢ <;> refine' this hl' (fun h => _) h
iterate 4 exact h
· -- pair cf cg
simp? [Seq.seq] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some,
Option.map_eq_some', exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
· -- comp cf cg
simp? [Bind.bind] at h ⊢ says simp only [bind, Option.mem_def, Option.bind_eq_some] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
· -- prec cf cg
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
· -- rfind' cf
simp? [Bind.bind] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.mem_def, Option.bind_eq_some] at h ⊢
refine' h.imp fun x => And.imp (hf _ _) _
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl'
#align nat.partrec.code.evaln_mono Nat.Partrec.Code.evaln_mono
theorem evaln_sound : ∀ {k c n x}, x ∈ evaln k c n → x ∈ eval c n
| 0, _, n, x, h => by simp [evaln] at h
| k + 1, c, n, x, h => by
induction' c with cf cg hf hg cf cg hf hg cf cg hf hg cf hf generalizing x n <;>
simp [eval, evaln, Bind.bind, Seq.seq] at h ⊢ <;>
cases' h with _ h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
· -- pair cf cg
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
· --comp hf hg
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
· -- prec cf cg
revert h
induction' n.unpair.2 with m IH generalizing x <;> simp
· apply hf
· refine' fun y h₁ h₂ => ⟨y, IH _ _, _⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Bind.bind] at this
exact this.2
· exact hg _ _ h₂
· -- rfind' cf
rcases h with ⟨m, h₁, h₂⟩
by_cases m0 : m = 0 <;> simp [m0] at h₂
· exact
⟨0, ⟨by simpa [m0] using hf _ _ h₁, fun {m} => (Nat.not_lt_zero _).elim⟩, by
injection h₂ with h₂; simp [h₂]⟩
· have := evaln_sound h₂
simp [eval] at this
rcases this with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
refine'
⟨y + 1, ⟨by simpa [add_comm, add_left_comm] using hy₁, fun {i} im => _⟩, by
simp [add_comm, add_left_comm]⟩
cases' i with i
· exact ⟨m, by simpa using hf _ _ h₁, m0⟩
· rcases hy₂ (Nat.lt_of_succ_lt_succ im) with ⟨z, hz, z0⟩
exact ⟨z, by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hz, z0⟩
#align nat.partrec.code.evaln_sound Nat.Partrec.Code.evaln_sound
theorem evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n :=
⟨fun h => by
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x <;> simp [eval, evaln, pure, PFun.pure, Seq.seq, Bind.bind] at h ⊢
iterate 4 exact ⟨⟨_, le_rfl⟩, h.symm⟩
case pair cf cg hf hg =>
rcases h with ⟨x, hx, y, hy, rfl⟩
rcases hf hx with ⟨k₁, hk₁⟩; rcases hg hy with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
refine'
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂, rfl⟩
case comp cf cg hf hg =>
rcases h with ⟨y, hy, hx⟩
rcases hg hy with ⟨k₁, hk₁⟩; rcases hf hx with ⟨k₂, hk₂⟩
refine' ⟨max k₁ k₂, _⟩
exact
⟨le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁, _,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) hk₁,
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂⟩
case prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction' n₂ with m IH generalizing x n <;> simp
· intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
· intro y hy hx
rcases IH hy with ⟨k₁, nk₁, hk₁⟩
rcases hg hx with ⟨k₂, hk₂⟩
refine'
⟨(max k₁ k₂).succ,
Nat.le_succ_of_le <| le_max_of_le_left <|
le_trans (le_max_left _ (Nat.pair n₁ m)) nk₁, y,
evaln_mono (Nat.succ_le_succ <| le_max_left _ _) _,
evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_right _ _) hk₂⟩
simp only [evaln._eq_8, bind, unpaired, unpair_pair, Option.mem_def, Option.bind_eq_some,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
case rfind' cf hf =>
rcases h with ⟨y, ⟨hy₁, hy₂⟩, rfl⟩
suffices ∃ k, y + n.unpair.2 ∈ evaln (k + 1) (rfind' cf) (Nat.pair n.unpair.1 n.unpair.2) by
simpa [evaln, Bind.bind]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction' y with y IH generalizing m <;> simp [evaln, Bind.bind]
· simp at hy₁
rcases hf hy₁ with ⟨k, hk⟩
exact ⟨_, Nat.le_of_lt_succ <| evaln_bound hk, _, hk, by simp; rfl⟩
· rcases hy₂ (Nat.succ_pos _) with ⟨a, ha, a0⟩
rcases hf ha with ⟨k₁, hk₁⟩
rcases IH m.succ (by simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using hy₁)
fun {i} hi => by
simpa [Nat.succ_eq_add_one, add_comm, add_left_comm] using
hy₂ (Nat.succ_lt_succ hi) with
⟨k₂, hk₂⟩
use (max k₁ k₂).succ
rw [zero_add] at hk₁
use Nat.le_succ_of_le <| le_max_of_le_left <| Nat.le_of_lt_succ <| evaln_bound hk₁
use a
use evaln_mono (Nat.succ_le_succ <| Nat.le_succ_of_le <| le_max_left _ _) hk₁
simpa [Nat.succ_eq_add_one, a0, -max_eq_left, -max_eq_right, add_comm, add_left_comm] using
evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk₂,
fun ⟨k, h⟩ => evaln_sound h⟩
#align nat.partrec.code.evaln_complete Nat.Partrec.Code.evaln_complete
section
open Primrec
private def lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L.get? (encode p)
let o ← l.get? n
o
private theorem hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_get?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_get?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd)
private def G (L : List (List (Option ℕ))) : Option (List (Option ℕ)) :=
Option.some <|
let a := ofNat (ℕ × Code) L.length
let k := a.1
let c := a.2
(List.range k).map fun n =>
k.casesOn Option.none fun k' =>
Nat.Partrec.Code.recOn c
(some 0) -- zero
(some (Nat.succ n))
(some n.unpair.1)
(some n.unpair.2)
(fun cf cg _ _ => do
let x ← lup L (k, cf) n
let y ← lup L (k, cg) n
some (Nat.pair x y))
(fun cf cg _ _ => do
let x ← lup L (k, cg) n
lup L (k, cf) x)
(fun cf cg _ _ =>
let z := n.unpair.1
n.unpair.2.casesOn (lup L (k, cf) z) fun y => do
let i ← lup L (k', c) (Nat.pair z y)
lup L (k, cg) (Nat.pair z (Nat.pair y i)))
(fun cf _ =>
let z := n.unpair.1
let m := n.unpair.2
do
let x ← lup L (k, cf) (Nat.pair z m)
x.casesOn (some m) fun _ => lup L (k', c) (Nat.pair z (m + 1)))
private theorem hG : Primrec G := by
have a := (Primrec.ofNat (ℕ × Code)).comp (Primrec.list_length (α := List (Option ℕ)))
have k := Primrec.fst.comp a
refine' Primrec.option_some.comp (Primrec.list_map (Primrec.list_range.comp k) (_ : Primrec _))
replace k := k.comp (Primrec.fst (β := ℕ))
have n := Primrec.snd (α := List (List (Option ℕ))) (β := ℕ)
refine' Primrec.nat_casesOn k (_root_.Primrec.const Option.none) (_ : Primrec _)
have k := k.comp (Primrec.fst (β := ℕ))
have n := n.comp (Primrec.fst (β := ℕ))
have k' := Primrec.snd (α := List (List (Option ℕ)) × ℕ) (β := ℕ)
have c := Primrec.snd.comp (a.comp <| (Primrec.fst (β := ℕ)).comp (Primrec.fst (β := ℕ)))
apply
Nat.Partrec.Code.rec_prim c
(_root_.Primrec.const (some 0))
(Primrec.option_some.comp (_root_.Primrec.succ.comp n))
(Primrec.option_some.comp (Primrec.fst.comp <| Primrec.unpair.comp n))
(Primrec.option_some.comp (Primrec.snd.comp <| Primrec.unpair.comp n))
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cf).pair n) ?_
unfold Primrec₂
conv =>
congr
· ext p
dsimp only []
erw [Option.bind_eq_bind, ← Option.map_eq_bind]
refine Primrec.option_map ((hlup.comp <| L.pair <| (k.pair cg).pair n).comp Primrec.fst) ?_
unfold Primrec₂
exact Primrec₂.natPair.comp (Primrec.snd.comp Primrec.fst) Primrec.snd
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
refine Primrec.option_bind (hlup.comp <| L.pair <| (k.pair cg).pair n) ?_
unfold Primrec₂
have h :=
hlup.comp ((L.comp Primrec.fst).pair <| ((k.pair cf).comp Primrec.fst).pair Primrec.snd)
exact h
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Code × Option ℕ × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have cg := (Primrec.fst.comp Primrec.snd).comp
(Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Code × Option ℕ × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
refine'
Primrec.nat_casesOn (Primrec.snd.comp (Primrec.unpair.comp n))
(hlup.comp <| L.pair <| (k.pair cf).pair z)
(_ : Primrec _)
have L := L.comp (Primrec.fst (β := ℕ))
have z := z.comp (Primrec.fst (β := ℕ))
have y := Primrec.snd
(α := ((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) (β := ℕ)
have h₁ := hlup.comp <| L.pair <| (((k'.pair c).comp Primrec.fst).comp Primrec.fst).pair
(Primrec₂.natPair.comp z y)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have z := z.comp (Primrec.fst (β := ℕ))
have y := y.comp (Primrec.fst (β := ℕ))
have i := Primrec.snd
(α := (((List (List (Option ℕ)) × ℕ) × ℕ) × Code × Code × Option ℕ × Option ℕ) × ℕ)
(β := ℕ)
have h₂ := hlup.comp ((L.comp Primrec.fst).pair <|
((k.pair cg).comp <| Primrec.fst.comp Primrec.fst).pair <|
Primrec₂.natPair.comp z <| Primrec₂.natPair.comp y i)
exact h₂
· have L := (Primrec.fst.comp Primrec.fst).comp
(Primrec.fst (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have k := k.comp (Primrec.fst (β := Code × Option ℕ))
have n := n.comp (Primrec.fst (β := Code × Option ℕ))
have cf := Primrec.fst.comp (Primrec.snd (α := (List (List (Option ℕ)) × ℕ) × ℕ)
(β := Code × Option ℕ))
have z := Primrec.fst.comp (Primrec.unpair.comp n)
have m := Primrec.snd.comp (Primrec.unpair.comp n)
have h₁ := hlup.comp <| L.pair <| (k.pair cf).pair (Primrec₂.natPair.comp z m)
refine' Primrec.option_bind h₁ (_ : Primrec _)
have m := m.comp (Primrec.fst (β := ℕ))
refine Primrec.nat_casesOn Primrec.snd (Primrec.option_some.comp m) ?_
unfold Primrec₂
exact (hlup.comp ((L.comp Primrec.fst).pair <|
((k'.pair c).comp <| Primrec.fst.comp Primrec.fst).pair
(Primrec₂.natPair.comp (z.comp Primrec.fst) (_root_.Primrec.succ.comp m)))).comp
Primrec.fst
private theorem evaln_map (k c n) :
((((List.range k).get? n).map (evaln k c)).bind fun b => b) = evaln k c n := by
by_cases kn : n < k
· simp [List.get?_range kn]
· rw [List.get?_len_le]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn
/-- The `Nat.Partrec.Code.evaln` function is primitive recursive. -/
theorem evaln_prim : Primrec fun a : (ℕ × Code) × ℕ => evaln a.1.1 a.1.2 a.2 :=
have :
Primrec₂ fun (_ : Unit) (n : ℕ) =>
let a := ofNat (ℕ × Code) n
(List.range a.1).map (evaln a.1 a.2) :=
Primrec.nat_strong_rec _ (hG.comp Primrec.snd).to₂ fun _ p => by
simp only [G, prod_ofNat_val, ofNat_nat, List.length_map, List.length_range,
Nat.pair_unpair, Option.some_inj]
refine List.map_congr fun n => ?_
have : List.range p = List.range (Nat.pair p.unpair.1 (encode (ofNat Code p.unpair.2))) := by
simp
rw [this]
generalize p.unpair.1 = k
generalize ofNat Code p.unpair.2 = c
intro nk
cases' k with k'
· simp [evaln]
let k := k' + 1
simp only [show k'.succ = k from rfl]
simp? [Nat.lt_succ_iff] at nk says simp only [List.mem_range, lt_succ_iff] at nk
have hg :
∀ {k' c' n},
Nat.pair k' (encode c') < Nat.pair k (encode c) →
lup ((List.range (Nat.pair k (encode c))).map fun n =>
(List.range n.unpair.1).map (evaln n.unpair.1 (ofNat Code n.unpair.2))) (k', c') n =
evaln k' c' n := by
intro k₁ c₁ n₁ hl
simp [lup, List.get?_range hl, evaln_map, Bind.bind]
cases' c with cf cg cf cg cf cg cf <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· cases' encode_lt_pair cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf), hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cf n
· rfl
cases evaln k cg n <;> rfl
· cases' encode_lt_comp cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [hg (Nat.pair_lt_pair_right _ lf)]
· cases' encode_lt_prec cf cg with lf lg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only [decode_eq_ofNat, Option.some.injEq]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases' evaln k cf n with x
· rfl
simp only [decode_eq_ofNat, Option.some.injEq, Option.some_bind]
cases x <;> simp [Nat.succ_ne_zero]
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
(Primrec.option_bind
(Primrec.list_get?.comp (this.comp (_root_.Primrec.const ())
(Primrec.encode_iff.2 Primrec.fst)) Primrec.snd) Primrec.snd.to₂).of_eq
fun ⟨⟨k, c⟩, n⟩ => by simp [evaln_map]
#align nat.partrec.code.evaln_prim Nat.Partrec.Code.evaln_prim
end
section
open Partrec Computable
theorem eval_eq_rfindOpt (c n) : eval c n = Nat.rfindOpt fun k => evaln k c n :=
Part.ext fun x => by
refine' evaln_complete.trans (Nat.rfindOpt_mono _).symm
intro a m n hl; apply evaln_mono hl
#align nat.partrec.code.eval_eq_rfind_opt Nat.Partrec.Code.eval_eq_rfindOpt
theorem eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(evaln_prim.to_comp.comp ((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by simp [eval_eq_rfindOpt]
#align nat.partrec.code.eval_part Nat.Partrec.Code.eval_part
/-- Roger's fixed-point theorem: Any total, computable `f` has a fixed point: That is, under the
interpretation given by `Nat.Partrec.Code.eval`, there is a code `c` such that `c` and `f c` have
the same evaluation.
-/
theorem fixed_point {f : Code → Code} (hf : Computable f) : ∃ c : Code, eval (f c) = eval c :=
let g (x y : ℕ) : Part ℕ := eval (ofNat Code x) x >>= fun b => eval (ofNat Code b) y
have : Partrec₂ g :=
(eval_part.comp ((Computable.ofNat _).comp fst) fst).bind
(eval_part.comp ((Computable.ofNat _).comp snd) (snd.comp fst)).to₂
let ⟨cg, eg⟩ := exists_code.1 this
have eg' : ∀ a n, eval cg (Nat.pair a n) = Part.map encode (g a n) := by simp [eg]
let F (x : ℕ) : Code := f (curry cg x)
have : Computable F :=
hf.comp (curry_prim.comp (_root_.Primrec.const cg) _root_.Primrec.id).to_comp
let ⟨cF, eF⟩ := exists_code.1 this
have eF' : eval cF (encode cF) = Part.some (encode (F (encode cF))) := by simp [eF]
⟨curry cg (encode cF),
funext fun n =>
show eval (f (curry cg (encode cF))) n = eval (curry cg (encode cF)) n by
simp [eg', eF', Part.map_id']⟩
#align nat.partrec.code.fixed_point Nat.Partrec.Code.fixed_point
theorem fixed_point₂ {f : Code → ℕ →. ℕ} (hf : Partrec₂ f) : ∃ c : Code, eval c = f c :=
let ⟨cf, ef⟩ := exists_code.1 hf
(fixed_point (curry_prim.comp (_root_.Primrec.const cf) Primrec.encode).to_comp).imp fun c e =>
funext fun n => by
|
simp [e.symm, ef, Part.map_id']
|
theorem fixed_point₂ {f : Code → ℕ →. ℕ} (hf : Partrec₂ f) : ∃ c : Code, eval c = f c :=
let ⟨cf, ef⟩ := exists_code.1 hf
(fixed_point (curry_prim.comp (_root_.Primrec.const cf) Primrec.encode).to_comp).imp fun c e =>
funext fun n => by
|
Mathlib.Computability.PartrecCode.1191_0.A3c3Aev6SyIRjCJ
|
theorem fixed_point₂ {f : Code → ℕ →. ℕ} (hf : Partrec₂ f) : ∃ c : Code, eval c = f c
|
Mathlib_Computability_PartrecCode
|
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
f : G →* H
emb : OpenEmbedding ⇑f
U : Set H
hU : U ∈ nhds 1
⊢ ⇑f ⁻¹' U ∈ nhds 1
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
|
apply emb.continuous.tendsto
|
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.69_0.BrtsnGem4TIvd8C
|
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case a
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
f : G →* H
emb : OpenEmbedding ⇑f
U : Set H
hU : U ∈ nhds 1
⊢ U ∈ nhds (f 1)
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
|
rwa [f.map_one]
|
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.69_0.BrtsnGem4TIvd8C
|
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
hU : U ∈ nhds 1
⊢ ∃ V W, ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
|
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
hU : ∃ t₁ ∈ nhds 1, ∃ t₂ ∈ nhds 1, t₁ ×ˢ t₂ ⊆ U
⊢ ∃ V W, ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
|
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case intro.intro.intro.intro
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
⊢ ∃ V W, ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
|
cases' is_nonarchimedean _ hU₁ with V hV
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case intro.intro.intro.intro.intro
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
V : OpenSubgroup G
hV : ↑V ⊆ U₁
⊢ ∃ V W, ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
|
cases' is_nonarchimedean _ hU₂ with W hW
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case intro.intro.intro.intro.intro.intro
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
V : OpenSubgroup G
hV : ↑V ⊆ U₁
W : OpenSubgroup K
hW : ↑W ⊆ U₂
⊢ ∃ V W, ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
|
use V
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
V : OpenSubgroup G
hV : ↑V ⊆ U₁
W : OpenSubgroup K
hW : ↑W ⊆ U₂
⊢ ∃ W, ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V;
|
use W
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V;
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
V : OpenSubgroup G
hV : ↑V ⊆ U₁
W : OpenSubgroup K
hW : ↑W ⊆ U₂
⊢ ↑V ×ˢ ↑W ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
|
rw [Set.prod_subset_iff]
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
V : OpenSubgroup G
hV : ↑V ⊆ U₁
W : OpenSubgroup K
hW : ↑W ⊆ U₂
⊢ ∀ x ∈ ↑V, ∀ y ∈ ↑W, (x, y) ∈ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
|
intro x hX y hY
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × K)
U₁ : Set G
hU₁ : U₁ ∈ nhds 1
U₂ : Set K
hU₂ : U₂ ∈ nhds 1
h : U₁ ×ˢ U₂ ⊆ U
V : OpenSubgroup G
hV : ↑V ⊆ U₁
W : OpenSubgroup K
hW : ↑W ⊆ U₂
x : G
hX : x ∈ ↑V
y : K
hY : y ∈ ↑W
⊢ (x, y) ∈ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
|
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.81_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × G)
hU : U ∈ nhds 1
V W : OpenSubgroup G
h : ↑V ×ˢ ↑W ⊆ U
⊢ ↑(V ⊓ W) ×ˢ ↑(V ⊓ W) ⊆ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by
|
refine' Set.Subset.trans (Set.prod_mono _ _) ‹_›
|
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.99_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case refine'_1
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × G)
hU : U ∈ nhds 1
V W : OpenSubgroup G
h : ↑V ×ˢ ↑W ⊆ U
⊢ ↑(V ⊓ W) ⊆ ↑V
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;>
|
simp
|
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;>
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.99_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case refine'_2
G : Type u_1
inst✝⁸ : Group G
inst✝⁷ : TopologicalSpace G
inst✝⁶ : NonarchimedeanGroup G
H : Type u_2
inst✝⁵ : Group H
inst✝⁴ : TopologicalSpace H
inst✝³ : TopologicalGroup H
K : Type u_3
inst✝² : Group K
inst✝¹ : TopologicalSpace K
inst✝ : NonarchimedeanGroup K
U : Set (G × G)
hU : U ∈ nhds 1
V W : OpenSubgroup G
h : ↑V ×ˢ ↑W ⊆ U
⊢ ↑(V ⊓ W) ⊆ ↑W
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;>
|
simp
|
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;>
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.99_0.BrtsnGem4TIvd8C
|
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U : OpenAddSubgroup R
⊢ ∃ V, ↑V * ↑V ⊆ ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
|
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U : OpenAddSubgroup R
⊢ 0 ∈ (fun p => p.1 * p.2) ⁻¹' ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by
|
simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U V : OpenAddSubgroup R
H : ↑V ×ˢ ↑V ⊆ (fun p => p.1 * p.2) ⁻¹' ↑U
⊢ ∃ V, ↑V * ↑V ⊆ ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
|
use V
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U V : OpenAddSubgroup R
H : ↑V ×ˢ ↑V ⊆ (fun p => p.1 * p.2) ⁻¹' ↑U
⊢ ↑V * ↑V ⊆ ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
|
rintro v ⟨a, b, ha, hb, hv⟩
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h.intro.intro.intro.intro
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U V : OpenAddSubgroup R
H : ↑V ×ˢ ↑V ⊆ (fun p => p.1 * p.2) ⁻¹' ↑U
v a b : R
ha : a ∈ ↑V
hb : b ∈ ↑V
hv : (fun x x_1 => x * x_1) a b = v
⊢ v ∈ ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
|
have hy := H (Set.mk_mem_prod ha hb)
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h.intro.intro.intro.intro
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U V : OpenAddSubgroup R
H : ↑V ×ˢ ↑V ⊆ (fun p => p.1 * p.2) ⁻¹' ↑U
v a b : R
ha : a ∈ ↑V
hb : b ∈ ↑V
hv : (fun x x_1 => x * x_1) a b = v
hy : (a, b) ∈ (fun p => p.1 * p.2) ⁻¹' ↑U
⊢ v ∈ ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
have hy := H (Set.mk_mem_prod ha hb)
|
simp only [Set.mem_preimage, SetLike.mem_coe, hv] at hy
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
have hy := H (Set.mk_mem_prod ha hb)
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h.intro.intro.intro.intro
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U V : OpenAddSubgroup R
H : ↑V ×ˢ ↑V ⊆ (fun p => p.1 * p.2) ⁻¹' ↑U
v a b : R
ha : a ∈ ↑V
hb : b ∈ ↑V
hv : (fun x x_1 => x * x_1) a b = v
hy : v ∈ U
⊢ v ∈ ↑U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
have hy := H (Set.mk_mem_prod ha hb)
simp only [Set.mem_preimage, SetLike.mem_coe, hv] at hy
|
rw [SetLike.mem_coe]
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
have hy := H (Set.mk_mem_prod ha hb)
simp only [Set.mem_preimage, SetLike.mem_coe, hv] at hy
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
case h.intro.intro.intro.intro
R : Type u_1
S : Type u_2
inst✝⁵ : Ring R
inst✝⁴ : TopologicalSpace R
inst✝³ : NonarchimedeanRing R
inst✝² : Ring S
inst✝¹ : TopologicalSpace S
inst✝ : NonarchimedeanRing S
U V : OpenAddSubgroup R
H : ↑V ×ˢ ↑V ⊆ (fun p => p.1 * p.2) ⁻¹' ↑U
v a b : R
ha : a ∈ ↑V
hb : b ∈ ↑V
hv : (fun x x_1 => x * x_1) a b = v
hy : v ∈ U
⊢ v ∈ U
|
/-
Copyright (c) 2021 Ashwin Iyengar. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Johan Commelin, Ashwin Iyengar, Patrick Massot
-/
import Mathlib.GroupTheory.Subgroup.Basic
import Mathlib.Topology.Algebra.OpenSubgroup
import Mathlib.Topology.Algebra.Ring.Basic
#align_import topology.algebra.nonarchimedean.basic from "leanprover-community/mathlib"@"83f81aea33931a1edb94ce0f32b9a5d484de6978"
/-!
# Nonarchimedean Topology
In this file we set up the theory of nonarchimedean topological groups and rings.
A nonarchimedean group is a topological group whose topology admits a basis of
open neighborhoods of the identity element in the group consisting of open subgroups.
A nonarchimedean ring is a topological ring whose underlying topological (additive)
group is nonarchimedean.
## Definitions
- `NonarchimedeanAddGroup`: nonarchimedean additive group.
- `NonarchimedeanGroup`: nonarchimedean multiplicative group.
- `NonarchimedeanRing`: nonarchimedean ring.
-/
open Pointwise
/-- A topological additive group is nonarchimedean if every neighborhood of 0
contains an open subgroup. -/
class NonarchimedeanAddGroup (G : Type*) [AddGroup G] [TopologicalSpace G] extends
TopologicalAddGroup G : Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : G), ∃ V : OpenAddSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_add_group NonarchimedeanAddGroup
/-- A topological group is nonarchimedean if every neighborhood of 1 contains an open subgroup. -/
@[to_additive]
class NonarchimedeanGroup (G : Type*) [Group G] [TopologicalSpace G] extends TopologicalGroup G :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (1 : G), ∃ V : OpenSubgroup G, (V : Set G) ⊆ U
#align nonarchimedean_group NonarchimedeanGroup
/-- A topological ring is nonarchimedean if its underlying topological additive
group is nonarchimedean. -/
class NonarchimedeanRing (R : Type*) [Ring R] [TopologicalSpace R] extends TopologicalRing R :
Prop where
is_nonarchimedean : ∀ U ∈ nhds (0 : R), ∃ V : OpenAddSubgroup R, (V : Set R) ⊆ U
#align nonarchimedean_ring NonarchimedeanRing
-- see Note [lower instance priority]
/-- Every nonarchimedean ring is naturally a nonarchimedean additive group. -/
instance (priority := 100) NonarchimedeanRing.to_nonarchimedeanAddGroup (R : Type*) [Ring R]
[TopologicalSpace R] [t : NonarchimedeanRing R] : NonarchimedeanAddGroup R :=
{ t with }
#align nonarchimedean_ring.to_nonarchimedean_add_group NonarchimedeanRing.to_nonarchimedeanAddGroup
namespace NonarchimedeanGroup
variable {G : Type*} [Group G] [TopologicalSpace G] [NonarchimedeanGroup G]
variable {H : Type*} [Group H] [TopologicalSpace H] [TopologicalGroup H]
variable {K : Type*} [Group K] [TopologicalSpace K] [NonarchimedeanGroup K]
/-- If a topological group embeds into a nonarchimedean group, then it is nonarchimedean. -/
@[to_additive]
theorem nonarchimedean_of_emb (f : G →* H) (emb : OpenEmbedding f) : NonarchimedeanGroup H :=
{ is_nonarchimedean := fun U hU =>
have h₁ : f ⁻¹' U ∈ nhds (1 : G) := by
apply emb.continuous.tendsto
rwa [f.map_one]
let ⟨V, hV⟩ := is_nonarchimedean (f ⁻¹' U) h₁
⟨{ Subgroup.map f V with isOpen' := emb.isOpenMap _ V.isOpen }, Set.image_subset_iff.2 hV⟩ }
#align nonarchimedean_group.nonarchimedean_of_emb NonarchimedeanGroup.nonarchimedean_of_emb
#align nonarchimedean_add_group.nonarchimedean_of_emb NonarchimedeanAddGroup.nonarchimedean_of_emb
/-- An open neighborhood of the identity in the cartesian product of two nonarchimedean groups
contains the cartesian product of an open neighborhood in each group. -/
@[to_additive NonarchimedeanAddGroup.prod_subset "An open neighborhood of the identity in
the cartesian product of two nonarchimedean groups contains the cartesian product of
an open neighborhood in each group."]
theorem prod_subset {U} (hU : U ∈ nhds (1 : G × K)) :
∃ (V : OpenSubgroup G) (W : OpenSubgroup K), (V : Set G) ×ˢ (W : Set K) ⊆ U := by
erw [nhds_prod_eq, Filter.mem_prod_iff] at hU
rcases hU with ⟨U₁, hU₁, U₂, hU₂, h⟩
cases' is_nonarchimedean _ hU₁ with V hV
cases' is_nonarchimedean _ hU₂ with W hW
use V; use W
rw [Set.prod_subset_iff]
intro x hX y hY
exact Set.Subset.trans (Set.prod_mono hV hW) h (Set.mem_sep hX hY)
#align nonarchimedean_group.prod_subset NonarchimedeanGroup.prod_subset
#align nonarchimedean_add_group.prod_subset NonarchimedeanAddGroup.prod_subset
/-- An open neighborhood of the identity in the cartesian square of a nonarchimedean group
contains the cartesian square of an open neighborhood in the group. -/
@[to_additive NonarchimedeanAddGroup.prod_self_subset "An open neighborhood of the identity in
the cartesian square of a nonarchimedean group contains the cartesian square of
an open neighborhood in the group."]
theorem prod_self_subset {U} (hU : U ∈ nhds (1 : G × G)) :
∃ V : OpenSubgroup G, (V : Set G) ×ˢ (V : Set G) ⊆ U :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V ⊓ W, by refine' Set.Subset.trans (Set.prod_mono _ _) ‹_› <;> simp⟩
#align nonarchimedean_group.prod_self_subset NonarchimedeanGroup.prod_self_subset
#align nonarchimedean_add_group.prod_self_subset NonarchimedeanAddGroup.prod_self_subset
/-- The cartesian product of two nonarchimedean groups is nonarchimedean. -/
@[to_additive "The cartesian product of two nonarchimedean groups is nonarchimedean."]
instance : NonarchimedeanGroup (G × K) where
is_nonarchimedean U hU :=
let ⟨V, W, h⟩ := prod_subset hU
⟨V.prod W, ‹_›⟩
end NonarchimedeanGroup
namespace NonarchimedeanRing
open NonarchimedeanRing
open NonarchimedeanAddGroup
variable {R S : Type*}
variable [Ring R] [TopologicalSpace R] [NonarchimedeanRing R]
variable [Ring S] [TopologicalSpace S] [NonarchimedeanRing S]
/-- The cartesian product of two nonarchimedean rings is nonarchimedean. -/
instance : NonarchimedeanRing (R × S) where
is_nonarchimedean := NonarchimedeanAddGroup.is_nonarchimedean
/-- Given an open subgroup `U` and an element `r` of a nonarchimedean ring, there is an open
subgroup `V` such that `r • V` is contained in `U`. -/
theorem left_mul_subset (U : OpenAddSubgroup R) (r : R) :
∃ V : OpenAddSubgroup R, r • (V : Set R) ⊆ U :=
⟨U.comap (AddMonoidHom.mulLeft r) (continuous_mul_left r), (U : Set R).image_preimage_subset _⟩
#align nonarchimedean_ring.left_mul_subset NonarchimedeanRing.left_mul_subset
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
have hy := H (Set.mk_mem_prod ha hb)
simp only [Set.mem_preimage, SetLike.mem_coe, hv] at hy
rw [SetLike.mem_coe]
|
exact hy
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U := by
let ⟨V, H⟩ :=
prod_self_subset
(IsOpen.mem_nhds (IsOpen.preimage continuous_mul U.isOpen)
(by simpa only [Set.mem_preimage, SetLike.mem_coe, Prod.snd_zero,
mul_zero] using U.zero_mem))
use V
rintro v ⟨a, b, ha, hb, hv⟩
have hy := H (Set.mk_mem_prod ha hb)
simp only [Set.mem_preimage, SetLike.mem_coe, hv] at hy
rw [SetLike.mem_coe]
|
Mathlib.Topology.Algebra.Nonarchimedean.Basic.143_0.BrtsnGem4TIvd8C
|
/-- An open subgroup of a nonarchimedean ring contains the square of another one. -/
theorem mul_subset (U : OpenAddSubgroup R) : ∃ V : OpenAddSubgroup R, (V : Set R) * V ⊆ U
|
Mathlib_Topology_Algebra_Nonarchimedean_Basic
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
⊢ ∀ (J : Type) [𝒥 : SmallCategory J] [inst : FinCategory J], HasLimitsOfShape J α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
|
intro J 𝒥₁ 𝒥₂
|
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
|
Mathlib.CategoryTheory.Limits.Lattice.54_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J✝ : Type w
inst✝³ : SmallCategory J✝
inst✝² : FinCategory J✝
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
J : Type
𝒥₁ : SmallCategory J
𝒥₂ : FinCategory J
⊢ HasLimitsOfShape J α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
|
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }
|
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
|
Mathlib.CategoryTheory.Limits.Lattice.54_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
⊢ ∀ (J : Type) [𝒥 : SmallCategory J] [inst : FinCategory J], HasColimitsOfShape J α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
|
intro J 𝒥₁ 𝒥₂
|
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
|
Mathlib.CategoryTheory.Limits.Lattice.61_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J✝ : Type w
inst✝³ : SmallCategory J✝
inst✝² : FinCategory J✝
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
J : Type
𝒥₁ : SmallCategory J
𝒥₂ : FinCategory J
⊢ HasColimitsOfShape J α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
|
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }
|
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
|
Mathlib.CategoryTheory.Limits.Lattice.61_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ ∏ f = Finset.inf Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
|
trans
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
|
Mathlib.CategoryTheory.Limits.Lattice.83_0.76fWBXDnykYGQ2r
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ ∏ f = ?m.33948
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ ?m.33948 = Finset.inf Fintype.elems f
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
|
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
|
Mathlib.CategoryTheory.Limits.Lattice.83_0.76fWBXDnykYGQ2r
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ (finiteLimitCone (Discrete.functor f)).cone.pt = Finset.inf Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
|
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
|
Mathlib.CategoryTheory.Limits.Lattice.83_0.76fWBXDnykYGQ2r
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ Finset.inf Finset.univ (f ∘ ⇑(Equiv.toEmbedding discreteEquiv)) = Finset.inf Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
|
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
|
Mathlib.CategoryTheory.Limits.Lattice.83_0.76fWBXDnykYGQ2r
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeInf α
inst✝¹ : OrderTop α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ Finset.inf Finset.univ f = Finset.inf Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
|
rfl
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
|
Mathlib.CategoryTheory.Limits.Lattice.83_0.76fWBXDnykYGQ2r
|
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ ∐ f = Finset.sup Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
|
trans
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
|
Mathlib.CategoryTheory.Limits.Lattice.97_0.76fWBXDnykYGQ2r
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ ∐ f = ?m.46970
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ ?m.46970 = Finset.sup Fintype.elems f
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
|
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
|
Mathlib.CategoryTheory.Limits.Lattice.97_0.76fWBXDnykYGQ2r
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ (finiteColimitCocone (Discrete.functor f)).cocone.pt = Finset.sup Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
|
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
|
Mathlib.CategoryTheory.Limits.Lattice.97_0.76fWBXDnykYGQ2r
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ Finset.sup Finset.univ (f ∘ ⇑(Equiv.toEmbedding discreteEquiv)) = Finset.sup Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
|
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
|
Mathlib.CategoryTheory.Limits.Lattice.97_0.76fWBXDnykYGQ2r
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝⁴ : SmallCategory J
inst✝³ : FinCategory J
inst✝² : SemilatticeSup α
inst✝¹ : OrderBot α
ι : Type u
inst✝ : Fintype ι
f : ι → α
⊢ Finset.sup Finset.univ f = Finset.sup Fintype.elems f
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
|
rfl
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
|
Mathlib.CategoryTheory.Limits.Lattice.97_0.76fWBXDnykYGQ2r
|
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
⊢ HasBinaryProducts α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
|
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
|
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
|
Mathlib.CategoryTheory.Limits.Lattice.112_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
⊢ ∀ (x y : α), HasLimit (pair x y)
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
|
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
|
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
|
Mathlib.CategoryTheory.Limits.Lattice.112_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
this : (∀ (J : Type u) {𝒥 : SmallCategory J}, FinCategory J → HasLimitsOfShape J α) → HasFiniteLimits α :=
hasFiniteLimits_of_hasFiniteLimits_of_size α
⊢ ∀ (x y : α), HasLimit (pair x y)
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
|
infer_instance
|
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
|
Mathlib.CategoryTheory.Limits.Lattice.112_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
this : ∀ (x y : α), HasLimit (pair x y)
⊢ HasBinaryProducts α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
|
apply hasBinaryProducts_of_hasLimit_pair
|
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
|
Mathlib.CategoryTheory.Limits.Lattice.112_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
x y : α
⊢ limit (pair x y) = Finset.inf Finset.univ (pair x y).toPrefunctor.obj
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by
|
rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
|
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by
|
Mathlib.CategoryTheory.Limits.Lattice.118_0.76fWBXDnykYGQ2r
|
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
x y : α
⊢ x ⊓ (y ⊓ ⊤) = x ⊓ y
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by
|
rw [inf_top_eq]
|
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by
|
Mathlib.CategoryTheory.Limits.Lattice.118_0.76fWBXDnykYGQ2r
|
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
⊢ HasBinaryCoproducts α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
|
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
|
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
|
Mathlib.CategoryTheory.Limits.Lattice.132_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
⊢ ∀ (x y : α), HasColimit (pair x y)
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
|
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
|
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
|
Mathlib.CategoryTheory.Limits.Lattice.132_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
this : (∀ (J : Type u) {𝒥 : SmallCategory J}, FinCategory J → HasColimitsOfShape J α) → HasFiniteColimits α :=
hasFiniteColimits_of_hasFiniteColimits_of_size α
⊢ ∀ (x y : α), HasColimit (pair x y)
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
|
infer_instance
|
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
|
Mathlib.CategoryTheory.Limits.Lattice.132_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
this : ∀ (x y : α), HasColimit (pair x y)
⊢ HasBinaryCoproducts α
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
|
apply hasBinaryCoproducts_of_hasColimit_pair
|
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
|
Mathlib.CategoryTheory.Limits.Lattice.132_0.76fWBXDnykYGQ2r
|
instance (priority
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
x y : α
⊢ colimit (pair x y) = Finset.sup Finset.univ (pair x y).toPrefunctor.obj
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by
|
rw [finite_colimit_eq_finset_univ_sup (pair x y)]
|
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by
|
Mathlib.CategoryTheory.Limits.Lattice.138_0.76fWBXDnykYGQ2r
|
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
x y : α
⊢ x ⊔ (y ⊔ ⊥) = x ⊔ y
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by
|
rw [sup_bot_eq]
|
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by
|
Mathlib.CategoryTheory.Limits.Lattice.138_0.76fWBXDnykYGQ2r
|
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
x y z : α
f : x ⟶ z
g : y ⟶ z
⊢ limit (cospan f g) = Finset.inf Finset.univ (cospan f g).toPrefunctor.obj
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by
|
rw [finite_limit_eq_finset_univ_inf]
|
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by
|
Mathlib.CategoryTheory.Limits.Lattice.151_0.76fWBXDnykYGQ2r
|
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeInf α
inst✝ : OrderTop α
x y z : α
f : x ⟶ z
g : y ⟶ z
⊢ z ⊓ (x ⊓ (y ⊓ ⊤)) = z ⊓ (x ⊓ y)
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by
|
rw [inf_top_eq]
|
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by
|
Mathlib.CategoryTheory.Limits.Lattice.151_0.76fWBXDnykYGQ2r
|
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
x y z : α
f : z ⟶ x
g : z ⟶ y
⊢ colimit (span f g) = Finset.sup Finset.univ (span f g).toPrefunctor.obj
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by rw [inf_top_eq]
_ = x ⊓ y := inf_eq_right.mpr (inf_le_of_left_le f.le)
#align category_theory.limits.complete_lattice.pullback_eq_inf CategoryTheory.Limits.CompleteLattice.pullback_eq_inf
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by
|
rw [finite_colimit_eq_finset_univ_sup]
|
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by
|
Mathlib.CategoryTheory.Limits.Lattice.165_0.76fWBXDnykYGQ2r
|
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
J : Type w
inst✝³ : SmallCategory J
inst✝² : FinCategory J
inst✝¹ : SemilatticeSup α
inst✝ : OrderBot α
x y z : α
f : z ⟶ x
g : z ⟶ y
⊢ z ⊔ (x ⊔ (y ⊔ ⊥)) = z ⊔ (x ⊔ y)
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by rw [inf_top_eq]
_ = x ⊓ y := inf_eq_right.mpr (inf_le_of_left_le f.le)
#align category_theory.limits.complete_lattice.pullback_eq_inf CategoryTheory.Limits.CompleteLattice.pullback_eq_inf
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by rw [finite_colimit_eq_finset_univ_sup]
_ = z ⊔ (x ⊔ (y ⊔ ⊥)) := rfl
_ = z ⊔ (x ⊔ y) := by
|
rw [sup_bot_eq]
|
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by rw [finite_colimit_eq_finset_univ_sup]
_ = z ⊔ (x ⊔ (y ⊔ ⊥)) := rfl
_ = z ⊔ (x ⊔ y) := by
|
Mathlib.CategoryTheory.Limits.Lattice.165_0.76fWBXDnykYGQ2r
|
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
inst✝¹ : CompleteLattice α
J : Type u
inst✝ : SmallCategory J
F : J ⥤ α
s : Cone F
⊢ ∀ b ∈ Set.range F.obj, s.pt ≤ b
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by rw [inf_top_eq]
_ = x ⊓ y := inf_eq_right.mpr (inf_le_of_left_le f.le)
#align category_theory.limits.complete_lattice.pullback_eq_inf CategoryTheory.Limits.CompleteLattice.pullback_eq_inf
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by rw [finite_colimit_eq_finset_univ_sup]
_ = z ⊔ (x ⊔ (y ⊔ ⊥)) := rfl
_ = z ⊔ (x ⊔ y) := by rw [sup_bot_eq]
_ = x ⊔ y := sup_eq_right.mpr (le_sup_of_le_left f.le)
#align category_theory.limits.complete_lattice.pushout_eq_sup CategoryTheory.Limits.CompleteLattice.pushout_eq_sup
end Semilattice
variable {α : Type u} [CompleteLattice α]
variable {J : Type u} [SmallCategory J]
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := iInf F.obj
π := { app := fun j => homOfLE (CompleteLattice.sInf_le _ _ (Set.mem_range_self _)) } }
isLimit :=
{ lift := fun s =>
homOfLE (CompleteLattice.le_sInf _ _ (by
|
rintro _ ⟨j, rfl⟩
|
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := iInf F.obj
π := { app := fun j => homOfLE (CompleteLattice.sInf_le _ _ (Set.mem_range_self _)) } }
isLimit :=
{ lift := fun s =>
homOfLE (CompleteLattice.le_sInf _ _ (by
|
Mathlib.CategoryTheory.Limits.Lattice.185_0.76fWBXDnykYGQ2r
|
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone
|
Mathlib_CategoryTheory_Limits_Lattice
|
case intro
α : Type u
inst✝¹ : CompleteLattice α
J : Type u
inst✝ : SmallCategory J
F : J ⥤ α
s : Cone F
j : J
⊢ s.pt ≤ F.obj j
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by rw [inf_top_eq]
_ = x ⊓ y := inf_eq_right.mpr (inf_le_of_left_le f.le)
#align category_theory.limits.complete_lattice.pullback_eq_inf CategoryTheory.Limits.CompleteLattice.pullback_eq_inf
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by rw [finite_colimit_eq_finset_univ_sup]
_ = z ⊔ (x ⊔ (y ⊔ ⊥)) := rfl
_ = z ⊔ (x ⊔ y) := by rw [sup_bot_eq]
_ = x ⊔ y := sup_eq_right.mpr (le_sup_of_le_left f.le)
#align category_theory.limits.complete_lattice.pushout_eq_sup CategoryTheory.Limits.CompleteLattice.pushout_eq_sup
end Semilattice
variable {α : Type u} [CompleteLattice α]
variable {J : Type u} [SmallCategory J]
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := iInf F.obj
π := { app := fun j => homOfLE (CompleteLattice.sInf_le _ _ (Set.mem_range_self _)) } }
isLimit :=
{ lift := fun s =>
homOfLE (CompleteLattice.le_sInf _ _ (by rintro _ ⟨j, rfl⟩;
|
exact (s.π.app j).le
|
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := iInf F.obj
π := { app := fun j => homOfLE (CompleteLattice.sInf_le _ _ (Set.mem_range_self _)) } }
isLimit :=
{ lift := fun s =>
homOfLE (CompleteLattice.le_sInf _ _ (by rintro _ ⟨j, rfl⟩;
|
Mathlib.CategoryTheory.Limits.Lattice.185_0.76fWBXDnykYGQ2r
|
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone
|
Mathlib_CategoryTheory_Limits_Lattice
|
α : Type u
inst✝¹ : CompleteLattice α
J : Type u
inst✝ : SmallCategory J
F : J ⥤ α
s : Cocone F
⊢ ∀ b ∈ Set.range F.obj, b ≤ s.pt
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by rw [inf_top_eq]
_ = x ⊓ y := inf_eq_right.mpr (inf_le_of_left_le f.le)
#align category_theory.limits.complete_lattice.pullback_eq_inf CategoryTheory.Limits.CompleteLattice.pullback_eq_inf
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by rw [finite_colimit_eq_finset_univ_sup]
_ = z ⊔ (x ⊔ (y ⊔ ⊥)) := rfl
_ = z ⊔ (x ⊔ y) := by rw [sup_bot_eq]
_ = x ⊔ y := sup_eq_right.mpr (le_sup_of_le_left f.le)
#align category_theory.limits.complete_lattice.pushout_eq_sup CategoryTheory.Limits.CompleteLattice.pushout_eq_sup
end Semilattice
variable {α : Type u} [CompleteLattice α]
variable {J : Type u} [SmallCategory J]
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := iInf F.obj
π := { app := fun j => homOfLE (CompleteLattice.sInf_le _ _ (Set.mem_range_self _)) } }
isLimit :=
{ lift := fun s =>
homOfLE (CompleteLattice.le_sInf _ _ (by rintro _ ⟨j, rfl⟩; exact (s.π.app j).le)) }
#align category_theory.limits.complete_lattice.limit_cone CategoryTheory.Limits.CompleteLattice.limitCone
/-- The colimit cocone over any functor into a complete lattice.
-/
def colimitCocone (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := iSup F.obj
ι := { app := fun j => homOfLE (CompleteLattice.le_sSup _ _ (Set.mem_range_self _)) } }
isColimit :=
{ desc := fun s =>
homOfLE (CompleteLattice.sSup_le _ _ (by
|
rintro _ ⟨j, rfl⟩
|
/-- The colimit cocone over any functor into a complete lattice.
-/
def colimitCocone (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := iSup F.obj
ι := { app := fun j => homOfLE (CompleteLattice.le_sSup _ _ (Set.mem_range_self _)) } }
isColimit :=
{ desc := fun s =>
homOfLE (CompleteLattice.sSup_le _ _ (by
|
Mathlib.CategoryTheory.Limits.Lattice.196_0.76fWBXDnykYGQ2r
|
/-- The colimit cocone over any functor into a complete lattice.
-/
def colimitCocone (F : J ⥤ α) : ColimitCocone F where
cocone
|
Mathlib_CategoryTheory_Limits_Lattice
|
case intro
α : Type u
inst✝¹ : CompleteLattice α
J : Type u
inst✝ : SmallCategory J
F : J ⥤ α
s : Cocone F
j : J
⊢ F.obj j ≤ s.pt
|
/-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import Mathlib.Order.CompleteLattice
import Mathlib.Data.Fintype.Lattice
import Mathlib.CategoryTheory.Limits.Shapes.Pullbacks
import Mathlib.CategoryTheory.Category.Preorder
import Mathlib.CategoryTheory.Limits.Shapes.Products
import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits
#align_import category_theory.limits.lattice from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Limits in lattice categories are given by infimums and supremums.
-/
universe w u
open CategoryTheory
open CategoryTheory.Limits
namespace CategoryTheory.Limits.CompleteLattice
section Semilattice
variable {α : Type u}
variable {J : Type w} [SmallCategory J] [FinCategory J]
/-- The limit cone over any functor from a finite diagram into a `SemilatticeInf` with `OrderTop`.
-/
def finiteLimitCone [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := Finset.univ.inf F.obj
π := { app := fun j => homOfLE (Finset.inf_le (Fintype.complete _)) } }
isLimit := { lift := fun s => homOfLE (Finset.le_inf fun j _ => (s.π.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_limit_cone CategoryTheory.Limits.CompleteLattice.finiteLimitCone
/--
The colimit cocone over any functor from a finite diagram into a `SemilatticeSup` with `OrderBot`.
-/
def finiteColimitCocone [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := Finset.univ.sup F.obj
ι := { app := fun i => homOfLE (Finset.le_sup (Fintype.complete _)) } }
isColimit := { desc := fun s => homOfLE (Finset.sup_le fun j _ => (s.ι.app j).down.down) }
#align category_theory.limits.complete_lattice.finite_colimit_cocone CategoryTheory.Limits.CompleteLattice.finiteColimitCocone
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteLimits_of_semilatticeInf_orderTop [SemilatticeInf α]
[OrderTop α] : HasFiniteLimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_limit := fun F => HasLimit.mk (finiteLimitCone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_limits_of_semilattice_inf_order_top CategoryTheory.Limits.CompleteLattice.hasFiniteLimits_of_semilatticeInf_orderTop
-- see Note [lower instance priority]
instance (priority := 100) hasFiniteColimits_of_semilatticeSup_orderBot [SemilatticeSup α]
[OrderBot α] : HasFiniteColimits α := ⟨by
intro J 𝒥₁ 𝒥₂
exact { has_colimit := fun F => HasColimit.mk (finiteColimitCocone F) }⟩
#align category_theory.limits.complete_lattice.has_finite_colimits_of_semilattice_sup_order_bot CategoryTheory.Limits.CompleteLattice.hasFiniteColimits_of_semilatticeSup_orderBot
/-- The limit of a functor from a finite diagram into a `SemilatticeInf` with `OrderTop` is the
infimum of the objects in the image.
-/
theorem finite_limit_eq_finset_univ_inf [SemilatticeInf α] [OrderTop α] (F : J ⥤ α) :
limit F = Finset.univ.inf F.obj :=
(IsLimit.conePointUniqueUpToIso (limit.isLimit F) (finiteLimitCone F).isLimit).to_eq
#align category_theory.limits.complete_lattice.finite_limit_eq_finset_univ_inf CategoryTheory.Limits.CompleteLattice.finite_limit_eq_finset_univ_inf
/-- The colimit of a functor from a finite diagram into a `SemilatticeSup` with `OrderBot`
is the supremum of the objects in the image.
-/
theorem finite_colimit_eq_finset_univ_sup [SemilatticeSup α] [OrderBot α] (F : J ⥤ α) :
colimit F = Finset.univ.sup F.obj :=
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit F) (finiteColimitCocone F).isColimit).to_eq
#align category_theory.limits.complete_lattice.finite_colimit_eq_finset_univ_sup CategoryTheory.Limits.CompleteLattice.finite_colimit_eq_finset_univ_sup
/--
A finite product in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum.
-/
theorem finite_product_eq_finset_inf [SemilatticeInf α] [OrderTop α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∏ f = Fintype.elems.inf f := by
trans
exact
(IsLimit.conePointUniqueUpToIso (limit.isLimit _)
(finiteLimitCone (Discrete.functor f)).isLimit).to_eq
change Finset.univ.inf (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.inf f
simp only [← Finset.inf_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_product_eq_finset_inf CategoryTheory.Limits.CompleteLattice.finite_product_eq_finset_inf
/-- A finite coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
theorem finite_coproduct_eq_finset_sup [SemilatticeSup α] [OrderBot α] {ι : Type u} [Fintype ι]
(f : ι → α) : ∐ f = Fintype.elems.sup f := by
trans
exact
(IsColimit.coconePointUniqueUpToIso (colimit.isColimit _)
(finiteColimitCocone (Discrete.functor f)).isColimit).to_eq
change Finset.univ.sup (f ∘ discreteEquiv.toEmbedding) = Fintype.elems.sup f
simp only [← Finset.sup_map, Finset.univ_map_equiv_to_embedding]
rfl
#align category_theory.limits.complete_lattice.finite_coproduct_eq_finset_sup CategoryTheory.Limits.CompleteLattice.finite_coproduct_eq_finset_sup
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeInf α] [OrderTop α] : HasBinaryProducts α := by
have : ∀ x y : α, HasLimit (pair x y) := by
letI := hasFiniteLimits_of_hasFiniteLimits_of_size.{u} α
infer_instance
apply hasBinaryProducts_of_hasLimit_pair
/-- The binary product in the category of a `SemilatticeInf` with `OrderTop` is the same as the
infimum.
-/
@[simp]
theorem prod_eq_inf [SemilatticeInf α] [OrderTop α] (x y : α) : Limits.prod x y = x ⊓ y :=
calc
Limits.prod x y = limit (pair x y) := rfl
_ = Finset.univ.inf (pair x y).obj := by rw [finite_limit_eq_finset_univ_inf (pair.{u} x y)]
_ = x ⊓ (y ⊓ ⊤) := rfl
-- Note: finset.inf is realized as a fold, hence the definitional equality
_ = x ⊓ y := by rw [inf_top_eq]
#align category_theory.limits.complete_lattice.prod_eq_inf CategoryTheory.Limits.CompleteLattice.prod_eq_inf
-- see Note [lower instance priority]
instance (priority := 100) [SemilatticeSup α] [OrderBot α] : HasBinaryCoproducts α := by
have : ∀ x y : α, HasColimit (pair x y) := by
letI := hasFiniteColimits_of_hasFiniteColimits_of_size.{u} α
infer_instance
apply hasBinaryCoproducts_of_hasColimit_pair
/-- The binary coproduct in the category of a `SemilatticeSup` with `OrderBot` is the same as the
supremum.
-/
@[simp]
theorem coprod_eq_sup [SemilatticeSup α] [OrderBot α] (x y : α) : Limits.coprod x y = x ⊔ y :=
calc
Limits.coprod x y = colimit (pair x y) := rfl
_ = Finset.univ.sup (pair x y).obj := by rw [finite_colimit_eq_finset_univ_sup (pair x y)]
_ = x ⊔ (y ⊔ ⊥) := rfl
-- Note: Finset.sup is realized as a fold, hence the definitional equality
_ = x ⊔ y := by rw [sup_bot_eq]
#align category_theory.limits.complete_lattice.coprod_eq_sup CategoryTheory.Limits.CompleteLattice.coprod_eq_sup
/-- The pullback in the category of a `SemilatticeInf` with `OrderTop` is the same as the infimum
over the objects.
-/
@[simp]
theorem pullback_eq_inf [SemilatticeInf α] [OrderTop α] {x y z : α} (f : x ⟶ z) (g : y ⟶ z) :
pullback f g = x ⊓ y :=
calc
pullback f g = limit (cospan f g) := rfl
_ = Finset.univ.inf (cospan f g).obj := by rw [finite_limit_eq_finset_univ_inf]
_ = z ⊓ (x ⊓ (y ⊓ ⊤)) := rfl
_ = z ⊓ (x ⊓ y) := by rw [inf_top_eq]
_ = x ⊓ y := inf_eq_right.mpr (inf_le_of_left_le f.le)
#align category_theory.limits.complete_lattice.pullback_eq_inf CategoryTheory.Limits.CompleteLattice.pullback_eq_inf
/-- The pushout in the category of a `SemilatticeSup` with `OrderBot` is the same as the supremum
over the objects.
-/
@[simp]
theorem pushout_eq_sup [SemilatticeSup α] [OrderBot α] (x y z : α) (f : z ⟶ x) (g : z ⟶ y) :
pushout f g = x ⊔ y :=
calc
pushout f g = colimit (span f g) := rfl
_ = Finset.univ.sup (span f g).obj := by rw [finite_colimit_eq_finset_univ_sup]
_ = z ⊔ (x ⊔ (y ⊔ ⊥)) := rfl
_ = z ⊔ (x ⊔ y) := by rw [sup_bot_eq]
_ = x ⊔ y := sup_eq_right.mpr (le_sup_of_le_left f.le)
#align category_theory.limits.complete_lattice.pushout_eq_sup CategoryTheory.Limits.CompleteLattice.pushout_eq_sup
end Semilattice
variable {α : Type u} [CompleteLattice α]
variable {J : Type u} [SmallCategory J]
/-- The limit cone over any functor into a complete lattice.
-/
def limitCone (F : J ⥤ α) : LimitCone F where
cone :=
{ pt := iInf F.obj
π := { app := fun j => homOfLE (CompleteLattice.sInf_le _ _ (Set.mem_range_self _)) } }
isLimit :=
{ lift := fun s =>
homOfLE (CompleteLattice.le_sInf _ _ (by rintro _ ⟨j, rfl⟩; exact (s.π.app j).le)) }
#align category_theory.limits.complete_lattice.limit_cone CategoryTheory.Limits.CompleteLattice.limitCone
/-- The colimit cocone over any functor into a complete lattice.
-/
def colimitCocone (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := iSup F.obj
ι := { app := fun j => homOfLE (CompleteLattice.le_sSup _ _ (Set.mem_range_self _)) } }
isColimit :=
{ desc := fun s =>
homOfLE (CompleteLattice.sSup_le _ _ (by rintro _ ⟨j, rfl⟩;
|
exact (s.ι.app j).le
|
/-- The colimit cocone over any functor into a complete lattice.
-/
def colimitCocone (F : J ⥤ α) : ColimitCocone F where
cocone :=
{ pt := iSup F.obj
ι := { app := fun j => homOfLE (CompleteLattice.le_sSup _ _ (Set.mem_range_self _)) } }
isColimit :=
{ desc := fun s =>
homOfLE (CompleteLattice.sSup_le _ _ (by rintro _ ⟨j, rfl⟩;
|
Mathlib.CategoryTheory.Limits.Lattice.196_0.76fWBXDnykYGQ2r
|
/-- The colimit cocone over any functor into a complete lattice.
-/
def colimitCocone (F : J ⥤ α) : ColimitCocone F where
cocone
|
Mathlib_CategoryTheory_Limits_Lattice
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f g : IsometryEquiv Q₁ Q₂
⊢ (fun f => ⇑f.toLinearEquiv) f = (fun f => ⇑f.toLinearEquiv) g →
(fun f => ⇑(LinearEquiv.symm f.toLinearEquiv)) f = (fun f => ⇑(LinearEquiv.symm f.toLinearEquiv)) g → f = g
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by
|
cases f
|
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.55_0.YOl7VzX5AOw0Rpu
|
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case mk
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
g : IsometryEquiv Q₁ Q₂
toLinearEquiv✝ : M₁ ≃ₗ[R] M₂
map_app'✝ : ∀ (m : M₁), Q₂ (AddHom.toFun toLinearEquiv✝.toAddHom m) = Q₁ m
⊢ (fun f => ⇑f.toLinearEquiv) { toLinearEquiv := toLinearEquiv✝, map_app' := map_app'✝ } =
(fun f => ⇑f.toLinearEquiv) g →
(fun f => ⇑(LinearEquiv.symm f.toLinearEquiv)) { toLinearEquiv := toLinearEquiv✝, map_app' := map_app'✝ } =
(fun f => ⇑(LinearEquiv.symm f.toLinearEquiv)) g →
{ toLinearEquiv := toLinearEquiv✝, map_app' := map_app'✝ } = g
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f;
|
cases g
|
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f;
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.55_0.YOl7VzX5AOw0Rpu
|
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case mk.mk
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
toLinearEquiv✝¹ : M₁ ≃ₗ[R] M₂
map_app'✝¹ : ∀ (m : M₁), Q₂ (AddHom.toFun toLinearEquiv✝¹.toAddHom m) = Q₁ m
toLinearEquiv✝ : M₁ ≃ₗ[R] M₂
map_app'✝ : ∀ (m : M₁), Q₂ (AddHom.toFun toLinearEquiv✝.toAddHom m) = Q₁ m
⊢ (fun f => ⇑f.toLinearEquiv) { toLinearEquiv := toLinearEquiv✝¹, map_app' := map_app'✝¹ } =
(fun f => ⇑f.toLinearEquiv) { toLinearEquiv := toLinearEquiv✝, map_app' := map_app'✝ } →
(fun f => ⇑(LinearEquiv.symm f.toLinearEquiv)) { toLinearEquiv := toLinearEquiv✝¹, map_app' := map_app'✝¹ } =
(fun f => ⇑(LinearEquiv.symm f.toLinearEquiv)) { toLinearEquiv := toLinearEquiv✝, map_app' := map_app'✝ } →
{ toLinearEquiv := toLinearEquiv✝¹, map_app' := map_app'✝¹ } =
{ toLinearEquiv := toLinearEquiv✝, map_app' := map_app'✝ }
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g;
|
simp (config := {contextual := true})
|
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g;
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.55_0.YOl7VzX5AOw0Rpu
|
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
src✝ : M₂ ≃ₗ[R] M₁ := LinearEquiv.symm f.toLinearEquiv
⊢ ∀ (m : M₂),
Q₁
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun,
left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
Q₂ m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by
|
intro m
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.87_0.YOl7VzX5AOw0Rpu
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
src✝ : M₂ ≃ₗ[R] M₁ := LinearEquiv.symm f.toLinearEquiv
m : M₂
⊢ Q₁
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun, left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
Q₂ m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m;
|
rw [← f.map_app]
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m;
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.87_0.YOl7VzX5AOw0Rpu
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
src✝ : M₂ ≃ₗ[R] M₁ := LinearEquiv.symm f.toLinearEquiv
m : M₂
⊢ Q₂
(f
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun,
left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m)) =
Q₂ m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app];
|
congr
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app];
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.87_0.YOl7VzX5AOw0Rpu
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case h.e_6.h
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
src✝ : M₂ ≃ₗ[R] M₁ := LinearEquiv.symm f.toLinearEquiv
m : M₂
⊢ f
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun, left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr;
|
exact f.toLinearEquiv.apply_symm_apply m
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr;
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.87_0.YOl7VzX5AOw0Rpu
|
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
g : IsometryEquiv Q₂ Q₃
src✝ : M₁ ≃ₗ[R] M₃ := f.toLinearEquiv ≪≫ₗ g.toLinearEquiv
⊢ ∀ (m : M₁),
Q₃
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun,
left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
Q₁ m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by
|
intro m
|
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.94_0.YOl7VzX5AOw0Rpu
|
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
g : IsometryEquiv Q₂ Q₃
src✝ : M₁ ≃ₗ[R] M₃ := f.toLinearEquiv ≪≫ₗ g.toLinearEquiv
m : M₁
⊢ Q₃
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun, left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
Q₁ m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m;
|
rw [← f.map_app, ← g.map_app]
|
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m;
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.94_0.YOl7VzX5AOw0Rpu
|
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁸ : CommSemiring R
inst✝⁷ : AddCommMonoid M
inst✝⁶ : AddCommMonoid M₁
inst✝⁵ : AddCommMonoid M₂
inst✝⁴ : AddCommMonoid M₃
inst✝³ : Module R M
inst✝² : Module R M₁
inst✝¹ : Module R M₂
inst✝ : Module R M₃
Q₁ : QuadraticForm R M₁
Q₂ : QuadraticForm R M₂
Q₃ : QuadraticForm R M₃
f : IsometryEquiv Q₁ Q₂
g : IsometryEquiv Q₂ Q₃
src✝ : M₁ ≃ₗ[R] M₃ := f.toLinearEquiv ≪≫ₗ g.toLinearEquiv
m : M₁
⊢ Q₃
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun, left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
Q₃ (g (f m))
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app];
|
rfl
|
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app];
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.94_0.YOl7VzX5AOw0Rpu
|
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁹ : CommSemiring R
inst✝⁸ : AddCommMonoid M
inst✝⁷ : AddCommMonoid M₁
inst✝⁶ : AddCommMonoid M₂
inst✝⁵ : AddCommMonoid M₃
inst✝⁴ : Module R M
inst✝³ : Module R M₁
inst✝² : Module R M₂
inst✝¹ : Module R M₃
inst✝ : Fintype ι
v : Basis ι R M
Q : QuadraticForm R M
f : M₁ ≃ₗ[R] M
src✝ : M ≃ₗ[R] M₁ := LinearEquiv.symm f
⊢ ∀ (m : M),
(comp Q ↑f)
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun,
left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m) =
Q m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
|
intro
|
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.132_0.YOl7VzX5AOw0Rpu
|
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M))
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝⁹ : CommSemiring R
inst✝⁸ : AddCommMonoid M
inst✝⁷ : AddCommMonoid M₁
inst✝⁶ : AddCommMonoid M₂
inst✝⁵ : AddCommMonoid M₃
inst✝⁴ : Module R M
inst✝³ : Module R M₁
inst✝² : Module R M₂
inst✝¹ : Module R M₃
inst✝ : Fintype ι
v : Basis ι R M
Q : QuadraticForm R M
f : M₁ ≃ₗ[R] M
src✝ : M ≃ₗ[R] M₁ := LinearEquiv.symm f
m✝ : M
⊢ (comp Q ↑f)
(AddHom.toFun
(↑{ toLinearMap := ↑src✝, invFun := src✝.invFun, left_inv := (_ : Function.LeftInverse src✝.invFun src✝.toFun),
right_inv := (_ : Function.RightInverse src✝.invFun src✝.toFun) }).toAddHom
m✝) =
Q m✝
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
|
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply]
|
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.132_0.YOl7VzX5AOw0Rpu
|
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M))
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹³ : CommSemiring R
inst✝¹² : AddCommMonoid M
inst✝¹¹ : AddCommMonoid M₁
inst✝¹⁰ : AddCommMonoid M₂
inst✝⁹ : AddCommMonoid M₃
inst✝⁸ : Module R M
inst✝⁷ : Module R M₁
inst✝⁶ : Module R M₂
inst✝⁵ : Module R M₃
inst✝⁴ : Fintype ι
v✝ : Basis ι R M
inst✝³ : Field K
inst✝² : Invertible 2
inst✝¹ : AddCommGroup V
inst✝ : Module K V
Q : QuadraticForm K V
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : BilinForm.iIsOrtho (associated Q) ⇑v
⊢ IsometryEquiv Q (weightedSumSquares K fun i => Q (v i))
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
|
let iso := Q.isometryEquivBasisRepr v
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.150_0.YOl7VzX5AOw0Rpu
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹³ : CommSemiring R
inst✝¹² : AddCommMonoid M
inst✝¹¹ : AddCommMonoid M₁
inst✝¹⁰ : AddCommMonoid M₂
inst✝⁹ : AddCommMonoid M₃
inst✝⁸ : Module R M
inst✝⁷ : Module R M₁
inst✝⁶ : Module R M₂
inst✝⁵ : Module R M₃
inst✝⁴ : Fintype ι
v✝ : Basis ι R M
inst✝³ : Field K
inst✝² : Invertible 2
inst✝¹ : AddCommGroup V
inst✝ : Module K V
Q : QuadraticForm K V
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : BilinForm.iIsOrtho (associated Q) ⇑v
iso : IsometryEquiv Q (basisRepr Q v) := isometryEquivBasisRepr Q v
⊢ IsometryEquiv Q (weightedSumSquares K fun i => Q (v i))
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
|
refine' ⟨iso, fun m => _⟩
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.150_0.YOl7VzX5AOw0Rpu
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹³ : CommSemiring R
inst✝¹² : AddCommMonoid M
inst✝¹¹ : AddCommMonoid M₁
inst✝¹⁰ : AddCommMonoid M₂
inst✝⁹ : AddCommMonoid M₃
inst✝⁸ : Module R M
inst✝⁷ : Module R M₁
inst✝⁶ : Module R M₂
inst✝⁵ : Module R M₃
inst✝⁴ : Fintype ι
v✝ : Basis ι R M
inst✝³ : Field K
inst✝² : Invertible 2
inst✝¹ : AddCommGroup V
inst✝ : Module K V
Q : QuadraticForm K V
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : BilinForm.iIsOrtho (associated Q) ⇑v
iso : IsometryEquiv Q (basisRepr Q v) := isometryEquivBasisRepr Q v
m : V
⊢ (weightedSumSquares K fun i => Q (v i)) (AddHom.toFun iso.toAddHom m) = Q m
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
|
convert iso.map_app m
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.150_0.YOl7VzX5AOw0Rpu
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case h.e'_2.h.e'_5
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹³ : CommSemiring R
inst✝¹² : AddCommMonoid M
inst✝¹¹ : AddCommMonoid M₁
inst✝¹⁰ : AddCommMonoid M₂
inst✝⁹ : AddCommMonoid M₃
inst✝⁸ : Module R M
inst✝⁷ : Module R M₁
inst✝⁶ : Module R M₂
inst✝⁵ : Module R M₃
inst✝⁴ : Fintype ι
v✝ : Basis ι R M
inst✝³ : Field K
inst✝² : Invertible 2
inst✝¹ : AddCommGroup V
inst✝ : Module K V
Q : QuadraticForm K V
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : BilinForm.iIsOrtho (associated Q) ⇑v
iso : IsometryEquiv Q (basisRepr Q v) := isometryEquivBasisRepr Q v
m : V
⊢ (weightedSumSquares K fun i => Q (v i)) = basisRepr Q v
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
convert iso.map_app m
|
rw [basisRepr_eq_of_iIsOrtho _ _ hv₁]
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
convert iso.map_app m
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.150_0.YOl7VzX5AOw0Rpu
|
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹⁴ : CommSemiring R
inst✝¹³ : AddCommMonoid M
inst✝¹² : AddCommMonoid M₁
inst✝¹¹ : AddCommMonoid M₂
inst✝¹⁰ : AddCommMonoid M₃
inst✝⁹ : Module R M
inst✝⁸ : Module R M₁
inst✝⁷ : Module R M₂
inst✝⁶ : Module R M₃
inst✝⁵ : Fintype ι
v : Basis ι R M
inst✝⁴ : Field K
inst✝³ : Invertible 2
inst✝² : AddCommGroup V
inst✝¹ : Module K V
inst✝ : FiniteDimensional K V
Q : QuadraticForm K V
hQ : Nondegenerate (associated Q)
⊢ ∃ w, Equivalent Q (weightedSumSquares K w)
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
convert iso.map_app m
rw [basisRepr_eq_of_iIsOrtho _ _ hv₁]
#align quadratic_form.isometry_weighted_sum_squares QuadraticForm.isometryEquivWeightedSumSquares
variable [FiniteDimensional K V]
open BilinForm
theorem equivalent_weightedSumSquares (Q : QuadraticForm K V) :
∃ w : Fin (FiniteDimensional.finrank K V) → K, Equivalent Q (weightedSumSquares K w) :=
let ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm _ Q)
⟨_, ⟨Q.isometryEquivWeightedSumSquares v hv₁⟩⟩
#align quadratic_form.equivalent_weighted_sum_squares QuadraticForm.equivalent_weightedSumSquares
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
|
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.172_0.YOl7VzX5AOw0Rpu
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case intro
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹⁴ : CommSemiring R
inst✝¹³ : AddCommMonoid M
inst✝¹² : AddCommMonoid M₁
inst✝¹¹ : AddCommMonoid M₂
inst✝¹⁰ : AddCommMonoid M₃
inst✝⁹ : Module R M
inst✝⁸ : Module R M₁
inst✝⁷ : Module R M₂
inst✝⁶ : Module R M₃
inst✝⁵ : Fintype ι
v✝ : Basis ι R M
inst✝⁴ : Field K
inst✝³ : Invertible 2
inst✝² : AddCommGroup V
inst✝¹ : Module K V
inst✝ : FiniteDimensional K V
Q : QuadraticForm K V
hQ : Nondegenerate (associated Q)
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : iIsOrtho ((associatedHom K) Q) ⇑v
⊢ ∃ w, Equivalent Q (weightedSumSquares K w)
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
convert iso.map_app m
rw [basisRepr_eq_of_iIsOrtho _ _ hv₁]
#align quadratic_form.isometry_weighted_sum_squares QuadraticForm.isometryEquivWeightedSumSquares
variable [FiniteDimensional K V]
open BilinForm
theorem equivalent_weightedSumSquares (Q : QuadraticForm K V) :
∃ w : Fin (FiniteDimensional.finrank K V) → K, Equivalent Q (weightedSumSquares K w) :=
let ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm _ Q)
⟨_, ⟨Q.isometryEquivWeightedSumSquares v hv₁⟩⟩
#align quadratic_form.equivalent_weighted_sum_squares QuadraticForm.equivalent_weightedSumSquares
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
|
have hv₂ := hv₁.not_isOrtho_basis_self_of_nondegenerate hQ
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.172_0.YOl7VzX5AOw0Rpu
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case intro
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹⁴ : CommSemiring R
inst✝¹³ : AddCommMonoid M
inst✝¹² : AddCommMonoid M₁
inst✝¹¹ : AddCommMonoid M₂
inst✝¹⁰ : AddCommMonoid M₃
inst✝⁹ : Module R M
inst✝⁸ : Module R M₁
inst✝⁷ : Module R M₂
inst✝⁶ : Module R M₃
inst✝⁵ : Fintype ι
v✝ : Basis ι R M
inst✝⁴ : Field K
inst✝³ : Invertible 2
inst✝² : AddCommGroup V
inst✝¹ : Module K V
inst✝ : FiniteDimensional K V
Q : QuadraticForm K V
hQ : Nondegenerate (associated Q)
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : iIsOrtho ((associatedHom K) Q) ⇑v
hv₂ : ∀ (i : Fin (FiniteDimensional.finrank K V)), ¬IsOrtho ((associatedHom K) Q) (v i) (v i)
⊢ ∃ w, Equivalent Q (weightedSumSquares K w)
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
convert iso.map_app m
rw [basisRepr_eq_of_iIsOrtho _ _ hv₁]
#align quadratic_form.isometry_weighted_sum_squares QuadraticForm.isometryEquivWeightedSumSquares
variable [FiniteDimensional K V]
open BilinForm
theorem equivalent_weightedSumSquares (Q : QuadraticForm K V) :
∃ w : Fin (FiniteDimensional.finrank K V) → K, Equivalent Q (weightedSumSquares K w) :=
let ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm _ Q)
⟨_, ⟨Q.isometryEquivWeightedSumSquares v hv₁⟩⟩
#align quadratic_form.equivalent_weighted_sum_squares QuadraticForm.equivalent_weightedSumSquares
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
have hv₂ := hv₁.not_isOrtho_basis_self_of_nondegenerate hQ
|
simp_rw [IsOrtho, associated_eq_self_apply] at hv₂
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
have hv₂ := hv₁.not_isOrtho_basis_self_of_nondegenerate hQ
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.172_0.YOl7VzX5AOw0Rpu
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
case intro
ι : Type u_1
R : Type u_2
K : Type u_3
M : Type u_4
M₁ : Type u_5
M₂ : Type u_6
M₃ : Type u_7
V : Type u_8
inst✝¹⁴ : CommSemiring R
inst✝¹³ : AddCommMonoid M
inst✝¹² : AddCommMonoid M₁
inst✝¹¹ : AddCommMonoid M₂
inst✝¹⁰ : AddCommMonoid M₃
inst✝⁹ : Module R M
inst✝⁸ : Module R M₁
inst✝⁷ : Module R M₂
inst✝⁶ : Module R M₃
inst✝⁵ : Fintype ι
v✝ : Basis ι R M
inst✝⁴ : Field K
inst✝³ : Invertible 2
inst✝² : AddCommGroup V
inst✝¹ : Module K V
inst✝ : FiniteDimensional K V
Q : QuadraticForm K V
hQ : Nondegenerate (associated Q)
v : Basis (Fin (FiniteDimensional.finrank K V)) K V
hv₁ : iIsOrtho ((associatedHom K) Q) ⇑v
hv₂ : ∀ (i : Fin (FiniteDimensional.finrank K V)), ¬Q (v i) = 0
⊢ ∃ w, Equivalent Q (weightedSumSquares K w)
|
/-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Eric Wieser
-/
import Mathlib.LinearAlgebra.QuadraticForm.Basic
import Mathlib.LinearAlgebra.QuadraticForm.Isometry
#align_import linear_algebra.quadratic_form.isometry from "leanprover-community/mathlib"@"14b69e9f3c16630440a2cbd46f1ddad0d561dee7"
/-!
# Isometric equivalences with respect to quadratic forms
## Main definitions
* `QuadraticForm.IsometryEquiv`: `LinearEquiv`s which map between two different quadratic forms
* `QuadraticForm.Equivalent`: propositional version of the above
## Main results
* `equivalent_weighted_sum_squares`: in finite dimensions, any quadratic form is equivalent to a
parametrization of `QuadraticForm.weightedSumSquares`.
-/
variable {ι R K M M₁ M₂ M₃ V : Type*}
namespace QuadraticForm
variable [CommSemiring R]
variable [AddCommMonoid M] [AddCommMonoid M₁] [AddCommMonoid M₂] [AddCommMonoid M₃]
variable [Module R M] [Module R M₁] [Module R M₂] [Module R M₃]
/-- An isometric equivalence between two quadratic spaces `M₁, Q₁` and `M₂, Q₂` over a ring `R`,
is a linear equivalence between `M₁` and `M₂` that commutes with the quadratic forms. -/
-- Porting note: not implemented @[nolint has_nonempty_instance]
structure IsometryEquiv (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂)
extends M₁ ≃ₗ[R] M₂ where
map_app' : ∀ m, Q₂ (toFun m) = Q₁ m
#align quadratic_form.isometry QuadraticForm.IsometryEquiv
/-- Two quadratic forms over a ring `R` are equivalent
if there exists an isometric equivalence between them:
a linear equivalence that transforms one quadratic form into the other. -/
def Equivalent (Q₁ : QuadraticForm R M₁) (Q₂ : QuadraticForm R M₂) : Prop :=
Nonempty (Q₁.IsometryEquiv Q₂)
#align quadratic_form.equivalent QuadraticForm.Equivalent
namespace IsometryEquiv
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
instance : LinearEquivClass (Q₁.IsometryEquiv Q₂) R M₁ M₂ where
coe f := f.toLinearEquiv
inv f := f.toLinearEquiv.symm
left_inv f := f.toLinearEquiv.left_inv
right_inv f := f.toLinearEquiv.right_inv
coe_injective' f g := by cases f; cases g; simp (config := {contextual := true})
map_add f := map_add f.toLinearEquiv
map_smulₛₗ f := map_smulₛₗ f.toLinearEquiv
-- Porting note: was `Coe`
instance : CoeOut (Q₁.IsometryEquiv Q₂) (M₁ ≃ₗ[R] M₂) :=
⟨IsometryEquiv.toLinearEquiv⟩
-- Porting note: syntaut
#noalign quadratic_form.isometry.to_linear_equiv_eq_coe
@[simp]
theorem coe_toLinearEquiv (f : Q₁.IsometryEquiv Q₂) : ⇑(f : M₁ ≃ₗ[R] M₂) = f :=
rfl
#align quadratic_form.isometry.coe_to_linear_equiv QuadraticForm.IsometryEquiv.coe_toLinearEquiv
@[simp]
theorem map_app (f : Q₁.IsometryEquiv Q₂) (m : M₁) : Q₂ (f m) = Q₁ m :=
f.map_app' m
#align quadratic_form.isometry.map_app QuadraticForm.IsometryEquiv.map_app
/-- The identity isometric equivalence between a quadratic form and itself. -/
@[refl]
def refl (Q : QuadraticForm R M) : Q.IsometryEquiv Q :=
{ LinearEquiv.refl R M with map_app' := fun _ => rfl }
#align quadratic_form.isometry.refl QuadraticForm.IsometryEquiv.refl
/-- The inverse isometric equivalence of an isometric equivalence between two quadratic forms. -/
@[symm]
def symm (f : Q₁.IsometryEquiv Q₂) : Q₂.IsometryEquiv Q₁ :=
{ (f : M₁ ≃ₗ[R] M₂).symm with
map_app' := by intro m; rw [← f.map_app]; congr; exact f.toLinearEquiv.apply_symm_apply m }
#align quadratic_form.isometry.symm QuadraticForm.IsometryEquiv.symm
/-- The composition of two isometric equivalences between quadratic forms. -/
@[trans]
def trans (f : Q₁.IsometryEquiv Q₂) (g : Q₂.IsometryEquiv Q₃) : Q₁.IsometryEquiv Q₃ :=
{ (f : M₁ ≃ₗ[R] M₂).trans (g : M₂ ≃ₗ[R] M₃) with
map_app' := by intro m; rw [← f.map_app, ← g.map_app]; rfl }
#align quadratic_form.isometry.trans QuadraticForm.IsometryEquiv.trans
/-- Isometric equivalences are isometric maps -/
@[simps]
def toIsometry (g : Q₁.IsometryEquiv Q₂) : Q₁ →qᵢ Q₂ where
toFun x := g x
__ := g
end IsometryEquiv
namespace Equivalent
variable {Q₁ : QuadraticForm R M₁} {Q₂ : QuadraticForm R M₂} {Q₃ : QuadraticForm R M₃}
@[refl]
theorem refl (Q : QuadraticForm R M) : Q.Equivalent Q :=
⟨IsometryEquiv.refl Q⟩
#align quadratic_form.equivalent.refl QuadraticForm.Equivalent.refl
@[symm]
theorem symm (h : Q₁.Equivalent Q₂) : Q₂.Equivalent Q₁ :=
h.elim fun f => ⟨f.symm⟩
#align quadratic_form.equivalent.symm QuadraticForm.Equivalent.symm
@[trans]
theorem trans (h : Q₁.Equivalent Q₂) (h' : Q₂.Equivalent Q₃) : Q₁.Equivalent Q₃ :=
h'.elim <| h.elim fun f g => ⟨f.trans g⟩
#align quadratic_form.equivalent.trans QuadraticForm.Equivalent.trans
end Equivalent
variable [Fintype ι] {v : Basis ι R M}
/-- A quadratic form composed with a `LinearEquiv` is isometric to itself. -/
def isometryEquivOfCompLinearEquiv (Q : QuadraticForm R M) (f : M₁ ≃ₗ[R] M) :
Q.IsometryEquiv (Q.comp (f : M₁ →ₗ[R] M)) :=
{ f.symm with
map_app' := by
intro
simp only [comp_apply, LinearEquiv.coe_coe, LinearEquiv.toFun_eq_coe,
LinearEquiv.apply_symm_apply, f.apply_symm_apply] }
#align quadratic_form.isometry_of_comp_linear_equiv QuadraticForm.isometryEquivOfCompLinearEquiv
/-- A quadratic form is isometrically equivalent to its bases representations. -/
noncomputable def isometryEquivBasisRepr (Q : QuadraticForm R M) (v : Basis ι R M) :
IsometryEquiv Q (Q.basisRepr v) :=
isometryEquivOfCompLinearEquiv Q v.equivFun.symm
#align quadratic_form.isometry_basis_repr QuadraticForm.isometryEquivBasisRepr
variable [Field K] [Invertible (2 : K)] [AddCommGroup V] [Module K V]
/-- Given an orthogonal basis, a quadratic form is isometrically equivalent with a weighted sum of
squares. -/
noncomputable def isometryEquivWeightedSumSquares (Q : QuadraticForm K V)
(v : Basis (Fin (FiniteDimensional.finrank K V)) K V)
(hv₁ : (associated (R := K) Q).iIsOrtho v) :
Q.IsometryEquiv (weightedSumSquares K fun i => Q (v i)) := by
let iso := Q.isometryEquivBasisRepr v
refine' ⟨iso, fun m => _⟩
convert iso.map_app m
rw [basisRepr_eq_of_iIsOrtho _ _ hv₁]
#align quadratic_form.isometry_weighted_sum_squares QuadraticForm.isometryEquivWeightedSumSquares
variable [FiniteDimensional K V]
open BilinForm
theorem equivalent_weightedSumSquares (Q : QuadraticForm K V) :
∃ w : Fin (FiniteDimensional.finrank K V) → K, Equivalent Q (weightedSumSquares K w) :=
let ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm _ Q)
⟨_, ⟨Q.isometryEquivWeightedSumSquares v hv₁⟩⟩
#align quadratic_form.equivalent_weighted_sum_squares QuadraticForm.equivalent_weightedSumSquares
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
have hv₂ := hv₁.not_isOrtho_basis_self_of_nondegenerate hQ
simp_rw [IsOrtho, associated_eq_self_apply] at hv₂
|
exact ⟨fun i => Units.mk0 _ (hv₂ i), ⟨Q.isometryEquivWeightedSumSquares v hv₁⟩⟩
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R := K) Q).Nondegenerate) :
∃ w : Fin (FiniteDimensional.finrank K V) → Kˣ, Equivalent Q (weightedSumSquares K w) := by
obtain ⟨v, hv₁⟩ := exists_orthogonal_basis (associated_isSymm K Q)
have hv₂ := hv₁.not_isOrtho_basis_self_of_nondegenerate hQ
simp_rw [IsOrtho, associated_eq_self_apply] at hv₂
|
Mathlib.LinearAlgebra.QuadraticForm.IsometryEquiv.172_0.YOl7VzX5AOw0Rpu
|
theorem equivalent_weightedSumSquares_units_of_nondegenerate' (Q : QuadraticForm K V)
(hQ : (associated (R
|
Mathlib_LinearAlgebra_QuadraticForm_IsometryEquiv
|
C : Type u₁
inst✝¹ : Category.{v₁, u₁} C
inst✝ : Preadditive C
X✝ Y✝ : Mat_ C
f : X✝ ⟶ Y✝
⊢ 𝟙 X✝ ≫ f = f
|
/-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.Algebra.BigOperators.Basic
import Mathlib.Algebra.BigOperators.Pi
import Mathlib.CategoryTheory.Limits.Shapes.Biproducts
import Mathlib.CategoryTheory.Preadditive.Basic
import Mathlib.CategoryTheory.Preadditive.AdditiveFunctor
import Mathlib.Data.Matrix.DMatrix
import Mathlib.Data.Matrix.Basic
import Mathlib.CategoryTheory.FintypeCat
import Mathlib.CategoryTheory.Preadditive.SingleObj
import Mathlib.Algebra.Opposites
#align_import category_theory.preadditive.Mat from "leanprover-community/mathlib"@"829895f162a1f29d0133f4b3538f4cd1fb5bffd3"
/-!
# Matrices over a category.
When `C` is a preadditive category, `Mat_ C` is the preadditive category
whose objects are finite tuples of objects in `C`, and
whose morphisms are matrices of morphisms from `C`.
There is a functor `Mat_.embedding : C ⥤ Mat_ C` sending morphisms to one-by-one matrices.
`Mat_ C` has finite biproducts.
## The additive envelope
We show that this construction is the "additive envelope" of `C`,
in the sense that any additive functor `F : C ⥤ D` to a category `D` with biproducts
lifts to a functor `Mat_.lift F : Mat_ C ⥤ D`,
Moreover, this functor is unique (up to natural isomorphisms) amongst functors `L : Mat_ C ⥤ D`
such that `embedding C ⋙ L ≅ F`.
(As we don't have 2-category theory, we can't explicitly state that `Mat_ C` is
the initial object in the 2-category of categories under `C` which have biproducts.)
As a consequence, when `C` already has finite biproducts we have `Mat_ C ≌ C`.
## Future work
We should provide a more convenient `Mat R`, when `R` is a ring,
as a category with objects `n : FinType`,
and whose morphisms are matrices with components in `R`.
Ideally this would conveniently interact with both `Mat_` and `Matrix`.
-/
open CategoryTheory CategoryTheory.Preadditive
open scoped BigOperators Classical
noncomputable section
namespace CategoryTheory
universe w v₁ v₂ u₁ u₂
variable (C : Type u₁) [Category.{v₁} C] [Preadditive C]
/-- An object in `Mat_ C` is a finite tuple of objects in `C`.
-/
structure Mat_ where
ι : Type
[fintype : Fintype ι]
X : ι → C
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_ CategoryTheory.Mat_
attribute [instance] Mat_.fintype
namespace Mat_
variable {C}
-- porting note: removed @[nolint has_nonempty_instance]
/-- A morphism in `Mat_ C` is a dependently typed matrix of morphisms. -/
def Hom (M N : Mat_ C) : Type v₁ :=
DMatrix M.ι N.ι fun i j => M.X i ⟶ N.X j
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_.hom CategoryTheory.Mat_.Hom
namespace Hom
/-- The identity matrix consists of identity morphisms on the diagonal, and zeros elsewhere. -/
def id (M : Mat_ C) : Hom M M := fun i j => if h : i = j then eqToHom (congr_arg M.X h) else 0
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_.hom.id CategoryTheory.Mat_.Hom.id
/-- Composition of matrices using matrix multiplication. -/
def comp {M N K : Mat_ C} (f : Hom M N) (g : Hom N K) : Hom M K := fun i k =>
∑ j : N.ι, f i j ≫ g j k
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_.hom.comp CategoryTheory.Mat_.Hom.comp
end Hom
section
attribute [local simp] Hom.id Hom.comp
instance : Category.{v₁} (Mat_ C) where
Hom := Hom
id := Hom.id
comp f g := f.comp g
id_comp f := by
|
simp (config := { unfoldPartialApp := true }) [dite_comp]
|
instance : Category.{v₁} (Mat_ C) where
Hom := Hom
id := Hom.id
comp f g := f.comp g
id_comp f := by
|
Mathlib.CategoryTheory.Preadditive.Mat.106_0.xG9GKY7NTklnF73
|
instance : Category.{v₁} (Mat_ C) where
Hom
|
Mathlib_CategoryTheory_Preadditive_Mat
|
C : Type u₁
inst✝¹ : Category.{v₁, u₁} C
inst✝ : Preadditive C
X✝ Y✝ : Mat_ C
f : X✝ ⟶ Y✝
⊢ f ≫ 𝟙 Y✝ = f
|
/-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.Algebra.BigOperators.Basic
import Mathlib.Algebra.BigOperators.Pi
import Mathlib.CategoryTheory.Limits.Shapes.Biproducts
import Mathlib.CategoryTheory.Preadditive.Basic
import Mathlib.CategoryTheory.Preadditive.AdditiveFunctor
import Mathlib.Data.Matrix.DMatrix
import Mathlib.Data.Matrix.Basic
import Mathlib.CategoryTheory.FintypeCat
import Mathlib.CategoryTheory.Preadditive.SingleObj
import Mathlib.Algebra.Opposites
#align_import category_theory.preadditive.Mat from "leanprover-community/mathlib"@"829895f162a1f29d0133f4b3538f4cd1fb5bffd3"
/-!
# Matrices over a category.
When `C` is a preadditive category, `Mat_ C` is the preadditive category
whose objects are finite tuples of objects in `C`, and
whose morphisms are matrices of morphisms from `C`.
There is a functor `Mat_.embedding : C ⥤ Mat_ C` sending morphisms to one-by-one matrices.
`Mat_ C` has finite biproducts.
## The additive envelope
We show that this construction is the "additive envelope" of `C`,
in the sense that any additive functor `F : C ⥤ D` to a category `D` with biproducts
lifts to a functor `Mat_.lift F : Mat_ C ⥤ D`,
Moreover, this functor is unique (up to natural isomorphisms) amongst functors `L : Mat_ C ⥤ D`
such that `embedding C ⋙ L ≅ F`.
(As we don't have 2-category theory, we can't explicitly state that `Mat_ C` is
the initial object in the 2-category of categories under `C` which have biproducts.)
As a consequence, when `C` already has finite biproducts we have `Mat_ C ≌ C`.
## Future work
We should provide a more convenient `Mat R`, when `R` is a ring,
as a category with objects `n : FinType`,
and whose morphisms are matrices with components in `R`.
Ideally this would conveniently interact with both `Mat_` and `Matrix`.
-/
open CategoryTheory CategoryTheory.Preadditive
open scoped BigOperators Classical
noncomputable section
namespace CategoryTheory
universe w v₁ v₂ u₁ u₂
variable (C : Type u₁) [Category.{v₁} C] [Preadditive C]
/-- An object in `Mat_ C` is a finite tuple of objects in `C`.
-/
structure Mat_ where
ι : Type
[fintype : Fintype ι]
X : ι → C
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_ CategoryTheory.Mat_
attribute [instance] Mat_.fintype
namespace Mat_
variable {C}
-- porting note: removed @[nolint has_nonempty_instance]
/-- A morphism in `Mat_ C` is a dependently typed matrix of morphisms. -/
def Hom (M N : Mat_ C) : Type v₁ :=
DMatrix M.ι N.ι fun i j => M.X i ⟶ N.X j
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_.hom CategoryTheory.Mat_.Hom
namespace Hom
/-- The identity matrix consists of identity morphisms on the diagonal, and zeros elsewhere. -/
def id (M : Mat_ C) : Hom M M := fun i j => if h : i = j then eqToHom (congr_arg M.X h) else 0
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_.hom.id CategoryTheory.Mat_.Hom.id
/-- Composition of matrices using matrix multiplication. -/
def comp {M N K : Mat_ C} (f : Hom M N) (g : Hom N K) : Hom M K := fun i k =>
∑ j : N.ι, f i j ≫ g j k
set_option linter.uppercaseLean3 false in
#align category_theory.Mat_.hom.comp CategoryTheory.Mat_.Hom.comp
end Hom
section
attribute [local simp] Hom.id Hom.comp
instance : Category.{v₁} (Mat_ C) where
Hom := Hom
id := Hom.id
comp f g := f.comp g
id_comp f := by simp (config := { unfoldPartialApp := true }) [dite_comp]
comp_id f := by
|
simp (config := { unfoldPartialApp := true }) [comp_dite]
|
instance : Category.{v₁} (Mat_ C) where
Hom := Hom
id := Hom.id
comp f g := f.comp g
id_comp f := by simp (config := { unfoldPartialApp := true }) [dite_comp]
comp_id f := by
|
Mathlib.CategoryTheory.Preadditive.Mat.106_0.xG9GKY7NTklnF73
|
instance : Category.{v₁} (Mat_ C) where
Hom
|
Mathlib_CategoryTheory_Preadditive_Mat
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.