roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
8.75 kB
13
def configuration (labels : Λ → Λ') (states : σ ≃ σ') (c : Cfg Γ Λ σ) :
    Cfg Γ Λ' σ' where
body · 3 lines
  l := c.l.map labels
  var := states c.var
  stk := c.stk
19
def 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 => .halt
34
theorem 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 => rfl
61
def program (labels : Λ ≃ Λ') (states : σ ≃ σ') (source : Λ → Stmt Γ Λ σ) :
    Λ' → Stmt Γ Λ' σ' := fun l => statement labels states (source (labels.symm l))
64
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]
      rfl
84
inductive Tape
body · 4 lines
  | original | cursor | scan | vertices | darts | counter | edge
  | tail | reverseIndex | head | scratch | indexScan
  | compareLeft | compareRight | reversed | output
  deriving DecidableEq
90
instance : 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 <;> simp
96
abbrev Alphabet (_ : Tape) := Bool
98
abbrev Flags := Bool × Bool × Option Bool
99
abbrev Ambient (q : Nat) := Flags × GenericGraphTables.RelationTable q
100
abbrev State (q : Nat) := Ambient q × Option Bool
102
def normal {q : Nat} (relation : GenericGraphTables.RelationTable q) (selfLoop : Bool) : State q :=
body · 1 line
  (((selfLoop, false, none), relation), none)
105
def initial (q : Nat) : State q := normal (Vector.replicate (q * q) false) false
107
def context {q : Nat} (ambient : Ambient q) : GenericGraphTables.RelationTable q × Bool :=
body · 1 line
  (ambient.2, ambient.1.1)
110
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
117
@[simp] theorem compareEquiv_normal {q : Nat}
    (relation : GenericGraphTables.RelationTable q) (selfLoop : Bool) :
    compareEquiv q (normal relation selfLoop) =
      ((relation, selfLoop), false, none, none) := rfl
122
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 _ := rfl
128
def setupPorts : Fin 7 → Tape :=
body · 1 line
  ![.original, .cursor, .vertices, .darts, .counter, .scratch, .edge]
131
theorem setupPorts_injective : Function.Injective setupPorts := by
proof · 2 lines
  intro i j h
  fin_cases i <;> fin_cases j <;> simp_all [setupPorts]
135
def headPorts : Fin 6 → Tape :=
body · 1 line
  ![.original, .reverseIndex, .scan, .indexScan, .head, .scratch]
138
theorem headPorts_injective : Function.Injective headPorts := by
proof · 2 lines
  intro i j h
  fin_cases i <;> fin_cases j <;> simp_all [headPorts]
142
def rowSources : Fin 5 → Tape := ![.vertices, .darts, .edge, .tail, .head]
143
def headerSources : Fin 2 → Tape := ![.vertices, .darts]
144
def fieldPorts : Fin 3 → Tape := ![.tail, .reverseIndex, .head]
146
def cleanupPorts : Fin 15 → Tape :=
body · 2 lines
  ![.original, .cursor, .scan, .vertices, .darts, .counter, .edge,
    .tail, .reverseIndex, .head, .scratch, .indexScan, .compareLeft, .compareRight, .reversed]
150
theorem cleanupPorts_ne_output (i : Fin 15) : cleanupPorts i ≠ .output := by
proof · 1 line
  fin_cases i <;> simp [cleanupPorts]
153
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).elim
175
def 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 stmt
180
def emitterStatement (stmt : TM2.Stmt Alphabet Λ ((Ambient q × Unit) × Option Bool)) :
    TM2.Stmt Alphabet Λ (State q) :=
body · 1 line
  MachineControl.statement id (emitterEquiv q) stmt
184
def 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)
187
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
192
def nextRow (next : Λ) : TM2.Stmt Alphabet Λ (State q) :=
body · 2 lines
  .push .edge (fun _ => true)
    (.load (fun state => (state.1, none)) (.goto fun _ => next))
196
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]
204
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 h