roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
7.01 kB
10
abbrev degree (d : Nat) : Nat := d * d
11
abbrev cloudSize (d : Nat) : Nat := degree d * degree d
12
abbrev rowFactor (d : Nat) : Nat := cloudSize d * degree d
14
structure Control (d : Nat) where
body · 6 lines
  firstH : Fin (cloudSize d) × Fin d
  lastInput : Fin d
  firstG : Fin (degree d)
  secondG : Fin (degree d)
  lastH : Fin (cloudSize d) × Fin d
  deriving DecidableEq, Fintype
22
def squarePorts {d : Nat} (s : Control d) : Fin (degree d) × Fin (degree d) :=
body · 1 line
  (rowIndex (degree d) (degree d)).symm s.firstH.1
25
def start {d : Nat} (H : Table (cloudSize d) d)
    (cloud : Fin (cloudSize d)) (port : Fin (degree d)) : Control d :=
body · 8 lines
  let ports := (rowIndex d d).symm port
  let first := lookup H (cloud, ports.1)
  let square := (rowIndex (degree d) (degree d)).symm first.1
  { firstH := first
    lastInput := ports.2
    firstG := square.1
    secondG := square.2
    lastH := first }
36
def receiveFirst {d : Nat} (s : Control d) (returnedPort : Fin (degree d)) : Control d :=
body · 1 line
  { s with firstG := returnedPort }
39
def receiveSecond {d : Nat} (H : Table (cloudSize d) d)
    (s : Control d) (returnedPort : Fin (degree d)) : Control d :=
body · 4 lines
  { s with
    secondG := returnedPort
    lastH := lookup H
      (rowIndex (degree d) (degree d) (returnedPort,s.firstG),s.lastInput) }
46
def firstOffset {d : Nat} (s : Control d) : Fin (degree d) := (squarePorts s).1
47
def secondOffset {d : Nat} (s : Control d) : Fin (degree d) := (squarePorts s).2
49
def outputPort {d : Nat} (s : Control d) : Fin (degree d) :=
body · 1 line
  rowIndex d d (s.lastH.2,s.firstH.2)
52
def outputOffset {d : Nat} (s : Control d) : Fin (rowFactor d) :=
body · 1 line
  rowIndex (cloudSize d) (degree d) (s.lastH.1,outputPort s)
55
def firstAddress {d : Nat} (vertex : Nat) (s : Control d) : Nat :=
body · 1 line
  degree d * vertex + (firstOffset s).val
58
def secondAddress {d : Nat} (vertex : Nat) (s : Control d) : Nat :=
body · 1 line
  degree d * vertex + (secondOffset s).val
61
def outputAddress {d : Nat} (vertex : Nat) (s : Control d) : Nat :=
body · 1 line
  rowFactor d * vertex + (outputOffset s).val
64
@[simp] theorem firstOffset_receiveFirst {d : Nat} (s : Control d) (r : Fin (degree d)) :
    firstOffset (receiveFirst s r) = firstOffset s := rfl
67
@[simp] theorem secondOffset_receiveFirst {d : Nat} (s : Control d) (r : Fin (degree d)) :
    secondOffset (receiveFirst s r) = secondOffset s := rfl
70
@[simp] theorem receiveFirst_port {d : Nat} (s : Control d) (r : Fin (degree d)) :
    (receiveFirst s r).firstG = r := rfl
73
@[simp] theorem receiveSecond_port {d : Nat} (H : Table (cloudSize d) d)
    (s : Control d) (r : Fin (degree d)) : (receiveSecond H s r).secondG = r := rfl
76
theorem firstAddress_eq_rowIndex {v d : Nat} (vertex : Fin v) (s : Control d) :
    firstAddress vertex.val s =
      (rowIndex v (degree d) (vertex,firstOffset s)).val := by
proof · 2 lines
  rw [rowIndex_val]
  exact Nat.add_comm _ _
82
theorem secondAddress_eq_rowIndex {v d : Nat} (vertex : Fin v) (s : Control d) :
    secondAddress vertex.val s =
      (rowIndex v (degree d) (vertex,secondOffset s)).val := by
proof · 2 lines
  rw [rowIndex_val]
  exact Nat.add_comm _ _
88
theorem firstAddress_lt {v d : Nat} (vertex : Fin v) (s : Control d) :
    firstAddress vertex.val s < v * degree d := by
proof · 2 lines
  rw [firstAddress_eq_rowIndex]
  exact (rowIndex v (degree d) (vertex,firstOffset s)).isLt
93
theorem secondAddress_lt {v d : Nat} (vertex : Fin v) (s : Control d) :
    secondAddress vertex.val s < v * degree d := by
