Download lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterModel.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 6.89 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterModel.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterModel.lean
-
curl -L -o GraphCounterModel.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/IndependentSets/Machines/GraphCounterModel.lean
6.89 kB
Lean 4 · 20
declarations statementsfull
inductive ExtraTape where body · 2 lines
| input | archive | scratch | output
deriving DecidableEqinstance : Fintype ExtraTape where body · 2 lines
elems := {.input, .archive, .scratch, .output}
complete tape := by cases tape <;> simpinductive ExtraLabel where body · 3 lines
| copyFirst | copySecond | seed | headerFirst | headerSecond
| clearInput | clockCopy | archiveCopy | finalReverse
deriving DecidableEqinstance : Fintype ExtraLabel where body · 3 lines
elems := {.copyFirst, .copySecond, .seed, .headerFirst, .headerSecond,
.clearInput, .clockCopy, .archiveCopy, .finalReverse}
complete label := by cases label <;> simpabbrev Tape := Fin 3 ⊕ ExtraTape abbrev Label := Fin 5 ⊕ ExtraLabel abbrev Alphabet (_ : Tape) := Bool abbrev State := MachineLogCounter.State × Option Bool def initialState : State := (MachineLogCounter.initialState, none) def clockLabel : Option (Fin 5) → Option Label body · 2 lines
| none => some (.inr .clearInput)
| some l => some (.inl l)def clockStatement : TM2.Stmt MachineLogCounter.Alphabet (Fin 5) MachineLogCounter.State →
TM2.Stmt Alphabet Label State body · 7 lines
| .push k f next => .push (.inl k) (fun state => f state.1) (clockStatement next)
| .peek k f next => .peek (.inl k) (fun state x => (f state.1 x, state.2)) (clockStatement next)
| .pop k f next => .pop (.inl k) (fun state x => (f state.1 x, state.2)) (clockStatement next)
| .load f next => .load (fun state => (f state.1, state.2)) (clockStatement next)
| .branch f yes no => .branch (fun state => f state.1) (clockStatement yes) (clockStatement no)
| .goto f => .goto (fun state => .inl (f state.1))
| .halt => .goto (fun _ => .inr .clearInput)def readHeader (again next : Label) : TM2.Stmt Alphabet Label State := body · 4 lines
.pop (.inr .input) (fun state head => (state.1, head))
(.branch (fun state => state.2.getD false)
(.push (.inl 0) (fun _ => true) (.goto fun _ => again))
(.load (fun state => (state.1, none)) (.goto fun _ => next)))def program : Label → TM2.Stmt Alphabet Label State body · 16 lines
| .inl l => clockStatement (MachineLogCounter.program l)
| .inr .copyFirst => Reduction.MachineTransfer.loopAt (.inr .input) (.inr .scratch)
id false (.inr .copyFirst) (some (.inr .copySecond))
| .inr .copySecond => MachineCopy.forkLoop (.inr .scratch) (.inr .input) (.inr .archive)
false (.inr .copySecond) (some (.inr .seed))
| .inr .seed => .push (.inl 0) (fun _ => false) (.goto fun _ => .inr .headerFirst)
| .inr .headerFirst => readHeader (.inr .headerFirst) (.inr .headerSecond)
| .inr .headerSecond => readHeader (.inr .headerSecond) (.inl 0)
| .inr .clearInput => MachineDrain.drain (.inr .input) (.inr .clearInput)
(some (.inr .clockCopy))
| .inr .clockCopy => Reduction.MachineTransfer.loopAt (.inl 2) (.inr .scratch)
id false (.inr .clockCopy) (some (.inr .archiveCopy))
| .inr .archiveCopy => Reduction.MachineTransfer.loopAt (.inr .archive) (.inr .scratch)
id false (.inr .archiveCopy) (some (.inr .finalReverse))
| .inr .finalReverse => Reduction.MachineTransfer.loopAt (.inr .scratch) (.inr .output)
id false (.inr .finalReverse) nonedef machine : FinTM2 where body · 9 lines
K := Tape
k₀ := .inr .input
k₁ := .inr .output
Γ := Alphabet
Λ := Label
main := .inr .copyFirst
σ := State
initialState := initialState
m := programdef clockTapes (tapes : Fin 3 → List Bool) (extra : ExtraTape → List Bool) : Tape → List Bool body · 2 lines
| .inl k => tapes k
| .inr k => extra kdef clockConfiguration (extra : ExtraTape → List Bool) (register : Option Bool)
(c : TM2.Cfg MachineLogCounter.Alphabet (Fin 5) MachineLogCounter.State) :
TM2.Cfg Alphabet Label State := ⟨clockLabel c.l, (c.var, register), clockTapes c.stk extra⟩ theorem clockTapes_update (tapes : Fin 3 → List Bool) (extra : ExtraTape → List Bool)
(k : Fin 3) (word : List Bool) :
clockTapes (Function.update tapes k word) extra =
Function.update (clockTapes tapes extra) (.inl k) word := by proof · 2 lines
funext tape
cases tape <;> simp [clockTapes, Function.comp_def]theorem clockStatement_simulation (extra : ExtraTape → List Bool) (register : Option Bool)
(statement : TM2.Stmt MachineLogCounter.Alphabet (Fin 5) MachineLogCounter.State)
(state : MachineLogCounter.State) (tapes : Fin 3 → List Bool) :
TM2.stepAux (clockStatement statement) (state, register) (clockTapes tapes extra) =
clockConfiguration extra register (TM2.stepAux statement state tapes) := by proof · 19 lines
induction statement generalizing state tapes with
| push k f next ih =>
simp only [clockStatement, TM2.stepAux, clockTapes]
rw [← clockTapes_update]
exact ih state (Function.update tapes k (f state :: tapes k))
| peek k f next ih =>
simpa only [clockStatement, TM2.stepAux, clockTapes] using ih (f state (tapes k).head?) tapes
| pop k f next ih =>
simp only [clockStatement, TM2.stepAux, clockTapes]
rw [← clockTapes_update]
exact ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail)
| load f next ih =>
simpa only [clockStatement, TM2.stepAux] using ih (f state) tapes
| branch f yes no ihYes ihNo =>
cases h : f state with
| false => simpa only [clockStatement, TM2.stepAux, h, Bool.cond_false] using ihNo state tapes
| true => simpa only [clockStatement, TM2.stepAux, h, Bool.cond_true] using ihYes state tapes
| goto f => rfl
| halt => rfltheorem clockStep (extra : ExtraTape → List Bool) (register : Option Bool)
(a b : MachineLogCounter.machine.Cfg)
(h : MachineLogCounter.machine.step a = some b) :
TM2.step program (clockConfiguration extra register a) =
some (clockConfiguration extra register b) := by proof · 9 lines
cases a with
| mk label state tapes =>
cases label with
| none => cases h
| some label =>
have hb := Option.some.inj h
subst b
exact congrArg some (clockStatement_simulation extra register
(MachineLogCounter.program label) state tapes)def clockInTime (extra : ExtraTape → List Bool) (register : Option Bool) (n : Nat) :
StateTransition.EvalsToInTime (TM2.step program)
(clockConfiguration extra register (initList MachineLogCounter.machine (encodeWord n)))
(some (clockConfiguration extra register
(haltList MachineLogCounter.machine (encodeWord (n.log2 + 1))))) (8 * n + 4) := body · 2 lines
MachineComposition.liftExecutionInTime _ _ (clockConfiguration extra register)
(clockStep extra register) (MachineLogCounter.outputsInTime n)Outline
- inductive
ExtraTape12 - instance
16 - inductive
ExtraLabel20 - instance
25 - abbrev
Tape30 - abbrev
Label31 - abbrev
Alphabet32 - abbrev
State33 - def
initialState35 - def
clockLabel37 - def
clockStatement41 - def
readHeader51 - def
program57 - def
machine75 - def
clockTapes86 - def
clockConfiguration90 - theorem
clockTapes_update94 - theorem
clockStatement_simulation101 - theorem
clockStep126 - def
clockInTime141