fact stringlengths 6 3.84k | type stringclasses 11 values | library stringclasses 32 values | imports listlengths 1 14 | filename stringlengths 20 95 | symbolic_name stringlengths 1 90 | docstring stringlengths 7 20k ⌀ |
|---|---|---|---|---|---|---|
primrec_rfind' : 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)
@[deprecated (since := "2025-05-12")] alias rfind_prim := primrec_rfind' | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | primrec_rfind' | null |
primrec_recOn' {α σ}
[Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code × Code × σ × σ → σ} (hpr : Primrec₂ pr)
{co : α → Code × Code × σ × σ → σ} (hco : Primrec₂ co) {pc : α → Code × Code × σ × σ → σ}
(hpc : Primrec₂ pc) {rf : α → Code × σ → σ} (hrf : Primrec₂ rf) :
let PR (a) cf cg hf hg := pr a (cf, cg, hf, hg)
let CO (a) cf cg hf hg := co a (cf, cg, hf, hg)
let PC (a) cf cg hf hg := pc a (cf, cg, hf, hg)
let RF (a) cf hf := rf a (cf, hf)
let F (a : α) (c : Code) : σ :=
Nat.Partrec.Code.recOn c (z a) (s a) (l a) (r a) (PR a) (CO a) (PC a) (RF a)
Primrec (fun a => F a (c a) : α → σ) := by
intro _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
letI a := p.1.1; letI IH := p.1.2; letI n := p.2.1; letI m := p.2.2
IH[m]?.bind fun s =>
IH[m.unpair.1]?.bind fun s₁ =>
IH[m.unpair.2]?.map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂))
(pr a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
have : Primrec G₁ :=
option_bind (list_getElem?.comp (snd.comp fst) (snd.comp snd)) <| .mk <|
option_bind ((list_getElem?.comp (snd.comp fst)
(fst.comp <| Primrec.unpair.comp (snd.comp snd))).comp fst) <| .mk <|
option_map ((list_getElem?.comp (snd.comp fst)
(snd.comp <| Primrec.unpair.comp (snd.comp snd))).comp <| fst.comp fst) <| .mk <|
have a := fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n := fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m := snd.comp (snd.comp <| fst.comp <| fst.comp fst)
have m₁ := fst.comp (Primrec.unpair.comp m)
have m₂ := snd.comp (Primrec.unpair.comp m)
have s := snd.comp (fst.comp fst)
have s₁ := snd.comp fst
have s₂ := snd
(nat_bodd.comp n).cond
((nat_bodd.comp <| nat_div2.comp n).cond
(hrf.comp a (((Primrec.ofNat Code).comp m).pair s))
(hpc.comp a (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
(Primrec.cond (nat_bodd.comp <| nat_div2.comp n)
(hco.comp a (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂))
(hpr.comp a (((Primrec.ofNat Code).comp m₁).pair <|
((Primrec.ofNat Code).comp m₂).pair <| s₁.pair s₂)))
let G : α → List σ → Option σ := fun a IH =>
IH.length.casesOn (some (z a)) fun n =>
n.casesOn (some (s a)) fun n =>
... | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | primrec_recOn' | null |
primrec_recOn {α σ}
[Primcodable α] [Primcodable σ] {c : α → Code} (hc : Primrec c) {z : α → σ}
(hz : Primrec z) {s : α → σ} (hs : Primrec s) {l : α → σ} (hl : Primrec l) {r : α → σ}
(hr : Primrec r) {pr : α → Code → Code → σ → σ → σ}
(hpr : Primrec fun a : α × Code × Code × σ × σ => pr a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{co : α → Code → Code → σ → σ → σ}
(hco : Primrec fun a : α × Code × Code × σ × σ => co a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.2.2.2)
{pc : α → Code → Code → σ → σ → σ}
(hpc : Primrec fun a : α × Code × Code × σ × σ => pc a.1 a.2.1 a.2.2.1 a.2.2.2.1 a.2.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) :=
primrec_recOn' hc hz hs hl hr
(pr := fun a b => pr a b.1 b.2.1 b.2.2.1 b.2.2.2) (.mk hpr)
(co := fun a b => co a b.1 b.2.1 b.2.2.1 b.2.2.2) (.mk hco)
(pc := fun a b => pc a b.1 b.2.1 b.2.2.1 b.2.2.2) (.mk hpc)
(rf := fun a b => rf a b.1 b.2) (.mk hrf)
@[deprecated (since := "2025-05-12")] alias rec_prim := primrec_recOn | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | primrec_recOn | Recursion on `Nat.Partrec.Code` is primitive recursive. |
computable_recOn {α σ} [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
intro _ _ _ _ F
let G₁ : (α × List σ) × ℕ × ℕ → Option σ := fun p =>
letI a := p.1.1; letI IH := p.1.2; letI n := p.2.1; letI m := p.2.2
IH[m]?.bind fun s =>
IH[m.unpair.1]?.bind fun s₁ =>
IH[m.unpair.2]?.map fun s₂ =>
cond n.bodd
(cond n.div2.bodd (rf a (ofNat Code m, s))
(pc a (ofNat Code m.unpair.1, ofNat Code m.unpair.2, s₁, s₂)))
(cond n.div2.bodd (co a (ofNat Code m.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_getElem?.comp (snd.comp fst) (snd.comp snd)) <| .mk ?_
refine option_bind ((list_getElem?.comp (snd.comp fst)
(fst.comp <| Computable.unpair.comp (snd.comp snd))).comp fst) <| .mk ?_
refine option_map ((list_getElem?.comp (snd.comp fst)
(snd.comp <| Computable.unpair.comp (snd.comp snd))).comp <| fst.comp fst) <| .mk ?_
exact
have a := fst.comp (fst.comp <| fst.comp <| fst.comp fst)
have n := fst.comp (snd.comp <| fst.comp <| fst.comp fst)
have m := 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 := snd.comp (fst.comp fst)
have s₁ := snd.comp fst
have s₂ := snd
(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 =>
... | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | computable_recOn | Recursion on `Nat.Partrec.Code` is computable. |
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) | def | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval | 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` |
@[simp]
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] | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_prec_zero | Helper lemma for the evaluation of `prec` in the base case. |
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 | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_prec_succ | Helper lemma for the evaluation of `prec` in the recursive case. |
@[simp]
eval_const : ∀ n m, eval (Code.const n) m = Part.some n
| 0, _ => rfl
| n + 1, m => by simp! [eval_const n m]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_const | null |
eval_id (n) : eval Code.id n = Part.some n := by simp! [Seq.seq, Code.id]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_id | null |
eval_curry (c n x) : eval (curry c n) x = eval c (Nat.pair n x) := by simp! [Seq.seq, curry] | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_curry | null |
primrec_const : Primrec Code.const :=
(_root_.Primrec.id.nat_iterate (_root_.Primrec.const zero)
(primrec₂_comp.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]
@[deprecated (since := "2025-05-12")] alias const_prim := primrec_const | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | primrec_const | null |
primrec₂_curry : Primrec₂ curry :=
primrec₂_comp.comp Primrec.fst <| primrec₂_pair.comp (primrec_const.comp Primrec.snd)
(_root_.Primrec.const Code.id)
@[deprecated (since := "2025-05-12")] alias curry_prim := primrec₂_curry | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | primrec₂_curry | null |
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₃⟩ | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | curry_inj | null |
smn :
∃ f : Code → ℕ → Code, Computable₂ f ∧ ∀ c n x, eval (f c n) x = eval c (Nat.pair n x) :=
⟨curry, Primrec₂.to_comp primrec₂_curry, eval_curry⟩ | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | smn | 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. |
exists_code {f : ℕ →. ℕ} : Nat.Partrec f ↔ ∃ c : Code, eval c = f := by
refine ⟨fun h => ?_, ?_⟩
· induction h with
| zero => exact ⟨zero, rfl⟩
| succ => exact ⟨succ, rfl⟩
| left => exact ⟨left, rfl⟩
| right => exact ⟨right, rfl⟩
| pair pf pg hf hg =>
rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
exact ⟨pair cf cg, rfl⟩
| comp pf pg hf hg =>
rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
exact ⟨comp cf cg, rfl⟩
| prec pf pg hf hg =>
rcases hf with ⟨cf, rfl⟩; rcases hg with ⟨cg, rfl⟩
exact ⟨prec cf cg, rfl⟩
| rfind 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']
· rintro ⟨c, rfl⟩
induction c with
| zero => exact Nat.Partrec.zero
| succ => exact Nat.Partrec.succ
| left => exact Nat.Partrec.left
| right => exact Nat.Partrec.right
| pair cf cg pf pg => exact pf.pair pg
| comp cf cg pf pg => exact pf.comp pg
| prec cf cg pf pg => exact pf.prec pg
| rfind' cf pf => exact pf.rfind' | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | exists_code | A function is partial recursive if and only if there is a code implementing it. Therefore,
`eval` is a **universal partial recursive function**. |
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)) | def | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | evaln | 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`. |
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 [Option.bind_eq_some_iff] using Nat.lt_succ_of_le | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | evaln_bound | null |
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_iff, 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 generalizing x n <;> rw [evaln] at h ⊢ <;> refine this hl' (fun h => ?_) h
iterate 4 exact h
case pair cf cg hf hg _ =>
simp? [Seq.seq, Option.bind_eq_some_iff] at h ⊢ says
simp only [Seq.seq, Option.map_eq_map, Option.mem_def, Option.bind_eq_some_iff,
Option.map_eq_some_iff, exists_exists_and_eq_and] at h ⊢
exact h.imp fun a => And.imp (hf _ _) <| Exists.imp fun b => And.imp_left (hg _ _)
case comp cf cg hf hg _ =>
simp? [Bind.bind, Option.bind_eq_some_iff] at h ⊢ says
simp only [bind, Option.mem_def, Option.bind_eq_some_iff] at h ⊢
exact h.imp fun a => And.imp (hg _ _) (hf _ _)
case prec cf cg hf hg _ =>
revert h
simp only [unpaired, bind, Option.mem_def]
induction n.unpair.2 <;> simp [Option.bind_eq_some_iff]
· apply hf
· exact fun y h₁ h₂ => ⟨y, evaln_mono hl' h₁, hg _ _ h₂⟩
case rfind' cf hf _ =>
simp? [Bind.bind, Option.bind_eq_some_iff] at h ⊢ says
simp only [unpaired, bind, pair_unpair, Option.pure_def, Option.mem_def,
Option.bind_eq_some_iff] at h ⊢
refine h.imp fun x => And.imp (hf _ _) ?_
by_cases x0 : x = 0 <;> simp [x0]
exact evaln_mono hl' | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | evaln_mono | null |
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 generalizing x n <;> simp [eval, evaln, Option.bind_eq_some_iff, Seq.seq] at h ⊢ <;>
obtain ⟨_, h⟩ := h
iterate 4 simpa [pure, PFun.pure, eq_comm] using h
case pair cf cg hf hg _ =>
rcases h with ⟨y, ef, z, eg, rfl⟩
exact ⟨_, hf _ _ ef, _, hg _ _ eg, rfl⟩
case comp cf cg hf hg _ =>
rcases h with ⟨y, eg, ef⟩
exact ⟨_, hg _ _ eg, hf _ _ ef⟩
case prec cf cg hf hg _ =>
revert h
induction n.unpair.2 generalizing x with simp [Option.bind_eq_some_iff]
| zero => apply hf
| succ m IH =>
refine fun y h₁ h₂ => ⟨y, IH _ ?_, ?_⟩
· have := evaln_mono k.le_succ h₁
simp [evaln, Option.bind_eq_some_iff] at this
exact this.2
· exact hg _ _ h₂
case rfind' cf hf _ =>
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 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]⟩
rcases 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 [add_comm, add_left_comm] using hz, z0⟩ | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | evaln_sound | null |
evaln_complete {c n x} : x ∈ eval c n ↔ ∃ k, x ∈ evaln k c n := by
refine ⟨fun h => ?_, fun ⟨k, h⟩ => evaln_sound h⟩
rsuffices ⟨k, h⟩ : ∃ k, x ∈ evaln (k + 1) c n
· exact ⟨k + 1, h⟩
induction c generalizing n x with
simp [eval, evaln, pure, PFun.pure, Seq.seq, Option.bind_eq_some_iff] at h ⊢
| 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⟩
| 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₂⟩
| prec cf cg hf hg =>
revert h
generalize n.unpair.1 = n₁; generalize n.unpair.2 = n₂
induction n₂ generalizing x n with simp [Option.bind_eq_some_iff]
| zero =>
intro h
rcases hf h with ⟨k, hk⟩
exact ⟨_, le_max_left _ _, evaln_mono (Nat.succ_le_succ <| le_max_right _ _) hk⟩
| succ m IH =>
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_iff,
Option.guard_eq_some', exists_and_left, exists_const]
exact ⟨le_trans (le_max_right _ _) nk₁, hk₁⟩
| 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, Option.bind_eq_some_iff]
revert hy₁ hy₂
generalize n.unpair.2 = m
intro hy₁ hy₂
induction y generalizing m with simp [evaln, Option.bind_eq_some_iff]
... | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | evaln_complete | null |
private lup (L : List (List (Option ℕ))) (p : ℕ × Code) (n : ℕ) := do
let l ← L[encode p]?
let o ← l[n]?
o | def | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | lup | null |
private hlup : Primrec fun p : _ × (_ × _) × _ => lup p.1 p.2.1 p.2.2 :=
Primrec.option_bind
(Primrec.list_getElem?.comp Primrec.fst (Primrec.encode.comp <| Primrec.fst.comp Primrec.snd))
(Primrec.option_bind (Primrec.list_getElem?.comp Primrec.snd <| Primrec.snd.comp <|
Primrec.snd.comp Primrec.fst) Primrec.snd) | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | hlup | null |
private 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))) | def | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | G | null |
private 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.primrec_recOn 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)
... | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | hG | null |
private evaln_map (k c n) :
((List.range k)[n]?.bind fun a ↦ evaln k c a) = evaln k c n := by
by_cases kn : n < k
· simp [List.getElem?_range kn]
· rw [List.getElem?_eq_none]
· cases e : evaln k c n
· rfl
exact kn.elim (evaln_bound e)
simpa using kn | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | evaln_map | null |
primrec_evaln : 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_left 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
rcases k with - | k'
· simp [evaln]
let k := k' + 1
simp only
simp only [List.mem_range, Nat.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.getElem?_range hl, evaln_map, Bind.bind, Option.bind_map]
obtain - | - | - | - | ⟨cf, cg⟩ | ⟨cf, cg⟩ | ⟨cf, cg⟩ | cf := c <;>
simp [evaln, nk, Bind.bind, Functor.map, Seq.seq, pure]
· obtain ⟨lf, lg⟩ := encode_lt_pair cf cg
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
· obtain ⟨lf, lg⟩ := encode_lt_comp cf cg
rw [hg (Nat.pair_lt_pair_right _ lg)]
cases evaln k cg n
· rfl
simp [k, hg (Nat.pair_lt_pair_right _ lf)]
· obtain ⟨lf, lg⟩ := encode_lt_prec cf cg
rw [hg (Nat.pair_lt_pair_right _ lf)]
cases n.unpair.2
· rfl
simp only
rw [hg (Nat.pair_lt_pair_left _ k'.lt_succ_self)]
cases evaln k' _ _
· rfl
simp [k, hg (Nat.pair_lt_pair_right _ lg)]
· have lf := encode_lt_rfind' cf
rw [hg (Nat.pair_lt_pair_right _ lf)]
... | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | primrec_evaln | The `Nat.Partrec.Code.evaln` function is primitive recursive. |
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 | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_eq_rfindOpt | null |
eval_part : Partrec₂ eval :=
(Partrec.rfindOpt
(primrec_evaln.to_comp.comp
((Computable.snd.pair (fst.comp fst)).pair (snd.comp fst))).to₂).of_eq
fun a => by simp [eval_eq_rfindOpt] | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | eval_part | null |
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 (primrec₂_curry.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 [F, g, eg', eF', Part.map_id']⟩ | theorem | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | fixed_point | **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. |
fixed_point₂ {f : Code → ℕ →. ℕ} (hf : Partrec₂ f) : ∃ c : Code, eval c = f c :=
let ⟨cf, ef⟩ := exists_code.1 hf
(fixed_point (primrec₂_curry.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 | Computability | [
"Mathlib.Computability.Partrec",
"Mathlib.Data.Option.Basic"
] | Mathlib/Computability/PartrecCode.lean | fixed_point₂ | **Kleene's second recursion theorem** |
eval {σ} (f : σ → Option σ) : σ → Part σ :=
PFun.fix fun s ↦ Part.some <| (f s).elim (Sum.inl s) Sum.inr | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | eval | Run a state transition function `σ → Option σ` "to completion". The return value is the last
state returned before a `none` result. If the state transition function always returns `some`,
then the computation diverges, returning `Part.none`. |
Reaches {σ} (f : σ → Option σ) : σ → σ → Prop :=
ReflTransGen fun a b ↦ b ∈ f a | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches | The reflexive transitive closure of a state transition function. `Reaches f a b` means
there is a finite sequence of steps `f a = some a₁`, `f a₁ = some a₂`, ... such that `aₙ = b`.
This relation permits zero steps of the state transition function. |
Reaches₁ {σ} (f : σ → Option σ) : σ → σ → Prop :=
TransGen fun a b ↦ b ∈ f a | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₁ | The transitive closure of a state transition function. `Reaches₁ f a b` means there is a
nonempty finite sequence of steps `f a = some a₁`, `f a₁ = some a₂`, ... such that `aₙ = b`.
This relation does not permit zero steps of the state transition function. |
reaches₁_eq {σ} {f : σ → Option σ} {a b c} (h : f a = f b) :
Reaches₁ f a c ↔ Reaches₁ f b c :=
TransGen.head'_iff.trans (TransGen.head'_iff.trans <| by rw [h]).symm | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | reaches₁_eq | null |
reaches_total {σ} {f : σ → Option σ} {a b c} (hab : Reaches f a b) (hac : Reaches f a c) :
Reaches f b c ∨ Reaches f c b :=
ReflTransGen.total_of_right_unique (fun _ _ _ ↦ Option.mem_unique) hab hac | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | reaches_total | null |
reaches₁_fwd {σ} {f : σ → Option σ} {a b c} (h₁ : Reaches₁ f a c) (h₂ : b ∈ f a) :
Reaches f b c := by
rcases TransGen.head'_iff.1 h₁ with ⟨b', hab, hbc⟩
cases Option.mem_unique hab h₂; exact hbc | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | reaches₁_fwd | null |
Reaches₀ {σ} (f : σ → Option σ) (a b : σ) : Prop :=
∀ c, Reaches₁ f b c → Reaches₁ f a c | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀ | A variation on `Reaches`. `Reaches₀ f a b` holds if whenever `Reaches₁ f b c` then
`Reaches₁ f a c`. This is a weaker property than `Reaches` and is useful for replacing states with
equivalent states without taking a step. |
Reaches₀.trans {σ} {f : σ → Option σ} {a b c : σ} (h₁ : Reaches₀ f a b)
(h₂ : Reaches₀ f b c) : Reaches₀ f a c
| _, h₃ => h₁ _ (h₂ _ h₃)
@[refl] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀.trans | null |
Reaches₀.refl {σ} {f : σ → Option σ} (a : σ) : Reaches₀ f a a
| _, h => h | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀.refl | null |
Reaches₀.single {σ} {f : σ → Option σ} {a b : σ} (h : b ∈ f a) : Reaches₀ f a b
| _, h₂ => h₂.head h | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀.single | null |
Reaches₀.head {σ} {f : σ → Option σ} {a b c : σ} (h : b ∈ f a) (h₂ : Reaches₀ f b c) :
Reaches₀ f a c :=
(Reaches₀.single h).trans h₂ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀.head | null |
Reaches₀.tail {σ} {f : σ → Option σ} {a b c : σ} (h₁ : Reaches₀ f a b) (h : c ∈ f b) :
Reaches₀ f a c :=
h₁.trans (Reaches₀.single h) | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀.tail | null |
reaches₀_eq {σ} {f : σ → Option σ} {a b} (e : f a = f b) : Reaches₀ f a b
| _, h => (reaches₁_eq e).2 h | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | reaches₀_eq | null |
Reaches₁.to₀ {σ} {f : σ → Option σ} {a b : σ} (h : Reaches₁ f a b) : Reaches₀ f a b
| _, h₂ => h.trans h₂ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₁.to₀ | null |
Reaches.to₀ {σ} {f : σ → Option σ} {a b : σ} (h : Reaches f a b) : Reaches₀ f a b
| _, h₂ => h₂.trans_right h | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches.to₀ | null |
Reaches₀.tail' {σ} {f : σ → Option σ} {a b c : σ} (h : Reaches₀ f a b) (h₂ : c ∈ f b) :
Reaches₁ f a c :=
h _ (TransGen.single h₂) | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches₀.tail' | null |
@[elab_as_elim]
evalInduction {σ} {f : σ → Option σ} {b : σ} {C : σ → Sort*} {a : σ}
(h : b ∈ eval f a) (H : ∀ a, b ∈ eval f a → (∀ a', f a = some a' → C a') → C a) : C a :=
PFun.fixInduction h fun a' ha' h' ↦
H _ ha' fun b' e ↦ h' _ <| Part.mem_some_iff.2 <| by rw [e]; rfl | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | evalInduction | (co-)Induction principle for `eval`. If a property `C` holds of any point `a` evaluating to `b`
which is either terminal (meaning `a = b`) or where the next point also satisfies `C`, then it
holds of any point where `eval f a` evaluates to `b`. This formalizes the notion that if
`eval f a` evaluates to `b` then it reaches terminal state `b` in finitely many steps. |
mem_eval {σ} {f : σ → Option σ} {a b} : b ∈ eval f a ↔ Reaches f a b ∧ f b = none := by
refine ⟨fun h ↦ ?_, fun ⟨h₁, h₂⟩ ↦ ?_⟩
· refine evalInduction h fun a h IH ↦ ?_
rcases e : f a with - | a'
· rw [Part.mem_unique h
(PFun.mem_fix_iff.2 <| Or.inl <| Part.mem_some_iff.2 <| by rw [e]; rfl)]
exact ⟨ReflTransGen.refl, e⟩
· rcases PFun.mem_fix_iff.1 h with (h | ⟨_, h, _⟩) <;> rw [e] at h <;>
cases Part.mem_some_iff.1 h
obtain ⟨h₁, h₂⟩ := IH a' e
exact ⟨ReflTransGen.head e h₁, h₂⟩
· refine ReflTransGen.head_induction_on h₁ ?_ fun h _ IH ↦ ?_
· refine PFun.mem_fix_iff.2 (Or.inl ?_)
rw [h₂]
apply Part.mem_some
· refine PFun.mem_fix_iff.2 (Or.inr ⟨_, ?_, IH⟩)
rw [h]
apply Part.mem_some | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | mem_eval | null |
eval_maximal₁ {σ} {f : σ → Option σ} {a b} (h : b ∈ eval f a) (c) : ¬Reaches₁ f b c
| bc => by
let ⟨_, b0⟩ := mem_eval.1 h
let ⟨b', h', _⟩ := TransGen.head'_iff.1 bc
cases b0.symm.trans h' | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | eval_maximal₁ | null |
eval_maximal {σ} {f : σ → Option σ} {a b} (h : b ∈ eval f a) {c} : Reaches f b c ↔ c = b :=
let ⟨_, b0⟩ := mem_eval.1 h
reflTransGen_iff_eq fun b' h' ↦ by cases b0.symm.trans h' | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | eval_maximal | null |
reaches_eval {σ} {f : σ → Option σ} {a b} (ab : Reaches f a b) : eval f a = eval f b := by
refine Part.ext fun _ ↦ ⟨fun h ↦ ?_, fun h ↦ ?_⟩
· have ⟨ac, c0⟩ := mem_eval.1 h
exact mem_eval.2 ⟨(or_iff_left_of_imp fun cb ↦ (eval_maximal h).1 cb ▸ ReflTransGen.refl).1
(reaches_total ab ac), c0⟩
· have ⟨bc, c0⟩ := mem_eval.1 h
exact mem_eval.2 ⟨ab.trans bc, c0⟩ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | reaches_eval | null |
Respects {σ₁ σ₂} (f₁ : σ₁ → Option σ₁) (f₂ : σ₂ → Option σ₂) (tr : σ₁ → σ₂ → Prop) :=
∀ ⦃a₁ a₂⦄, tr a₁ a₂ → (match f₁ a₁ with
| some b₁ => ∃ b₂, tr b₁ b₂ ∧ Reaches₁ f₂ a₂ b₂
| none => f₂ a₂ = none : Prop) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Respects | Given a relation `tr : σ₁ → σ₂ → Prop` between state spaces, and state transition functions
`f₁ : σ₁ → Option σ₁` and `f₂ : σ₂ → Option σ₂`, `Respects f₁ f₂ tr` means that if `tr a₁ a₂` holds
initially and `f₁` takes a step to `a₂` then `f₂` will take one or more steps before reaching a
state `b₂` satisfying `tr a₂ b₂`, and if `f₁ a₁` terminates then `f₂ a₂` also terminates.
Such a relation `tr` is also known as a refinement. |
tr_reaches₁ {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : Respects f₁ f₂ tr) {a₁ a₂}
(aa : tr a₁ a₂) {b₁} (ab : Reaches₁ f₁ a₁ b₁) : ∃ b₂, tr b₁ b₂ ∧ Reaches₁ f₂ a₂ b₂ := by
induction ab with
| single ac =>
have := H aa
rwa [show f₁ a₁ = _ from ac] at this
| @tail c₁ d₁ _ cd IH =>
rcases IH with ⟨c₂, cc, ac₂⟩
have := H cc
rw [show f₁ c₁ = _ from cd] at this
rcases this with ⟨d₂, dd, cd₂⟩
exact ⟨_, dd, ac₂.trans cd₂⟩ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_reaches₁ | null |
tr_reaches {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : Respects f₁ f₂ tr) {a₁ a₂}
(aa : tr a₁ a₂) {b₁} (ab : Reaches f₁ a₁ b₁) : ∃ b₂, tr b₁ b₂ ∧ Reaches f₂ a₂ b₂ := by
rcases reflTransGen_iff_eq_or_transGen.1 ab with (rfl | ab)
· exact ⟨_, aa, ReflTransGen.refl⟩
· have ⟨b₂, bb, h⟩ := tr_reaches₁ H aa ab
exact ⟨b₂, bb, h.to_reflTransGen⟩ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_reaches | null |
tr_reaches_rev {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : Respects f₁ f₂ tr) {a₁ a₂}
(aa : tr a₁ a₂) {b₂} (ab : Reaches f₂ a₂ b₂) :
∃ c₁ c₂, Reaches f₂ b₂ c₂ ∧ tr c₁ c₂ ∧ Reaches f₁ a₁ c₁ := by
induction ab with
| refl => exact ⟨_, _, ReflTransGen.refl, aa, ReflTransGen.refl⟩
| tail _ cd IH =>
rcases IH with ⟨e₁, e₂, ce, ee, ae⟩
rcases ReflTransGen.cases_head ce with (rfl | ⟨d', cd', de⟩)
· have := H ee
revert this
rcases eg : f₁ e₁ with - | g₁ <;> simp only [and_imp, exists_imp]
· intro c0
cases cd.symm.trans c0
· intro g₂ gg cg
rcases TransGen.head'_iff.1 cg with ⟨d', cd', dg⟩
cases Option.mem_unique cd cd'
exact ⟨_, _, dg, gg, ae.tail eg⟩
· cases Option.mem_unique cd cd'
exact ⟨_, _, de, ee, ae⟩ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_reaches_rev | null |
tr_eval {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : Respects f₁ f₂ tr) {a₁ b₁ a₂}
(aa : tr a₁ a₂) (ab : b₁ ∈ eval f₁ a₁) : ∃ b₂, tr b₁ b₂ ∧ b₂ ∈ eval f₂ a₂ := by
obtain ⟨ab, b0⟩ := mem_eval.1 ab
rcases tr_reaches H aa ab with ⟨b₂, bb, ab⟩
refine ⟨_, bb, mem_eval.2 ⟨ab, ?_⟩⟩
have := H bb; rwa [b0] at this | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_eval | null |
tr_eval_rev {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : Respects f₁ f₂ tr) {a₁ b₂ a₂}
(aa : tr a₁ a₂) (ab : b₂ ∈ eval f₂ a₂) : ∃ b₁, tr b₁ b₂ ∧ b₁ ∈ eval f₁ a₁ := by
obtain ⟨ab, b0⟩ := mem_eval.1 ab
rcases tr_reaches_rev H aa ab with ⟨c₁, c₂, bc, cc, ac⟩
cases (reflTransGen_iff_eq (Option.eq_none_iff_forall_not_mem.1 b0)).1 bc
refine ⟨_, cc, mem_eval.2 ⟨ac, ?_⟩⟩
have := H cc
rcases hfc : f₁ c₁ with - | d₁
· rfl
rw [hfc] at this
rcases this with ⟨d₂, _, bd⟩
rcases TransGen.head'_iff.1 bd with ⟨e, h, _⟩
cases b0.symm.trans h | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_eval_rev | null |
tr_eval_dom {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂ → Prop} (H : Respects f₁ f₂ tr) {a₁ a₂}
(aa : tr a₁ a₂) : (eval f₂ a₂).Dom ↔ (eval f₁ a₁).Dom :=
⟨fun h ↦
let ⟨_, _, h, _⟩ := tr_eval_rev H aa ⟨h, rfl⟩
h,
fun h ↦
let ⟨_, _, h, _⟩ := tr_eval H aa ⟨h, rfl⟩
h⟩ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_eval_dom | null |
FRespects {σ₁ σ₂} (f₂ : σ₂ → Option σ₂) (tr : σ₁ → σ₂) (a₂ : σ₂) : Option σ₁ → Prop
| some b₁ => Reaches₁ f₂ a₂ (tr b₁)
| none => f₂ a₂ = none | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | FRespects | A simpler version of `Respects` when the state transition relation `tr` is a function. |
frespects_eq {σ₁ σ₂} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂} {a₂ b₂} (h : f₂ a₂ = f₂ b₂) :
∀ {b₁}, FRespects f₂ tr a₂ b₁ ↔ FRespects f₂ tr b₂ b₁
| some _ => reaches₁_eq h
| none => by unfold FRespects; rw [h] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | frespects_eq | null |
fun_respects {σ₁ σ₂ f₁ f₂} {tr : σ₁ → σ₂} :
(Respects f₁ f₂ fun a b ↦ tr a = b) ↔ ∀ ⦃a₁⦄, FRespects f₂ tr (tr a₁) (f₁ a₁) :=
forall_congr' fun a₁ ↦ by
cases f₁ a₁ <;> simp only [FRespects, exists_eq_left', forall_eq'] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | fun_respects | null |
tr_eval' {σ₁ σ₂} (f₁ : σ₁ → Option σ₁) (f₂ : σ₂ → Option σ₂) (tr : σ₁ → σ₂)
(H : Respects f₁ f₂ fun a b ↦ tr a = b) (a₁) : eval f₂ (tr a₁) = tr <$> eval f₁ a₁ :=
Part.ext fun b₂ ↦
⟨fun h ↦
let ⟨b₁, bb, hb⟩ := tr_eval_rev H rfl h
(Part.mem_map_iff _).2 ⟨b₁, hb, bb⟩,
fun h ↦ by
rcases (Part.mem_map_iff _).1 h with ⟨b₁, ab, bb⟩
rcases tr_eval H rfl ab with ⟨_, rfl, h⟩
rwa [bb] at h⟩
/-! | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr_eval' | null |
Stmt
| move : Dir → Stmt
| write : Γ → Stmt | inductive | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Stmt | A Turing machine "statement" is just a command to either move
left or right, or write a symbol on the tape. |
Stmt.inhabited [Inhabited Γ] : Inhabited (Stmt Γ) :=
⟨Stmt.write default⟩ | instance | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Stmt.inhabited | null |
Cfg [Inhabited Γ] where
/-- The current machine state. -/
q : Λ
/-- The current state of the tape: current symbol, left and right parts. -/
Tape : Tape Γ
variable {Γ Λ}
variable [Inhabited Λ] | structure | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Cfg | A Post-Turing machine with symbol type `Γ` and label type `Λ`
is a function which, given the current state `q : Λ` and
the tape head `a : Γ`, either halts (returns `none`) or returns
a new state `q' : Λ` and a `Stmt` describing what to do,
either a move left or right, or a write command.
Both `Λ` and `Γ` are required to be inhabited; the default value
for `Γ` is the "blank" tape value, and the default value of `Λ` is
the initial state. -/
@[nolint unusedArguments] -- this is a deliberate addition, see comment
def Machine [Inhabited Λ] :=
Λ → Γ → Option (Λ × (Stmt Γ))
instance Machine.inhabited [Inhabited Λ] : Inhabited (Machine Γ Λ) := by
unfold Machine; infer_instance
/-- The configuration state of a Turing machine during operation
consists of a label (machine state), and a tape.
The tape is represented in the form `(a, L, R)`, meaning the tape looks like `L.rev ++ [a] ++ R`
with the machine currently reading the `a`. The lists are
automatically extended with blanks as the machine moves around. |
Cfg.inhabited : Inhabited (Cfg Γ Λ) := ⟨⟨default, default⟩⟩ | instance | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Cfg.inhabited | null |
step (M : Machine Γ Λ) : Cfg Γ Λ → Option (Cfg Γ Λ) :=
fun ⟨q, T⟩ ↦ (M q T.1).map fun ⟨q', a⟩ ↦ ⟨q', match a with
| Stmt.move d => T.move d
| Stmt.write a => T.write a⟩ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | step | Execution semantics of the Turing machine. |
Reaches (M : Machine Γ Λ) : Cfg Γ Λ → Cfg Γ Λ → Prop := ReflTransGen fun a b ↦ b ∈ step M a | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Reaches | The statement `Reaches M s₁ s₂` means that `s₂` is obtained
starting from `s₁` after a finite number of steps from `s₂`. |
init (l : List Γ) : Cfg Γ Λ := ⟨default, Tape.mk₁ l⟩ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | init | The initial configuration. |
eval (M : Machine Γ Λ) (l : List Γ) : Part (ListBlank Γ) :=
(Turing.eval (step M) (init l)).map fun c ↦ c.Tape.right₀ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | eval | Evaluate a Turing machine on initial input to a final state,
if it terminates. |
Supports (M : Machine Γ Λ) (S : Set Λ) :=
default ∈ S ∧ ∀ {q a q' s}, (q', s) ∈ M q a → q ∈ S → q' ∈ S | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Supports | The raw definition of a Turing machine does not require that
`Γ` and `Λ` are finite, and in practice we will be interested
in the infinite `Λ` case. We recover instead a notion of
"effectively finite" Turing machines, which only make use of a
finite subset of their states. We say that a set `S ⊆ Λ`
supports a Turing machine `M` if `S` is closed under the
transition function and contains the initial state. |
step_supports (M : Machine Γ Λ) {S : Set Λ} (ss : Supports M S) :
∀ {c c' : Cfg Γ Λ}, c' ∈ step M c → c.q ∈ S → c'.q ∈ S := by
intro ⟨q, T⟩ c' h₁ h₂
rcases Option.map_eq_some_iff.1 h₁ with ⟨⟨q', a⟩, h, rfl⟩
exact ss.2 h h₂ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | step_supports | null |
univ_supports (M : Machine Γ Λ) : Supports M Set.univ := by
constructor <;> intros <;> apply Set.mem_univ | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | univ_supports | null |
Stmt.map (f : PointedMap Γ Γ') : Stmt Γ → Stmt Γ'
| Stmt.move d => Stmt.move d
| Stmt.write a => Stmt.write (f a) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Stmt.map | Map a TM statement across a function. This does nothing to move statements and maps the write
values. |
Cfg.map (f : PointedMap Γ Γ') (g : Λ → Λ') : Cfg Γ Λ → Cfg Γ' Λ'
| ⟨q, T⟩ => ⟨g q, T.map f⟩
variable (M : Machine Γ Λ) (f₁ : PointedMap Γ Γ') (f₂ : PointedMap Γ' Γ) (g₁ : Λ → Λ') (g₂ : Λ' → Λ) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Cfg.map | Map a configuration across a function, given `f : Γ → Γ'` a map of the alphabets and
`g : Λ → Λ'` a map of the machine states. |
Machine.map : Machine Γ' Λ'
| q, l => (M (g₂ q) (f₂ l)).map (Prod.map g₁ (Stmt.map f₁)) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Machine.map | Because the state transition function uses the alphabet and machine states in both the input
and output, to map a machine from one alphabet and machine state space to another we need functions
in both directions, essentially an `Equiv` without the laws. |
Machine.map_step {S : Set Λ} (f₂₁ : Function.RightInverse f₁ f₂)
(g₂₁ : ∀ q ∈ S, g₂ (g₁ q) = q) :
∀ c : Cfg Γ Λ,
c.q ∈ S → (step M c).map (Cfg.map f₁ g₁) = step (M.map f₁ f₂ g₁ g₂) (Cfg.map f₁ g₁ c)
| ⟨q, T⟩, h => by
unfold step Machine.map Cfg.map
simp only [Turing.Tape.map_fst, g₂₁ q h, f₂₁ _]
rcases M q T.1 with (_ | ⟨q', d | a⟩); · rfl
· simp only [Option.map_some, Tape.map_move f₁]
rfl
· simp only [Option.map_some, Tape.map_write]
rfl | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Machine.map_step | null |
map_init (g₁ : PointedMap Λ Λ') (l : List Γ) : (init l).map f₁ g₁ = init (l.map f₁) :=
congr (congr_arg Cfg.mk g₁.map_pt) (Tape.map_mk₁ _ _) | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | map_init | null |
Machine.map_respects (g₁ : PointedMap Λ Λ') (g₂ : Λ' → Λ) {S} (ss : Supports M S)
(f₂₁ : Function.RightInverse f₁ f₂) (g₂₁ : ∀ q ∈ S, g₂ (g₁ q) = q) :
Respects (step M) (step (M.map f₁ f₂ g₁ g₂)) fun a b ↦ a.q ∈ S ∧ Cfg.map f₁ g₁ a = b := by
intro c _ ⟨cs, rfl⟩
cases e : step M c
· rw [← M.map_step f₁ f₂ g₁ g₂ f₂₁ g₂₁ _ cs, e]
rfl
· refine ⟨_, ⟨step_supports M ss e cs, rfl⟩, TransGen.single ?_⟩
rw [← M.map_step f₁ f₂ g₁ g₂ f₂₁ g₂₁ _ cs, e]
rfl | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Machine.map_respects | null |
Stmt
| move : Dir → Stmt → Stmt
| write : (Γ → σ → Γ) → Stmt → Stmt
| load : (Γ → σ → σ) → Stmt → Stmt
| branch : (Γ → σ → Bool) → Stmt → Stmt → Stmt
| goto : (Γ → σ → Λ) → Stmt
| halt : Stmt
open Stmt | inductive | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Stmt | The TM1 model is a simplification and extension of TM0
(Post-Turing model) in the direction of Wang B-machines. The machine's
internal state is extended with a (finite) store `σ` of variables
that may be accessed and updated at any time.
A machine is given by a `Λ` indexed set of procedures or functions.
Each function has a body which is a `Stmt`, which can either be a
`move` or `write` command, a `branch` (if statement based on the
current tape value), a `load` (set the variable value),
a `goto` (call another function), or `halt`. Note that here
most statements do not have labels; `goto` commands can only
go to a new function. All commands have access to the variable value
and current tape value. |
Stmt.inhabited : Inhabited (Stmt Γ Λ σ) := ⟨halt⟩ | instance | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Stmt.inhabited | null |
Cfg [Inhabited Γ] where
/-- The statement (if any) which is currently evaluated -/
l : Option Λ
/-- The current value of the variable store -/
var : σ
/-- The current state of the tape -/
Tape : Tape Γ | structure | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Cfg | The configuration of a TM1 machine is given by the currently
evaluating statement, the variable store value, and the tape. |
Cfg.inhabited [Inhabited Γ] [Inhabited σ] : Inhabited (Cfg Γ Λ σ) :=
⟨⟨default, default, default⟩⟩
variable {Γ Λ σ} | instance | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Cfg.inhabited | null |
stepAux [Inhabited Γ] : Stmt Γ Λ σ → σ → Tape Γ → Cfg Γ Λ σ
| move d q, v, T => stepAux q v (T.move d)
| write a q, v, T => stepAux q v (T.write (a T.1 v))
| load s q, v, T => stepAux q (s T.1 v) T
| branch p q₁ q₂, v, T => cond (p T.1 v) (stepAux q₁ v T) (stepAux q₂ v T)
| goto l, v, T => ⟨some (l T.1 v), v, T⟩
| halt, v, T => ⟨none, v, T⟩ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stepAux | The semantics of TM1 evaluation. |
step [Inhabited Γ] (M : Λ → Stmt Γ Λ σ) : Cfg Γ Λ σ → Option (Cfg Γ Λ σ)
| ⟨none, _, _⟩ => none
| ⟨some l, v, T⟩ => some (stepAux (M l) v T) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | step | The state transition function. |
SupportsStmt (S : Finset Λ) : Stmt Γ Λ σ → Prop
| move _ q => SupportsStmt S q
| write _ q => SupportsStmt S q
| load _ q => SupportsStmt S q
| branch _ q₁ q₂ => SupportsStmt S q₁ ∧ SupportsStmt S q₂
| goto l => ∀ a v, l a v ∈ S
| halt => True
open scoped Classical in | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | SupportsStmt | A set `S` of labels supports the statement `q` if all the `goto`
statements in `q` refer only to other functions in `S`. |
noncomputable stmts₁ : Stmt Γ Λ σ → Finset (Stmt Γ Λ σ)
| Q@(move _ q) => insert Q (stmts₁ q)
| Q@(write _ q) => insert Q (stmts₁ q)
| Q@(load _ q) => insert Q (stmts₁ q)
| Q@(branch _ q₁ q₂) => insert Q (stmts₁ q₁ ∪ stmts₁ q₂)
| Q => {Q} | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts₁ | The subterm closure of a statement. |
stmts₁_self {q : Stmt Γ Λ σ} : q ∈ stmts₁ q := by
cases q <;> simp only [stmts₁, Finset.mem_insert_self, Finset.mem_singleton_self] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts₁_self | null |
stmts₁_trans {q₁ q₂ : Stmt Γ Λ σ} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := by
classical
intro h₁₂ q₀ h₀₁
induction q₂ with (
simp only [stmts₁] at h₁₂ ⊢
simp only [Finset.mem_insert, Finset.mem_union, Finset.mem_singleton] at h₁₂)
| branch p q₁ q₂ IH₁ IH₂ =>
rcases h₁₂ with (rfl | h₁₂ | h₁₂)
· unfold stmts₁ at h₀₁
exact h₀₁
· grind
· grind
| goto l => subst h₁₂; exact h₀₁
| halt => subst h₁₂; exact h₀₁
| _ _ q IH =>
rcases h₁₂ with rfl | h₁₂
· exact h₀₁
· grind | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts₁_trans | null |
stmts₁_supportsStmt_mono {S : Finset Λ} {q₁ q₂ : Stmt Γ Λ σ} (h : q₁ ∈ stmts₁ q₂)
(hs : SupportsStmt S q₂) : SupportsStmt S q₁ := by
induction q₂ with
simp only [stmts₁, SupportsStmt, Finset.mem_insert, Finset.mem_union, Finset.mem_singleton]
at h hs
| branch p q₁ q₂ IH₁ IH₂ => rcases h with (rfl | h | h); exacts [hs, IH₁ h hs.1, IH₂ h hs.2]
| goto l => subst h; exact hs
| halt => subst h; trivial
| _ _ q IH => rcases h with (rfl | h) <;> [exact hs; exact IH h hs]
open scoped Classical in | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts₁_supportsStmt_mono | null |
noncomputable stmts (M : Λ → Stmt Γ Λ σ) (S : Finset Λ) : Finset (Option (Stmt Γ Λ σ)) :=
Finset.insertNone (S.biUnion fun q ↦ stmts₁ (M q)) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts | The set of all statements in a Turing machine, plus one extra value `none` representing the
halt state. This is used in the TM1 to TM0 reduction. |
stmts_trans {M : Λ → Stmt Γ Λ σ} {S : Finset Λ} {q₁ q₂ : Stmt Γ Λ σ} (h₁ : q₁ ∈ stmts₁ q₂) :
some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := by
simp only [stmts, Finset.mem_insertNone, Finset.mem_biUnion, Option.mem_def, Option.some.injEq,
forall_eq', exists_imp, and_imp]
exact fun l ls h₂ ↦ ⟨_, ls, stmts₁_trans h₂ h₁⟩
variable [Inhabited Λ] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts_trans | null |
Supports (M : Λ → Stmt Γ Λ σ) (S : Finset Λ) :=
default ∈ S ∧ ∀ q ∈ S, SupportsStmt S (M q) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | Supports | A set `S` of labels supports machine `M` if all the `goto`
statements in the functions in `S` refer only to other functions
in `S`. |
stmts_supportsStmt {M : Λ → Stmt Γ Λ σ} {S : Finset Λ} {q : Stmt Γ Λ σ}
(ss : Supports M S) : some q ∈ stmts M S → SupportsStmt S q := by
simp only [stmts, Finset.mem_insertNone, Finset.mem_biUnion, Option.mem_def, Option.some.injEq,
forall_eq', exists_imp, and_imp]
exact fun l ls h ↦ stmts₁_supportsStmt_mono h (ss.2 _ ls)
variable [Inhabited Γ] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | stmts_supportsStmt | null |
step_supports (M : Λ → Stmt Γ Λ σ) {S : Finset Λ} (ss : Supports M S) :
∀ {c c' : Cfg Γ Λ σ}, c' ∈ step M c → c.l ∈ Finset.insertNone S → c'.l ∈ Finset.insertNone S
| ⟨some l₁, v, T⟩, c', h₁, h₂ => by
replace h₂ := ss.2 _ (Finset.some_mem_insertNone.1 h₂)
simp only [step, Option.mem_def, Option.some.injEq] at h₁; subst c'
revert h₂; induction M l₁ generalizing v T with intro hs
| branch p q₁' q₂' IH₁ IH₂ =>
unfold stepAux; cases p T.1 v
· exact IH₂ _ _ hs.2
· exact IH₁ _ _ hs.1
| goto => exact Finset.some_mem_insertNone.2 (hs _ _)
| halt => apply Multiset.mem_cons_self
| _ _ q IH => exact IH _ _ hs
variable [Inhabited σ] | theorem | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | step_supports | null |
init (l : List Γ) : Cfg Γ Λ σ :=
⟨some default, default, Tape.mk₁ l⟩ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | init | The initial state, given a finite input that is placed on the tape starting at the TM head and
going to the right. |
eval (M : Λ → Stmt Γ Λ σ) (l : List Γ) : Part (ListBlank Γ) :=
(Turing.eval (step M) (init l)).map fun c ↦ c.Tape.right₀ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | eval | Evaluate a TM to completion, resulting in an output list on the tape (with an indeterminate
number of blanks on the end). |
trAux (s : Γ) : TM1.Stmt Γ Λ σ → σ → Λ' M × TM0.Stmt Γ
| TM1.Stmt.move d q, v => ((some q, v), move d)
| TM1.Stmt.write a q, v => ((some q, v), write (a s v))
| TM1.Stmt.load a q, v => trAux s q (a s v)
| TM1.Stmt.branch p q₁ q₂, v => cond (p s v) (trAux s q₁ v) (trAux s q₂ v)
| TM1.Stmt.goto l, v => ((some (M (l s v)), v), write s)
| TM1.Stmt.halt, v => ((none, v), write s) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | trAux | The base machine state space is a pair of an `Option Stmt₁` representing the current program
to be executed, or `none` for the halt state, and a `σ` which is the local state (stored in the TM,
not the tape). Because there are an infinite number of programs, this state space is infinite, but
for a finitely supported TM1 machine and a finite type `σ`, only finitely many of these states are
reachable. -/
@[nolint unusedArguments] -- We need the M assumption
def Λ' (M : Λ → TM1.Stmt Γ Λ σ) :=
Option (TM1.Stmt Γ Λ σ) × σ
instance : Inhabited (Λ' M) :=
⟨(some (M default), default)⟩
open TM0.Stmt
/-- The core TM1 → TM0 translation function. Here `s` is the current value on the tape, and the
`Stmt₁` is the TM1 statement to translate, with local state `v : σ`. We evaluate all regular
instructions recursively until we reach either a `move` or `write` command, or a `goto`; in the
latter case we emit a dummy `write s` step and transition to the new target location. |
tr : TM0.Machine Γ (Λ' M)
| (none, _), _ => none
| (some q, v), s => some (trAux M s q v) | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | tr | The translated TM0 machine (given the TM1 machine input). |
trCfg [Inhabited Γ] : TM1.Cfg Γ Λ σ → TM0.Cfg Γ (Λ' M)
| ⟨l, v, T⟩ => ⟨(l.map M, v), T⟩ | def | Computability | [
"Mathlib.Computability.Tape",
"Mathlib.Data.Finset.Prod",
"Mathlib.Data.Finset.Option",
"Mathlib.Data.Fintype.Defs",
"Mathlib.Data.PFun"
] | Mathlib/Computability/PostTuringMachine.lean | trCfg | Translate configurations from TM1 to TM0. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.