mathlib3 documentation

computability.turing_machine

Turing machines #

THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.

This file defines a sequence of simple machine languages, starting with Turing machines and working up to more complex languages based on Wang B-machines.

Naming conventions #

Each model of computation in this file shares a naming convention for the elements of a model of computation. These are the parameters for the language:

All of these variables denote "essentially finite" types, but for technical reasons it is convenient to allow them to be infinite anyway. When using an infinite type, we will be interested to prove that only finitely many values of the type are ever interacted with.

Given these parameters, there are a few common structures for the model that arise:

def turing.blank_extends {Γ : Type u_1} [inhabited Γ] (l₁ l₂ : list Γ) :
Prop

The blank_extends partial order holds of l₁ and l₂ if l₂ is obtained by adding blanks (default : Γ) to the end of l₁.

Equations
@[refl]
theorem turing.blank_extends.refl {Γ : Type u_1} [inhabited Γ] (l : list Γ) :
@[trans]
theorem turing.blank_extends.trans {Γ : Type u_1} [inhabited Γ] {l₁ l₂ l₃ : list Γ} :
theorem turing.blank_extends.below_of_le {Γ : Type u_1} [inhabited Γ] {l l₁ l₂ : list Γ} :
def turing.blank_extends.above {Γ : Type u_1} [inhabited Γ] {l l₁ l₂ : list Γ} (h₁ : turing.blank_extends l l₁) (h₂ : turing.blank_extends l l₂) :

Any two extensions by blank l₁,l₂ of l have a common join (which can be taken to be the longer of l₁ and l₂).

Equations
theorem turing.blank_extends.above_of_le {Γ : Type u_1} [inhabited Γ] {l l₁ l₂ : list Γ} :
def turing.blank_rel {Γ : Type u_1} [inhabited Γ] (l₁ l₂ : list Γ) :
Prop

blank_rel is the symmetric closure of blank_extends, turning it into an equivalence relation. Two lists are related by blank_rel if one extends the other by blanks.

Equations
@[refl]
theorem turing.blank_rel.refl {Γ : Type u_1} [inhabited Γ] (l : list Γ) :
@[symm]
theorem turing.blank_rel.symm {Γ : Type u_1} [inhabited Γ] {l₁ l₂ : list Γ} :
@[trans]
theorem turing.blank_rel.trans {Γ : Type u_1} [inhabited Γ] {l₁ l₂ l₃ : list Γ} :
def turing.blank_rel.above {Γ : Type u_1} [inhabited Γ] {l₁ l₂ : list Γ} (h : turing.blank_rel l₁ l₂) :

Given two blank_rel lists, there exists (constructively) a common join.

Equations
def turing.blank_rel.below {Γ : Type u_1} [inhabited Γ] {l₁ l₂ : list Γ} (h : turing.blank_rel l₁ l₂) :

Given two blank_rel lists, there exists (constructively) a common meet.

Equations
def turing.blank_rel.setoid (Γ : Type u_1) [inhabited Γ] :

Construct a setoid instance for blank_rel.

Equations
def turing.list_blank (Γ : Type u_1) [inhabited Γ] :
Type u_1

A list_blank Γ is a quotient of list Γ by extension by blanks at the end. This is used to represent half-tapes of a Turing machine, so that we can pretend that the list continues infinitely with blanks.

Equations
Instances for turing.list_blank
@[protected, reducible]
def turing.list_blank.lift_on {Γ : Type u_1} [inhabited Γ] {α : Sort u_2} (l : turing.list_blank Γ) (f : list Γ α) (H : (a b : list Γ), turing.blank_extends a b f a = f b) :
α

A modified version of quotient.lift_on' specialized for list_blank, with the stronger precondition blank_extends instead of blank_rel.

Equations

The quotient map turning a list into a list_blank.

Equations
@[protected]
theorem turing.list_blank.induction_on {Γ : Type u_1} [inhabited Γ] {p : turing.list_blank Γ Prop} (q : turing.list_blank Γ) (h : (a : list Γ), p (turing.list_blank.mk a)) :
p q
def turing.list_blank.head {Γ : Type u_1} [inhabited Γ] (l : turing.list_blank Γ) :
Γ

The head of a list_blank is well defined.

Equations
@[simp]
theorem turing.list_blank.head_mk {Γ : Type u_1} [inhabited Γ] (l : list Γ) :

The tail of a list_blank is well defined (up to the tail of blanks).

Equations
def turing.list_blank.cons {Γ : Type u_1} [inhabited Γ] (a : Γ) (l : turing.list_blank Γ) :

We can cons an element onto a list_blank.

Equations
@[simp]
@[simp]
theorem turing.list_blank.head_cons {Γ : Type u_1} [inhabited Γ] (a : Γ) (l : turing.list_blank Γ) :
@[simp]
theorem turing.list_blank.tail_cons {Γ : Type u_1} [inhabited Γ] (a : Γ) (l : turing.list_blank Γ) :
@[simp]

The cons and head/tail functions are mutually inverse, unlike in the case of list where this only holds for nonempty lists.

theorem turing.list_blank.exists_cons {Γ : Type u_1} [inhabited Γ] (l : turing.list_blank Γ) :

The cons and head/tail functions are mutually inverse, unlike in the case of list where this only holds for nonempty lists.

def turing.list_blank.nth {Γ : Type u_1} [inhabited Γ] (l : turing.list_blank Γ) (n : ) :
Γ

The n-th element of a list_blank is well defined for all n : ℕ, unlike in a list.

Equations
@[simp]
theorem turing.list_blank.nth_mk {Γ : Type u_1} [inhabited Γ] (l : list Γ) (n : ) :
@[simp]
theorem turing.list_blank.nth_zero {Γ : Type u_1} [inhabited Γ] (l : turing.list_blank Γ) :
l.nth 0 = l.head
@[simp]
theorem turing.list_blank.nth_succ {Γ : Type u_1} [inhabited Γ] (l : turing.list_blank Γ) (n : ) :
l.nth (n + 1) = l.tail.nth n
@[ext]
theorem turing.list_blank.ext {Γ : Type u_1} [inhabited Γ] {L₁ L₂ : turing.list_blank Γ} :
( (i : ), L₁.nth i = L₂.nth i) L₁ = L₂
@[simp]

