roseking's picture
Add lean source and binary files (part 64)
92416a2 verified
Raw History Blame Contribute Delete
6.63 kB
import Mathlib
namespace OAI
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
section
namespace UniqueGames.Foundations.Complexity.MachineEmbedding
open Turing.TM2
variable {K E Λ Λextra σ τ : Type} {Γ : K → Type} {Δ : E → Type}
abbrev Alphabet (Γ : K → Type) (Δ : E → Type) : K ⊕ E → Type
| .inl k => Γ k
| .inr e => Δ e
def tapes (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e)) :
∀ j, List (Alphabet Γ Δ j)
| .inl k => source k
| .inr e => extra e
@[simp] theorem tapes_inl (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e))
(k : K) : tapes source extra (.inl k) = source k := rfl
@[simp] theorem tapes_inr (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e))
(e : E) : tapes source extra (.inr e) = extra e := rfl
def label (haltTarget : Option (Λ ⊕ Λextra)) : Option Λ → Option (Λ ⊕ Λextra)
| none => haltTarget
| some l => some (.inl l)
def configuration (haltTarget : Option (Λ ⊕ Λextra)) (extraState : τ)
(extraTapes : ∀ e, List (Δ e)) («c» : Cfg Γ Λ σ) :
Cfg (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ) where
l := label haltTarget «c».l
var := («c».var, extraState)
stk := tapes «c».stk extraTapes
def statement (haltTarget : Option (Λ ⊕ Λextra)) :
Stmt Γ Λ σ → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)
| .push k f next => .push (.inl k) (fun st => f st.1) (statement haltTarget next)
| .peek k f next => .peek (.inl k) (fun st v => (f st.1 v, st.2))
(statement haltTarget next)
| .pop k f next => .pop (.inl k) (fun st v => (f st.1 v, st.2))
(statement haltTarget next)
| .load f next => .load (fun st => (f st.1, st.2)) (statement haltTarget next)
| .branch f yes no => .branch (fun st => f st.1)
(statement haltTarget yes) (statement haltTarget no)
| .goto f => .goto (fun st => .inl (f st.1))
| .halt => match haltTarget with
| none => .halt
| some l => .goto (fun _ => l)
variable [DecidableEq K] [DecidableEq E]
theorem tapes_update (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e))
(k : K) (value : List (Γ k)) :
tapes (Function.update source k value) extra =
Function.update (tapes source extra) (.inl k) value := by
funext j
cases j with
| inl j =>
by_cases h : j = k
· subst j
simp [Function.update]
· simp [Function.update, h]
| inr e => simp [Function.update]
theorem stepAux_simulation (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) :
stepAux (statement haltTarget q) (state, extraState) (tapes source extraTapes) =
configuration haltTarget extraState extraTapes (stepAux q state source) := by
induction q generalizing state source with
| push k f next ih =>
simp only [statement, stepAux, tapes_inl]
rw [← tapes_update]
exact ih state (Function.update source k (f state :: source k))
| peek k f next ih =>
simpa only [statement, stepAux, tapes_inl] using
ih (f state (source k).head?) source
| pop k f next ih =>
simp only [statement, stepAux, tapes_inl]
rw [← tapes_update]
exact ih (f state (source k).head?) (Function.update source k (source k).tail)
| load f next ih =>
simpa only [statement, stepAux] using ih (f state) source
| branch f yes no ihYes ihNo =>
cases h : f state with
| false => simpa only [statement, stepAux, h, Bool.cond_false] using ihNo state source
| true => simpa only [statement, stepAux, h, Bool.cond_true] using ihYes state source
| goto f => rfl
| halt => cases haltTarget <;> rfl
theorem stepAux_preserves_extra_state (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) :
(stepAux (statement haltTarget q) (state, extraState)
(tapes source extraTapes)).var.2 = extraState := by
rw [stepAux_simulation]
rfl
theorem stepAux_preserves_extra_tape (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) (e : E) :
(stepAux (statement haltTarget q) (state, extraState)
(tapes source extraTapes)).stk (.inr e) = extraTapes e := by
rw [stepAux_simulation]
rfl
def program (haltTarget : Option (Λ ⊕ Λextra)) (source : Λ → Stmt Γ Λ σ)
(extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)) :
Λ ⊕ Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)
| .inl l => statement haltTarget (source l)
| .inr l => extra l
theorem step_running (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(source : Λ → Stmt Γ Λ σ)
(extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ))
(l : Λ) (state : σ) (sourceTapes : ∀ k, List (Γ k)) :
step (program haltTarget source extra)
(configuration haltTarget extraState extraTapes ⟨some l, state, sourceTapes⟩) =
some (configuration haltTarget extraState extraTapes
(stepAux (source l) state sourceTapes)) := by
change some (stepAux (statement haltTarget (source l)) (state, extraState)
(tapes sourceTapes extraTapes)) = _
rw [stepAux_simulation]
theorem step_simulation (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(source : Λ → Stmt Γ Λ σ)
(extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ))
(a b : Cfg Γ Λ σ) (h : step source a = some b) :
step (program haltTarget source extra)
(configuration haltTarget extraState extraTapes a) =
some (configuration haltTarget extraState extraTapes b) := by
cases a with
| mk l state sourceTapes =>
cases l with
| none => simp [step] at h
| some l =>
have hb : stepAux (source l) state sourceTapes = b := Option.some.inj h
rw [← hb]
exact step_running haltTarget extraState extraTapes source extra l state sourceTapes
end UniqueGames.Foundations.Complexity.MachineEmbedding
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end OAI