roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
7.61 kB
11
def remainingEvents (table : GraphTables.Table) (r : Nat) : List (Fin table.darts) :=
body · 1 line
  (List.finRange table.darts).drop r
14
def processedEvents (table : GraphTables.Table) (r : Nat) : List (Fin table.darts) :=
body · 1 line
  (List.finRange table.darts).take r
17
def inputStream (table : GraphTables.Table) (events : List (Fin table.darts)) : List Bool :=
body · 1 line
  events.flatMap (fun e => encodeWords (GraphTables.rowWords table.rows[e]))
20
def outputStream (plan : Plan) (table : GraphTables.Table)
    (events : List (Fin table.darts)) : List Bool :=
body · 2 lines
  events.flatMap (fun e => plan.flatMap (Emitter.commandBits (rowOperands table e)
    (rowRelation table e)))
25
@[simp] theorem remainingEvents_zero (table : GraphTables.Table) :
    remainingEvents table 0 = List.finRange table.darts := by
proof · 1 line
  simp [remainingEvents]
29
@[simp] theorem remainingEvents_length (table : GraphTables.Table) (r : Nat) :
    (remainingEvents table r).length = table.darts - r := by
proof · 1 line
  simp [remainingEvents]
33
@[simp] theorem remainingEvents_done (table : GraphTables.Table) :
    remainingEvents table table.darts = [] := by
proof · 1 line
  simp [remainingEvents]
37
theorem remainingEvents_cons (table : GraphTables.Table) (r : Nat) (h : r < table.darts) :
    remainingEvents table r = ⟨r, h⟩ :: remainingEvents table (r + 1) := by
proof · 4 lines
  unfold remainingEvents
  rw [List.drop_eq_getElem_cons (l := List.finRange table.darts) (i := r)
    (by simpa using h)]
  simp
44
@[simp] theorem processedEvents_zero (table : GraphTables.Table) :
    processedEvents table 0 = [] := by simp [processedEvents]
47
@[simp] theorem processedEvents_done (table : GraphTables.Table) :
    processedEvents table table.darts = List.finRange table.darts := by
proof · 1 line
  simp [processedEvents]
51
theorem processedEvents_succ (table : GraphTables.Table) (r : Nat) (h : r < table.darts) :
    processedEvents table (r + 1) = processedEvents table r ++ [⟨r, h⟩] := by
proof · 4 lines
  unfold processedEvents
  rw [List.take_succ_eq_append_getElem (l := List.finRange table.darts) (i := r)
    (by simpa using h)]
  simp
58
theorem processed_append_remaining (table : GraphTables.Table) (r : Nat) :
    processedEvents table r ++ remainingEvents table r = List.finRange table.darts := by
proof · 1 line
  exact List.take_append_drop r (List.finRange table.darts)
62
@[simp] theorem inputStream_nil (table : GraphTables.Table) : inputStream table [] = [] := rfl
64
@[simp] theorem inputStream_cons (table : GraphTables.Table) (e : Fin table.darts)
    (events : List (Fin table.darts)) :
    inputStream table (e :: events) =
      encodeWords (GraphTables.rowWords table.rows[e]) ++ inputStream table events := rfl
69
@[simp] theorem inputStream_append (table : GraphTables.Table)
    (first rest : List (Fin table.darts)) :
    inputStream table (first ++ rest) = inputStream table first ++ inputStream table rest := by
proof · 1 line
  simp only [inputStream, List.flatMap_append]
74
@[simp] theorem outputStream_nil (plan : Plan) (table : GraphTables.Table) :
    outputStream plan table [] = [] := rfl
77
@[simp] theorem outputStream_cons (plan : Plan) (table : GraphTables.Table)
    (e : Fin table.darts) (events : List (Fin table.darts)) :
    outputStream plan table (e :: events) =
      plan.flatMap (Emitter.commandBits (rowOperands table e) (rowRelation table e)) ++
        outputStream plan table events := rfl
83
@[simp] theorem outputStream_append (plan : Plan) (table : GraphTables.Table)
    (first rest : List (Fin table.darts)) :
    outputStream plan table (first ++ rest) =
      outputStream plan table first ++ outputStream plan table rest := by
proof · 1 line
  simp only [outputStream, List.flatMap_append]