Apply a function to a value stored at the nth position of the list.

Equations
theorem turing.list_blank.nth_modify_nth {Γ : Type u_1} [inhabited Γ] (f : Γ Γ) (n i : ) (L : turing.list_blank Γ) :
(turing.list_blank.modify_nth f n L).nth i = ite (i = n) (f (L.nth i)) (L.nth i)
structure turing.pointed_map (Γ : Type u) (Γ' : Type v) [inhabited Γ] [inhabited Γ'] :
Type (max u v)

A pointed map of inhabited types is a map that sends one default value to the other.

Instances for turing.pointed_map
@[protected, instance]
def turing.pointed_map.inhabited {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] :
Equations
@[protected, instance]
def turing.pointed_map.has_coe_to_fun {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] :
has_coe_to_fun (turing.pointed_map Γ Γ') (λ (_x : turing.pointed_map Γ Γ'), Γ Γ')
Equations
@[simp]
theorem turing.pointed_map.mk_val {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : Γ Γ') (pt : f inhabited.default = inhabited.default) :
{f := f, map_pt' := pt} = f
@[simp]
@[simp]
theorem turing.pointed_map.head_map {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (l : list Γ) :
def turing.list_blank.map {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (l : turing.list_blank Γ) :

The map function on lists is well defined on list_blanks provided that the map is pointed.

Equations
@[simp]
@[simp]
theorem turing.list_blank.head_map {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (l : turing.list_blank Γ) :
@[simp]
@[simp]
theorem turing.list_blank.nth_map {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (l : turing.list_blank Γ) (n : ) :
def turing.proj {ι : Type u_1} {Γ : ι Type u_2} [Π (i : ι), inhabited (Γ i)] (i : ι) :
turing.pointed_map (Π (i : ι), Γ i) (Γ i)

The i-th projection as a pointed map.

Equations
theorem turing.proj_map_nth {ι : Type u_1} {Γ : ι Type u_2} [Π (i : ι), inhabited (Γ i)] (i : ι) (L : turing.list_blank (Π (i : ι), Γ i)) (n : ) :
theorem turing.list_blank.map_modify_nth {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (F : turing.pointed_map Γ Γ') (f : Γ Γ) (f' : Γ' Γ') (H : (x : Γ), F (f x) = f' (F x)) (n : ) (L : turing.list_blank Γ) :
@[simp]

Append a list on the left side of a list_blank.

Equations
@[simp]
theorem turing.list_blank.append_mk {Γ : Type u_1} [inhabited Γ] (l₁ l₂ : list Γ) :
theorem turing.list_blank.append_assoc {Γ : Type u_1} [inhabited Γ] (l₁ l₂ : list Γ) (l₃ : turing.list_blank Γ) :
def turing.list_blank.bind {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (l : turing.list_blank Γ) (f : Γ list Γ') (hf : (n : ), f inhabited.default = list.replicate n inhabited.default) :

The bind function on lists is well defined on list_blanks provided that the default element is sent to a sequence of default elements.

Equations
@[simp]
theorem turing.list_blank.bind_mk {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (l : list Γ) (f : Γ list Γ') (hf : (n : ), f inhabited.default = list.replicate n inhabited.default) :
@[simp]
theorem turing.list_blank.cons_bind {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (a : Γ) (l : turing.list_blank Γ) (f : Γ list Γ') (hf : (n : ), f inhabited.default = list.replicate n inhabited.default) :
structure turing.tape (Γ : Type u_1) [inhabited Γ] :
Type u_1

The tape of a Turing machine is composed of a head element (which we imagine to be the current position of the head), together with two list_blanks denoting the portions of the tape going off to the left and right. When the Turing machine moves right, an element is pulled from the right side and becomes the new head, while the head element is consed onto the left side.

Instances for turing.tape
@[protected, instance]
@[protected, instance]
inductive turing.dir  :

A direction for the turing machine move command, either left or right.

Instances for turing.dir
def turing.tape.left₀ {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) :

The "inclusive" left side of the tape, including both left and head.

Equations
def turing.tape.right₀ {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) :

The "inclusive" right side of the tape, including both right and head.

Equations

Move the tape in response to a motion of the Turing machine. Note that T.move dir.left makes T.left smaller; the Turing machine is moving left and the tape is moving right.

Equations
def turing.tape.mk' {Γ : Type u_1} [inhabited Γ] (L R : turing.list_blank Γ) :

Construct a tape from a left side and an inclusive right side.

Equations
@[simp]
theorem turing.tape.mk'_left {Γ : Type u_1} [inhabited Γ] (L R : turing.list_blank Γ) :
@[simp]
theorem turing.tape.mk'_head {Γ : Type u_1} [inhabited Γ] (L R : turing.list_blank Γ) :
@[simp]
theorem turing.tape.mk'_right {Γ : Type u_1} [inhabited Γ] (L R : turing.list_blank Γ) :
@[simp]
theorem turing.tape.mk'_right₀ {Γ : Type u_1} [inhabited Γ] (L R : turing.list_blank Γ) :
@[simp]
theorem turing.tape.exists_mk' {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) :
def turing.tape.mk₂ {Γ : Type u_1} [inhabited Γ] (L R : list Γ) :

Construct a tape from a left side and an inclusive right side.

Equations
def turing.tape.mk₁ {Γ : Type u_1} [inhabited Γ] (l : list Γ) :

Construct a tape from a list, with the head of the list at the TM head and the rest going to the right.

Equations
def turing.tape.nth {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) :
Γ

The nth function of a tape is integer-valued, with index 0 being the head, negative indexes on the left and positive indexes on the right. (Picture a number line.)

Equations
@[simp]
theorem turing.tape.nth_zero {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) :
T.nth 0 = T.head
theorem turing.tape.right₀_nth {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) (n : ) :
@[simp]
theorem turing.tape.mk'_nth_nat {Γ : Type u_1} [inhabited Γ] (L R : turing.list_blank Γ) (n : ) :
@[simp]
theorem turing.tape.move_left_nth {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) (i : ) :
@[simp]
theorem turing.tape.move_right_nth {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) (i : ) :
def turing.tape.write {Γ : Type u_1} [inhabited Γ] (b : Γ) (T : turing.tape Γ) :

Replace the current value of the head on the tape.

Equations
@[simp]
theorem turing.tape.write_self {Γ : Type u_1} [inhabited Γ] (T : turing.tape Γ) :
@[simp]
theorem turing.tape.write_nth {Γ : Type u_1} [inhabited Γ] (b : Γ) (T : turing.tape Γ) {i : } :
(turing.tape.write b T).nth i = ite (i = 0) b (T.nth i)
def turing.tape.map {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (T : turing.tape Γ) :

Apply a pointed map to a tape to change the alphabet.

Equations
@[simp]
theorem turing.tape.map_fst {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (T : turing.tape Γ) :
@[simp]
theorem turing.tape.map_write {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (b : Γ) (T : turing.tape Γ) :
theorem turing.tape.map_move {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (T : turing.tape Γ) (d : turing.dir) :
theorem turing.tape.map_mk₂ {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (L R : list Γ) :
theorem turing.tape.map_mk₁ {Γ : Type u_1} {Γ' : Type u_2} [inhabited Γ] [inhabited Γ'] (f : turing.pointed_map Γ Γ') (l : list Γ) :
def turing.eval {σ : Type u_1} (f : σ option σ) :
σ part σ

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.

Equations
def turing.reaches {σ : Type u_1} (f : σ option σ) :
σ σ Prop

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.

Equations
def turing.reaches₁ {σ : Type u_1} (f : σ option σ) :
σ σ Prop

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.

Equations
theorem turing.reaches₁_eq {σ : Type u_1} {f : σ option σ} {a b c : σ} (h : f a = f b) :
theorem turing.reaches_total {σ : Type u_1} {f : σ option σ} {a b c : σ} (hab : turing.reaches f a b) (hac : turing.reaches f a c) :
theorem turing.reaches₁_fwd {σ : Type u_1} {f : σ option σ} {a b c : σ} (h₁ : turing.reaches₁ f a c) (h₂ : b f a) :
def turing.reaches₀ {σ : Type u_1} (f : σ option σ) (a b : σ) :
Prop

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.

Equations
theorem turing.reaches₀.trans {σ : Type u_1} {f : σ option σ} {a b c : σ} (h₁ : turing.reaches₀ f a b) (h₂ : turing.reaches₀ f b c) :
@[refl]
theorem turing.reaches₀.refl {σ : Type u_1} {f : σ option σ} (a : σ) :
theorem turing.reaches₀.single {σ : Type u_1} {f : σ option σ} {a b : σ} (h : b f a) :
theorem turing.reaches₀.head {σ : Type u_1} {f : σ option σ} {a b c : σ} (h : b f a) (h₂ : turing.reaches₀ f b c) :
theorem turing.reaches₀.tail {σ : Type u_1} {f : σ option σ} {a b c : σ} (h₁ : turing.reaches₀ f a b) (h : c f b) :
theorem turing.reaches₀_eq {σ : Type u_1} {f : σ option σ} {a b : σ} (e : f a = f b) :
theorem turing.reaches₁.to₀ {σ : Type u_1} {f : σ option σ} {a b : σ} (h : turing.reaches₁ f a b) :
theorem turing.reaches.to₀ {σ : Type u_1} {f : σ option σ} {a b : σ} (h : turing.reaches f a b) :
theorem turing.reaches₀.tail' {σ : Type u_1} {f : σ option σ} {a b c : σ} (h : turing.reaches₀ f a b) (h₂ : c f b) :
def turing.eval_induction {σ : Type u_1} {f : σ option σ} {b : σ} {C : σ Sort u_2} {a : σ} (h : b turing.eval f a) (H : Π (a : σ), b turing.eval f a (Π (a' : σ), f a = option.some a' C a') C a) :
C a

(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.

Equations
theorem turing.mem_eval {σ : Type u_1} {f : σ option σ} {a b : σ} :
theorem turing.eval_maximal₁ {σ : Type u_1} {f : σ option σ} {a b : σ} (h : b turing.eval f a) (c : σ) :
theorem turing.eval_maximal {σ : Type u_1} {f : σ option σ} {a b : σ} (h : b turing.eval f a) {c : σ} :
turing.reaches f b c c = b
theorem turing.reaches_eval {σ : Type u_1} {f : σ option σ} {a b : σ} (ab : turing.reaches f a b) :
def turing.respects {σ₁ : Type u_1} {σ₂ : Type u_2} (f₁ : σ₁ option σ₁) (f₂ : σ₂ option σ₂) (tr : σ₁ σ₂ Prop) :
Prop

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.

Equations
theorem turing.tr_reaches₁ {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂ Prop} (H : turing.respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) {b₁ : σ₁} (ab : turing.reaches₁ f₁ a₁ b₁) :
(b₂ : σ₂), tr b₁ b₂ turing.reaches₁ f₂ a₂ b₂
theorem turing.tr_reaches {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂ Prop} (H : turing.respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) {b₁ : σ₁} (ab : turing.reaches f₁ a₁ b₁) :
(b₂ : σ₂), tr b₁ b₂ turing.reaches f₂ a₂ b₂
theorem turing.tr_reaches_rev {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂ Prop} (H : turing.respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) {b₂ : σ₂} (ab : turing.reaches f₂ a₂ b₂) :
(c₁ : σ₁) (c₂ : σ₂), turing.reaches f₂ b₂ c₂ tr c₁ c₂ turing.reaches f₁ a₁ c₁
theorem turing.tr_eval {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂ Prop} (H : turing.respects f₁ f₂ tr) {a₁ b₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) (ab : b₁ turing.eval f₁ a₁) :
(b₂ : σ₂), tr b₁ b₂ b₂ turing.eval f₂ a₂
theorem turing.tr_eval_rev {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂ Prop} (H : turing.respects f₁ f₂ tr) {a₁ : σ₁} {b₂ a₂ : σ₂} (aa : tr a₁ a₂) (ab : b₂ turing.eval f₂ a₂) :
(b₁ : σ₁), tr b₁ b₂ b₁ turing.eval f₁ a₁
theorem turing.tr_eval_dom {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂ Prop} (H : turing.respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) :
(turing.eval f₂ a₂).dom (turing.eval f₁ a₁).dom
def turing.frespects {σ₁ : Type u_1} {σ₂ : Type u_2} (f₂ : σ₂ option σ₂) (tr : σ₁ σ₂) (a₂ : σ₂) :
option σ₁ Prop

A simpler version of respects when the state transition relation tr is a function.

Equations
theorem turing.frespects_eq {σ₁ : Type u_1} {σ₂ : Type u_2} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂} {a₂ b₂ : σ₂} (h : f₂ a₂ = f₂ b₂) {b₁ : option σ₁} :
turing.frespects f₂ tr a₂ b₁ turing.frespects f₂ tr b₂ b₁
theorem turing.fun_respects {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ option σ₁} {f₂ : σ₂ option σ₂} {tr : σ₁ σ₂} :
turing.respects f₁ f₂ (λ (a : σ₁) (b : σ₂), tr a = b) ⦃a₁ : σ₁⦄, turing.frespects f₂ tr (tr a₁) (f₁ a₁)
theorem turing.tr_eval' {σ₁ σ₂ : Type u_1} (f₁ : σ₁ option σ₁) (f₂ : σ₂ option σ₂) (tr : σ₁ σ₂) (H : turing.respects f₁ f₂ (λ (a : σ₁) (b : σ₂), tr a = b)) (a₁ : σ₁) :
turing.eval f₂ (tr a₁) = tr <$> turing.eval f₁ a₁

The TM0 model #

A TM0 turing machine is essentially a Post-Turing machine, adapted for type theory.

A Post-Turing machine with symbol type Γ and label type Λ is a function Λ → Γ → option (Λ × stmt), where a stmt can be either move left, move right or write a for a : Γ. The machine works over a "tape", a doubly-infinite sequence of elements of Γ, and an instantaneous configuration, cfg, is a label q : Λ indicating the current internal state of the machine, and a tape Γ (which is essentially ℤ →₀ Γ). The evolution is described by the step function:

The initial state takes a list Γ and produces a tape Γ where the head of the list is the head of the tape and the rest of the list extends to the right, with the left side all blank. The final state takes the entire right side of the tape right or equal to the current position of the machine. (This is actually a list_blank Γ, not a list Γ, because we don't know, at this level of generality, where the output ends. If equality to default : Γ is decidable we can trim the list to remove the infinite tail of blanks.)

inductive turing.TM0.stmt (Γ : Type u_1) [inhabited Γ] :
Type u_1

A Turing machine "statement" is just a command to either move left or right, or write a symbol on the tape.

Instances for turing.TM0.stmt
@[nolint]
def turing.TM0.machine (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) [inhabited Λ] :
Type (max u_2 u_1)

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.

Equations
Instances for turing.TM0.machine
@[protected, instance]
def turing.TM0.machine.inhabited (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) [inhabited Λ] :
Equations
structure turing.TM0.cfg (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) [inhabited Λ] :
Type (max u_1 u_2)

The configuration state of a Turing machine during operation consists of a label (machine state), and a tape, 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.

Instances for turing.TM0.cfg
@[protected, instance]
def turing.TM0.cfg.inhabited (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) [inhabited Λ] :
Equations
def turing.TM0.step {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] (M : turing.TM0.machine Γ Λ) :

Execution semantics of the Turing machine.

Equations
def turing.TM0.reaches {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] (M : turing.TM0.machine Γ Λ) :

The statement reaches M s₁ s₂ means that s₂ is obtained starting from s₁ after a finite number of steps from s₂.

Equations
def turing.TM0.init {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] (l : list Γ) :

The initial configuration.

Equations
def turing.TM0.eval {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] (M : turing.TM0.machine Γ Λ) (l : list Γ) :

Evaluate a Turing machine on initial input to a final state, if it terminates.

Equations
def turing.TM0.supports {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] (M : turing.TM0.machine Γ Λ) (S : set Λ) :
Prop

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.

Equations
theorem turing.TM0.step_supports {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] (M : turing.TM0.machine Γ Λ) {S : set Λ} (ss : turing.TM0.supports M S) {c c' : turing.TM0.cfg Γ Λ} :
c' turing.TM0.step M c c.q S c'.q S
def turing.TM0.stmt.map {Γ : Type u_1} [inhabited Γ] {Γ' : Type u_2} [inhabited Γ'] (f : turing.pointed_map Γ Γ') :

Map a TM statement across a function. This does nothing to move statements and maps the write values.

Equations
def turing.TM0.cfg.map {Γ : Type u_1} [inhabited Γ] {Γ' : Type u_2} [inhabited Γ'] {Λ : Type u_3} [inhabited Λ] {Λ' : Type u_4} [inhabited Λ'] (f : turing.pointed_map Γ Γ') (g : Λ Λ') :

Map a configuration across a function, given f : Γ → Γ' a map of the alphabets and g : Λ → Λ' a map of the machine states.

Equations
def turing.TM0.machine.map {Γ : Type u_1} [inhabited Γ] {Γ' : Type u_2} [inhabited Γ'] {Λ : Type u_3} [inhabited Λ] {Λ' : Type u_4} [inhabited Λ'] (M : turing.TM0.machine Γ Λ) (f₁ : turing.pointed_map Γ Γ') (f₂ : turing.pointed_map Γ' Γ) (g₁ : Λ Λ') (g₂ : Λ' Λ) :

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.

Equations
theorem turing.TM0.machine.map_step {Γ : Type u_1} [inhabited Γ] {Γ' : Type u_2} [inhabited Γ'] {Λ : Type u_3} [inhabited Λ] {Λ' : Type u_4} [inhabited Λ'] (M : turing.TM0.machine Γ Λ) (f₁ : turing.pointed_map Γ Γ') (f₂ : turing.pointed_map Γ' Γ) (g₁ : Λ Λ') (g₂ : Λ' Λ) {S : set Λ} (f₂₁ : function.right_inverse f₁ f₂) (g₂₁ : (q : Λ), q S g₂ (g₁ q) = q) (c : turing.TM0.cfg Γ Λ) :
c.q S option.map (turing.TM0.cfg.map f₁ g₁) (turing.TM0.step M c) = turing.TM0.step (M.map f₁ f₂ g₁ g₂) (turing.TM0.cfg.map f₁ g₁ c)
theorem turing.TM0.map_init {Γ : Type u_1} [inhabited Γ] {Γ' : Type u_2} [inhabited Γ'] {Λ : Type u_3} [inhabited Λ] {Λ' : Type u_4} [inhabited Λ'] (f₁ : turing.pointed_map Γ Γ') (g₁ : turing.pointed_map Λ Λ') (l : list Γ) :
theorem turing.TM0.machine.map_respects {Γ : Type u_1} [inhabited Γ] {Γ' : Type u_2} [inhabited Γ'] {Λ : Type u_3} [inhabited Λ] {Λ' : Type u_4} [inhabited Λ'] (M : turing.TM0.machine Γ Λ) (f₁ : turing.pointed_map Γ Γ') (f₂ : turing.pointed_map Γ' Γ) (g₁ : turing.pointed_map Λ Λ') (g₂ : Λ' Λ) {S : set Λ} (ss : turing.TM0.supports M S) (f₂₁ : function.right_inverse f₁ f₂) (g₂₁ : (q : Λ), q S g₂ (g₁ q) = q) :
turing.respects (turing.TM0.step M) (turing.TM0.step (M.map f₁ f₂ g₁ g₂)) (λ (a : turing.TM0.cfg Γ Λ) (b : turing.TM0.cfg Γ' Λ'), a.q S turing.TM0.cfg.map f₁ g₁ a = b)

The TM1 model #

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. Most of the regular commands are allowed to use the current value a of the local variables and the value T.head on the tape to calculate what to write or how to change local state, but the statements themselves have a fixed structure. The stmts can be as follows:

Note that here most statements do not have labels; goto commands can only go to a new function. Only the goto and halt statements actually take a step; the rest is done by recursion on statements and so take 0 steps. (There is a uniform bound on many statements can be executed before the next goto, so this is an O(1) speedup with the constant depending on the machine.)

The halt command has a one step stutter before actually halting so that any changes made before the halt have a chance to be "committed", since the eval relation uses the final configuration before the halt as the output, and move and write etc. take 0 steps in this model.

inductive turing.TM1.stmt (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) (σ : Type u_3) :
Type (max u_1 u_2 u_3)

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.

Instances for turing.TM1.stmt
@[protected, instance]
def turing.TM1.stmt.inhabited (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) (σ : Type u_3) :
Equations
structure turing.TM1.cfg (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) (σ : Type u_3) :
Type (max u_1 u_2 u_3)

The configuration of a TM1 machine is given by the currently evaluating statement, the variable store value, and the tape.

Instances for turing.TM1.cfg
@[protected, instance]
def turing.TM1.cfg.inhabited (Γ : Type u_1) [inhabited Γ] (Λ : Type u_2) (σ : Type u_3) [inhabited σ] :
Equations
def turing.TM1.step {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} (M : Λ turing.TM1.stmt Γ Λ σ) :

The state transition function.

Equations
def turing.TM1.supports_stmt {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} (S : finset Λ) :
turing.TM1.stmt Γ Λ σ Prop

A set S of labels supports the statement q if all the goto statements in q refer only to other functions in S.

Equations
theorem turing.TM1.stmts₁_self {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {q : turing.TM1.stmt Γ Λ σ} :
theorem turing.TM1.stmts₁_trans {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {q₁ q₂ : turing.TM1.stmt Γ Λ σ} :
theorem turing.TM1.stmts₁_supports_stmt_mono {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {S : finset Λ} {q₁ q₂ : turing.TM1.stmt Γ Λ σ} (h : q₁ turing.TM1.stmts₁ q₂) (hs : turing.TM1.supports_stmt S q₂) :
noncomputable def turing.TM1.stmts {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} (M : Λ turing.TM1.stmt Γ Λ σ) (S : finset Λ) :

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.

Equations
theorem turing.TM1.stmts_trans {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {M : Λ turing.TM1.stmt Γ Λ σ} {S : finset Λ} {q₁ q₂ : turing.TM1.stmt Γ Λ σ} (h₁ : q₁ turing.TM1.stmts₁ q₂) :
def turing.TM1.supports {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [inhabited Λ] (M : Λ turing.TM1.stmt Γ Λ σ) (S : finset Λ) :
Prop

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.

Equations
theorem turing.TM1.stmts_supports_stmt {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [inhabited Λ] {M : Λ turing.TM1.stmt Γ Λ σ} {S : finset Λ} {q : turing.TM1.stmt Γ Λ σ} (ss : turing.TM1.supports M S) :
theorem turing.TM1.step_supports {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [inhabited Λ] (M : Λ turing.TM1.stmt Γ Λ σ) {S : finset Λ} (ss : turing.TM1.supports M S) {c c' : turing.TM1.cfg Γ Λ σ} :
def turing.TM1.init {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [inhabited Λ] [inhabited σ] (l : list Γ) :

The initial state, given a finite input that is placed on the tape starting at the TM head and going to the right.

Equations
def turing.TM1.eval {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [inhabited Λ] [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) (l : list Γ) :

Evaluate a TM to completion, resulting in an output list on the tape (with an indeterminate number of blanks on the end).

Equations

TM1 emulator in TM0 #

To prove that TM1 computable functions are TM0 computable, we need to reduce each TM1 program to a TM0 program. So suppose a TM1 program is given. We take the following:

Even though stmt₁ contains a statement called halt, we must separate it from none (some halt steps to none and none actually halts) because there is a one step stutter in the TM1 semantics.

@[nolint]
def turing.TM1to0.Λ' {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) :
Type (max (max u_1 u_2 u_3) u_3)

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.

Equations
Instances for turing.TM1to0.Λ'
@[protected, instance]
def turing.TM1to0.Λ'.inhabited {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) :
Equations
def turing.TM1to0.tr_aux {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) (s : Γ) :

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.

Equations
def turing.TM1to0.tr {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) :

The translated TM0 machine (given the TM1 machine input).

Equations
def turing.TM1to0.tr_cfg {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) :

Translate configurations from TM1 to TM0.

Equations
theorem turing.TM1to0.tr_respects {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) :
theorem turing.TM1to0.tr_eval {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) (l : list Γ) :
noncomputable def turing.TM1to0.tr_stmts {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) [fintype σ] (S : finset Λ) :

Given a finite set of accessible Λ machine states, there is a finite set of accessible machine states in the target (even though the type Λ' is infinite).

Equations
theorem turing.TM1to0.tr_supports {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) [fintype σ] {S : finset Λ} (ss : turing.TM1.supports M S) :

TM1(Γ) emulator in TM1(bool) #

The most parsimonious Turing machine model that is still Turing complete is TM0 with Γ = bool. Because our construction in the previous section reducing TM1 to TM0 doesn't change the alphabet, we can do the alphabet reduction on TM1 instead of TM0 directly.

The basic idea is to use a bijection between Γ and a subset of vector bool n, where n is a fixed constant. Each tape element is represented as a block of n bools. Whenever the machine wants to read a symbol from the tape, it traverses over the block, performing n branch instructions to each any of the 2^n results.

For the write instruction, we have to use a goto because we need to follow a different code path depending on the local state, which is not available in the TM1 model, so instead we jump to a label computed using the read value and the local state, which performs the writing and returns to normal execution.

Emulation overhead is O(1). If not for the above write behavior it would be 1-1 because we are exploiting the 0-step behavior of regular commands to avoid taking steps, but there are nevertheless a bounded number of write calls between goto statements because TM1 statements are finitely long.

theorem turing.TM1to1.exists_enc_dec {Γ : Type u_1} [inhabited Γ] [fintype Γ] :
(n : ) (enc : Γ vector bool n) (dec : vector bool n Γ), enc inhabited.default = vector.replicate n bool.ff (a : Γ), dec (enc a) = a
inductive turing.TM1to1.Λ' {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] :
Type (max u_1 u_2 u_3)

The configuration state of the TM.

Instances for turing.TM1to1.Λ'
def turing.TM1to1.move {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } (d : turing.dir) (q : turing.TM1.stmt bool turing.TM1to1.Λ' σ) :

A move left or right corresponds to n moves across the super-cell.

Equations
def turing.TM1to1.read {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } (dec : vector bool n Γ) (f : Γ turing.TM1.stmt bool turing.TM1to1.Λ' σ) :

To read a symbol from the tape, we use read_aux to traverse the symbol, then return to the original position with n moves to the left.

Equations
def turing.TM1to1.tr_normal {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } (dec : vector bool n Γ) :

Translate a normal instruction. For the write command, we use a goto indirection so that we can access the current value of the tape.

Equations
def turing.TM1to1.tr_tape' {Γ : Type u_1} [inhabited Γ] {n : } {enc : Γ vector bool n} (enc0 : enc inhabited.default = vector.replicate n bool.ff) (L R : turing.list_blank Γ) :

The low level tape corresponding to the given tape over alphabet Γ.

Equations
def turing.TM1to1.tr_tape {Γ : Type u_1} [inhabited Γ] {n : } {enc : Γ vector bool n} (enc0 : enc inhabited.default = vector.replicate n bool.ff) (T : turing.tape Γ) :

The low level tape corresponding to the given tape over alphabet Γ.

Equations
def turing.TM1to1.tr {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } (enc : Γ vector bool n) (dec : vector bool n Γ) (M : Λ turing.TM1.stmt Γ Λ σ) :

The top level program.

Equations
def turing.TM1to1.tr_cfg {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } {enc : Γ vector bool n} (enc0 : enc inhabited.default = vector.replicate n bool.ff) :

The machine configuration translation.

Equations
theorem turing.TM1to1.step_aux_read {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } {enc : Γ vector bool n} (dec : vector bool n Γ) (enc0 : enc inhabited.default = vector.replicate n bool.ff) (encdec : (a : Γ), dec (enc a) = a) (f : Γ turing.TM1.stmt bool turing.TM1to1.Λ' σ) (v : σ) (L R : turing.list_blank Γ) :
theorem turing.TM1to1.tr_respects {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } {enc : Γ vector bool n} (dec : vector bool n Γ) (enc0 : enc inhabited.default = vector.replicate n bool.ff) (M : Λ turing.TM1.stmt Γ Λ σ) (encdec : (a : Γ), dec (enc a) = a) :
noncomputable def turing.TM1to1.tr_supp {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] (M : Λ turing.TM1.stmt Γ Λ σ) [fintype Γ] (S : finset Λ) :

The set of accessible machine states, assuming that the input machine is supported on S, are the normal states embedded from S, plus all write states accessible from these states.

Equations
theorem turing.TM1to1.tr_supports {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] {σ : Type u_3} [inhabited σ] {n : } {enc : Γ vector bool n} (dec : vector bool n Γ) (M : Λ turing.TM1.stmt Γ Λ σ) [fintype Γ] {S : finset Λ} (ss : turing.TM1.supports M S) :

TM0 emulator in TM1 #

To establish that TM0 and TM1 are equivalent computational models, we must also have a TM0 emulator in TM1. The main complication here is that TM0 allows an action to depend on the value at the head and local state, while TM1 doesn't (in order to have more programming language-like semantics). So we use a computed goto to go to a state that performes the desired action and then returns to normal execution.

One issue with this is that the halt instruction is supposed to halt immediately, not take a step to a halting state. To resolve this we do a check for halt first, then goto (with an unreachable branch).

inductive turing.TM0to1.Λ' {Γ : Type u_1} [inhabited Γ] {Λ : Type u_2} [inhabited Λ] :
Type (max u_1 u_2)

The machine states for a TM1 emulating a TM0 machine. States of the TM0 machine are embedded as normal q states, but the actual operation is split into two parts, a jump to act s q followed by the action and a jump to the next normal state.

Instances for turing.TM0to1.Λ'

The program.

Equations

The configuration translation.

Equations

The TM2 model #

The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks, each with elements of different types (the alphabet of stack k : K is Γ k). The statements are:

The configuration is a tuple (l, var, stk) where l : option Λ is the current label to run or none for the halting state, var : σ is the (finite) internal state, and stk : ∀ k, list (Γ k) is the collection of stacks. (Note that unlike the TM0 and TM1 models, these are not list_blanks, they have definite ends that can be detected by the pop command.)

Given a designated stack k and a value L : list (Γ k), the initial configuration has all the stacks empty except the designated "input" stack; in eval this designated stack also functions as the output stack.

inductive turing.TM2.stmt {K : Type u_1} [decidable_eq K] (Γ : K Type u_2) (Λ : Type u_3) (σ : Type u_4) :
Type (max u_1 u_2 u_3 u_4)

The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks. The operation push puts an element on one of the stacks, and pop removes an element from a stack (and modifying the internal state based on the result). peek modifies the internal state but does not remove an element.

Instances for turing.TM2.stmt
@[protected, instance]
def turing.TM2.stmt.inhabited {K : Type u_1} [decidable_eq K] (Γ : K Type u_2) (Λ : Type u_3) (σ : Type u_4) :
Equations
structure turing.TM2.cfg {K : Type u_1} [decidable_eq K] (Γ : K Type u_2) (Λ : Type u_3) (σ : Type u_4) :
Type (max u_1 u_2 u_3 u_4)

A configuration in the TM2 model is a label (or none for the halt state), the state of local variables, and the stacks. (Note that the stacks are not list_blanks, they have a definite size.)

Instances for turing.TM2.cfg
@[protected, instance]
def turing.TM2.cfg.inhabited {K : Type u_1} [decidable_eq K] (Γ : K Type u_2) (Λ : Type u_3) (σ : Type u_4) [inhabited σ] :
Equations
@[simp]
def turing.TM2.step_aux {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} :
turing.TM2.stmt Γ Λ σ σ (Π (k : K), list (Γ k)) turing.TM2.cfg Γ Λ σ

The step function for the TM2 model.

Equations
@[simp]
def turing.TM2.step {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} (M : Λ turing.TM2.stmt Γ Λ σ) :

The step function for the TM2 model.

Equations
def turing.TM2.reaches {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} (M : Λ turing.TM2.stmt Γ Λ σ) :
turing.TM2.cfg Γ Λ σ turing.TM2.cfg Γ Λ σ Prop

The (reflexive) reachability relation for the TM2 model.

Equations
theorem turing.TM2.stmts₁_self {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} {q : turing.TM2.stmt Γ Λ σ} :
theorem turing.TM2.stmts₁_trans {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} {q₁ q₂ : turing.TM2.stmt Γ Λ σ} :
theorem turing.TM2.stmts₁_supports_stmt_mono {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} {S : finset Λ} {q₁ q₂ : turing.TM2.stmt Γ Λ σ} (h : q₁ turing.TM2.stmts₁ q₂) (hs : turing.TM2.supports_stmt S q₂) :
noncomputable def turing.TM2.stmts {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} (M : Λ turing.TM2.stmt Γ Λ σ) (S : finset Λ) :

The set of statements accessible from initial set S of labels.

Equations
theorem turing.TM2.stmts_trans {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} {M : Λ turing.TM2.stmt Γ Λ σ} {S : finset Λ} {q₁ q₂ : turing.TM2.stmt Γ Λ σ} (h₁ : q₁ turing.TM2.stmts₁ q₂) :
def turing.TM2.supports {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} [inhabited Λ] (M : Λ turing.TM2.stmt Γ Λ σ) (S : finset Λ) :
Prop

Given a TM2 machine M and a set S of states, supports M S means that all states in S jump only to other states in S.

Equations
theorem turing.TM2.stmts_supports_stmt {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} [inhabited Λ] {M : Λ turing.TM2.stmt Γ Λ σ} {S : finset Λ} {q : turing.TM2.stmt Γ Λ σ} (ss : turing.TM2.supports M S) :
theorem turing.TM2.step_supports {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} [inhabited Λ] (M : Λ turing.TM2.stmt Γ Λ σ) {S : finset Λ} (ss : turing.TM2.supports M S) {c c' : turing.TM2.cfg Γ Λ σ} :
def turing.TM2.init {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} [inhabited Λ] [inhabited σ] (k : K) (L : list (Γ k)) :

The initial state of the TM2 model. The input is provided on a designated stack.

Equations
def turing.TM2.eval {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} {σ : Type u_4} [inhabited Λ] [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) (k : K) (L : list (Γ k)) :
part (list (Γ k))

Evaluates a TM2 program to completion, with the output on the same stack as the input.

Equations

TM2 emulator in TM1 #

To prove that TM2 computable functions are TM1 computable, we need to reduce each TM2 program to a TM1 program. So suppose a TM2 program is given. This program has to maintain a whole collection of stacks, but we have only one tape, so we must "multiplex" them all together. Pictorially, if stack 1 contains [a, b] and stack 2 contains [c, d, e, f] then the tape looks like this:

 bottom:  ... | _ | T | _ | _ | _ | _ | ...
 stack 1: ... | _ | b | a | _ | _ | _ | ...
 stack 2: ... | _ | f | e | d | c | _ | ...

where a tape element is a vertical slice through the diagram. Here the alphabet is Γ' := bool × ∀ k, option (Γ k), where:

In "resting" position, the TM is sitting at the position marked bottom. For non-stack actions, it operates in place, but for the stack actions push, peek, and pop, it must shuttle to the end of the appropriate stack, make its changes, and then return to the bottom. So the states are:

Because of the shuttling, emulation overhead is O(n), where n is the current maximum of the length of all stacks. Therefore a program that takes k steps to run in TM2 takes O((m+k)k) steps to run when emulated in TM1, where m is the length of the input.

theorem turing.TM2to1.stk_nth_val {K : Type u_1} {Γ : K Type u_2} {L : turing.list_blank (Π (k : K), option (Γ k))} {k : K} {S : list (Γ k)} (n : ) (hL : turing.list_blank.map (turing.proj k) L = turing.list_blank.mk (list.map option.some S).reverse) :
L.nth n k = S.reverse.nth n
@[nolint]
def turing.TM2to1.Γ' {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} :
Type (max u_1 u_2)

The alphabet of the TM2 simulator on TM1 is a marker for the stack bottom, plus a vector of stack elements for each stack, or none if the stack does not extend this far.

Equations
Instances for turing.TM2to1.Γ'
@[protected, instance]
Equations
@[protected, instance]
def turing.TM2to1.Γ'.fintype {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} [fintype K] [Π (k : K), fintype (Γ k)] :
Equations

The bottom marker is fixed throughout the calculation, so we use the add_bottom function to express the program state in terms of a tape with only the stacks themselves.

Equations
theorem turing.TM2to1.add_bottom_map {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} (L : turing.list_blank (Π (k : K), option (Γ k))) :
theorem turing.TM2to1.add_bottom_modify_nth {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} (f : (Π (k : K), option (Γ k)) Π (k : K), option (Γ k)) (L : turing.list_blank (Π (k : K), option (Γ k))) (n : ) :
theorem turing.TM2to1.add_bottom_nth_snd {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} (L : turing.list_blank (Π (k : K), option (Γ k))) (n : ) :
theorem turing.TM2to1.add_bottom_nth_succ_fst {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} (L : turing.list_blank (Π (k : K), option (Γ k))) (n : ) :
inductive turing.TM2to1.st_act {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {σ : Type u_4} [inhabited σ] (k : K) :
Type (max u_2 u_4)

A stack action is a command that interacts with the top of a stack. Our default position is at the bottom of all the stacks, so we have to hold on to this action while going to the end to modify the stack.

Instances for turing.TM2to1.st_act
@[protected, instance]
def turing.TM2to1.st_act.inhabited {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {σ : Type u_4} [inhabited σ] {k : K} :
Equations
def turing.TM2to1.st_var {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {σ : Type u_4} [inhabited σ] {k : K} (v : σ) (l : list (Γ k)) :

The effect of a stack action on the local variables, given the value of the stack.

Equations
def turing.TM2to1.st_write {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {σ : Type u_4} [inhabited σ] {k : K} (v : σ) (l : list (Γ k)) :

The effect of a stack action on the stack.

Equations
def turing.TM2to1.stmt_st_rec {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] {C : turing.TM2.stmt Γ Λ σ Sort l} (H₁ : Π (k : K) (s : turing.TM2to1.st_act k) (q : turing.TM2.stmt Γ Λ σ), C q C (turing.TM2to1.st_run s q)) (H₂ : Π (a : σ σ) (q : turing.TM2.stmt Γ Λ σ), C q C (turing.TM2.stmt.load a q)) (H₃ : Π (p : σ bool) (q₁ q₂ : turing.TM2.stmt Γ Λ σ), C q₁ C q₂ C (turing.TM2.stmt.branch p q₁ q₂)) (H₄ : Π (l : σ Λ), C (turing.TM2.stmt.goto l)) (H₅ : C turing.TM2.stmt.halt) (n : turing.TM2.stmt Γ Λ σ) :
C n

We have partitioned the TM2 statements into "stack actions", which require going to the end of the stack, and all other actions, which do not. This is a modified recursor which lumps the stack actions into one.

Equations
theorem turing.TM2to1.supports_run {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (S : finset Λ) {k : K} (s : turing.TM2to1.st_act k) (q : turing.TM2.stmt Γ Λ σ) :
inductive turing.TM2to1.Λ' {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] :
Type (max u_1 u_2 u_3 u_4)

The machine states of the TM2 emulator. We can either be in a normal state when waiting for the next TM2 action, or we can be in the "go" and "return" states to go to the top of the stack and return to the bottom, respectively.

Instances for turing.TM2to1.Λ'

The program corresponding to state transitions at the end of a stack. Here we start out just after the top of the stack, and should end just after the new top of the stack.

Equations
def turing.TM2to1.tr_init {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} (k : K) (L : list (Γ k)) :

The initial state for the TM2 emulator, given an initial TM2 state. All stacks start out empty except for the input stack, and the stack bottom mark is set at the head.

Equations
theorem turing.TM2to1.step_run {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] {k : K} (q : turing.TM2.stmt Γ Λ σ) (v : σ) (S : Π (k : K), list (Γ k)) (s : turing.TM2to1.st_act k) :
theorem turing.TM2to1.tr_normal_run {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] {k : K} (s : turing.TM2to1.st_act k) (q : turing.TM2.stmt Γ Λ σ) :
def turing.TM2to1.tr {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) :

The TM2 emulator machine states written as a TM1 program. This handles the go and ret states, which shuttle to and from a stack top.

Equations
inductive turing.TM2to1.tr_cfg {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) :

The relation between TM2 configurations and TM1 configurations of the TM2 emulator.

theorem turing.TM2to1.tr_respects {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) :
theorem turing.TM2to1.tr_cfg_init {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) (k : K) (L : list (Γ k)) :
theorem turing.TM2to1.tr_eval_dom {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) (k : K) (L : list (Γ k)) :
theorem turing.TM2to1.tr_eval {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) (k : K) (L : list (Γ k)) {L₁ : turing.list_blank turing.TM2to1.Γ'} {L₂ : list (Γ k)} (H₁ : L₁ turing.TM1.eval (turing.TM2to1.tr M) (turing.TM2to1.tr_init k L)) (H₂ : L₂ turing.TM2.eval M k L) :
(S : Π (k : K), list (Γ k)) (L' : turing.list_blank (Π (k : K), option (Γ k))), turing.TM2to1.add_bottom L' = L₁ ( (k : K), turing.list_blank.map (turing.proj k) L' = turing.list_blank.mk (list.map option.some (S k)).reverse) S k = L₂
noncomputable def turing.TM2to1.tr_supp {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) (S : finset Λ) :

The support of a set of TM2 states in the TM2 emulator.

Equations
theorem turing.TM2to1.tr_supports {K : Type u_1} [decidable_eq K] {Γ : K Type u_2} {Λ : Type u_3} [inhabited Λ] {σ : Type u_4} [inhabited σ] (M : Λ turing.TM2.stmt Γ Λ σ) {S : finset Λ} (ss : turing.TM2.supports M S) :