Download lean/OAI/Combinatorics/IndependentSets/Machines/MachineControl.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 8.75 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/IndependentSets/Machines/MachineControl.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Combinatorics/IndependentSets/Machines/MachineControl.lean
-
curl -L -o MachineControl.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/IndependentSets/Machines/MachineControl.lean
8.75 kB
Lean 4 · 34
declarations statementsfull
def configuration (labels : Λ → Λ') (states : σ ≃ σ') (c : Cfg Γ Λ σ) :
Cfg Γ Λ' σ' where body · 3 lines
l := c.l.map labels
var := states c.var
stk := c.stkdef statement (labels : Λ → Λ') (states : σ ≃ σ') : Stmt Γ Λ σ → Stmt Γ Λ' σ' body · 11 lines
| .push k f next => .push k (fun st => f (states.symm st)) (statement labels states next)
| .peek k f next => .peek k (fun st v => states (f (states.symm st) v))
(statement labels states next)
| .pop k f next => .pop k (fun st v => states (f (states.symm st) v))
(statement labels states next)
| .load f next => .load (fun st => states (f (states.symm st)))
(statement labels states next)
| .branch f yes no => .branch (fun st => f (states.symm st))
(statement labels states yes) (statement labels states no)
| .goto f => .goto (fun st => labels (f (states.symm st)))
| .halt => .halttheorem stepAux_simulation (labels : Λ → Λ') (states : σ ≃ σ')
(q : Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) :
stepAux (statement labels states q) (states state) tapes =
configuration labels states (stepAux q state tapes) := by proof · 22 lines
induction q generalizing state tapes with
| push k f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using
ih state (Function.update tapes k (f state :: tapes k))
| peek k f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using
ih (f state (tapes k).head?) tapes
| pop k f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using
ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail)
| load f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using ih (f state) tapes
| branch f yes no ihYes ihNo =>
cases h : f state with
| false =>
simpa only [statement, stepAux, Equiv.symm_apply_apply, h, Bool.cond_false] using
ihNo state tapes
| true =>
simpa only [statement, stepAux, Equiv.symm_apply_apply, h, Bool.cond_true] using
ihYes state tapes
| goto f => simp [statement, stepAux, configuration]
| halt => rfldef program (labels : Λ ≃ Λ') (states : σ ≃ σ') (source : Λ → Stmt Γ Λ σ) :
Λ' → Stmt Γ Λ' σ' := fun l => statement labels states (source (labels.symm l)) theorem step_simulation (labels : Λ ≃ Λ') (states : σ ≃ σ')
(source : Λ → Stmt Γ Λ σ) (c : Cfg Γ Λ σ) :
step (program labels states source) (configuration labels states c) =
(step source c).map (configuration labels states) := by proof · 9 lines
cases c with
| mk l state tapes =>
cases l with
| none => rfl
| some l =>
change some (stepAux (statement labels states (source (labels.symm (labels l))))
(states state) tapes) = _
rw [Equiv.symm_apply_apply, stepAux_simulation]
rflinductive Tape body · 4 lines
| original | cursor | scan | vertices | darts | counter | edge
| tail | reverseIndex | head | scratch | indexScan
| compareLeft | compareRight | reversed | output
deriving DecidableEqinstance : Fintype Tape where body · 4 lines
elems := {.original, .cursor, .scan, .vertices, .darts, .counter, .edge,
.tail, .reverseIndex, .head, .scratch, .indexScan,
.compareLeft, .compareRight, .reversed, .output}
complete tape := by cases tape <;> simpabbrev Alphabet (_ : Tape) := Bool abbrev Flags := Bool × Bool × Option Bool abbrev Ambient (q : Nat) := Flags × GenericGraphTables.RelationTable q abbrev State (q : Nat) := Ambient q × Option Bool def normal {q : Nat} (relation : GenericGraphTables.RelationTable q) (selfLoop : Bool) : State q := body · 1 line
(((selfLoop, false, none), relation), none)def initial (q : Nat) : State q := normal (Vector.replicate (q * q) false) false def context {q : Nat} (ambient : Ambient q) : GenericGraphTables.RelationTable q × Bool := body · 1 line
(ambient.2, ambient.1.1)def compareEquiv (q : Nat) : State q ≃
MachineCompare.State (GenericGraphTables.RelationTable q × Bool) where body · 4 lines
toFun s := ((s.1.2, s.1.1.1), s.1.1.2.1, s.1.1.2.2, s.2)
invFun s := (((s.1.2, s.2.1, s.2.2.1), s.1.1), s.2.2.2)
left_inv _ := rfl
right_inv _ := rfl@[simp] theorem compareEquiv_normal {q : Nat}
(relation : GenericGraphTables.RelationTable q) (selfLoop : Bool) :
compareEquiv q (normal relation selfLoop) =
((relation, selfLoop), false, none, none) := rfl def emitterEquiv (q : Nat) : ((Ambient q × Unit) × Option Bool) ≃ State q where body · 4 lines
toFun s := (s.1.1, s.2)
invFun s := ((s.1, ()), s.2)
left_inv s := by rcases s with ⟨⟨s, u⟩, r⟩; cases u; rfl
right_inv _ := rfldef setupPorts : Fin 7 → Tape := body · 1 line
![.original, .cursor, .vertices, .darts, .counter, .scratch, .edge]
theorem setupPorts_injective : Function.Injective setupPorts := by proof · 2 lines
intro i j h fin_cases i <;> fin_cases j <;> simp_all [setupPorts]
def headPorts : Fin 6 → Tape := body · 1 line
![.original, .reverseIndex, .scan, .indexScan, .head, .scratch]
theorem headPorts_injective : Function.Injective headPorts := by proof · 2 lines
intro i j h fin_cases i <;> fin_cases j <;> simp_all [headPorts]
def rowSources : Fin 5 → Tape := ![.vertices, .darts, .edge, .tail, .head] def headerSources : Fin 2 → Tape := ![.vertices, .darts] def fieldPorts : Fin 3 → Tape := ![.tail, .reverseIndex, .head] def cleanupPorts : Fin 15 → Tape := body · 2 lines
![.original, .cursor, .scan, .vertices, .darts, .counter, .edge,
.tail, .reverseIndex, .head, .scratch, .indexScan, .compareLeft, .compareRight, .reversed]theorem cleanupPorts_ne_output (i : Fin 15) : cleanupPorts i ≠ .output := by proof · 1 line
fin_cases i <;> simp [cleanupPorts]
theorem cleanupPorts_cover (tape : Tape) (h : tape ≠ .output) :
∃ i, cleanupPorts i = tape := by proof · 17 lines
cases tape
· exact ⟨0, rfl⟩
· exact ⟨1, rfl⟩
· exact ⟨2, rfl⟩
· exact ⟨3, rfl⟩
· exact ⟨4, rfl⟩
· exact ⟨5, rfl⟩
· exact ⟨6, rfl⟩
· exact ⟨7, rfl⟩
· exact ⟨8, rfl⟩
· exact ⟨9, rfl⟩
· exact ⟨10, rfl⟩
· exact ⟨11, rfl⟩
· exact ⟨12, rfl⟩
· exact ⟨13, rfl⟩
· exact ⟨14, rfl⟩
· exact (h rfl).elimdef compareStatement (stmt : TM2.Stmt Alphabet Λ
(MachineCompare.State (GenericGraphTables.RelationTable q × Bool))) :
TM2.Stmt Alphabet Λ (State q) := body · 1 line
MachineControl.statement id (compareEquiv q).symm stmtdef emitterStatement (stmt : TM2.Stmt Alphabet Λ ((Ambient q × Unit) × Option Bool)) :
TM2.Stmt Alphabet Λ (State q) := body · 1 line
MachineControl.statement id (emitterEquiv q) stmtdef storeLoop (value : Bool) (next : Λ) : TM2.Stmt Alphabet Λ (State q) := body · 1 line
.load (fun state => (((value, false, none), state.1.2), none)) (.goto fun _ => next)theorem stepAux_storeLoop (value : Bool) (next : Λ) (state : State q)
(base : Tape → List Bool) :
TM2.stepAux (storeLoop value next) state base =
⟨some next, normal state.1.2 value, base⟩ := rfl def nextRow (next : Λ) : TM2.Stmt Alphabet Λ (State q) := body · 2 lines
.push .edge (fun _ => true)
(.load (fun state => (state.1, none)) (.goto fun _ => next))theorem stepAux_nextRow (next : Λ) (state : State q)
(base : Tape → List Bool) (edge : Nat) (suffix : List Bool)
(he : base .edge = encodeWord edge ++ suffix) :
TM2.stepAux (nextRow next) state base =
⟨some next, (state.1, none),
Function.update base .edge (encodeWord (edge + 1) ++ suffix)⟩ := by proof · 1 line
simp [nextRow, TM2.stepAux, he, encodeWord, List.replicate_succ]def transportInTime {K τ υ : Type} [DecidableEq K] {Γ : K → Type}
(e : τ ≃ υ) (p : Λ → TM2.Stmt Γ Λ τ)
{start finish : TM2.Cfg Γ Λ τ} {budget : Nat}
(run : StateTransition.EvalsToInTime (TM2.step p) start (some finish) budget) :
StateTransition.EvalsToInTime (TM2.step (MachineControl.program (Equiv.refl Λ) e p))
(MachineControl.configuration id e start)
(some (MachineControl.configuration id e finish)) budget := by body · 5 lines
apply MachineComposition.liftExecutionInTime _ _ (MachineControl.configuration id e) _ run
intro a b hab
have h := MachineControl.step_simulation (Equiv.refl Λ) e p a
rw [hab] at h
exact hOutline
- def
configuration13 - def
statement19 - theorem
stepAux_simulation34 - def
program61 - theorem
step_simulation64 - inductive
Tape84 - instance
90 - abbrev
Alphabet96 - abbrev
Flags98 - abbrev
Ambient99 - abbrev
State100 - def
normal102 - def
initial105 - def
context107 - def
compareEquiv110 - theorem
compareEquiv_normal117 - def
emitterEquiv122 - def
setupPorts128 - theorem
setupPorts_injective131 - def
headPorts135 - theorem
headPorts_injective138 - def
rowSources142 - def
headerSources143 - def
fieldPorts144 - def
cleanupPorts146 - theorem
cleanupPorts_ne_output150 - theorem
cleanupPorts_cover153 - def
compareStatement175 - def
emitterStatement180 - def
storeLoop184 - theorem
stepAux_storeLoop187 - def
nextRow192 - theorem
stepAux_nextRow196 - def
transportInTime204