roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
3.78 kB
14
def rotationWords (table : Table v d) : List Nat := table.rows.toList.map Fin.val
16
@[simp] theorem rotationWords_length (table : Table v d) :
    (rotationWords table).length = v * d := by
proof · 1 line
  simp [rotationWords]
20
theorem rotationWords_getElem (table : Table v d) (i : ℕ) (hi : i < v * d) :
    (rotationWords table)[i]'(by simpa only [rotationWords_length] using hi) =
      (reverseIndex table ⟨i, hi⟩).val := by
proof · 2 lines
  simp only [rotationWords, List.getElem_map, Vector.getElem_toList,
    reverseIndex, Fin.getElem_fin]
26
theorem rotationWords_get (table : Table v d) (i : Fin (v * d)) :
    (rotationWords table).get
      ⟨i.val, by simpa only [rotationWords_length] using i.isLt⟩ =
      (reverseIndex table i).val := by
proof · 1 line
  simpa only [List.get_eq_getElem] using rotationWords_getElem table i.val i.isLt
32
theorem rotationWords_getElem? (table : Table v d) (i : Fin (v * d)) :
    (rotationWords table)[i.val]? = some (reverseIndex table i).val := by
proof · 3 lines
  apply List.getElem?_eq_some_iff.mpr
  exact ⟨by simpa only [rotationWords_length] using i.isLt,
    rotationWords_getElem table i.val i.isLt⟩
38
theorem rotationWords_getElem?_none (table : Table v d) (i : ℕ) (hi : v * d ≤ i) :
    (rotationWords table)[i]? = none :=
proof · 1 line
  List.getElem?_eq_none (by simpa only [rotationWords_length] using hi)
42
theorem rotationWords_entry_lt (table : Table v d) (n : ℕ)
    (hn : n ∈ rotationWords table) : n < v * d := by
proof · 2 lines
  obtain ⟨a, _, rfl⟩ := List.mem_map.mp hn
  exact a.isLt
47
theorem rotationWords_lookup (table : Table v d) (x : Fin v × Fin d) :
    (rotationWords table)[(rowIndex v d x).val]? =
      some (rowIndex v d (lookup table x)).val := by
proof · 2 lines
  simpa only [lookup, Equiv.apply_symm_apply] using
    rotationWords_getElem? table (rowIndex v d x)
53
theorem rotationWords_row_order (table : Table v d) (x : Fin v × Fin d) :
    (rotationWords table)[x.2.val + d * x.1.val]? =
      some ((lookup table x).2.val + d * (lookup table x).1.val) := by
proof · 1 line
  simpa only [rowIndex_val] using rotationWords_lookup table x
58
@[simp] theorem decode_rotationWords (table : Table v d) :
    decodeWords (encodeWords (rotationWords table)) = some (rotationWords table) :=
proof · 1 line
  Complexity.decodeWords_encodeWords _
62
theorem decoded_lookup (table : Table v d) (i : Fin (v * d)) :
    (decodeWords (encodeWords (rotationWords table))).bind (fun words => words[i.val]?) =
      some (reverseIndex table i).val := by
proof · 2 lines
  rw [decode_rotationWords]
  exact rotationWords_getElem? table i
68
theorem decoded_coordinate_lookup (table : Table v d) (x : Fin v × Fin d) :
    (decodeWords (encodeWords (rotationWords table))).bind
        (fun words => words[x.2.val + d * x.1.val]?) =
      some ((lookup table x).2.val + d * (lookup table x).1.val) := by
proof · 2 lines
  rw [decode_rotationWords]
  exact rotationWords_row_order table x
75
theorem encode_rotationWords_length_eq (table : Table v d) :
    (encodeWords (rotationWords table)).length = (rotationWords table).sum + v * d := by
proof · 1 line
  rw [Complexity.encodeWords_length, rotationWords_length]
79
theorem encode_rotationWords_length_le (table : Table v d) :
    (encodeWords (rotationWords table)).length ≤ (v * d) * (v * d + 1) := by
proof · 5 lines
  calc
    _ ≤ (rotationWords table).length * (v * d + 1) :=
      Complexity.encodeWords_length_le _ _
        (fun n hn => (rotationWords_entry_lt table n hn).le)
    _ = _ := by rw [rotationWords_length]