cfahlgren1/math / lean /OAI /Computability /MinUncut /Machines /MachineUnaryCounter.lean
cfahlgren1's picture
download
raw
38.8 kB
import Mathlib
import OAI.Computability.MinUncut.Machines.Machine
namespace OAI
namespace MinUncutGames.Foundations.Complexity.MachineUnaryCounter
open Turing
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
def guard (counter : K) (bodyLabel exitLabel : Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) :=
.peek counter (fun state head => (state.1, head))
(.branch (fun state => state.2.getD false)
(.pop counter (fun state _ => (state.1, none)) (.goto fun _ => bodyLabel))
(.load (fun state => (state.1, none)) (.goto fun _ => exitLabel)))
def counterTapes (counter : K) (base : K → List Bool) (n : Nat)
(suffix : List Bool) : K → List Bool :=
Function.update base counter (encodeWord n ++ suffix)
@[simp] theorem counterTapes_counter (counter : K) (base : K → List Bool)
(n : Nat) (suffix : List Bool) :
counterTapes counter base n suffix counter = encodeWord n ++ suffix := by
simp [counterTapes]
theorem counterTapes_other (counter other : K) (hne : other ≠ counter)
(base : K → List Bool) (n : Nat) (suffix : List Bool) :
counterTapes counter base n suffix other = base other := by
simp [counterTapes, hne]
omit [DecidableEq K] in
theorem guardPushBound (counter : K) (bodyLabel exitLabel : Λ) :
Runtime.statementPushBound (guard (σ := σ) counter bodyLabel exitLabel) = 0 := by
rfl
theorem stepAux_succ (counter : K) (bodyLabel exitLabel : Λ)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.stepAux (guard counter bodyLabel exitLabel) (ambient, register)
(counterTapes counter base (n + 1) suffix) =
⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by
simp [guard, TM2.stepAux, counterTapes, encodeWord, List.replicate_succ]
theorem stepAux_zero (counter : K) (bodyLabel exitLabel : Λ)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.stepAux (guard counter bodyLabel exitLabel) (ambient, register)
(counterTapes counter base 0 suffix) =
⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by
simp [guard, TM2.stepAux, counterTapes, encodeWord]
theorem guardStep_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.step program
⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩ =
some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by
change some (TM2.stepAux (program guardLabel) (ambient, register)
(counterTapes counter base (n + 1) suffix)) = _
rw [atGuard, stepAux_succ]
theorem guardStep_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
TM2.step program
⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩ =
some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by
change some (TM2.stepAux (program guardLabel) (ambient, register)
(counterTapes counter base 0 suffix)) = _
rw [atGuard, stepAux_zero]
theorem guardTrace_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[1]
(some ⟨some guardLabel, (ambient, register),
counterTapes counter base (n + 1) suffix⟩) =
some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard
base n suffix ambient register
theorem guardTrace_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[1]
(some ⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩) =
some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩ := by
simpa only [Function.iterate_one, MachineComposition.advance_some] using
guardStep_zero counter guardLabel bodyLabel exitLabel program atGuard
base suffix ambient register
def guardInTime_succ (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some guardLabel, (ambient, register), counterTapes counter base (n + 1) suffix⟩
(some ⟨some bodyLabel, (ambient, none), counterTapes counter base n suffix⟩) 1 where
steps := 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[1] _ = _
exact guardTrace_succ counter guardLabel bodyLabel exitLabel program atGuard
base n suffix ambient register
steps_le_m := Nat.le_refl _
def guardInTime_zero (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(base : K → List Bool) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some guardLabel, (ambient, register), counterTapes counter base 0 suffix⟩
(some ⟨some exitLabel, (ambient, none), counterTapes counter base 0 suffix⟩) 1 where
steps := 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[1] _ = _
exact guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard
base suffix ambient register
steps_le_m := Nat.le_refl _
end MinUncutGames.Foundations.Complexity.MachineUnaryCounter
namespace MinUncutGames.Foundations.Complexity.MachineLookup
open Turing
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
def discard (source : K) (loopLabel returnLabel : Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) :=
.pop source (fun state head => (state.1, head))
(.branch (fun state => state.2.getD false)
(.goto fun _ => loopLabel)
(.load (fun state => (state.1, none)) (.goto fun _ => returnLabel)))
def select (source destination : K) (copyLabel rejected : Λ) :
TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) :=
.peek source (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(Hastad.SourceMachine.fieldStart destination copyLabel)
(.load (fun state => (state.1, none)) (.goto fun _ => rejected)))
inductive Label
| guard | skip | select | copy | accepted | rejected
deriving DecidableEq
protected abbrev Label.enumList : List Label := [.guard, .skip, .select, .copy, .accepted,
.rejected]
protected theorem Label.enumList_getElem?_ctorIdx_eq (x : Label) :
Label.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem Label.enumList_nodup : Label.enumList.Nodup := by decide
instance : Fintype Label where
elems := ⟨Label.enumList, Label.enumList_nodup⟩
complete x := by cases x <;> decide
def program (index source destination : K) :
Label → TM2.Stmt (Alphabet (K := K)) Label (σ × Option Bool)
| .guard => MachineUnaryCounter.guard index .skip .select
| .skip => discard source .skip .guard
| .select => select source destination .copy .rejected
| .copy => Hastad.SourceMachine.fieldLoop source destination .copy (some .accepted)
| .accepted => .halt
| .rejected => .halt
def tapes (index source destination : K) (base : K → List Bool)
(counter input output : List Bool) : K → List Bool :=
Function.update (Function.update (Function.update base index counter) source input)
destination output
@[simp] theorem tapes_index (index source destination : K)
(his : index ≠ source) (hid : index ≠ destination)
(base : K → List Bool) (counter input output : List Bool) :
tapes index source destination base counter input output index = counter := by
simp [tapes, his, hid]
@[simp] theorem tapes_source (index source destination : K)
(hsd : source ≠ destination) (base : K → List Bool)
(counter input output : List Bool) :
tapes index source destination base counter input output source = input := by
simp [tapes, hsd]
@[simp] theorem tapes_destination (index source destination : K)
(base : K → List Bool) (counter input output : List Bool) :
tapes index source destination base counter input output destination = output := by
simp [tapes]
theorem tapes_other (index source destination p : K)
(hi : p ≠ index) (hs : p ≠ source) (hd : p ≠ destination)
(base : K → List Bool) (counter input output : List Bool) :
tapes index source destination base counter input output p = base p := by
simp [tapes, hi, hs, hd]
theorem update_index (index source destination : K)
(his : index ≠ source) (hid : index ≠ destination)
(base : K → List Bool) (counter input output replacement : List Bool) :
Function.update (tapes index source destination base counter input output) index replacement =
tapes index source destination base replacement input output := by
funext p
by_cases hi : p = index
· subst p; simp [tapes, his, hid]
· by_cases hs : p = source
· subst p; by_cases hs : source = destination <;> simp [tapes, hi, hs, Ne.symm hid]
· by_cases hd : p = destination
· subst p; simp [tapes, hi]
· simp [tapes, hi, hs, hd]
theorem update_source (index source destination : K) (hsd : source ≠ destination)
(base : K → List Bool) (counter input output replacement : List Bool) :
Function.update (tapes index source destination base counter input output) source replacement =
tapes index source destination base counter replacement output := by
funext p
by_cases hs : p = source
· subst p; simp [tapes, hsd]
· by_cases hd : p = destination
· subst p; simp [tapes, hs]
· simp [tapes, hs, hd]
theorem update_destination (index source destination : K)
(base : K → List Bool) (counter input output replacement : List Bool) :
Function.update (tapes index source destination base counter input output)
destination replacement = tapes index source destination base counter input replacement := by
simp [tapes]
theorem fieldTapes_eq (index source destination : K) (hsd : source ≠ destination)
(base : K → List Bool) (counter input output input' output' : List Bool) :
Hastad.SourceMachine.fieldTapes source destination
(tapes index source destination base counter input output) input' output' =
tapes index source destination base counter input' output' := by
rw [Hastad.SourceMachine.fieldTapes, update_source _ _ _ hsd, update_destination]
def machine : FinTM2 where
K := Fin 3
k₀ := 1
k₁ := 2
Γ _ := Bool
Λ := Label
main := .guard
σ := Unit × Option Bool
initialState := ((), none)
m := program 0 1 2
open Turing
variable {K Λ σ : Type} [DecidableEq K]
private theorem discard_update_twice (source : K) (base : K → List Bool)
(input replacement : List Bool) :
Function.update (Function.update base source input) source replacement =
Function.update base source replacement := by
funext p
by_cases hp : p = source
· subst p; simp
· simp [hp]
theorem discardStep_delimiter (source : K) (loopLabel returnLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atLoop : program loopLabel = discard source loopLabel returnLabel)
(base : K → List Bool) (suffix : List Bool) (ambient : σ)
(register : Option Bool) :
TM2.step program
⟨some loopLabel, (ambient, register), Function.update base source (false :: suffix)⟩ =
some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(Function.update base source (false :: suffix))) = _
rw [atLoop]
simp [discard, TM2.stepAux]
theorem discardStep_true (source : K) (loopLabel returnLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atLoop : program loopLabel = discard source loopLabel returnLabel)
(base : K → List Bool) (input : List Bool) (ambient : σ)
(register : Option Bool) :
TM2.step program
⟨some loopLabel, (ambient, register), Function.update base source (true :: input)⟩ =
some ⟨some loopLabel, (ambient, some true), Function.update base source input⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(Function.update base source (true :: input))) = _
rw [atLoop]
simp [discard, TM2.stepAux]
theorem discard_empty_step (source : K) (loopLabel returnLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atLoop : program loopLabel = discard source loopLabel returnLabel)
(base : K → List Bool) (hinput : base source = [])
(ambient : σ) (register : Option Bool) :
TM2.step program ⟨some loopLabel, (ambient, register), base⟩ =
some ⟨some returnLabel, (ambient, none), base⟩ := by
have hupdate : Function.update base source [] = base := by
funext p
by_cases hp : p = source
· subst p; simp [hinput]
· simp [hp]
change some (TM2.stepAux (program loopLabel) (ambient, register) base) = _
rw [atLoop]
simp [discard, TM2.stepAux, hinput, hupdate]
theorem discardTrace_update (source : K) (loopLabel returnLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atLoop : program loopLabel = discard source loopLabel returnLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[n + 1]
(some ⟨some loopLabel, (ambient, register),
Function.update base source (encodeWord n ++ suffix)⟩) =
some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩ := by
induction n generalizing register with
| zero =>
simpa only [Nat.zero_add, Function.iterate_one, MachineComposition.advance_some,
encodeWord, List.replicate_zero, List.nil_append, List.singleton_append] using
discardStep_delimiter source loopLabel returnLabel program atLoop
base suffix ambient register
| succ n ih =>
rw [Function.iterate_succ_apply]
simp only [encodeWord, List.replicate_succ, List.cons_append]
change (MachineComposition.advance (TM2.step program))^[n + 1]
(TM2.step program ⟨some loopLabel, (ambient, register),
Function.update base source (true :: (encodeWord n ++ suffix))⟩) = _
rw [discardStep_true source loopLabel returnLabel program atLoop, ih]
theorem discardTrace (source : K) (loopLabel returnLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atLoop : program loopLabel = discard source loopLabel returnLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(hinput : base source = encodeWord n ++ suffix)
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[n + 1]
(some ⟨some loopLabel, (ambient, register), base⟩) =
some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩ := by
have hbase : Function.update base source (encodeWord n ++ suffix) = base := by
funext p
by_cases hp : p = source
· subst p; simp [hinput]
· simp [hp]
have h := discardTrace_update source loopLabel returnLabel program atLoop
base n suffix ambient register
rw [hbase] at h
exact h
def discardInTime (source : K) (loopLabel returnLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atLoop : program loopLabel = discard source loopLabel returnLabel)
(base : K → List Bool) (n : Nat) (suffix : List Bool)
(hinput : base source = encodeWord n ++ suffix)
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some loopLabel, (ambient, register), base⟩
(some ⟨some returnLabel, (ambient, none), Function.update base source suffix⟩)
(n + 1) where
steps := n + 1
evals_in_steps := discardTrace source loopLabel returnLabel program atLoop
base n suffix hinput ambient register
steps_le_m := Nat.le_refl _
end MinUncutGames.Foundations.Complexity.MachineLookup
namespace MinUncutGames.Foundations.Complexity.MachineCopy
open Turing
variable {K Λ σ β : Type} [DecidableEq K]
abbrev Alphabet (β : Type) (_ : K) := β
def forkLoop (source left right : K) (fallback : β) (loopLabel : Λ) (exit : Option Λ) :
TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β) :=
.pop source (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(.push left (fun state => state.2.getD fallback)
(.push right (fun state => state.2.getD fallback) (.goto fun _ => loopLabel)))
(Reduction.MachineTransfer.exitAt right exit))
def forkTapes (source left right : K) (base : K → List β)
(input leftOutput rightOutput : List β) : K → List β :=
Function.update (Function.update (Function.update base source input) left leftOutput)
right rightOutput
@[simp] theorem forkTapes_source (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right)
(base : K → List β) (input leftOutput rightOutput : List β) :
forkTapes source left right base input leftOutput rightOutput source = input := by
simp [forkTapes, sourceLeft, sourceRight]
@[simp] theorem forkTapes_left (source left right : K) (leftRight : left ≠ right)
(base : K → List β) (input leftOutput rightOutput : List β) :
forkTapes source left right base input leftOutput rightOutput left = leftOutput := by
simp [forkTapes, leftRight]
@[simp] theorem forkTapes_right (source left right : K)
(base : K → List β) (input leftOutput rightOutput : List β) :
forkTapes source left right base input leftOutput rightOutput right = rightOutput := by
simp [forkTapes]
@[simp] theorem forkTapes_self (source left right : K) (base : K → List β) :
forkTapes source left right base (base source) (base left) (base right) = base := by
simp [forkTapes]
private theorem update_fork_source (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(base : K → List β) (input leftOutput rightOutput replacement : List β) :
Function.update (forkTapes source left right base input leftOutput rightOutput)
source replacement = forkTapes source left right base replacement leftOutput rightOutput := by
funext k
by_cases hs : k = source
· subst k; simp [forkTapes, sourceLeft, sourceRight]
· by_cases hl : k = left
· subst k; simp [forkTapes, Ne.symm sourceLeft, leftRight]
· by_cases hr : k = right
· subst k; simp [forkTapes, Ne.symm sourceRight]
· simp [forkTapes, hs, hl, hr]
private theorem update_fork_left (source left right : K) (leftRight : left ≠ right)
(base : K → List β) (input leftOutput rightOutput replacement : List β) :
Function.update (forkTapes source left right base input leftOutput rightOutput)
left replacement = forkTapes source left right base input replacement rightOutput := by
funext k
by_cases hl : k = left
· subst k; simp [forkTapes, leftRight]
· by_cases hr : k = right
· subst k; simp [forkTapes, Ne.symm leftRight]
· simp [forkTapes, hl, hr]
private theorem update_fork_right (source left right : K)
(base : K → List β) (input leftOutput rightOutput replacement : List β) :
Function.update (forkTapes source left right base input leftOutput rightOutput)
right replacement = forkTapes source left right base input leftOutput replacement := by
simp [forkTapes]
theorem forkStep_empty (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (leftOutput rightOutput : List β) (ambient : σ) (register : Option β) :
TM2.step program
⟨some loopLabel, (ambient, register), forkTapes source left right base [] leftOutput rightOutput⟩ =
some ⟨exit, (ambient, none), forkTapes source left right base [] leftOutput rightOutput⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(forkTapes source left right base [] leftOutput rightOutput)) = _
rw [atLoop]
cases exit <;>
simp [forkLoop, Reduction.MachineTransfer.exitAt, TM2.stepAux, sourceLeft, sourceRight,
leftRight, update_fork_source]
theorem forkStep_cons (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (head : β) (input leftOutput rightOutput : List β)
(ambient : σ) (register : Option β) :
TM2.step program
⟨some loopLabel, (ambient, register),
forkTapes source left right base (head :: input) leftOutput rightOutput⟩ =
some ⟨some loopLabel, (ambient, some head),
forkTapes source left right base input (head :: leftOutput) (head :: rightOutput)⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(forkTapes source left right base (head :: input) leftOutput rightOutput)) = _
rw [atLoop]
simp [forkLoop, TM2.stepAux, sourceLeft, sourceRight, leftRight,
update_fork_source, update_fork_left, update_fork_right]
theorem forkTrace (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (input leftOutput rightOutput : List β)
(ambient : σ) (register : Option β) :
(MachineComposition.advance (TM2.step program))^[input.length + 1]
(some ⟨some loopLabel, (ambient, register),
forkTapes source left right base input leftOutput rightOutput⟩) =
some ⟨exit, (ambient, none), forkTapes source left right base []
(input.reverse ++ leftOutput) (input.reverse ++ rightOutput)⟩ := by
induction input generalizing leftOutput rightOutput register with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one,
MachineComposition.advance_some, List.reverse_nil, List.nil_append] using
forkStep_empty source left right sourceLeft sourceRight leftRight fallback loopLabel exit
program atLoop base leftOutput rightOutput ambient register
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[input.length + 1]
(TM2.step program ⟨some loopLabel, (ambient, register),
forkTapes source left right base (head :: input) leftOutput rightOutput⟩) = _
rw [forkStep_cons source left right sourceLeft sourceRight leftRight fallback loopLabel exit
program atLoop base head input leftOutput rightOutput ambient register, ih]
simp only [List.reverse_cons, List.append_assoc, List.singleton_append]
def forkInTime (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (ambient : σ) (register : Option β) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some loopLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none), forkTapes source left right base []
((base source).reverse ++ base left) ((base source).reverse ++ base right)⟩)
((base source).length + 1) where
steps := (base source).length + 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[(base source).length + 1]
(some ⟨some loopLabel, (ambient, register), base⟩) = _
simpa only [forkTapes_self] using
forkTrace source left right sourceLeft sourceRight leftRight fallback loopLabel exit
program atLoop base (base source) (base left) (base right) ambient register
steps_le_m := Nat.le_refl _
def drainedTapes (source scratch : K) (base : K → List β) : K → List β :=
Reduction.MachineTransfer.tapesAt source scratch base [] (base source).reverse
theorem restoredTapes (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch) (base : K → List β)
(scratchEmpty : base scratch = []) :
forkTapes scratch source destination (drainedTapes source scratch base) []
(base source) (base source ++ base destination) =
Function.update base destination (base source ++ base destination) := by
funext k
by_cases hs : k = source
· subst k
simp [forkTapes, sourceDestination]
· by_cases hd : k = destination
· subst k; simp [forkTapes]
· by_cases ht : k = scratch
· subst k
simp [forkTapes, Ne.symm sourceScratch, Ne.symm destinationScratch, scratchEmpty]
· simp [forkTapes, drainedTapes, Reduction.MachineTransfer.tapesAt, hs, hd, ht]
theorem copyTrace (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch)
(fallback : β) (firstLabel secondLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atFirst : program firstLabel =
Reduction.MachineTransfer.loopAt source scratch id fallback firstLabel (some secondLabel))
(atSecond : program secondLabel = forkLoop scratch source destination fallback secondLabel exit)
(base : K → List β) (scratchEmpty : base scratch = [])
(ambient : σ) (register : Option β) :
(MachineComposition.advance (TM2.step program))^[2 * ((base source).length + 1)]
(some ⟨some firstLabel, (ambient, register), base⟩) =
some ⟨exit, (ambient, none),
Function.update base destination (base source ++ base destination)⟩ := by
have first := Reduction.MachineTransfer.transferAt_fromTapes source scratch sourceScratch
id fallback firstLabel (some secondLabel) program atFirst base ambient register
change (MachineComposition.advance (TM2.step program))^[(base source).length + 1]
(some ⟨some firstLabel, (ambient, register), base⟩) = _ at first
simp only [List.map_id, scratchEmpty, List.append_nil] at first
have second := forkTrace scratch source destination (Ne.symm sourceScratch)
(Ne.symm destinationScratch) sourceDestination fallback secondLabel exit program atSecond
(drainedTapes source scratch base)
(drainedTapes source scratch base scratch) (drainedTapes source scratch base source)
(drainedTapes source scratch base destination) ambient none
simp only [forkTapes_self] at second
have scratchWord : drainedTapes source scratch base scratch = (base source).reverse := by
simp [drainedTapes]
have sourceWord : drainedTapes source scratch base source = [] := by
simp [drainedTapes, sourceScratch]
have destinationWord : drainedTapes source scratch base destination = base destination := by
simp [drainedTapes, Reduction.MachineTransfer.tapesAt, Ne.symm sourceDestination,
destinationScratch]
rw [scratchWord, sourceWord, destinationWord] at second
simp only [List.reverse_reverse, List.append_nil, List.length_reverse] at second
rw [restoredTapes source destination scratch sourceDestination sourceScratch destinationScratch
base scratchEmpty] at second
rw [show 2 * ((base source).length + 1) =
((base source).length + 1) + ((base source).length + 1) by omega,
Function.iterate_add_apply, first]
exact second
def copyInTime (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch)
(fallback : β) (firstLabel secondLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atFirst : program firstLabel =
Reduction.MachineTransfer.loopAt source scratch id fallback firstLabel (some secondLabel))
(atSecond : program secondLabel = forkLoop scratch source destination fallback secondLabel exit)
(base : K → List β) (scratchEmpty : base scratch = [])
(ambient : σ) (register : Option β) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some firstLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none),
Function.update base destination (base source ++ base destination)⟩)
(2 * ((base source).length + 1)) where
steps := 2 * ((base source).length + 1)
evals_in_steps := copyTrace source destination scratch sourceDestination sourceScratch
destinationScratch fallback firstLabel secondLabel exit program atFirst atSecond base
scratchEmpty ambient register
steps_le_m := Nat.le_refl _
end MinUncutGames.Foundations.Complexity.MachineCopy
namespace MinUncutGames.Foundations.Complexity.MachineCountedLoop
open Turing
open scoped BigOperators
open MachineUnaryCounter
variable {K Λ σ : Type} [DecidableEq K]
def guardConfiguration (counter : K) (guardLabel : Λ) (suffix : List Bool)
(ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (n : Nat) :
TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) :=
⟨some guardLabel, (ambient n, register n), counterTapes counter (base n) n suffix⟩
def bodyConfiguration (counter : K) (bodyLabel : Λ) (suffix : List Bool)
(ambient : Nat → σ) (base : Nat → K → List Bool) (n : Nat) :
TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) :=
⟨some bodyLabel, (ambient (n + 1), none), counterTapes counter (base (n + 1)) n suffix⟩
def exitConfiguration (counter : K) (exitLabel : Λ) (suffix : List Bool)
(ambient : Nat → σ) (base : Nat → K → List Bool) :
TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) :=
⟨some exitLabel, (ambient 0, none), counterTapes counter (base 0) 0 suffix⟩
def BodyTraces (counter : K) (guardLabel bodyLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) : Prop :=
∀ r, r < n →
(MachineComposition.advance (TM2.step program))^[cost r]
(some (bodyConfiguration counter bodyLabel suffix ambient base r)) =
some (guardConfiguration counter guardLabel suffix ambient register base r)
def totalSteps (cost : Nat → Nat) (n : Nat) : Nat :=
(∑ r ∈ Finset.range n, cost r) + n + 1
@[simp] theorem totalSteps_zero (cost : Nat → Nat) : totalSteps cost 0 = 1 := by
simp [totalSteps]
theorem totalSteps_succ (cost : Nat → Nat) (n : Nat) :
totalSteps cost (n + 1) = (totalSteps cost n + cost n) + 1 := by
simp only [totalSteps, Finset.sum_range_succ]
omega
theorem loopTrace (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat)
(bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix
ambient register base cost n) :
(MachineComposition.advance (TM2.step program))^[totalSteps cost n]
(some (guardConfiguration counter guardLabel suffix ambient register base n)) =
some (exitConfiguration counter exitLabel suffix ambient base) := by
revert bodyTraces
induction n with
| zero =>
intro _
simpa only [totalSteps_zero, guardConfiguration, exitConfiguration] using
guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard
(base 0) suffix (ambient 0) (register 0)
| succ n ih =>
intro bodyTraces
rw [totalSteps_succ, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[totalSteps cost n + cost n]
(TM2.step program
⟨some guardLabel, (ambient (n + 1), register (n + 1)),
counterTapes counter (base (n + 1)) (n + 1) suffix⟩) = _
rw [guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard
(base (n + 1)) n suffix (ambient (n + 1)) (register (n + 1)),
Function.iterate_add_apply]
change (MachineComposition.advance (TM2.step program))^[totalSteps cost n]
((MachineComposition.advance (TM2.step program))^[cost n]
(some (bodyConfiguration counter bodyLabel suffix ambient base n))) = _
rw [bodyTraces n (Nat.lt_succ_self n)]
exact ih (fun r hr => bodyTraces r (Nat.lt_trans hr (Nat.lt_succ_self n)))
theorem totalSteps_le (cost : Nat → Nat) (n B : Nat)
(bodyBound : ∀ r, r < n → cost r ≤ B) :
totalSteps cost n ≤ n * (B + 1) + 1 := by
have hsum : (∑ r ∈ Finset.range n, cost r) ≤ n * B := by
calc
(∑ r ∈ Finset.range n, cost r) ≤ ∑ _r ∈ Finset.range n, B :=
Finset.sum_le_sum (fun r hr => bodyBound r (Finset.mem_range.mp hr))
_ = n * B := by simp
simpa only [totalSteps, Nat.mul_add, Nat.mul_one] using
Nat.add_le_add_right (Nat.add_le_add_right hsum n) 1
def loopInTime (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n B : Nat)
(bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix
ambient register base cost n)
(bodyBound : ∀ r, r < n → cost r ≤ B) :
StateTransition.EvalsToInTime (TM2.step program)
(guardConfiguration counter guardLabel suffix ambient register base n)
(some (exitConfiguration counter exitLabel suffix ambient base))
(n * (B + 1) + 1) where
steps := totalSteps cost n
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] _ = _
exact loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix
ambient register base cost n bodyTraces
steps_le_m := totalSteps_le cost n B bodyBound
omit [DecidableEq K] in
theorem baseFrame (base : Nat → K → List Bool) (frame : K → Prop) (n : Nat)
(bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) :
∀ k, frame k → base n k = base 0 k := by
revert bodyFrame
induction n with
| zero => intro _ k _; rfl
| succ n ih =>
intro bodyFrame k hk
exact (bodyFrame n (Nat.lt_succ_self n) k hk).trans
(ih (fun r hr => bodyFrame r (Nat.lt_trans hr (Nat.lt_succ_self n))) k hk)
theorem counterFrame (counter : K) (suffix : List Bool)
(base : Nat → K → List Bool) (frame : K → Prop) (n : Nat)
(counterOutside : ∀ k, frame k → k ≠ counter)
(bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) :
∀ k, frame k →
counterTapes counter (base n) n suffix k = counterTapes counter (base 0) 0 suffix k := by
intro k hk
rw [counterTapes_other counter k (counterOutside k hk),
counterTapes_other counter k (counterOutside k hk)]
exact baseFrame base frame n bodyFrame k hk
theorem loopTrace_framed (counter : K) (guardLabel bodyLabel exitLabel : Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(atGuard : program guardLabel = guard counter bodyLabel exitLabel)
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat)
(bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix
ambient register base cost n)
(frame : K → Prop) (counterOutside : ∀ k, frame k → k ≠ counter)
(bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) :
((MachineComposition.advance (TM2.step program))^[totalSteps cost n]
(some (guardConfiguration counter guardLabel suffix ambient register base n)) =
some (exitConfiguration counter exitLabel suffix ambient base)) ∧
(∀ k, frame k →
(guardConfiguration counter guardLabel suffix ambient register base n).stk k =
(exitConfiguration counter exitLabel suffix ambient base).stk k) :=
⟨loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix
ambient register base cost n bodyTraces,
counterFrame counter suffix base frame n counterOutside bodyFrame⟩
end MinUncutGames.Foundations.Complexity.MachineCountedLoop
end OAI

Xet Storage Details

Size:
38.8 kB
·
Xet hash:
5ae6c1ef72dc4c5ac65d50d012d5e5aa0ace319aa79664cb555229a03d81beac

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