roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
8.54 kB
import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.GraphCounterModel
namespace OAI
namespace IndependentSetsGames.Foundations.Complexity.GraphCounterModel
open Turing
def startExtra (input archive : List Bool) : ExtraTape → List Bool
| .input => input
| .archive => archive
| _ => []
def startMemory (input archive sum : List Bool) : Tape → List Bool
| .inl k => if k = 0 then sum else []
| .inr k => startExtra input archive k
theorem startMemory_input_update (input archive sum replacement : List Bool) :
Function.update (startMemory input archive sum) (.inr .input) replacement =
startMemory replacement archive sum := by
funext tape
cases tape with
| inl k => simp [startMemory]
| inr k => cases k <;> simp [startMemory, startExtra]
theorem startMemory_sum_update (input archive sum replacement : List Bool) :
Function.update (startMemory input archive sum) (.inl 0) replacement =
startMemory input archive replacement := by
funext tape
cases tape with
| inl k => by_cases h : k = 0 <;> simp [startMemory, h]
| inr k => simp [startMemory]
theorem headerStep_zero (again next : Label)
(atHeader : program again = readHeader again next)
(suffix archive : List Bool) (sum : Nat)
(state : MachineLogCounter.State) (register : Option Bool) :
TM2.step program
⟨some again, (state, register), startMemory (encodeWord 0 ++ suffix) archive (encodeWord sum)⟩ =
some ⟨some next, (state, none), startMemory suffix archive (encodeWord sum)⟩ := by
change some (TM2.stepAux (program again) _ _) = _
rw [atHeader]
simp only [readHeader, TM2.stepAux, startMemory, startExtra, encodeWord,
List.replicate_zero, List.nil_append, List.singleton_append, List.head?_cons,
List.tail_cons, Option.getD_some, Bool.cond_false]
rw [startMemory_input_update]
theorem headerStep_succ (again next : Label)
(atHeader : program again = readHeader again next)
(n sum : Nat) (suffix archive : List Bool)
(state : MachineLogCounter.State) (register : Option Bool) :
TM2.step program
⟨some again, (state, register),
startMemory (encodeWord (n + 1) ++ suffix) archive (encodeWord sum)⟩ =
some ⟨some again, (state, some true),
startMemory (encodeWord n ++ suffix) archive (encodeWord (sum + 1))⟩ := by
change some (TM2.stepAux (program again) _ _) = _
rw [atHeader]
simp only [readHeader, TM2.stepAux, startMemory, startExtra, encodeWord,
List.replicate_succ, List.cons_append, List.head?_cons, List.tail_cons,
Option.getD_some, Bool.cond_true]
rw [startMemory_input_update, startMemory_sum_update]
rfl
theorem headerTrace (again next : Label)
(atHeader : program again = readHeader again next)
(n sum : Nat) (suffix archive : List Bool)
(state : MachineLogCounter.State) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[n + 1]
(some ⟨some again, (state, register),
startMemory (encodeWord n ++ suffix) archive (encodeWord sum)⟩) =
some ⟨some next, (state, none), startMemory suffix archive (encodeWord (n + sum))⟩ := by
induction n generalizing sum register with
| zero =>
simpa only [Nat.zero_add, Function.iterate_one, MachineComposition.advance_some] using
headerStep_zero again next atHeader suffix archive sum state register
| succ n ih =>
rw [Function.iterate_succ_apply, MachineComposition.advance_some]
rw [headerStep_succ again next atHeader n sum suffix archive state register]
simpa only [Nat.add_assoc, Nat.add_comm 1 sum] using ih (sum + 1) (some true)
def headerInTime (again next : Label)
(atHeader : program again = readHeader again next)
(n sum : Nat) (suffix archive : List Bool)
(state : MachineLogCounter.State) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some again, (state, register), startMemory (encodeWord n ++ suffix) archive (encodeWord sum)⟩
(some ⟨some next, (state, none), startMemory suffix archive (encodeWord (n + sum))⟩)
(n + 1) where
steps := n + 1
evals_in_steps := headerTrace again next atHeader n sum suffix archive state register
steps_le_m := le_rfl
theorem initialMemory (word : List Bool) :
initList machine word =
⟨some (.inr .copyFirst), initialState, startMemory word [] []⟩ := by
have ht : (initList machine word).stk = startMemory word [] [] := by
funext tape
cases tape with
| inl k => simp [initList, machine, startMemory]
| inr extra => cases extra <;> rfl
exact congrArg (TM2.Cfg.mk _ _) ht
theorem clockInitialMemory (word rest : List Bool) (sum : Nat) :
clockConfiguration (startExtra rest word) none
(initList MachineLogCounter.machine (encodeWord sum)) =
⟨some (.inl 0), initialState, startMemory rest word (encodeWord sum)⟩ := by
have ht : clockTapes (initList MachineLogCounter.machine (encodeWord sum)).stk
(startExtra rest word) = startMemory rest word (encodeWord sum) := by
funext tape
cases tape with
| inl index => fin_cases index <;> rfl
| inr k => rfl
exact congrArg (TM2.Cfg.mk _ _) ht
def startInTime (n m : Nat) (rest : List Bool) :
StateTransition.EvalsToInTime machine.step
(initList machine (encodeWords [n, m] ++ rest))
(some (clockConfiguration (startExtra rest (encodeWords [n, m] ++ rest)) none
(initList MachineLogCounter.machine (encodeWord (n + m)))))
(2 * ((encodeWords [n, m] ++ rest).length + 1) + 1 + (n + 1) + (m + 1)) := by
let word := encodeWords [n, m] ++ rest
let b₀ := startMemory word [] []
let b₁ := startMemory word word []
let b₂ := startMemory word word (encodeWord 0)
have hc : Function.update b₀ (.inr .archive) (b₀ (.inr .input) ++ b₀ (.inr .archive)) = b₁ := by
funext tape
cases tape with
| inl k => simp [b₀, b₁, startMemory]
| inr k => cases k <;> simp [b₀, b₁, startMemory, startExtra]
let copy := MachineCopy.copyInTime (.inr ExtraTape.input) (.inr ExtraTape.archive)
(.inr ExtraTape.scratch) (by decide) (by decide) (by decide) false
(.inr ExtraLabel.copyFirst) (.inr ExtraLabel.copySecond) (some (.inr ExtraLabel.seed))
program rfl rfl b₀ rfl MachineLogCounter.initialState none
have copy' : StateTransition.EvalsToInTime (TM2.step program)
⟨some (.inr .copyFirst), initialState, b₀⟩
(some ⟨some (.inr .seed), initialState, b₁⟩) (2 * (word.length + 1)) := by
have h := copy
rw [hc] at h
simpa only [initialState, show b₀ (.inr .input) = word from rfl] using h
have seed : StateTransition.EvalsToInTime (TM2.step program)
⟨some (.inr .seed), initialState, b₁⟩
(some ⟨some (.inr .headerFirst), initialState, b₂⟩) 1 := by
refine ⟨⟨1, ?_⟩, le_rfl⟩
change some (TM2.stepAux (program (.inr .seed)) initialState b₁) = _
simp only [program, TM2.stepAux, b₁, startMemory]
rw [startMemory_sum_update]
rfl
have first := headerInTime (.inr .headerFirst) (.inr .headerSecond) rfl
n 0 (encodeWord m ++ rest) word MachineLogCounter.initialState none
have first' : StateTransition.EvalsToInTime (TM2.step program)
⟨some (.inr .headerFirst), initialState, b₂⟩
(some ⟨some (.inr .headerSecond), initialState,
startMemory (encodeWord m ++ rest) word (encodeWord n)⟩) (n + 1) := by
simpa only [b₂, initialState, word, encodeWords, List.append_nil, List.append_assoc,
Nat.add_zero] using first
have second := headerInTime (.inr .headerSecond) (.inl 0) rfl
m n rest word MachineLogCounter.initialState none
have second' : StateTransition.EvalsToInTime (TM2.step program)
⟨some (.inr .headerSecond), initialState,
startMemory (encodeWord m ++ rest) word (encodeWord n)⟩
(some ⟨some (.inl 0), initialState, startMemory rest word (encodeWord (n + m))⟩)
(m + 1) := by simpa only [initialState, Nat.add_comm m n] using second
let p₀ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ copy' seed
let p₁ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀ first'
let p := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₁ second'
rw [initialMemory, clockInitialMemory]
exact {
toEvalsTo := p.toEvalsTo
steps_le_m := by
have h := p.steps_le_m
change p.steps ≤ 2 * (word.length + 1) + 1 + (n + 1) + (m + 1)
omega
}
end IndependentSetsGames.Foundations.Complexity.GraphCounterModel
end OAI