import OAI.Combinatorics.EditDistance.DelimiterAlignment namespace OAI /-! # Binary delimiter codes and padding -/ namespace EditCode open EditDistortion variable {A : Type*} {t : ℕ} def width (t : ℕ) : ℕ := 2 * t + 3 def bit (label : A → Fin t → Bool) (a : A) (r : ℕ) : Bool := if r < 2 then true else if h : r % 2 = 1 ∧ r / 2 - 1 < t then label a ⟨r / 2 - 1,h.2⟩ else false def block (label : A → Fin t → Bool) (a : A) : List Bool := List.ofFn (fun r : Fin (width t) => bit label a r) def encode (label : A → Fin t → Bool) (x : List A) : List Bool := x.flatMap (block label) def padded (label : A → Fin t → Bool) (x : List A) (r : ℕ) : List Bool := encode label x ++ List.replicate r false @[simp] lemma block_length (label : A → Fin t → Bool) (a : A) : (block label a).length = width t := List.length_ofFn @[simp] lemma width_pos : 0 < width t := by unfold width; omega @[simp] lemma encode_length (label : A → Fin t → Bool) (x : List A) : (encode label x).length = width t * x.length := by induction x with | nil => simp [encode] | cons a x ih => simp_all [encode, Nat.mul_add, Nat.add_comm] @[simp] lemma padded_length (label : A → Fin t → Bool) (x : List A) (r : ℕ) : (padded label x r).length = width t * x.length + r := by simp [padded] @[simp] lemma bit_zero (label : A → Fin t → Bool) (a : A) : bit label a 0 = true := by simp [bit] @[simp] lemma bit_one (label : A → Fin t → Bool) (a : A) : bit label a 1 = true := by simp [bit] lemma bit_label (label : A → Fin t → Bool) (a : A) (i : Fin t) : bit label a (2 * i.val + 3) = label a i := by have hm : (2 * i.val + 3) % 2 = 1 := by omega have hd : (2 * i.val + 3) / 2 - 1 = i.val := by omega simp [bit, show ¬2*i.val+3<2 by omega, hm, hd] lemma bit_even (label : A → Fin t → Bool) (a : A) {r : ℕ} (hr : 2 ≤ r) (he : r % 2 = 0) : bit label a r = false := by simp [bit,show ¬r<2 by omega,he] lemma bit_last (label : A → Fin t → Bool) (a : A) : bit label a (width t - 1) = false := by apply bit_even · unfold width; omega · unfold width; omega lemma bit_consecutive (label : A → Fin t → Bool) {a : A} {r : ℕ} (h₁ : bit label a r = true) (h₂ : bit label a (r+1) = true) : r = 0 := by by_contra h by_cases he : r % 2 = 0 · have hr : 2 ≤ r := by omega rw [bit_even label a hr he] at h₁ cases h₁ · have he : (r+1)%2=0 := by omega rw [bit_even label a (by omega) he] at h₂ cases h₂ lemma encode_get_block (label : A → Fin t → Bool) (x : List A) (i : Fin x.length) (r : Fin (width t)) : (encode label x)[width t * i.val + r.val]'(by rw [encode_length] have hm := Nat.mul_le_mul_left (width t) i.isLt simp only [Nat.mul_succ] at hm have := r.isLt omega) = bit label (x.get i) r := by induction x with | nil => exact Fin.elim0 i | cons a x ih => by_cases hi : i.val = 0 · simp only [encode, List.flatMap_cons] rw [List.getElem_append_left (by simp [hi])] simp [block,hi,List.get_eq_getElem] · let j : Fin x.length := ⟨i.val-1,by simpa only [List.length_cons] using (show i.val-1 l[2*i.val+3]?) he simp only [block, List.getElem?_ofFn] at hv simp only [dite_eq_left hir] at hv simpa only [bit_label, Option.some.injEq] using hv end EditCode end OAI