89
theorem inputStream_remaining_cons (table : GraphTables.Table) (r : Nat)
    (h : r < table.darts) :
    inputStream table (remainingEvents table r) =
      encodeWords (GraphTables.rowWords table.rows[(⟨r, h⟩ : Fin table.darts)]) ++
        inputStream table (remainingEvents table (r + 1)) := by
proof · 1 line
  rw [remainingEvents_cons table r h, inputStream_cons]
96
theorem outputStream_remaining_cons (plan : Plan) (table : GraphTables.Table) (r : Nat)
    (h : r < table.darts) :
    outputStream plan table (remainingEvents table r) =
      plan.flatMap (Emitter.commandBits (rowOperands table ⟨r, h⟩)
        (rowRelation table ⟨r, h⟩)) ++
          outputStream plan table (remainingEvents table (r + 1)) := by
proof · 1 line
  rw [remainingEvents_cons table r h, outputStream_cons]
104
theorem outputStream_processed_succ (plan : Plan) (table : GraphTables.Table) (r : Nat)
    (h : r < table.darts) :
    outputStream plan table (processedEvents table (r + 1)) =
      outputStream plan table (processedEvents table r) ++
        plan.flatMap (Emitter.commandBits (rowOperands table ⟨r, h⟩)
          (rowRelation table ⟨r, h⟩)) := by
proof · 2 lines
  rw [processedEvents_succ table r h, outputStream_append]
  simp
113
theorem inputStream_split (table : GraphTables.Table) (r : Nat) :
    inputStream table (List.finRange table.darts) =
      inputStream table (processedEvents table r) ++
        inputStream table (remainingEvents table r) := by
proof · 1 line
  rw [← inputStream_append, processed_append_remaining]
119
theorem outputStream_split (plan : Plan) (table : GraphTables.Table) (r : Nat) :
    outputStream plan table (List.finRange table.darts) =
      outputStream plan table (processedEvents table r) ++
        outputStream plan table (remainingEvents table r) := by
proof · 1 line
  rw [← outputStream_append, processed_append_remaining]
125
private theorem encodeWords_flatMap_rows {α : Type} (rows : List α) (words : α → List Nat) :
    encodeWords (rows.flatMap words) = rows.flatMap (fun row => encodeWords (words row)) := by
proof · 3 lines
  induction rows with
  | nil => rfl
  | cons row rows ih => simp only [List.flatMap_cons, encodeWords_append, ih]
131
theorem rowList_eq_finRange_map (table : GraphTables.Table) :
    GraphTables.rowList table = (List.finRange table.darts).map (fun e => table.rows[e]) := by
proof · 3 lines
  rw [← List.ofFn_eq_map, ← Vector.toList_ofFn]
  change table.rows.toList = (Vector.ofFn (fun i => table.rows[i.val])).toList
  rw [Vector.ofFn_getElem]
137
theorem inputStream_all (table : GraphTables.Table) :
    inputStream table (List.finRange table.darts) = tableRowsBits (genericTable table) := by
proof · 8 lines
  rw [tableRowsBits, genericTable_rowList]
  erw [List.flatMap_map]
  change inputStream table (List.finRange table.darts) =
    encodeWords ((GraphTables.rowList table).flatMap
      (fun row => GenericGraphTables.rowWords (genericRow row)))
  simp only [genericRow_words]
  rw [rowList_eq_finRange_map, List.flatMap_map, encodeWords_flatMap_rows]
  rfl
148
theorem inputStream_remaining_zero (table : GraphTables.Table) :
    inputStream table (remainingEvents table 0) = tableRowsBits (genericTable table) := by
proof · 1 line
  rw [remainingEvents_zero, inputStream_all]
152
theorem outputStream_accumulator_succ (plan : Plan) (table : GraphTables.Table)
    (r : Nat) (h : r < table.darts) (header : List Bool) :
    (plan.flatMap (Emitter.commandBits (rowOperands table ⟨r, h⟩)
      (rowRelation table ⟨r, h⟩))).reverse ++
        (header ++ outputStream plan table (processedEvents table r)).reverse =
      (header ++ outputStream plan table (processedEvents table (r + 1))).reverse := by
proof · 2 lines
  rw [outputStream_processed_succ plan table r h]
  simp only [List.reverse_append, List.append_assoc]