File size: 3,765 Bytes
f0f05de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import OAI.Combinatorics.BinaryEdit.Model
import Mathlib.Tactic
namespace OAI
/-! Indexing fixed-width concatenated codewords. -/
namespace TreeEdit.BinaryLower
universe u v
variable {α : Type u} {β : Type v} {P : ℕ}
/-- A block-coordinate lookup in the actual concatenation extension. -/
theorem getElem?_encode_block (c : α → Word β P) (x : List α)
(i j : ℕ) (hi : i < x.length) (hj : j < P) :
(encode c x)[i * P + j]? = (c x[i]).val[j]? := by
induction x generalizing i with
| nil => simp only [List.length_nil] at hi; omega
| cons a x ih =>
cases i with
| zero =>
simp only [Nat.zero_mul, Nat.zero_add, List.getElem_cons_zero, encode,
List.flatMap_cons]
exact List.getElem?_append_left (by simpa only [(c a).property] using hj)
| succ i =>
have hi' : i < x.length := by simpa only [List.length_cons, Nat.succ_lt_succ_iff] using hi
simp only [encode, List.flatMap_cons, List.getElem_cons_succ]
rw [List.getElem?_append_right (by rw [(c a).property]; simp only [Nat.add_mul, Nat.one_mul]; omega)]
rw [(c a).property]
have heq : (i + 1) * P + j - P = i * P + j := by
simp only [Nat.add_mul, Nat.one_mul]
omega
rw [heq]
exact ih i hi'
/-- Any encoded position is found by its quotient block and remainder offset. -/
theorem getElem?_encode_quotient (c : α → Word β P) (x : List α)
(k : ℕ) (hP : 0 < P) (hk : k / P < x.length) :
(encode c x)[k]? = (c x[k / P]).val[k % P]? := by
have h := getElem?_encode_block c x (k / P) (k % P) hk (Nat.mod_lt k hP)
simpa only [Nat.div_add_mod'] using h
/-- The total lookup counterpart of `getElem?_encode_block`. -/
theorem getElem_encode_block (c : α → Word β P) (x : List α)
(i j : ℕ) (hi : i < x.length) (hj : j < P)
(hbit : i * P + j < (encode c x).length) :
(encode c x)[i * P + j] =
(c x[i]).val[j]'(by rw [(c x[i]).property]; exact hj) := by
have h := getElem?_encode_block c x i j hi hj
simpa only [List.getElem?_eq_getElem hbit,
List.getElem?_eq_getElem (by rw [(c x[i]).property]; exact hj), Option.some.injEq] using h
/-- Total lookup at an arbitrary encoded position. -/
theorem getElem_encode_quotient (c : α → Word β P) (x : List α)
(k : ℕ) (hP : 0 < P) (hk : k / P < x.length)
(hbit : k < (encode c x).length) :
(encode c x)[k] =
(c x[k / P]).val[k % P]'(by rw [(c x[k / P]).property]; exact Nat.mod_lt k hP) := by
have h := getElem?_encode_quotient c x k hP hk
simpa only [List.getElem?_eq_getElem hbit,
List.getElem?_eq_getElem (by rw [(c x[k / P]).property]; exact Nat.mod_lt k hP),
Option.some.injEq] using h
/-- A segment wholly contained in one encoded block is exactly the
corresponding segment of that codeword. -/
theorem encode_segment_eq (c : α → Word β P) (x : List α)
(i j a : ℕ) (hi : i < x.length) (hja : j + a ≤ P) :
((encode c x).drop (i * P + j)).take a =
(((c x[i]).val).drop j).take a := by
apply List.ext_getElem?
intro k
by_cases hk : k < a
· have hjk : j + k < P := by omega
simpa only [List.getElem?_take, hk, ite_true, List.getElem?_drop,
Nat.add_assoc] using getElem?_encode_block c x i (j + k) hi hjk
· simp only [List.getElem?_take, hk, ite_false]
/-- The same segment identity with start specified as one global index. -/
theorem encode_segment_quotient_eq (c : α → Word β P) (x : List α)
(k a : ℕ) (hk : k / P < x.length) (hka : k % P + a ≤ P) :
((encode c x).drop k).take a =
(((c x[k / P]).val).drop (k % P)).take a := by
simpa only [Nat.div_add_mod'] using
encode_segment_eq c x (k / P) (k % P) a hk hka
end TreeEdit.BinaryLower
end OAI
|