math / data /lean /OAI /Combinatorics /EditDistance /BinaryEmbedding.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
4.25 kB
import OAI.Combinatorics.EditDistance.BlockAlignment
namespace OAI
/-!
# Fixed-length binary embeddings with unrestricted edit distance
-/
namespace EditDistortion
variable {A B : Type*}
lemma Script.flatMap {n w : β„•} {x y : List A} (h : Script false n x y)
(c : A β†’ List B) (hc : βˆ€ a, (c a).length = w) :
Script false (w*n) (x.flatMap c) (y.flatMap c) := by
induction h with
| nil x => simpa using Script.nil (substitutions := false) (x.flatMap c)
| @cons n x y z first rest ih =>
have he : Script false w (x.flatMap c) (y.flatMap c) := by
cases first with
| insert p q a =>
have h := ((Script.to_nil (s := false) (c a)).symm.append_left (p.flatMap c)).append_right (q.flatMap c)
simpa only [hc,List.append_nil,List.flatMap_append,List.flatMap_cons,List.append_assoc] using h
| delete p q a =>
have h := ((Script.to_nil (s := false) (c a)).append_left (p.flatMap c)).append_right (q.flatMap c)
simpa only [hc,List.append_nil,List.flatMap_append,List.flatMap_cons,List.append_assoc] using h
| substitute hf => cases hf
simpa only [Nat.mul_add,Nat.mul_one,Nat.add_comm] using he.trans ih
lemma indel_flatMap_le (c : A β†’ List B) (w : β„•) (hc : βˆ€ a, (c a).length = w) (x y : List A) :
indel (x.flatMap c) (y.flatMap c) ≀ w * indel x y :=
cost_le ((cost_spec (s := false) x y).flatMap c hc)
lemma indel_replicate_le (a : A) (r s : β„•) :
indel (List.replicate r a) (List.replicate s a) ≀ (r-s)+(s-r) := by
by_cases h : r ≀ s
Β· have hs : (List.replicate r a).Sublist (List.replicate s a) := (List.replicate_sublist_replicate a).mpr h
have he := (Script.of_sublist hs).symm
have hcost := cost_le he
simpa only [cost,indel,List.length_replicate,Nat.sub_eq_zero_of_le h,Nat.zero_add] using hcost
Β· have hs : (List.replicate s a).Sublist (List.replicate r a) := (List.replicate_sublist_replicate a).mpr (by omega)
have hcost := cost_le (Script.of_sublist hs)
simpa only [cost,indel,List.length_replicate,Nat.sub_eq_zero_of_le (show s ≀ r by omega),Nat.add_zero] using hcost
end EditDistortion
namespace EditCode
open EditDistortion
variable {A : Type*} {t : β„•}
theorem indel_code_upper (label : A β†’ Fin t β†’ Bool) (x y : List A) :
indel (encode label x) (encode label y) ≀ width t * indel x y :=
indel_flatMap_le (block label) (width t) (block_length label) x y
def fixed (label : A β†’ Fin t β†’ Bool) (D : β„•) (x : List A) : List Bool :=
padded label x (width t * (D-x.length))
@[simp] lemma fixed_length (label : A β†’ Fin t β†’ Bool) (D : β„•) (x : List A) (hx : x.length ≀ D) :
(fixed label D x).length = width t * D := by
simp only [fixed,padded_length,←Nat.mul_add,Nat.add_sub_of_le hx]
theorem indel_fixed_upper (label : A β†’ Fin t β†’ Bool) (D : β„•) (x y : List A)
(hx : x.length ≀ D) (hy : y.length ≀ D) :
indel (fixed label D x) (fixed label D y) ≀ 2 * width t * indel x y := by
have h := cost_append_le (s := false) (encode label x) (encode label y)
(List.replicate (width t*(D-x.length)) false) (List.replicate (width t*(D-y.length)) false)
have hr := indel_replicate_le false (width t*(D-x.length)) (width t*(D-y.length))
have hc := indel_code_upper label x y
have hl := cost_length_bounds (s := false) x y
change x.length ≀ y.length + indel x y ∧ y.length ≀ x.length + indel x y at hl
have hd : width t*(D-x.length)-width t*(D-y.length) +
(width t*(D-y.length)-width t*(D-x.length)) ≀ width t*indel x y := by
rw [←Nat.mul_sub_left_distrib,←Nat.mul_sub_left_distrib,←Nat.mul_add]
apply Nat.mul_le_mul_left
omega
change indel (fixed label D x) (fixed label D y) ≀
indel (encode label x) (encode label y) +
indel (List.replicate (width t*(D-x.length)) false) (List.replicate (width t*(D-y.length)) false) at h
nlinarith
end EditCode
namespace EditCode
variable (A : Type*) [Fintype A]
noncomputable def labels : A β†ͺ (Fin (Nat.clog 2 (Fintype.card A)) β†’ Bool) :=
Classical.choice (Function.Embedding.nonempty_of_card_le (by
simpa only [Fintype.card_fun,Fintype.card_fin,Fintype.card_bool] using
Nat.le_pow_clog (by norm_num : 1 < 2) (Fintype.card A)))
end EditCode
end OAI