proof · 2 lines
  rw [secondAddress_eq_rowIndex]
  exact (rowIndex v (degree d) (vertex,secondOffset s)).isLt
98
theorem lookup_vertex_div {v q : Nat} (G : Table v q) (x : Fin v × Fin q) :
    (lookup G x).1.val = (reverseIndex G (rowIndex v q x)).val / q := rfl
101
theorem lookup_port_mod {v q : Nat} (G : Table v q) (x : Fin v × Fin q) :
    (lookup G x).2.val = (reverseIndex G (rowIndex v q x)).val % q := rfl
104
theorem lookup_port_eq_remainder {v q : Nat} (G : Table v q) (x : Fin v × Fin q)
    (r : Fin q) (hr : r.val = (reverseIndex G (rowIndex v q x)).val % q) :
    (lookup G x).2 = r := Fin.ext ((lookup_port_mod G x).trans hr.symm)
108
theorem rowIndex_lookup {v q : Nat} (G : Table v q) (x : Fin v × Fin q) :
    rowIndex v q (lookup G x) = reverseIndex G (rowIndex v q x) := by
proof · 1 line
  simp only [lookup, Equiv.apply_symm_apply]
112
def outputPair {v d : Nat} (vertex : Fin v) (s : Control d) :
    Fin (v * cloudSize d) × Fin (degree d) :=
body · 1 line
  (rowIndex v (cloudSize d) (vertex,s.lastH.1),outputPort s)
116
theorem outputAddress_eq_rowIndex {v d : Nat} (vertex : Fin v) (s : Control d) :
    outputAddress vertex.val s =
      (rowIndex (v * cloudSize d) (degree d) (outputPair vertex s)).val := by
proof · 2 lines
  simp only [outputAddress, outputOffset, outputPair, rowIndex_val, rowFactor]
  ring
122
theorem outputAddress_lt {v d : Nat} (vertex : Fin v) (s : Control d) :
    outputAddress vertex.val s < (v * cloudSize d) * degree d := by
proof · 2 lines
  rw [outputAddress_eq_rowIndex]
  exact (rowIndex (v * cloudSize d) (degree d) (outputPair vertex s)).isLt
127
def evaluateRow {v d : Nat} (G : Table v (degree d)) (H : Table (cloudSize d) d)
    (vertex : Fin v) (cloud : Fin (cloudSize d)) (port : Fin (degree d)) :
    Fin v × Control d :=
body · 5 lines
  let s₀ := start H cloud port
  let g₁ := lookup G (vertex,firstOffset s₀)
  let s₁ := receiveFirst s₀ g₁.2
  let g₂ := lookup G (g₁.1,secondOffset s₁)
  (g₂.1, receiveSecond H s₁ g₂.2)
136
theorem evaluateRow_eq_lookup_step {v d : Nat}
    (G : Table v (degree d)) (H : Table (cloudSize d) d)
    (vertex : Fin v) (cloud : Fin (cloudSize d)) (port : Fin (degree d)) :
    outputPair (evaluateRow G H vertex cloud port).1 (evaluateRow G H vertex cloud port).2 =
      lookup (step G H) (rowIndex v (cloudSize d) (vertex,cloud),port) := by
proof · 3 lines
  rw [lookup_step]
  simp only [evaluateRow, start, receiveFirst, receiveSecond, firstOffset, secondOffset,
    squarePorts, outputPair, outputPort, stepLookup, Equiv.symm_apply_apply]
145
theorem outputAddress_eq_step_row {v d : Nat}
    (G : Table v (degree d)) (H : Table (cloudSize d) d)
    (vertex : Fin v) (cloud : Fin (cloudSize d)) (port : Fin (degree d)) :
    outputAddress (evaluateRow G H vertex cloud port).1.val (evaluateRow G H vertex cloud port).2 =
      (rowIndex (v * cloudSize d) (degree d)
        (lookup (step G H) (rowIndex v (cloudSize d) (vertex,cloud),port))).val := by
proof · 1 line
  rw [outputAddress_eq_rowIndex, evaluateRow_eq_lookup_step]
153
theorem outputAddress_eq_step_reverseIndex {v d : Nat}
    (G : Table v (degree d)) (H : Table (cloudSize d) d)
    (vertex : Fin v) (cloud : Fin (cloudSize d)) (port : Fin (degree d)) :
    outputAddress (evaluateRow G H vertex cloud port).1.val (evaluateRow G H vertex cloud port).2 =
      (reverseIndex (step G H) (rowIndex (v * cloudSize d) (degree d)
        (rowIndex v (cloudSize d) (vertex,cloud),port))).val := by
proof · 1 line
  rw [outputAddress_eq_step_row, rowIndex_lookup]