roseking's picture
Add lean source and binary files (part 59)
8df23f4 verified
Raw History Blame Contribute Delete
14.8 kB
import Mathlib.Tactic.FinCases
import OAI.Computability.BinPacking.Computation.GraphCounterStart
import OAI.Computability.BinPacking.PCP.PCPSource
namespace OAI
namespace BinPackingGames.Foundations.Complexity.GraphCounterFinish
open Turing GraphCounterModel
def extraTapes (rest original : List Bool) : ExtraTape → List Bool
| .input => rest
| .archive => original
| _ => []
def frame (input archive counter reversed output : List Bool) : Tape → List Bool
| .inl i => if i = 2 then counter else []
| .inr .input => input
| .inr .archive => archive
| .inr .scratch => reversed
| .inr .output => output
private theorem trace_trans {α : Type*} (f : α → α) {a b : Nat} {x y z : α}
(first : f^[a] x = y) (second : f^[b] y = z) : f^[a + b] x = z := by
rw [Nat.add_comm, Function.iterate_add_apply, first, second]
theorem start_eq (counter rest original : List Bool) :
clockConfiguration (extraTapes rest original) none
(haltList MachineLogCounter.machine counter) =
⟨some (.inr .clearInput), initialState, frame rest original counter [] []⟩ := by
rw [MachineLogCounter.haltList_eq]
unfold clockConfiguration
congr 1
funext tape
cases tape with
| inl i => fin_cases i <;> simp [clockTapes, frame, MachineLogCounter.configuration,
MachineLogCounter.rawTapes]
| inr k => cases k <;> rfl
theorem finish_eq (word : List Bool) :
(⟨none, initialState, frame [] [] [] [] word⟩ : TM2.Cfg Alphabet Label State) =
haltList machine word := by
unfold haltList
congr 1
funext tape
cases tape with
| inl i => simp [machine, frame]
| inr k => (cases k <;> simp [machine, frame]); rfl
theorem drainTrace (rest original counter : List Bool) :
(MachineComposition.advance (TM2.step program))^[rest.length + 1]
(some ⟨some (.inr .clearInput), initialState, frame rest original counter [] []⟩) =
some ⟨some (.inr .clockCopy), initialState, frame [] original counter [] []⟩ := by
have h := (MachineDrain.drainInTime (Sum.inr ExtraTape.input) (Sum.inr ExtraLabel.clearInput)
(some (.inr .clockCopy)) program rfl (frame rest original counter [] [])
MachineLogCounter.initialState none).evals_in_steps
change (MachineComposition.advance (TM2.step program))^[rest.length + 1]
(some ⟨some (.inr .clearInput), initialState, frame rest original counter [] []⟩) = _ at h
have update : Function.update (frame rest original counter [] []) (.inr .input) [] =
frame [] original counter [] [] := by
funext tape
cases tape with
| inl i => simp [frame]
| inr k => cases k <;> simp [frame]
simpa only [update, initialState] using h
theorem clockCopyTrace (original counter : List Bool) :
(MachineComposition.advance (TM2.step program))^[counter.length + 1]
(some ⟨some (.inr .clockCopy), initialState, frame [] original counter [] []⟩) =
some ⟨some (.inr .archiveCopy), initialState, frame [] original [] counter.reverse []⟩ := by
have h := (Reduction.MachineTransfer.transferAtInTime (Sum.inl (2 : Fin 3))
(Sum.inr ExtraTape.scratch) (by decide) id false (.inr .clockCopy)
(some (.inr .archiveCopy)) program rfl (frame [] original counter [] [])
MachineLogCounter.initialState none).evals_in_steps
change (MachineComposition.advance (TM2.step program))^[counter.length + 1]
(some ⟨some (.inr .clockCopy), initialState, frame [] original counter [] []⟩) = _ at h
have update : Reduction.MachineTransfer.tapesAt (Sum.inl (2 : Fin 3))
(Sum.inr ExtraTape.scratch) (frame [] original counter [] []) []
((frame [] original counter [] [] (.inl 2)).reverse.map id ++
frame [] original counter [] [] (.inr .scratch)) =
frame [] original [] counter.reverse [] := by
funext tape
cases tape with
| inl i => fin_cases i <;> simp [Reduction.MachineTransfer.tapesAt, frame]
| inr k => cases k <;> simp [Reduction.MachineTransfer.tapesAt, frame]
simpa only [update, initialState] using h
theorem archiveCopyTrace (original counter : List Bool) :
(MachineComposition.advance (TM2.step program))^[original.length + 1]
(some ⟨some (.inr .archiveCopy), initialState, frame [] original [] counter.reverse []⟩) =
some ⟨some (.inr .finalReverse), initialState,
frame [] [] [] (original.reverse ++ counter.reverse) []⟩ := by
have h := (Reduction.MachineTransfer.transferAtInTime (Sum.inr ExtraTape.archive)
(Sum.inr ExtraTape.scratch) (by decide) id false (.inr .archiveCopy)
(some (.inr .finalReverse)) program rfl (frame [] original [] counter.reverse [])
MachineLogCounter.initialState none).evals_in_steps
change (MachineComposition.advance (TM2.step program))^[original.length + 1]
(some ⟨some (.inr .archiveCopy), initialState, frame [] original [] counter.reverse []⟩) = _ at h
have update : Reduction.MachineTransfer.tapesAt (Sum.inr ExtraTape.archive)
(Sum.inr ExtraTape.scratch) (frame [] original [] counter.reverse []) []
((frame [] original [] counter.reverse [] (.inr .archive)).reverse.map id ++
frame [] original [] counter.reverse [] (.inr .scratch)) =
frame [] [] [] (original.reverse ++ counter.reverse) [] := by
funext tape
cases tape with
| inl i => simp [Reduction.MachineTransfer.tapesAt, frame]
| inr k => cases k <;> simp [Reduction.MachineTransfer.tapesAt, frame]
simpa only [update, initialState] using h
theorem reverseTrace (original counter : List Bool) :
(MachineComposition.advance (TM2.step program))^[original.length + counter.length + 1]
(some ⟨some (.inr .finalReverse), initialState,
frame [] [] [] (original.reverse ++ counter.reverse) []⟩) =
some (haltList machine (counter ++ original)) := by
have h := (Reduction.MachineTransfer.transferAtInTime (Sum.inr ExtraTape.scratch)
(Sum.inr ExtraTape.output) (by decide) id false (.inr .finalReverse) none program rfl
(frame [] [] [] (original.reverse ++ counter.reverse) []) MachineLogCounter.initialState none).evals_in_steps
change (MachineComposition.advance (TM2.step program))^[
(original.reverse ++ counter.reverse).length + 1]
(some ⟨some (.inr .finalReverse), initialState,
frame [] [] [] (original.reverse ++ counter.reverse) []⟩) = _ at h
have update : Reduction.MachineTransfer.tapesAt (Sum.inr ExtraTape.scratch)
(Sum.inr ExtraTape.output) (frame [] [] [] (original.reverse ++ counter.reverse) []) []
((frame [] [] [] (original.reverse ++ counter.reverse) [] (.inr .scratch)).reverse.map id ++
frame [] [] [] (original.reverse ++ counter.reverse) [] (.inr .output)) =
frame [] [] [] [] (counter ++ original) := by
funext tape
cases tape with
| inl i => simp [Reduction.MachineTransfer.tapesAt, frame]
| inr k => cases k <;> simp [Reduction.MachineTransfer.tapesAt, frame]
rw [update] at h
rw [← finish_eq]
simpa only [List.length_append, List.length_reverse, initialState] using! h
theorem finishTrace (counter rest original : List Bool) :
(MachineComposition.advance (TM2.step program))^[
rest.length + 2 * counter.length + 2 * original.length + 4]
(some (clockConfiguration (extraTapes rest original) none
(haltList MachineLogCounter.machine counter))) =
some (haltList machine (counter ++ original)) := by
rw [start_eq]
have total := trace_trans _
(trace_trans _ (trace_trans _ (drainTrace rest original counter)
(clockCopyTrace original counter)) (archiveCopyTrace original counter))
(reverseTrace original counter)
simpa only [show rest.length + 1 + (counter.length + 1) + (original.length + 1) +
(original.length + counter.length + 1) =
rest.length + 2 * counter.length + 2 * original.length + 4 by omega] using total
def finishInTime (counter rest original : List Bool) :
StateTransition.EvalsToInTime (TM2.step program)
(clockConfiguration (extraTapes rest original) none (haltList MachineLogCounter.machine counter))
(some (haltList machine (counter ++ original)))
(rest.length + 2 * counter.length + 2 * original.length + 4) where
steps := rest.length + 2 * counter.length + 2 * original.length + 4
evals_in_steps := finishTrace counter rest original
steps_le_m := le_rfl
end BinPackingGames.Foundations.Complexity.GraphCounterFinish
namespace BinPackingGames.Foundations.Complexity.GraphCounterPrefix
open Turing PCP GraphCounterModel GraphCounterFinish
def rawOutput (n m : Nat) (rest : List Bool) : List Bool :=
encodeWord ((n + m).log2 + 1) ++ (encodeWords [n, m] ++ rest)
def rawInTime (n m : Nat) (rest : List Bool) :
TM2OutputsInTime machine (encodeWords [n, m] ++ rest) (some (rawOutput n m rest))
(20 * (encodeWords [n, m] ++ rest).length + 30) := by
let original := encodeWords [n, m] ++ rest
let start := startInTime n m rest
let clock := clockInTime (extraTapes rest original) none (n + m)
let finish := finishInTime (encodeWord ((n + m).log2 + 1)) rest original
have start' : StateTransition.EvalsToInTime (TM2.step program)
(initList machine original)
(some (clockConfiguration (extraTapes rest original) none
(initList MachineLogCounter.machine (encodeWord (n + m)))))
(2 * (original.length + 1) + 1 + (n + 1) + (m + 1)) := start
let first := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ start' clock
let total := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ first finish
refine { toEvalsTo := total.toEvalsTo, steps_le_m := ?_ }
have bound := total.steps_le_m
have hlog := Nat.log2_le_self (n + m)
have hlength : original.length = n + m + 2 + rest.length := by
simp only [original, List.length_append, encodeWords, encodeWord_length, List.length_nil]
omega
simp only [encodeWord_length] at bound
change total.steps ≤ 20 * original.length + 30
omega
def count (table : GraphTables.Table) : Nat := (table.vertices + table.darts).log2 + 1
def output (table : GraphTables.Table) : List Bool :=
encodeWord (count table) ++ GraphTables.tableBits table
def rowsBits (table : GraphTables.Table) : List Bool :=
encodeWords ((GraphTables.rowList table).flatMap GraphTables.rowWords)
theorem tableBits_decomposition (table : GraphTables.Table) :
GraphTables.tableBits table = encodeWords [table.vertices, table.darts] ++ rowsBits table :=
encodeWords_append _ _
def outputsInTime (table : GraphTables.Table) :
TM2OutputsInTime machine (GraphTables.tableBits table) (some (output table))
(20 * (GraphTables.tableBits table).length + 30) := by
have run := rawInTime table.vertices table.darts (rowsBits table)
simpa only [rawOutput, output, count, tableBits_decomposition] using run
noncomputable def computableInPolyTime :
TM2ComputableInPolyTime GraphTables.tableBits id output where
tm := machine
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := 20 * Polynomial.X + 30
outputsFun table := by
change TM2OutputsInTime machine ((GraphTables.tableBits table).map id)
(some ((output table).map id))
((20 * Polynomial.X + 30 : Polynomial Nat).eval (GraphTables.tableBits table).length)
erw [List.map_id, List.map_id]
simpa only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_ofNat,
Polynomial.eval_X] using outputsInTime table
end BinPackingGames.Foundations.Complexity.GraphCounterPrefix
namespace BinPackingGames.Foundations.Complexity.GraphIterationBounds
open PCP RoundTables
def inputBits (input : Input) : List Bool := GraphTables.tableBits input.val
def countedBits (input : Input) : List Bool := GraphCounterPrefix.output input.val
def words (H : BaseTable) (input : Input) (i : Nat) : List Bool :=
GraphTables.tableBits (TableIteration.runTables H i input.val)
def count (input : Input) : Nat := GraphCounterPrefix.count input.val
noncomputable def lengthPolynomial : Polynomial Nat :=
(GraphTableComplexity.encodingPolynomial 64).comp (PCPIterationMachine.sizePolynomial sizeFactor)
theorem inputBits_le_countedBits (input : Input) : (inputBits input).length ≤ (countedBits input).length := by
simp only [inputBits, countedBits, GraphCounterPrefix.output, List.length_append]
omega
theorem size_le_inputBits (input : Input) : size input ≤ (inputBits input).length := by
have h := GraphTableComplexity.size_add_two_le_bits input.val
unfold size inputBits
omega
theorem count_le (input : Input) : count input ≤ (countedBits input).length + 1 :=
(PCPIterationMachine.rounds_le (size input)).trans
(Nat.add_le_add_right ((size_le_inputBits input).trans (inputBits_le_countedBits input)) 1)
theorem intermediate_size (H : BaseTable) (input : Input) (i : Nat) (hi : i ≤ count input) :
size (TableIteration.run H i input) ≤ PCPIterationMachine.sizeEnvelope sizeFactor (size input) := by
apply PCPIterationMachine.semanticSize_bound
(fun j => size (TableIteration.run H j input)) sizeFactor (size input)
sizeFactor_positive (Nat.le_succ _) _ i hi
intro j _
exact step_size H (TableIteration.run H j input)
theorem intermediate_bits (H : BaseTable) (input : Input) (i : Nat) (hi : i ≤ count input) :
(words H input i).length ≤ lengthPolynomial.eval (countedBits input).length := by
have hsize := intermediate_size H input i hi
have hinput := (size_le_inputBits input).trans (inputBits_le_countedBits input)
have heval := MachineComposition.natPolynomial_eval_mono
(PCPIterationMachine.sizePolynomial sizeFactor) hinput
rw [PCPIterationMachine.sizePolynomial_eval, PCPIterationMachine.sizePolynomial_eval] at heval
have hs := hsize.trans heval
have htable : (TableIteration.runTables H i input.val).vertices +
(TableIteration.runTables H i input.val).darts ≤
PCPIterationMachine.sizeEnvelope sizeFactor (countedBits input).length := by
rw [← TableIteration.run_val]
exact hs
rw [lengthPolynomial, Polynomial.eval_comp, PCPIterationMachine.sizePolynomial_eval]
exact GraphTableComplexity.bits_le_of_size_le _ htable
noncomputable def runtimePolynomial (bodyTime : Polynomial Nat) : Polynomial Nat :=
(Polynomial.X + 1) * (bodyTime.comp lengthPolynomial + 2 * lengthPolynomial + 4) +
2 * lengthPolynomial + 4
theorem runtimePolynomial_eval (bodyTime : Polynomial Nat) (N : Nat) :
(runtimePolynomial bodyTime).eval N =
(N + 1) * (bodyTime.eval (lengthPolynomial.eval N) + 2 * lengthPolynomial.eval N + 4) +
2 * lengthPolynomial.eval N + 4 := by
simp only [runtimePolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_X,
Polynomial.eval_one, Polynomial.eval_ofNat, Polynomial.eval_comp]
end BinPackingGames.Foundations.Complexity.GraphIterationBounds
end OAI