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 ⌀ |
|---|---|---|---|---|---|---|
@[simp]
zero'_eval : zero'.eval = fun v => pure (0 :: v) := by simp [eval]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | zero'_eval | null |
succ_eval : succ.eval = fun v => pure [v.headI.succ] := by simp [eval]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | succ_eval | null |
tail_eval : tail.eval = fun v => pure v.tail := by simp [eval]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | tail_eval | null |
cons_eval (f fs) : (cons f fs).eval = fun v => do {
let n ← Code.eval f v
let ns ← Code.eval fs v
pure (n.headI :: ns) } := by simp [eval]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | cons_eval | null |
comp_eval (f g) : (comp f g).eval = fun v => g.eval v >>= f.eval := by simp [eval]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | comp_eval | null |
case_eval (f g) :
(case f g).eval = fun v => v.headI.rec (f.eval v.tail) fun y _ => g.eval (y::v.tail) := by
simp [eval]
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | case_eval | null |
fix_eval (f) : (fix f).eval =
PFun.fix fun v => (f.eval v).map fun v =>
if v.headI = 0 then Sum.inl v.tail else Sum.inr v.tail := by
simp [eval] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | fix_eval | null |
nil : Code :=
tail.comp succ
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | nil | `nil` is the constant nil function: `nil v = []`. |
nil_eval (v) : nil.eval v = pure [] := by simp [nil] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | nil_eval | null |
id : Code :=
tail.comp zero'
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | id | `id` is the identity function: `id v = v`. |
id_eval (v) : id.eval v = pure v := by simp [id] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | id_eval | null |
head : Code :=
cons id nil
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | head | `head` gets the head of the input list: `head [] = [0]`, `head (n :: v) = [n]`. |
head_eval (v) : head.eval v = pure [v.headI] := by simp [head] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | head_eval | null |
zero : Code :=
cons zero' nil
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | zero | `zero` is the constant zero function: `zero v = [0]`. |
zero_eval (v) : zero.eval v = pure [0] := by simp [zero] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | zero_eval | null |
pred : Code :=
case zero head
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | pred | `pred` returns the predecessor of the head of the input:
`pred [] = [0]`, `pred (0 :: v) = [0]`, `pred (n+1 :: v) = [n]`. |
pred_eval (v) : pred.eval v = pure [v.headI.pred] := by
simp [pred]; cases v.headI <;> simp | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | pred_eval | null |
rfind (f : Code) : Code :=
comp pred <| comp (fix <| cons f <| cons succ tail) zero' | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | rfind | `rfind f` performs the function of the `rfind` primitive of partial recursive functions.
`rfind f v` returns the smallest `n` such that `(f (n :: v)).head = 0`.
It is implemented as:
rfind f v = pred (fix (fun (n::v) => f (n::v) :: n+1 :: v) (0 :: v))
The idea is that the initial state is `0 :: v`, and the `fix` keeps `n :: v` as its internal state;
it calls `f (n :: v)` as the exit test and `n+1 :: v` as the next state. At the end we get
`n+1 :: v` where `n` is the desired output, and `pred (n+1 :: v) = [n]` returns the result. |
prec (f g : Code) : Code :=
let G :=
cons tail <|
cons succ <|
cons (comp pred tail) <|
cons (comp g <| cons id <| comp tail tail) <| comp tail <| comp tail tail
let F := case id <| comp (comp (comp tail tail) (fix G)) zero'
cons (comp F (cons head <| cons (comp f tail) tail)) nil
attribute [-simp] Part.bind_eq_bind Part.map_eq_map Part.pure_eq_some | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | prec | `prec f g` implements the `prec` (primitive recursion) operation of partial recursive
functions. `prec f g` evaluates as:
* `prec f g [] = [f []]`
* `prec f g (0 :: v) = [f v]`
* `prec f g (n+1 :: v) = [g (n :: prec f g (n :: v) :: v)]`
It is implemented as:
G (a :: b :: IH :: v) = (b :: a+1 :: b-1 :: g (a :: IH :: v) :: v)
F (0 :: f_v :: v) = (f_v :: v)
F (n+1 :: f_v :: v) = (fix G (0 :: n :: f_v :: v)).tail.tail
prec f g (a :: v) = [(F (a :: f v :: v)).head]
Because `fix` always evaluates its body at least once, we must special case the `0` case to avoid
calling `g` more times than necessary (which could be bad if `g` diverges). If the input is
`0 :: v`, then `F (0 :: f v :: v) = (f v :: v)` so we return `[f v]`. If the input is `n+1 :: v`,
we evaluate the function from the bottom up, with initial state `0 :: n :: f v :: v`. The first
number counts up, providing arguments for the applications to `g`, while the second number counts
down, providing the exit condition (this is the initial `b` in the return value of `G`, which is
stripped by `fix`). After the `fix` is complete, the final state is `n :: 0 :: res :: v` where
`res` is the desired result, and the rest reduces this to `[res]`. |
exists_code.comp {m n} {f : List.Vector ℕ n →. ℕ} {g : Fin n → List.Vector ℕ m →. ℕ}
(hf : ∃ c : Code, ∀ v : List.Vector ℕ n, c.eval v.1 = pure <$> f v)
(hg : ∀ i, ∃ c : Code, ∀ v : List.Vector ℕ m, c.eval v.1 = pure <$> g i v) :
∃ c : Code, ∀ v : List.Vector ℕ m,
c.eval v.1 = pure <$> ((List.Vector.mOfFn fun i => g i v) >>= f) := by
rsuffices ⟨cg, hg⟩ :
∃ c : Code, ∀ v : List.Vector ℕ m,
c.eval v.1 = Subtype.val <$> List.Vector.mOfFn fun i => g i v
· obtain ⟨cf, hf⟩ := hf
exact
⟨cf.comp cg, fun v => by
simp [hg, hf, map_bind]
rfl⟩
clear hf f
induction n with
| zero => exact ⟨nil, fun v => by simp [Vector.mOfFn]; rfl⟩
| succ n IH =>
obtain ⟨cg, hg₁⟩ := hg 0
obtain ⟨cl, hl⟩ := IH fun i => hg i.succ
exact
⟨cons cg cl, fun v => by
simp [Vector.mOfFn, hg₁, map_bind, hl]
rfl⟩ | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | exists_code.comp | null |
exists_code {n} {f : List.Vector ℕ n →. ℕ} (hf : Nat.Partrec' f) :
∃ c : Code, ∀ v : List.Vector ℕ n, c.eval v.1 = pure <$> f v := by
induction hf with
| prim hf =>
induction hf with
| zero => exact ⟨zero', fun ⟨[], _⟩ => rfl⟩
| succ => exact ⟨succ, fun ⟨[v], _⟩ => rfl⟩
| get i =>
refine Fin.succRec (fun n => ?_) (fun n i IH => ?_) i
· exact ⟨head, fun ⟨List.cons a as, _⟩ => by simp; rfl⟩
· obtain ⟨c, h⟩ := IH
exact ⟨c.comp tail, fun v => by simpa [← Vector.get_tail, Bind.bind] using h v.tail⟩
| comp g hf hg IHf IHg =>
simpa [Part.bind_eq_bind] using exists_code.comp IHf IHg
| @prec n f g _ _ IHf IHg =>
obtain ⟨cf, hf⟩ := IHf
obtain ⟨cg, hg⟩ := IHg
simp only [Part.map_eq_map, Part.map_some, PFun.coe_val] at hf hg
refine ⟨prec cf cg, fun v => ?_⟩
rw [← v.cons_head_tail]
specialize hf v.tail
replace hg := fun a b => hg (a ::ᵥ b ::ᵥ v.tail)
simp only [Vector.cons_val, Vector.tail_val] at hf hg
simp only [Part.map_eq_map, Part.map_some, Vector.cons_val, Vector.tail_cons,
Vector.head_cons, PFun.coe_val, Vector.tail_val]
simp only [← Part.pure_eq_some] at hf hg ⊢
induction v.head with
simp [prec, hf, Part.bind_assoc, ← Part.bind_some_eq_map, Part.bind_some, Bind.bind]
| succ n _ =>
suffices ∀ a b, a + b = n →
(n.succ :: 0 ::
g (n ::ᵥ Nat.rec (f v.tail) (fun y IH => g (y ::ᵥ IH ::ᵥ v.tail)) n ::ᵥ v.tail) ::
v.val.tail : List ℕ) ∈
PFun.fix
(fun v : List ℕ => Part.bind (cg.eval (v.headI :: v.tail.tail))
(fun x => Part.some (if v.tail.headI = 0
then Sum.inl
(v.headI.succ :: v.tail.headI.pred :: x.headI :: v.tail.tail.tail : List ℕ)
else Sum.inr
(v.headI.succ :: v.tail.headI.pred :: x.headI :: v.tail.tail.tail))))
(a :: b :: Nat.rec (f v.tail) (fun y IH => g (y ::ᵥ IH ::ᵥ v.tail)) a :: v.val.tail) by
erw [Part.eq_some_iff.2 (this 0 n (zero_add n))]
simp only [List.headI, Part.bind_some, List.tail_cons]
intro a b e
induction b generalizing a with
| zero =>
refine PFun.mem_fix_iff.2 (Or.inl <| Part.eq_some_iff.1 ?_)
simp only [hg, ← e, Part.bind_some, List.tail_cons, pure]
rfl
| succ b IH =>
refine PFun.mem_fix_iff.2 (Or.inr ⟨_, ?_, IH (a + 1) (by rwa [add_right_comm])⟩)
... | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | exists_code | null |
Cont
| halt
| cons₁ : Code → List ℕ → Cont → Cont
| cons₂ : List ℕ → Cont → Cont
| comp : Code → Cont → Cont
| fix : Code → Cont → Cont
deriving Inhabited | inductive | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Cont | The type of continuations, built up during evaluation of a `Code` expression. |
Cont.eval : Cont → List ℕ →. List ℕ
| Cont.halt => pure
| Cont.cons₁ fs as k => fun v => do
let ns ← Code.eval fs as
Cont.eval k (v.headI :: ns)
| Cont.cons₂ ns k => fun v => Cont.eval k (ns.headI :: v)
| Cont.comp f k => fun v => Code.eval f v >>= Cont.eval k
| Cont.fix f k => fun v => if v.headI = 0 then k.eval v.tail else f.fix.eval v.tail >>= k.eval | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Cont.eval | The semantics of a continuation. |
Cfg
| halt : List ℕ → Cfg
| ret : Cont → List ℕ → Cfg
deriving Inhabited | inductive | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Cfg | The set of configurations of the machine:
* `halt v`: The machine is about to stop and `v : List ℕ` is the result.
* `ret k v`: The machine is about to pass `v : List ℕ` to continuation `k : Cont`.
We don't have a state corresponding to normal evaluation because these are evaluated immediately
to a `ret` "in zero steps" using the `stepNormal` function. |
stepNormal : Code → Cont → List ℕ → Cfg
| Code.zero' => fun k v => Cfg.ret k (0::v)
| Code.succ => fun k v => Cfg.ret k [v.headI.succ]
| Code.tail => fun k v => Cfg.ret k v.tail
| Code.cons f fs => fun k v => stepNormal f (Cont.cons₁ fs v k) v
| Code.comp f g => fun k v => stepNormal g (Cont.comp f k) v
| Code.case f g => fun k v =>
v.headI.rec (stepNormal f k v.tail) fun y _ => stepNormal g k (y::v.tail)
| Code.fix f => fun k v => stepNormal f (Cont.fix f k) v | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepNormal | Evaluating `c : Code` in a continuation `k : Cont` and input `v : List ℕ`. This goes by
recursion on `c`, building an augmented continuation and a value to pass to it.
* `zero' v = 0 :: v` evaluates immediately, so we return it to the parent continuation
* `succ v = [v.headI.succ]` evaluates immediately, so we return it to the parent continuation
* `tail v = v.tail` evaluates immediately, so we return it to the parent continuation
* `cons f fs v = (f v).headI :: fs v` requires two sub-evaluations, so we evaluate
`f v` in the continuation `k (_.headI :: fs v)` (called `Cont.cons₁ fs v k`)
* `comp f g v = f (g v)` requires two sub-evaluations, so we evaluate
`g v` in the continuation `k (f _)` (called `Cont.comp f k`)
* `case f g v = v.head.casesOn (f v.tail) (fun n => g (n :: v.tail))` has the information needed
to evaluate the case statement, so we do that and transition to either
`f v` or `g (n :: v.tail)`.
* `fix f v = let v' := f v; if v'.headI = 0 then k v'.tail else fix f v'.tail`
needs to first evaluate `f v`, so we do that and leave the rest for the continuation (called
`Cont.fix f k`) |
stepRet : Cont → List ℕ → Cfg
| Cont.halt, v => Cfg.halt v
| Cont.cons₁ fs as k, v => stepNormal fs (Cont.cons₂ v k) as
| Cont.cons₂ ns k, v => stepRet k (ns.headI :: v)
| Cont.comp f k, v => stepNormal f k v
| Cont.fix f k, v => if v.headI = 0 then stepRet k v.tail else stepNormal f (Cont.fix f k) v.tail | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepRet | Evaluating a continuation `k : Cont` on input `v : List ℕ`. This is the second part of
evaluation, when we receive results from continuations built by `stepNormal`.
* `Cont.halt v = v`, so we are done and transition to the `Cfg.halt v` state
* `Cont.cons₁ fs as k v = k (v.headI :: fs as)`, so we evaluate `fs as` now with the continuation
`k (v.headI :: _)` (called `cons₂ v k`).
* `Cont.cons₂ ns k v = k (ns.headI :: v)`, where we now have everything we need to evaluate
`ns.headI :: v`, so we return it to `k`.
* `Cont.comp f k v = k (f v)`, so we call `f v` with `k` as the continuation.
* `Cont.fix f k v = k (if v.headI = 0 then k v.tail else fix f v.tail)`, where `v` is a value,
so we evaluate the if statement and either call `k` with `v.tail`, or call `fix f v` with `k` as
the continuation (which immediately calls `f` with `Cont.fix f k` as the continuation). |
step : Cfg → Option Cfg
| Cfg.halt _ => none
| Cfg.ret k v => some (stepRet k v) | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | step | If we are not done (in `Cfg.halt` state), then we must be still stuck on a continuation, so
this main loop calls `stepRet` with the new continuation. The overall `step` function transitions
from one `Cfg` to another, only halting at the `Cfg.halt` state. |
Cont.then : Cont → Cont → Cont
| Cont.halt => fun k' => k'
| Cont.cons₁ fs as k => fun k' => Cont.cons₁ fs as (k.then k')
| Cont.cons₂ ns k => fun k' => Cont.cons₂ ns (k.then k')
| Cont.comp f k => fun k' => Cont.comp f (k.then k')
| Cont.fix f k => fun k' => Cont.fix f (k.then k') | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Cont.then | In order to extract a compositional semantics from the sequential execution behavior of
configurations, we observe that continuations have a monoid structure, with `Cont.halt` as the unit
and `Cont.then` as the multiplication. `Cont.then k₁ k₂` runs `k₁` until it halts, and then takes
the result of `k₁` and passes it to `k₂`.
We will not prove it is associative (although it is), but we are instead interested in the
associativity law `k₂ (eval c k₁) = eval c (k₁.then k₂)`. This holds at both the sequential and
compositional levels, and allows us to express running a machine without the ambient continuation
and relate it to the original machine's evaluation steps. In the literature this is usually
where one uses Turing machines embedded inside other Turing machines, but this approach allows us
to avoid changing the ambient type `Cfg` in the middle of the recursion. |
Cont.then_eval {k k' : Cont} {v} : (k.then k').eval v = k.eval v >>= k'.eval := by
induction k generalizing v with
| halt => simp only [Cont.eval, Cont.then, pure_bind]
| cons₁ => simp only [Cont.eval, Cont.then, bind_assoc, *]
| cons₂ => simp only [Cont.eval, Cont.then, *]
| comp _ _ k_ih => simp only [Cont.eval, Cont.then, bind_assoc, ← k_ih]
| fix _ _ k_ih =>
simp only [Cont.eval, Cont.then, *]
split_ifs <;> [rfl; simp only [← k_ih, bind_assoc]] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Cont.then_eval | null |
Cfg.then : Cfg → Cont → Cfg
| Cfg.halt v => fun k' => stepRet k' v
| Cfg.ret k v => fun k' => Cfg.ret (k.then k') v | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Cfg.then | The `then k` function is a "configuration homomorphism". Its operation on states is to append
`k` to the continuation of a `Cfg.ret` state, and to run `k` on `v` if we are in the `Cfg.halt v`
state. |
stepNormal_then (c) (k k' : Cont) (v) :
stepNormal c (k.then k') v = (stepNormal c k v).then k' := by
induction c generalizing k v with simp only [stepNormal, *]
| cons c c' ih _ => rw [← ih, Cont.then]
| comp c c' _ ih' => rw [← ih', Cont.then]
| case => cases v.headI <;> simp only [Nat.rec_zero]
| fix c ih => rw [← ih, Cont.then]
| _ => simp only [Cfg.then] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepNormal_then | The `stepNormal` function respects the `then k'` homomorphism. Note that this is an exact
equality, not a simulation; the original and embedded machines move in lock-step until the
embedded machine reaches the halt state. |
stepRet_then {k k' : Cont} {v} : stepRet (k.then k') v = (stepRet k v).then k' := by
induction k generalizing v with simp only [Cont.then, stepRet, *]
| cons₁ =>
rw [← stepNormal_then]
rfl
| comp =>
rw [← stepNormal_then]
| fix _ _ k_ih =>
split_ifs
· rw [← k_ih]
· rw [← stepNormal_then]
rfl
| _ => simp only [Cfg.then] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepRet_then | The `stepRet` function respects the `then k'` homomorphism. Note that this is an exact
equality, not a simulation; the original and embedded machines move in lock-step until the
embedded machine reaches the halt state. |
Code.Ok (c : Code) :=
∀ k v, Turing.eval step (stepNormal c k v) =
Code.eval c v >>= fun v => Turing.eval step (Cfg.ret k v) | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Code.Ok | This is a temporary definition, because we will prove in `code_is_ok` that it always holds.
It asserts that `c` is semantically correct; that is, for any `k` and `v`,
`eval (stepNormal c k v) = eval (Cfg.ret k (Code.eval c v))`, as an equality of partial values
(so one diverges iff the other does).
In particular, we can let `k = Cont.halt`, and then this asserts that `stepNormal c Cont.halt v`
evaluates to `Cfg.halt (Code.eval c v)`. |
Code.Ok.zero {c} (h : Code.Ok c) {v} :
Turing.eval step (stepNormal c Cont.halt v) = Cfg.halt <$> Code.eval c v := by
rw [h, ← bind_pure_comp]; congr; funext v
exact Part.eq_some_iff.2 (mem_eval.2 ⟨ReflTransGen.single rfl, rfl⟩) | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | Code.Ok.zero | null |
stepNormal.is_ret (c k v) : ∃ k' v', stepNormal c k v = Cfg.ret k' v' := by
induction c generalizing k v with
| cons _f fs IHf _IHfs => apply IHf
| comp f _g _IHf IHg => apply IHg
| case f g IHf IHg =>
rw [stepNormal]
simp only []
cases v.headI <;> [apply IHf; apply IHg]
| fix f IHf => apply IHf
| _ => exact ⟨_, _, rfl⟩ | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepNormal.is_ret | null |
cont_eval_fix {f k v} (fok : Code.Ok f) :
Turing.eval step (stepNormal f (Cont.fix f k) v) =
f.fix.eval v >>= fun v => Turing.eval step (Cfg.ret k v) := by
refine Part.ext fun x => ?_
simp only [Part.bind_eq_bind, Part.mem_bind_iff]
constructor
· suffices ∀ c, x ∈ eval step c → ∀ v c', c = Cfg.then c' (Cont.fix f k) →
Reaches step (stepNormal f Cont.halt v) c' →
∃ v₁ ∈ f.eval v, ∃ v₂ ∈ if List.headI v₁ = 0 then pure v₁.tail else f.fix.eval v₁.tail,
x ∈ eval step (Cfg.ret k v₂) by
intro h
obtain ⟨v₁, hv₁, v₂, hv₂, h₃⟩ :=
this _ h _ _ (stepNormal_then _ Cont.halt _ _) ReflTransGen.refl
refine ⟨v₂, PFun.mem_fix_iff.2 ?_, h₃⟩
simp only [Part.eq_some_iff.2 hv₁, Part.map_some]
split_ifs at hv₂ ⊢
· rw [Part.mem_some_iff.1 hv₂]
exact Or.inl (Part.mem_some _)
· exact Or.inr ⟨_, Part.mem_some _, hv₂⟩
refine fun c he => evalInduction he fun y h IH => ?_
rintro v (⟨v'⟩ | ⟨k', v'⟩) rfl hr <;> rw [Cfg.then] at h IH <;> simp only [] at h IH
· have := mem_eval.2 ⟨hr, rfl⟩
rw [fok, Part.bind_eq_bind, Part.mem_bind_iff] at this
obtain ⟨v'', h₁, h₂⟩ := this
rw [reaches_eval] at h₂
swap
· exact ReflTransGen.single rfl
cases Part.mem_unique h₂ (mem_eval.2 ⟨ReflTransGen.refl, rfl⟩)
refine ⟨v', h₁, ?_⟩
rw [stepRet] at h
revert h
by_cases he : v'.headI = 0 <;> simp only [if_pos, if_false, he] <;> intro h
· refine ⟨_, Part.mem_some _, ?_⟩
rw [reaches_eval]
· exact h
exact ReflTransGen.single rfl
· obtain ⟨k₀, v₀, e₀⟩ := stepNormal.is_ret f Cont.halt v'.tail
have e₁ := stepNormal_then f Cont.halt (Cont.fix f k) v'.tail
rw [e₀, Cont.then, Cfg.then] at e₁
simp only [] at e₁
obtain ⟨v₁, hv₁, v₂, hv₂, h₃⟩ :=
IH (stepRet (k₀.then (Cont.fix f k)) v₀) (by rw [stepRet, if_neg he, e₁]; rfl)
v'.tail _ stepRet_then (by apply ReflTransGen.single; rw [e₀]; rfl)
refine ⟨_, PFun.mem_fix_iff.2 ?_, h₃⟩
simp only [Part.eq_some_iff.2 hv₁, Part.map_some, Part.mem_some_iff]
split_ifs at hv₂ ⊢ <;> [exact Or.inl (congr_arg Sum.inl (Part.mem_some_iff.1 hv₂));
exact Or.inr ⟨_, rfl, hv₂⟩]
· exact IH _ rfl _ _ stepRet_then (ReflTransGen.tail hr rfl)
· rintro ⟨v', he, hr⟩
rw [reaches_eval] at hr
swap
... | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | cont_eval_fix | null |
code_is_ok (c) : Code.Ok c := by
induction c with (intro k v; rw [stepNormal])
| cons f fs IHf IHfs =>
rw [Code.eval, IHf]
simp only [bind_assoc, pure_bind]; congr; funext v
rw [reaches_eval]; swap
· exact ReflTransGen.single rfl
rw [stepRet, IHfs]; congr; funext v'
refine Eq.trans (b := eval step (stepRet (Cont.cons₂ v k) v')) ?_ (Eq.symm ?_) <;>
exact reaches_eval (ReflTransGen.single rfl)
| comp f g IHf IHg =>
rw [Code.eval, IHg]
simp only [bind_assoc]; congr; funext v
rw [reaches_eval]; swap
· exact ReflTransGen.single rfl
rw [stepRet, IHf]
| case f g IHf IHg =>
simp only [Code.eval]
cases v.headI <;> simp only [Nat.rec_zero, Part.bind_eq_bind] <;> [apply IHf; apply IHg]
| fix f IHf => rw [cont_eval_fix IHf]
| _ => simp only [Code.eval, pure_bind] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | code_is_ok | null |
stepNormal_eval (c v) : eval step (stepNormal c Cont.halt v) = Cfg.halt <$> c.eval v :=
(code_is_ok c).zero | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepNormal_eval | null |
stepRet_eval {k v} : eval step (stepRet k v) = Cfg.halt <$> k.eval v := by
induction k generalizing v with
| halt =>
simp only [Cont.eval, map_pure]
exact Part.eq_some_iff.2 (mem_eval.2 ⟨ReflTransGen.refl, rfl⟩)
| cons₁ fs as k IH =>
rw [Cont.eval, stepRet, code_is_ok]
simp only [← bind_pure_comp, bind_assoc]; congr; funext v'
rw [reaches_eval]; swap
· exact ReflTransGen.single rfl
rw [stepRet, IH, bind_pure_comp]
| cons₂ ns k IH => rw [Cont.eval, stepRet]; exact IH
| comp f k IH =>
rw [Cont.eval, stepRet, code_is_ok]
simp only [← bind_pure_comp, bind_assoc]; congr; funext v'
rw [reaches_eval]; swap
· exact ReflTransGen.single rfl
rw [IH, bind_pure_comp]
| fix f k IH =>
rw [Cont.eval, stepRet]; simp only
split_ifs; · exact IH
simp only [← bind_pure_comp, bind_assoc, cont_eval_fix (code_is_ok _)]
congr; funext; rw [bind_pure_comp, ← IH]
exact reaches_eval (ReflTransGen.single rfl) | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.PostTuringMachine",
"Mathlib.Tactic.DeriveFintype"
] | Mathlib/Computability/TMConfig.lean | stepRet_eval | null |
Γ'
| consₗ
| cons
| bit0
| bit1
deriving DecidableEq, Inhabited, Fintype
@[simp] theorem default_Γ' : (default : Γ') = .consₗ := rfl | inductive | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Γ' | The alphabet for the stacks in the program. `bit0` and `bit1` are used to represent `ℕ` values
as lists of binary digits, `cons` is used to separate `List ℕ` values, and `consₗ` is used to
separate `List (List ℕ)` values. See the section documentation. |
K'
| main
| rev
| aux
| stack
deriving DecidableEq, Inhabited
open K' | inductive | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K' | The four stacks used by the program. `main` is used to store the input value in `trNormal`
mode and the output value in `Λ'.ret` mode, while `stack` is used to keep all the data for the
continuations. `rev` is used to store reversed lists when transferring values between stacks, and
`aux` is only used once in `cons₁`. See the section documentation. |
Cont'
| halt
| cons₁ : Code → Cont' → Cont'
| cons₂ : Cont' → Cont'
| comp : Code → Cont' → Cont'
| fix : Code → Cont' → Cont'
deriving DecidableEq, Inhabited | inductive | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Cont' | Continuations as in `ToPartrec.Cont` but with the data removed. This is done because we want
the set of all continuations in the program to be finite (so that it can ultimately be encoded into
the finite state machine of a Turing machine), but a continuation can handle a potentially infinite
number of data values during execution. |
Λ'
| move (p : Γ' → Bool) (k₁ k₂ : K') (q : Λ')
| clear (p : Γ' → Bool) (k : K') (q : Λ')
| copy (q : Λ')
| push (k : K') (s : Option Γ' → Option Γ') (q : Λ')
| read (f : Option Γ' → Λ')
| succ (q : Λ')
| pred (q₁ q₂ : Λ')
| ret (k : Cont')
compile_inductive% Code
compile_inductive% Cont'
compile_inductive% K'
compile_inductive% Λ' | inductive | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Λ' | The set of program positions. We make extensive use of inductive types here to let us describe
"subroutines"; for example `clear p k q` is a program that clears stack `k`, then does `q` where
`q` is another label. In order to prevent this from resulting in an infinite number of distinct
accessible states, we are careful to be non-recursive (although loops are okay). See the section
documentation for a description of all the programs. |
Λ'.instInhabited : Inhabited Λ' :=
⟨Λ'.ret Cont'.halt⟩ | instance | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Λ'.instInhabited | null |
Λ'.instDecidableEq : DecidableEq Λ' := fun a b => by
induction a generalizing b <;> cases b <;> first
| apply Decidable.isFalse; rintro ⟨⟨⟩⟩; done
| exact decidable_of_iff' _ (by simp [funext_iff]; rfl) | instance | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Λ'.instDecidableEq | null |
Stmt' :=
TM2.Stmt (fun _ : K' => Γ') Λ' (Option Γ') deriving Inhabited | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Stmt' | The type of TM2 statements used by this machine. |
Cfg' :=
TM2.Cfg (fun _ : K' => Γ') Λ' (Option Γ') deriving Inhabited
open TM2.Stmt | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | Cfg' | The type of TM2 configurations used by this machine. |
@[simp]
natEnd : Γ' → Bool
| Γ'.consₗ => true
| Γ'.cons => true
| _ => false
attribute [nolint simpNF] natEnd.eq_3 | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | natEnd | A predicate that detects the end of a natural number, either `Γ'.cons` or `Γ'.consₗ` (or
implicitly the end of the list), for use in predicate-taking functions like `move` and `clear`. |
@[simp]
pop' (k : K') : Stmt' → Stmt' :=
pop k fun _ v => v | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | pop' | Pop a value from the stack and place the result in local store. |
@[simp]
peek' (k : K') : Stmt' → Stmt' :=
peek k fun _ v => v | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | peek' | Peek a value from the stack and place the result in local store. |
@[simp]
push' (k : K') : Stmt' → Stmt' :=
push k fun x => x.iget | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | push' | Push the value in the local store to the given stack. |
unrev :=
Λ'.move (fun _ => false) rev main | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | unrev | Move everything from the `rev` stack to the `main` stack (reversed). |
moveExcl (p k₁ k₂ q) :=
Λ'.move p k₁ k₂ <| Λ'.push k₁ id q | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | moveExcl | Move elements from `k₁` to `k₂` while `p` holds, with the last element being left on `k₁`. |
move₂ (p k₁ k₂ q) :=
moveExcl p k₁ rev <| Λ'.move (fun _ => false) rev k₂ q | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | move₂ | Move elements from `k₁` to `k₂` without reversion, by performing a double move via the `rev`
stack. |
head (k : K') (q : Λ') : Λ' :=
Λ'.move natEnd k rev <|
(Λ'.push rev fun _ => some Γ'.cons) <|
Λ'.read fun s =>
(if s = some Γ'.consₗ then id else Λ'.clear (fun x => x = Γ'.consₗ) k) <| unrev q | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | head | Assuming `trList v` is on the front of stack `k`, remove it, and push `v.headI` onto `main`.
See the section documentation. |
@[simp]
trNormal : Code → Cont' → Λ'
| Code.zero', k => (Λ'.push main fun _ => some Γ'.cons) <| Λ'.ret k
| Code.succ, k => head main <| Λ'.succ <| Λ'.ret k
| Code.tail, k => Λ'.clear natEnd main <| Λ'.ret k
| Code.cons f fs, k =>
(Λ'.push stack fun _ => some Γ'.consₗ) <|
Λ'.move (fun _ => false) main rev <| Λ'.copy <| trNormal f (Cont'.cons₁ fs k)
| Code.comp f g, k => trNormal g (Cont'.comp f k)
| Code.case f g, k => Λ'.pred (trNormal f k) (trNormal g k)
| Code.fix f, k => trNormal f (Cont'.fix f k) | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trNormal | The program that evaluates code `c` with continuation `k`. This expects an initial state where
`trList v` is on `main`, `trContStack k` is on `stack`, and `aux` and `rev` are empty.
See the section documentation for details. |
tr : Λ' → Stmt'
| Λ'.move p k₁ k₂ q =>
pop' k₁ <|
branch (fun s => s.elim true p) (goto fun _ => q)
(push' k₂ <| goto fun _ => Λ'.move p k₁ k₂ q)
| Λ'.push k f q =>
branch (fun s => (f s).isSome) ((push k fun s => (f s).iget) <| goto fun _ => q)
(goto fun _ => q)
| Λ'.read q => goto q
| Λ'.clear p k q =>
pop' k <| branch (fun s => s.elim true p) (goto fun _ => q) (goto fun _ => Λ'.clear p k q)
| Λ'.copy q =>
pop' rev <|
branch Option.isSome (push' main <| push' stack <| goto fun _ => Λ'.copy q) (goto fun _ => q)
| Λ'.succ q =>
pop' main <|
branch (fun s => s = some Γ'.bit1) ((push rev fun _ => Γ'.bit0) <| goto fun _ => Λ'.succ q) <|
branch (fun s => s = some Γ'.cons)
((push main fun _ => Γ'.cons) <| (push main fun _ => Γ'.bit1) <| goto fun _ => unrev q)
((push main fun _ => Γ'.bit1) <| goto fun _ => unrev q)
| Λ'.pred q₁ q₂ =>
pop' main <|
branch (fun s => s = some Γ'.bit0)
((push rev fun _ => Γ'.bit1) <| goto fun _ => Λ'.pred q₁ q₂) <|
branch (fun s => natEnd s.iget) (goto fun _ => q₁)
(peek' main <|
branch (fun s => natEnd s.iget) (goto fun _ => unrev q₂)
((push rev fun _ => Γ'.bit0) <| goto fun _ => unrev q₂))
| Λ'.ret (Cont'.cons₁ fs k) =>
goto fun _ =>
move₂ (fun _ => false) main aux <|
move₂ (fun s => s = Γ'.consₗ) stack main <|
move₂ (fun _ => false) aux stack <| trNormal fs (Cont'.cons₂ k)
| Λ'.ret (Cont'.cons₂ k) => goto fun _ => head stack <| Λ'.ret k
| Λ'.ret (Cont'.comp f k) => goto fun _ => trNormal f k
| Λ'.ret (Cont'.fix f k) =>
pop' main <|
goto fun s =>
cond (natEnd s.iget) (Λ'.ret k) <| Λ'.clear natEnd main <| trNormal f (Cont'.fix f k)
| Λ'.ret Cont'.halt => (load fun _ => none) <| halt
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr | The main program. See the section documentation for details. |
tr_move (p k₁ k₂ q) : tr (Λ'.move p k₁ k₂ q) =
pop' k₁ (branch (fun s => s.elim true p) (goto fun _ => q)
(push' k₂ <| goto fun _ => Λ'.move p k₁ k₂ q)) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_move | null |
tr_push (k f q) : tr (Λ'.push k f q) = branch (fun s => (f s).isSome)
((push k fun s => (f s).iget) <| goto fun _ => q) (goto fun _ => q) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_push | null |
tr_read (q) : tr (Λ'.read q) = goto q := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_read | null |
tr_clear (p k q) : tr (Λ'.clear p k q) = pop' k (branch
(fun s => s.elim true p) (goto fun _ => q) (goto fun _ => Λ'.clear p k q)) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_clear | null |
tr_copy (q) : tr (Λ'.copy q) = pop' rev (branch Option.isSome
(push' main <| push' stack <| goto fun _ => Λ'.copy q) (goto fun _ => q)) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_copy | null |
tr_succ (q) : tr (Λ'.succ q) = pop' main (branch (fun s => s = some Γ'.bit1)
((push rev fun _ => Γ'.bit0) <| goto fun _ => Λ'.succ q) <|
branch (fun s => s = some Γ'.cons)
((push main fun _ => Γ'.cons) <| (push main fun _ => Γ'.bit1) <| goto fun _ => unrev q)
((push main fun _ => Γ'.bit1) <| goto fun _ => unrev q)) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_succ | null |
tr_pred (q₁ q₂) : tr (Λ'.pred q₁ q₂) = pop' main (branch (fun s => s = some Γ'.bit0)
((push rev fun _ => Γ'.bit1) <| goto fun _ => Λ'.pred q₁ q₂) <|
branch (fun s => natEnd s.iget) (goto fun _ => q₁)
(peek' main <|
branch (fun s => natEnd s.iget) (goto fun _ => unrev q₂)
((push rev fun _ => Γ'.bit0) <| goto fun _ => unrev q₂))) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_pred | null |
tr_ret_cons₁ (fs k) : tr (Λ'.ret (Cont'.cons₁ fs k)) = goto fun _ =>
move₂ (fun _ => false) main aux <|
move₂ (fun s => s = Γ'.consₗ) stack main <|
move₂ (fun _ => false) aux stack <| trNormal fs (Cont'.cons₂ k) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_ret_cons₁ | null |
tr_ret_cons₂ (k) : tr (Λ'.ret (Cont'.cons₂ k)) =
goto fun _ => head stack <| Λ'.ret k := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_ret_cons₂ | null |
tr_ret_comp (f k) : tr (Λ'.ret (Cont'.comp f k)) = goto fun _ => trNormal f k := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_ret_comp | null |
tr_ret_fix (f k) : tr (Λ'.ret (Cont'.fix f k)) = pop' main (goto fun s =>
cond (natEnd s.iget) (Λ'.ret k) <| Λ'.clear natEnd main <| trNormal f (Cont'.fix f k)) := rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_ret_fix | null |
tr_ret_halt : tr (Λ'.ret Cont'.halt) = (load fun _ => none) halt := rfl | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | tr_ret_halt | null |
trCont : Cont → Cont'
| Cont.halt => Cont'.halt
| Cont.cons₁ c _ k => Cont'.cons₁ c (trCont k)
| Cont.cons₂ _ k => Cont'.cons₂ (trCont k)
| Cont.comp c k => Cont'.comp c (trCont k)
| Cont.fix c k => Cont'.fix c (trCont k) | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trCont | Translating a `Cont` continuation to a `Cont'` continuation simply entails dropping all the
data. This data is instead encoded in `trContStack` in the configuration. |
trPosNum : PosNum → List Γ'
| PosNum.one => [Γ'.bit1]
| PosNum.bit0 n => Γ'.bit0 :: trPosNum n
| PosNum.bit1 n => Γ'.bit1 :: trPosNum n | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trPosNum | We use `PosNum` to define the translation of binary natural numbers. A natural number is
represented as a little-endian list of `bit0` and `bit1` elements:
1 = [bit1]
2 = [bit0, bit1]
3 = [bit1, bit1]
4 = [bit0, bit0, bit1]
In particular, this representation guarantees no trailing `bit0`'s at the end of the list. |
trNum : Num → List Γ'
| Num.zero => []
| Num.pos n => trPosNum n | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trNum | We use `Num` to define the translation of binary natural numbers. Positive numbers are
translated using `trPosNum`, and `trNum 0 = []`. So there are never any trailing `bit0`'s in
a translated `Num`.
0 = []
1 = [bit1]
2 = [bit0, bit1]
3 = [bit1, bit1]
4 = [bit0, bit0, bit1] |
trNat (n : ℕ) : List Γ' :=
trNum n
@[simp] | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trNat | Because we use binary encoding, we define `trNat` in terms of `trNum`, using `Num`, which are
binary natural numbers. (We could also use `Nat.binaryRecOn`, but `Num` and `PosNum` make for
easy inductions.) |
trNat_zero : trNat 0 = [] := by rw [trNat, Nat.cast_zero]; rfl | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trNat_zero | null |
trNat_default : trNat default = [] :=
trNat_zero | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trNat_default | null |
@[simp]
trList : List ℕ → List Γ'
| [] => []
| n::ns => trNat n ++ Γ'.cons :: trList ns | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trList | Lists are translated with a `cons` after each encoded number.
For example:
[] = []
[0] = [cons]
[1] = [bit1, cons]
[6, 0] = [bit0, bit1, bit1, cons, cons] |
@[simp]
trLList : List (List ℕ) → List Γ'
| [] => []
| l::ls => trList l ++ Γ'.consₗ :: trLList ls | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trLList | Lists of lists are translated with a `consₗ` after each encoded list.
For example:
[] = []
[[]] = [consₗ]
[[], []] = [consₗ, consₗ]
[[0]] = [cons, consₗ]
[[1, 2], [0]] = [bit1, cons, bit0, bit1, cons, consₗ, cons, consₗ] |
@[simp]
contStack : Cont → List (List ℕ)
| Cont.halt => []
| Cont.cons₁ _ ns k => ns :: contStack k
| Cont.cons₂ ns k => ns :: contStack k
| Cont.comp _ k => contStack k
| Cont.fix _ k => contStack k | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | contStack | The data part of a continuation is a list of lists, which is encoded on the `stack` stack
using `trLList`. |
trContStack (k : Cont) :=
trLList (contStack k) | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trContStack | The data part of a continuation is a list of lists, which is encoded on the `stack` stack
using `trLList`. |
K'.elim (a b c d : List Γ') : K' → List Γ'
| K'.main => a
| K'.rev => b
| K'.aux => c
| K'.stack => d | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim | This is the nondependent eliminator for `K'`, but we use it specifically here in order to
represent the stack data as four lists rather than as a function `K' → List Γ'`, because this makes
rewrites easier. The theorems `K'.elim_update_main` et. al. show how such a function is updated
after an `update` to one of the components. |
K'.elim_main (a b c d) : K'.elim a b c d K'.main = a := rfl | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_main | null |
K'.elim_rev (a b c d) : K'.elim a b c d K'.rev = b := rfl | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_rev | null |
K'.elim_aux (a b c d) : K'.elim a b c d K'.aux = c := rfl | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_aux | null |
K'.elim_stack (a b c d) : K'.elim a b c d K'.stack = d := rfl
attribute [simp] K'.elim
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_stack | null |
K'.elim_update_main {a b c d a'} : update (K'.elim a b c d) main a' = K'.elim a' b c d := by
funext x; cases x <;> rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_update_main | null |
K'.elim_update_rev {a b c d b'} : update (K'.elim a b c d) rev b' = K'.elim a b' c d := by
funext x; cases x <;> rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_update_rev | null |
K'.elim_update_aux {a b c d c'} : update (K'.elim a b c d) aux c' = K'.elim a b c' d := by
funext x; cases x <;> rfl
@[simp] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_update_aux | null |
K'.elim_update_stack {a b c d d'} :
update (K'.elim a b c d) stack d' = K'.elim a b c d' := by funext x; cases x <;> rfl | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | K'.elim_update_stack | null |
halt (v : List ℕ) : Cfg' :=
⟨none, none, K'.elim (trList v) [] [] []⟩ | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | halt | The halting state corresponding to a `List ℕ` output value. |
TrCfg : Cfg → Cfg' → Prop
| Cfg.ret k v, c' =>
∃ s, c' = ⟨some (Λ'.ret (trCont k)), s, K'.elim (trList v) [] [] (trContStack k)⟩
| Cfg.halt v, c' => c' = halt v | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | TrCfg | The `Cfg` states map to `Cfg'` states almost one to one, except that in normal operation the
local store contains an arbitrary garbage value. To make the final theorem cleaner we explicitly
clear it in the halt state so that there is exactly one configuration corresponding to output `v`. |
splitAtPred {α} (p : α → Bool) : List α → List α × Option α × List α
| [] => ([], none, [])
| a :: as =>
cond (p a) ([], some a, as) <|
let ⟨l₁, o, l₂⟩ := splitAtPred p as
⟨a::l₁, o, l₂⟩ | def | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | splitAtPred | This could be a general list definition, but it is also somewhat specialized to this
application. `splitAtPred p L` will search `L` for the first element satisfying `p`.
If it is found, say `L = l₁ ++ a :: l₂` where `a` satisfies `p` but `l₁` does not, then it returns
`(l₁, some a, l₂)`. Otherwise, if there is no such element, it returns `(L, none, [])`. |
splitAtPred_eq {α} (p : α → Bool) :
∀ L l₁ o l₂,
(∀ x ∈ l₁, p x = false) →
Option.elim' (L = l₁ ∧ l₂ = []) (fun a => p a = true ∧ L = l₁ ++ a::l₂) o →
splitAtPred p L = (l₁, o, l₂)
| [], _, none, _, _, ⟨rfl, rfl⟩ => rfl
| [], l₁, some o, l₂, _, ⟨_, h₃⟩ => by simp at h₃
| a :: L, l₁, o, l₂, h₁, h₂ => by
rw [splitAtPred]
have IH := splitAtPred_eq p L
rcases o with - | o
· rcases l₁ with - | ⟨a', l₁⟩ <;> rcases h₂ with ⟨⟨⟩, rfl⟩
rw [h₁ a (List.Mem.head _), cond, IH L none [] _ ⟨rfl, rfl⟩]
exact fun x h => h₁ x (List.Mem.tail _ h)
· rcases l₁ with - | ⟨a', l₁⟩ <;> rcases h₂ with ⟨h₂, ⟨⟩⟩
· rw [h₂, cond]
rw [h₁ a (List.Mem.head _), cond, IH l₁ (some o) l₂ _ ⟨h₂, _⟩] <;> try rfl
exact fun x h => h₁ x (List.Mem.tail _ h) | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | splitAtPred_eq | null |
splitAtPred_false {α} (L : List α) : splitAtPred (fun _ => false) L = (L, none, []) :=
splitAtPred_eq _ _ _ _ _ (fun _ _ => rfl) ⟨rfl, rfl⟩ | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | splitAtPred_false | null |
move_ok {p k₁ k₂ q s L₁ o L₂} {S : K' → List Γ'} (h₁ : k₁ ≠ k₂)
(e : splitAtPred p (S k₁) = (L₁, o, L₂)) :
Reaches₁ (TM2.step tr) ⟨some (Λ'.move p k₁ k₂ q), s, S⟩
⟨some q, o, update (update S k₁ L₂) k₂ (L₁.reverseAux (S k₂))⟩ := by
induction L₁ generalizing S s with
| nil =>
rw [(_ : [].reverseAux _ = _), Function.update_eq_self]
swap
· rw [Function.update_of_ne h₁.symm, List.reverseAux_nil]
refine TransGen.head' rfl ?_
rw [tr]; simp only [pop', TM2.stepAux]
revert e; rcases S k₁ with - | ⟨a, Sk⟩ <;> intro e
· cases e
rfl
simp only [splitAtPred, Option.elim, List.head?, List.tail_cons] at e ⊢
revert e; cases p a <;> intro e <;>
simp only [cond_false, cond_true, Prod.mk.injEq, true_and, false_and, reduceCtorEq] at e ⊢
simp only [e]
rfl
| cons a L₁ IH =>
refine TransGen.head rfl ?_
rw [tr]; simp only [pop', Option.elim, TM2.stepAux, push']
rcases e₁ : S k₁ with - | ⟨a', Sk⟩ <;> rw [e₁, splitAtPred] at e
· cases e
cases e₂ : p a' <;> simp only [e₂, cond] at e
swap
· cases e
rcases e₃ : splitAtPred p Sk with ⟨_, _, _⟩
rw [e₃] at e
cases e
simp only [List.head?_cons, e₂, List.tail_cons, cond_false]
convert @IH _ (update (update S k₁ Sk) k₂ (a :: S k₂)) _ using 2 <;>
simp [Function.update_of_ne, h₁, h₁.symm, e₃, List.reverseAux]
simp [Function.update_comm h₁.symm] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | move_ok | null |
unrev_ok {q s} {S : K' → List Γ'} :
Reaches₁ (TM2.step tr) ⟨some (unrev q), s, S⟩
⟨some q, none, update (update S rev []) main (List.reverseAux (S rev) (S main))⟩ :=
move_ok (by decide) <| splitAtPred_false _ | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | unrev_ok | null |
move₂_ok {p k₁ k₂ q s L₁ o L₂} {S : K' → List Γ'} (h₁ : k₁ ≠ rev ∧ k₂ ≠ rev ∧ k₁ ≠ k₂)
(h₂ : S rev = []) (e : splitAtPred p (S k₁) = (L₁, o, L₂)) :
Reaches₁ (TM2.step tr) ⟨some (move₂ p k₁ k₂ q), s, S⟩
⟨some q, none, update (update S k₁ (o.elim id List.cons L₂)) k₂ (L₁ ++ S k₂)⟩ := by
refine (move_ok h₁.1 e).trans (TransGen.head rfl ?_)
simp only [TM2.step, Option.mem_def, Option.elim]
cases o <;> simp only <;> rw [tr]
<;> simp only [id, TM2.stepAux, Option.isSome, cond_true, cond_false]
· convert move_ok h₁.2.1.symm (splitAtPred_false _) using 2
simp only [Function.update_comm h₁.1, Function.update_idem]
rw [show update S rev [] = S by rw [← h₂, Function.update_eq_self]]
simp only [Function.update_of_ne h₁.2.2.symm, Function.update_of_ne h₁.2.1,
Function.update_of_ne h₁.1.symm, List.reverseAux_eq, h₂, Function.update_self,
List.append_nil, List.reverse_reverse]
· convert move_ok h₁.2.1.symm (splitAtPred_false _) using 2
simp only [h₂, Function.update_comm h₁.1, List.reverseAux_eq, Function.update_self,
List.append_nil, Function.update_idem]
rw [show update S rev [] = S by rw [← h₂, Function.update_eq_self]]
simp only [Function.update_of_ne h₁.1.symm, Function.update_of_ne h₁.2.2.symm,
Function.update_of_ne h₁.2.1, Function.update_self, List.reverse_reverse] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | move₂_ok | null |
clear_ok {p k q s L₁ o L₂} {S : K' → List Γ'} (e : splitAtPred p (S k) = (L₁, o, L₂)) :
Reaches₁ (TM2.step tr) ⟨some (Λ'.clear p k q), s, S⟩ ⟨some q, o, update S k L₂⟩ := by
induction L₁ generalizing S s with
| nil =>
refine TransGen.head' rfl ?_
rw [tr]; simp only [pop', TM2.step, Option.mem_def, TM2.stepAux, Option.elim]
revert e; rcases S k with - | ⟨a, Sk⟩ <;> intro e
· cases e
rfl
simp only [splitAtPred, List.head?, List.tail_cons] at e ⊢
revert e; cases p a <;> intro e <;>
simp only [cond_false, cond_true, Prod.mk.injEq, true_and, false_and, reduceCtorEq] at e ⊢
rcases e with ⟨e₁, e₂⟩
rw [e₁, e₂]
| cons a L₁ IH =>
refine TransGen.head rfl ?_
rw [tr]; simp only [pop', TM2.step, Option.mem_def, TM2.stepAux, Option.elim]
rcases e₁ : S k with - | ⟨a', Sk⟩ <;> rw [e₁, splitAtPred] at e
· cases e
cases e₂ : p a' <;> simp only [e₂, cond] at e
swap
· cases e
rcases e₃ : splitAtPred p Sk with ⟨_, _, _⟩
rw [e₃] at e
cases e
simp only [List.head?_cons, e₂, List.tail_cons, cond_false]
convert @IH _ (update S k Sk) _ using 2 <;> simp [e₃] | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | clear_ok | null |
copy_ok (q s a b c d) :
Reaches₁ (TM2.step tr) ⟨some (Λ'.copy q), s, K'.elim a b c d⟩
⟨some q, none, K'.elim (List.reverseAux b a) [] c (List.reverseAux b d)⟩ := by
induction b generalizing a d s with
| nil =>
refine TransGen.single ?_
simp
| cons x b IH =>
refine TransGen.head rfl ?_
rw [tr]
simp only [TM2.step, Option.mem_def, TM2.stepAux, elim_rev, List.head?_cons, Option.isSome_some,
List.tail_cons, elim_update_rev, elim_main, elim_update_main,
elim_stack, elim_update_stack, cond_true, List.reverseAux_cons, pop', push']
exact IH _ _ _ | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | copy_ok | null |
trPosNum_natEnd : ∀ (n), ∀ x ∈ trPosNum n, natEnd x = false
| PosNum.one, _, List.Mem.head _ => rfl
| PosNum.bit0 _, _, List.Mem.head _ => rfl
| PosNum.bit0 n, _, List.Mem.tail _ h => trPosNum_natEnd n _ h
| PosNum.bit1 _, _, List.Mem.head _ => rfl
| PosNum.bit1 n, _, List.Mem.tail _ h => trPosNum_natEnd n _ h | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trPosNum_natEnd | null |
trNum_natEnd : ∀ (n), ∀ x ∈ trNum n, natEnd x = false
| Num.pos n, x, h => trPosNum_natEnd n x h | theorem | Computability | [
"Mathlib.Computability.Halting",
"Mathlib.Computability.TuringMachine",
"Mathlib.Data.Num.Lemmas",
"Mathlib.Tactic.DeriveFintype",
"Mathlib.Computability.TMConfig"
] | Mathlib/Computability/TMToPartrec.lean | trNum_natEnd | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.