cfahlgren1/math / lean /OAI /Computability /MaxCut /Machines /MachineStateFrame.lean
cfahlgren1's picture
download
raw
9.93 kB
import Mathlib
import OAI.Computability.MaxCut.Machines.MachineSubroutine
namespace OAI
namespace MaxCutGames.Foundations.Complexity.MachineStateFrame
open Turing
variable {K Λ Λ' σ τ : Type} {Γ : K → Type}
/-- Same code and stacks, with an untouched caller-register component. -/
def frameStatement : TM2.Stmt Γ Λ σ → TM2.Stmt Γ Λ (σ × τ)
| .push k f next => .push k (fun state => f state.1) (frameStatement next)
| .peek k f next => .peek k (fun state bit => (f state.1 bit, state.2))
(frameStatement next)
| .pop k f next => .pop k (fun state bit => (f state.1 bit, state.2))
(frameStatement next)
| .load f next => .load (fun state => (f state.1, state.2)) (frameStatement next)
| .branch f yes no => .branch (fun state => f state.1)
(frameStatement yes) (frameStatement no)
| .goto f => .goto (fun state => f state.1)
| .halt => .halt
def frameConfiguration (ambient : τ) (c : TM2.Cfg Γ Λ σ) : TM2.Cfg Γ Λ (σ × τ) :=
⟨c.l, (c.var, ambient), c.stk⟩
def frameProgram (source : Λ → TM2.Stmt Γ Λ σ) : Λ → TM2.Stmt Γ Λ (σ × τ) :=
fun label => frameStatement (source label)
@[simp] theorem frameConfiguration_state (ambient : τ) (c : TM2.Cfg Γ Λ σ) :
(frameConfiguration ambient c).var.1 = c.var := rfl
@[simp] theorem frameConfiguration_metadata (ambient : τ) (c : TM2.Cfg Γ Λ σ) :
(frameConfiguration ambient c).var.2 = ambient := rfl
@[simp] theorem frameConfiguration_tapes (ambient : τ) (c : TM2.Cfg Γ Λ σ) :
(frameConfiguration ambient c).stk = c.stk := rfl
theorem frameStatement_pushBound (q : TM2.Stmt Γ Λ σ) :
Runtime.statementPushBound (frameStatement (τ := τ) q) = Runtime.statementPushBound q := by
induction q <;> simp_all only [frameStatement, Runtime.statementPushBound]
/-- Optional continuation and labels use the existing subroutine semantics. -/
def statement (labels : Λ → Λ') (exit : Option Λ') (q : TM2.Stmt Γ Λ σ) :
TM2.Stmt Γ Λ' (σ × τ) :=
MachineSubroutine.statement labels exit (frameStatement q)
def configuration (labels : Λ → Λ') (exit : Option Λ') (ambient : τ)
(c : TM2.Cfg Γ Λ σ) : TM2.Cfg Γ Λ' (σ × τ) :=
MachineSubroutine.configuration labels exit (frameConfiguration ambient c)
@[simp] theorem configuration_state (labels : Λ → Λ') (exit : Option Λ')
(ambient : τ) (c : TM2.Cfg Γ Λ σ) :
(configuration labels exit ambient c).var.1 = c.var := rfl
@[simp] theorem configuration_metadata (labels : Λ → Λ') (exit : Option Λ')
(ambient : τ) (c : TM2.Cfg Γ Λ σ) :
(configuration labels exit ambient c).var.2 = ambient := rfl
@[simp] theorem configuration_tapes (labels : Λ → Λ') (exit : Option Λ')
(ambient : τ) (c : TM2.Cfg Γ Λ σ) :
(configuration labels exit ambient c).stk = c.stk := rfl
theorem statement_pushBound (labels : Λ → Λ') (exit : Option Λ')
(q : TM2.Stmt Γ Λ σ) :
Runtime.statementPushBound (statement (τ := τ) labels exit q) =
Runtime.statementPushBound q := by
induction q <;> simp_all only [statement, frameStatement,
MachineSubroutine.statement, Runtime.statementPushBound]
cases exit <;> rfl
variable [DecidableEq K]
theorem frame_stepAux (q : TM2.Stmt Γ Λ σ) (state : σ) (ambient : τ)
(tapes : ∀ k, List (Γ k)) :
TM2.stepAux (frameStatement q) (state, ambient) tapes =
frameConfiguration ambient (TM2.stepAux q state tapes) := by
induction q generalizing state tapes with
| push k f next ih =>
simpa only [frameStatement, TM2.stepAux] using
ih state (Function.update tapes k (f state :: tapes k))
| peek k f next ih =>
simpa only [frameStatement, TM2.stepAux] using ih (f state (tapes k).head?) tapes
| pop k f next ih =>
simpa only [frameStatement, TM2.stepAux] using
ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail)
| load f next ih => simpa only [frameStatement, TM2.stepAux] using ih (f state) tapes
| branch f yes no ihYes ihNo =>
cases h : f state with
| false => simpa only [frameStatement, TM2.stepAux, h, Bool.cond_false] using ihNo state tapes
| true => simpa only [frameStatement, TM2.stepAux, h, Bool.cond_true] using ihYes state tapes
| goto f => rfl
| halt => rfl
/-- This identity also covers the final halted configuration. -/
theorem frame_step (source : Λ → TM2.Stmt Γ Λ σ) (ambient : τ)
(c : TM2.Cfg Γ Λ σ) :
TM2.step (frameProgram source) (frameConfiguration ambient c) =
(TM2.step source c).map (frameConfiguration ambient) := by
rcases c with ⟨label, state, tapes⟩
cases label with
| none => rfl
| some label =>
change some (TM2.stepAux (frameStatement (source label)) (state, ambient) tapes) = _
rw [frame_stepAux]
rfl
theorem frame_advance (source : Λ → TM2.Stmt Γ Λ σ) (ambient : τ)
(c : Option (TM2.Cfg Γ Λ σ)) :
MachineComposition.advance (TM2.step (frameProgram source))
(c.map (frameConfiguration ambient)) =
(MachineComposition.advance (TM2.step source) c).map (frameConfiguration ambient) := by
cases c with
| none => rfl
| some c => exact frame_step source ambient c
theorem frame_iterate (source : Λ → TM2.Stmt Γ Λ σ) (ambient : τ)
(steps : ℕ) (c : Option (TM2.Cfg Γ Λ σ)) :
(MachineComposition.advance (TM2.step (frameProgram source)))^[steps]
(c.map (frameConfiguration ambient)) =
((MachineComposition.advance (TM2.step source))^[steps] c).map
(frameConfiguration ambient) := by
induction steps with
| zero => rfl
| succ steps ih =>
rw [Function.iterate_succ_apply', Function.iterate_succ_apply', ih]
exact frame_advance source ambient _
/-- Actual execution with the same step count, budget, and tapes. -/
def frameExecution (source : Λ → TM2.Stmt Γ Λ σ) (ambient : τ)
{start : TM2.Cfg Γ Λ σ} {finish : Option (TM2.Cfg Γ Λ σ)} {budget : ℕ}
(run : StateTransition.EvalsToInTime (TM2.step source) start finish budget) :
StateTransition.EvalsToInTime (TM2.step (frameProgram source))
(frameConfiguration ambient start) (finish.map (frameConfiguration ambient)) budget where
steps := run.steps
evals_in_steps := by
have h := run.evals_in_steps
change (MachineComposition.advance (TM2.step source))^[run.steps] (some start) = finish at h
change (MachineComposition.advance (TM2.step (frameProgram source)))^[run.steps]
((some start).map (frameConfiguration ambient)) = finish.map (frameConfiguration ambient)
rw [frame_iterate, h]
steps_le_m := run.steps_le_m
@[simp] theorem frameExecution_steps (source : Λ → TM2.Stmt Γ Λ σ) (ambient : τ)
{start : TM2.Cfg Γ Λ σ} {finish : Option (TM2.Cfg Γ Λ σ)} {budget : ℕ}
(run : StateTransition.EvalsToInTime (TM2.step source) start finish budget) :
(frameExecution source ambient run).steps = run.steps := rfl
theorem stepAux_simulation (labels : Λ → Λ') (exit : Option Λ')
(q : TM2.Stmt Γ Λ σ) (state : σ) (ambient : τ) (tapes : ∀ k, List (Γ k)) :
TM2.stepAux (statement labels exit q) (state, ambient) tapes =
configuration labels exit ambient (TM2.stepAux q state tapes) := by
rw [statement, MachineSubroutine.stepAux_simulation, frame_stepAux]
rfl
theorem step_simulation (labels : Λ → Λ') (exit : Option Λ') (ambient : τ)
(source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' (σ × τ))
(atLabels : ∀ l, target (labels l) = statement labels exit (source l))
(a b : TM2.Cfg Γ Λ σ) (h : TM2.step source a = some b) :
TM2.step target (configuration labels exit ambient a) =
some (configuration labels exit ambient b) := by
have framed : TM2.step (frameProgram source) (frameConfiguration ambient a) =
some (frameConfiguration ambient b) := by
rw [frame_step, h]
rfl
exact MachineSubroutine.step_simulation labels exit (frameProgram source) target
atLabels _ _ framed
theorem trace (labels : Λ → Λ') (exit : Option Λ') (ambient : τ)
(source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' (σ × τ))
(atLabels : ∀ l, target (labels l) = statement labels exit (source l))
(steps : ℕ) (a b : TM2.Cfg Γ Λ σ)
(run : (MachineComposition.advance (TM2.step source))^[steps] (some a) = some b) :
(MachineComposition.advance (TM2.step target))^[steps]
(some (configuration labels exit ambient a)) = some (configuration labels exit ambient b) :=
MachineComposition.liftSuccessfulTrace (TM2.step source) (TM2.step target)
(configuration labels exit ambient) (step_simulation labels exit ambient source target atLabels)
steps a b run
def execution (labels : Λ → Λ') (exit : Option Λ') (ambient : τ)
(source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' (σ × τ))
(atLabels : ∀ l, target (labels l) = statement labels exit (source l))
{a b : TM2.Cfg Γ Λ σ} {budget : ℕ}
(run : StateTransition.EvalsToInTime (TM2.step source) a (some b) budget) :
StateTransition.EvalsToInTime (TM2.step target)
(configuration labels exit ambient a) (some (configuration labels exit ambient b)) budget :=
MachineComposition.liftExecutionInTime (TM2.step source) (TM2.step target)
(configuration labels exit ambient) (step_simulation labels exit ambient source target atLabels) run
@[simp] theorem execution_steps (labels : Λ → Λ') (exit : Option Λ') (ambient : τ)
(source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' (σ × τ))
(atLabels : ∀ l, target (labels l) = statement labels exit (source l))
{a b : TM2.Cfg Γ Λ σ} {budget : ℕ}
(run : StateTransition.EvalsToInTime (TM2.step source) a (some b) budget) :
(execution labels exit ambient source target atLabels run).steps = run.steps := rfl
end MaxCutGames.Foundations.Complexity.MachineStateFrame
end OAI

Xet Storage Details

Size:
9.93 kB
·
Xet hash:
8c2c8e135b062be42026a9bd8009b9176c447a700dc69fa46c2241d6cab9f72b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.