Download lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterPrefix.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 12.1 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterPrefix.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterPrefix.lean
-
curl -L -o GraphCounterPrefix.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterPrefix.lean
12.1 kB
Lean 4 · 30
declarations statementsfull
def rawOutput (n m : Nat) (rest : List Bool) : List Bool := body · 1 line
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 body · 20 lines
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
omegadef count (table : GraphTables.Table) : Nat := (table.vertices + table.darts).log2 + 1 def output (table : GraphTables.Table) : List Bool := body · 1 line
encodeWord (count table) ++ GraphTables.tableBits tabledef rowsBits (table : GraphTables.Table) : List Bool := body · 1 line
encodeWords ((GraphTables.rowList table).flatMap GraphTables.rowWords)theorem tableBits_decomposition (table : GraphTables.Table) :
GraphTables.tableBits table = encodeWords [table.vertices, table.darts] ++ rowsBits table := proof · 1 line
encodeWords_append _ _
def outputsInTime (table : GraphTables.Table) :
TM2OutputsInTime machine (GraphTables.tableBits table) (some (output table))
(20 * (GraphTables.tableBits table).length + 30) := by body · 2 lines
have run := rawInTime table.vertices table.darts (rowsBits table)
simpa only [rawOutput, output, count, tableBits_decomposition] using runnoncomputable def computableInPolyTime :
TM2ComputableInPolyTime GraphTables.tableBits id output where body · 13 lines
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)
dsimp only [machine, Alphabet]
rw [List.map_id, List.map_id]
simp only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_ofNat,
Polynomial.eval_X]
exact outputsInTime tabledef rounds (n : Nat) : Nat := n.log2 + 1 theorem rounds_le (n : Nat) : rounds n ≤ n + 1 := proof · 1 line
Nat.add_le_add_right (Nat.log2_le_self n) 1theorem two_pow_rounds_le (n : Nat) : 2 ^ rounds n ≤ 2 * (n + 1) := by proof · 5 lines
by_cases hn : n = 0
· subst n; decide
· have h := Nat.log2_self_le hn
simp only [rounds, pow_succ]
omegatheorem growth_pow_rounds_le (growth n : Nat) :
growth ^ rounds n ≤ (2 * (n + 1)) ^ growth := by proof · 5 lines
calc
growth ^ rounds n ≤ (2 ^ growth) ^ rounds n :=
Nat.pow_le_pow_left (Nat.le_of_lt (Nat.lt_two_pow_self (n := growth))) _
_ = (2 ^ rounds n) ^ growth := by simp only [← pow_mul, Nat.mul_comm]
_ ≤ (2 * (n + 1)) ^ growth := Nat.pow_le_pow_left (two_pow_rounds_le n) growthdef sizeEnvelope (growth n : Nat) : Nat := (n + 1) * (2 * (n + 1)) ^ growth theorem semanticSize_geometric (sizes : Nat → Nat) (growth n : Nat)
(initialBound : sizes 0 ≤ n + 1)
(growthBound : ∀ i, i < rounds n → sizes (i + 1) ≤ growth * sizes i)
(i : Nat) (hi : i ≤ rounds n) : sizes i ≤ (n + 1) * growth ^ i := by proof · 9 lines
induction i with
| zero => simpa only [pow_zero, Nat.mul_one] using initialBound
| succ i ih =>
have ilt : i < rounds n := by omega
calc
sizes (i + 1) ≤ growth * sizes i := growthBound i ilt
_ ≤ growth * ((n + 1) * growth ^ i) :=
Nat.mul_le_mul_left growth (ih (by omega))
_ = (n + 1) * growth ^ (i + 1) := by rw [pow_succ]; ac_rfltheorem semanticSize_bound (sizes : Nat → Nat) (growth n : Nat)
(growthPositive : 0 < growth)
(initialBound : sizes 0 ≤ n + 1)
(growthBound : ∀ i, i < rounds n → sizes (i + 1) ≤ growth * sizes i)
(i : Nat) (hi : i ≤ rounds n) : sizes i ≤ sizeEnvelope growth n := by proof · 7 lines
calc
sizes i ≤ (n + 1) * growth ^ i :=
semanticSize_geometric sizes growth n initialBound growthBound i hi
_ ≤ (n + 1) * growth ^ rounds n :=
Nat.mul_le_mul_left _ (Nat.pow_le_pow_right growthPositive hi)
_ ≤ sizeEnvelope growth n :=
Nat.mul_le_mul_left _ (growth_pow_rounds_le growth n)noncomputable def sizePolynomial (growth : Nat) : Polynomial Nat := body · 1 line
(Polynomial.X + 1) * (2 * (Polynomial.X + 1)) ^ growth@[simp] theorem sizePolynomial_eval (growth n : Nat) :
(sizePolynomial growth).eval n = sizeEnvelope growth n := by proof · 1 line
simp [sizePolynomial, sizeEnvelope]
noncomputable def timePolynomial (growth : Nat)
(encodingSize bodyTime : Polynomial Nat) : Polynomial Nat := body · 2 lines
(Polynomial.X + 1) *
((bodyTime.comp encodingSize).comp (sizePolynomial growth) + 1) + 2@[simp] theorem timePolynomial_eval (growth n : Nat)
(encodingSize bodyTime : Polynomial Nat) :
(timePolynomial growth encodingSize bodyTime).eval n =
(n + 1) * (bodyTime.eval (encodingSize.eval (sizeEnvelope growth n)) + 1) + 2 := by proof · 1 line
simp [timePolynomial]
abbrev Alphabet (_ : K) := Bool abbrev State (σ : Type) := σ × Option Bool abbrev Labels (Λ : Type) := Bool ⊕ Λ def program (counter : K) (entry : Λ)
(body : Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ)) :
Labels Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ) body · 3 lines
| .inl false => MachineUnaryCounter.guard counter (.inr entry) (.inl true)
| .inl true => .pop counter (fun state _ => (state.1, none)) .halt
| .inr label => body label@[simp] theorem program_guard (counter : K) (entry : Λ)
(body : Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ)) :
program counter entry body (.inl false) =
MachineUnaryCounter.guard counter (.inr entry) (.inl true) := rfl def machine [Fintype K] [Fintype Λ] [Fintype σ]
(counter output : K) (entry : Λ) (initial : σ)
(body : Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ)) : FinTM2 where body · 9 lines
K := K
k₀ := counter
k₁ := output
Γ := Alphabet
Λ := Labels Λ
main := .inl false
σ := State σ
initialState := (initial, none)
m := program counter entry bodydef haltedConfiguration (counter : K) (suffix : List Bool)
(ambient : Nat → σ) (base : Nat → K → List Bool) :
TM2.Cfg (Alphabet (K := K)) (Labels Λ) (State σ) := body · 1 line
⟨none, (ambient 0, none), Function.update (base 0) counter suffix⟩theorem haltStep (counter : K) (entry : Λ)
(body : Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ))
(suffix : List Bool) (ambient : Nat → σ) (base : Nat → K → List Bool) :
TM2.step (program counter entry body)
(exitConfiguration counter (.inl true) suffix ambient base) =
some (haltedConfiguration counter suffix ambient base) := by proof · 3 lines
change some (TM2.stepAux (.pop counter (fun state _ => (state.1, none)) .halt)
(ambient 0, none) (MachineUnaryCounter.counterTapes counter (base 0) 0 suffix)) = _
simp [TM2.stepAux, MachineUnaryCounter.counterTapes, encodeWord, haltedConfiguration]theorem iterationTrace (counter : K) (entry : Λ)
(body : Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ))
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat)
(bodyTraces : BodyTraces counter (.inl false) (.inr entry)
(program counter entry body) suffix ambient register base cost (rounds n)) :
(MachineComposition.advance (TM2.step (program counter entry body)))^[
totalSteps cost (rounds n) + 1]
(some (guardConfiguration counter (.inl false) suffix ambient register base (rounds n))) =
some (haltedConfiguration counter suffix ambient base) := by proof · 5 lines
rw [Function.iterate_succ_apply']
rw [loopTrace counter (.inl false) (.inr entry) (.inl true)
(program counter entry body) (program_guard counter entry body) suffix
ambient register base cost (rounds n) bodyTraces]
exact haltStep counter entry body suffix ambient basetheorem bodyCosts_bound (growth n : Nat) (growthPositive : 0 < growth)
(sizes encodedSizes cost : Nat → Nat) (encodingSize bodyTime : Polynomial Nat)
(initialBound : sizes 0 ≤ n + 1)
(growthBound : ∀ i, i < rounds n → sizes (i + 1) ≤ growth * sizes i)
(encodingBound : ∀ r, r < rounds n →
encodedSizes r ≤ encodingSize.eval (sizes (rounds n - (r + 1))))
(bodyCost : ∀ r, r < rounds n → cost r ≤ bodyTime.eval (encodedSizes r)) :
∀ r, r < rounds n →
cost r ≤ bodyTime.eval (encodingSize.eval (sizeEnvelope growth n)) := by proof · 7 lines
intro r hr
have hs := semanticSize_bound sizes growth n growthPositive initialBound growthBound
(rounds n - (r + 1)) (Nat.sub_le _ _)
exact (bodyCost r hr).trans
(MachineComposition.natPolynomial_eval_mono bodyTime
((encodingBound r hr).trans
(MachineComposition.natPolynomial_eval_mono encodingSize hs)))def iterationInTime (counter : K) (entry : Λ)
(body : Λ → TM2.Stmt (Alphabet (K := K)) (Labels Λ) (State σ))
(suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool)
(base : Nat → K → List Bool) (cost : Nat → Nat) (growth n : Nat)
(growthPositive : 0 < growth)
(bodyTraces : BodyTraces counter (.inl false) (.inr entry)
(program counter entry body) suffix ambient register base cost (rounds n))
(sizes encodedSizes : Nat → Nat) (encodingSize bodyTime : Polynomial Nat)
(initialBound : sizes 0 ≤ n + 1)
(growthBound : ∀ i, i < rounds n → sizes (i + 1) ≤ growth * sizes i)
(encodingBound : ∀ r, r < rounds n →
encodedSizes r ≤ encodingSize.eval (sizes (rounds n - (r + 1))))
(bodyCost : ∀ r, r < rounds n → cost r ≤ bodyTime.eval (encodedSizes r)) :
StateTransition.EvalsToInTime (TM2.step (program counter entry body))
(guardConfiguration counter (.inl false) suffix ambient register base (rounds n))
(some (haltedConfiguration counter suffix ambient base))
((timePolynomial growth encodingSize bodyTime).eval n) where body · 11 lines
steps := totalSteps cost (rounds n) + 1
evals_in_steps := iterationTrace counter entry body suffix ambient register base cost n bodyTraces
steps_le_m := by
rw [timePolynomial_eval]
have h := totalSteps_le cost (rounds n)
(bodyTime.eval (encodingSize.eval (sizeEnvelope growth n)))
(bodyCosts_bound growth n growthPositive sizes encodedSizes cost encodingSize bodyTime
initialBound growthBound encodingBound bodyCost)
have hm := Nat.mul_le_mul_right
(bodyTime.eval (encodingSize.eval (sizeEnvelope growth n)) + 1) (rounds_le n)
omegaOutline
- def
rawOutput12 - def
rawInTime15 - def
count39 - def
output41 - def
rowsBits44 - theorem
tableBits_decomposition47 - def
outputsInTime51 - def
computableInPolyTime57 - def
rounds80 - theorem
rounds_le82 - theorem
two_pow_rounds_le85 - theorem
growth_pow_rounds_le92 - def
sizeEnvelope100 - theorem
semanticSize_geometric102 - theorem
semanticSize_bound116 - def
sizePolynomial129 - theorem
sizePolynomial_eval132 - def
timePolynomial136 - theorem
timePolynomial_eval141 - abbrev
Alphabet149 - abbrev
State150 - abbrev
Labels151 - def
program153 - theorem
program_guard161 - def
machine166 - def
haltedConfiguration179 - theorem
haltStep184 - theorem
iterationTrace194 - theorem
bodyCosts_bound210 - def
iterationInTime227