cfahlgren1's picture
download
raw
10.6 kB
import OAI.Combinatorics.EditApproximation.Geometry.Geometry
import OAI.Combinatorics.EditApproximation.Definitions
namespace OAI
/-!
# Connection costs with arbitrary gaps and overlaps
This proves the edit-cost part of the connection inequality of Section 2
for actual substrings and edit scripts. Prefixes telescope across the
child intervals; common-prefix cancellation then removes the outside
prefix. The children need not have ordered or shared target endpoints.
-/
namespace EditApproximation
open EditDistortion
universe u
theorem edit_drop_insert_le {α : Type u} (p q : List α) (a : α) (k : ℕ) :
edit ((p ++ q).drop k) ((p ++ a :: q).drop k) ≤ 1 := by
by_cases hk : k ≤ p.length
· simp only [List.drop_append_of_le_length hk]
exact cost_le (Script.single (Step.insert (p.drop k) q a))
· have hp : p.length ≤ k := by omega
have hpos : 0 < k - p.length := by omega
simp only [List.drop_append, List.drop_eq_nil_of_le hp, List.nil_append,
List.drop_cons hpos]
apply (edit_drop_indices_le q (k - p.length) (k - p.length - 1)).trans
unfold Nat.dist
omega
theorem edit_drop_step_le {α : Type u} {x y : List α} (h : Step true x y) (k : ℕ) :
edit (x.drop k) (y.drop k) ≤ 1 := by
cases h with
| insert p q a => exact edit_drop_insert_le p q a k
| delete p q a =>
rw [show edit ((p ++ a :: q).drop k) ((p ++ q).drop k) =
edit ((p ++ q).drop k) ((p ++ a :: q).drop k) from cost_symm _ _]
exact edit_drop_insert_le p q a k
| substitute _ p q a b =>
by_cases hk : k ≤ p.length
· simp only [List.drop_append_of_le_length hk]
exact cost_le (Script.single (Step.substitute rfl (p.drop k) q a b))
· have hp : p.length ≤ k := by omega
have hpos : 0 < k - p.length := by omega
simp only [List.drop_append, List.drop_eq_nil_of_le hp, List.nil_append,
List.drop_cons hpos]
have hrefl : edit (q.drop (k - p.length - 1)) (q.drop (k - p.length - 1)) = 0 :=
cost_refl _
rw [hrefl]
omega
theorem edit_drop_script_le {α : Type u} {x y : List α} {n : ℕ}
(h : Script true n x y) (k : ℕ) : edit (x.drop k) (y.drop k) ≤ n := by
induction h with
| nil x => exact (cost_refl (x.drop k)).le
| @cons n x y z step rest ih =>
have hstep := edit_drop_step_le step k
exact (cost_triangle (s := true) (x.drop k) (y.drop k) (z.drop k)).trans
(by change edit (x.drop k) (y.drop k) + edit (y.drop k) (z.drop k) ≤ n + 1; omega)
theorem edit_drop_nonexpansive {α : Type u} (x y : List α) (k : ℕ) :
edit (x.drop k) (y.drop k) ≤ edit x y := by
simpa only [cost, edit] using edit_drop_script_le (cost_spec (s := true) x y) k
theorem edit_append_left_cancel {α : Type u} (pre x y : List α) :
edit (pre ++ x) (pre ++ y) = edit x y := by
apply Nat.le_antisymm
· exact cost_le ((cost_spec (s := true) x y).append_left pre)
· simpa only [List.drop_append_length] using
edit_drop_nonexpansive (pre ++ x) (pre ++ y) pre.length
theorem edit_append_right_le {α : Type u} (x y suffix : List α) :
edit (x ++ suffix) (y ++ suffix) ≤ edit x y :=
cost_le ((cost_spec (s := true) x y).append_right suffix)
/-- The outside and successive endpoint charges, including the final jump. -/
def targetConcatenation {α : Type u} (word : List α)
(states : List (TargetInterval word.length)) : List α :=
(states.map fun state => substring word state.lo state.hi).flatten
theorem prefix_append_substring {α : Type u} (word : List α) {lo hi : ℕ}
(horder : lo ≤ hi) : word.take lo ++ substring word lo hi = word.take hi := by
have htake : (word.take hi).take lo = word.take lo := by
simp only [List.take_take, Nat.min_eq_left horder]
rw [substring, ← htake]
exact List.take_append_drop lo (word.take hi)
theorem prefix_child_step {α : Type u} (word : List α) (start : ℕ)
(state : TargetInterval word.length) :
edit (word.take start ++ substring word state.lo state.hi) (word.take state.hi) ≤
Nat.dist start state.lo := by
rw [← prefix_append_substring word state.ordered]
apply (edit_append_right_le _ _ _).trans
simpa only [substring, List.drop_zero] using
edit_substring_right_le word 0 start state.lo
theorem prefix_connection_bound {α : Type u} (word : List α)
(states : List (TargetInterval word.length)) (start finish : ℕ) :
edit (word.take start ++ targetConcatenation word states) (word.take finish) ≤
connectionBudget start states finish := by
induction states generalizing start with
| nil =>
simpa only [targetConcatenation, List.map_nil, List.flatten_nil, List.append_nil,
connectionBudget, substring, List.drop_zero] using
edit_substring_right_le word 0 start finish
| cons state rest ih =>
simp only [targetConcatenation, List.map_cons, List.flatten_cons]
rw [← List.append_assoc]
have hstep := (edit_append_right_le _ _ (targetConcatenation word rest)).trans
(prefix_child_step word start state)
have htri := cost_triangle (s := true)
((word.take start ++ substring word state.lo state.hi) ++ targetConcatenation word rest)
(word.take state.hi ++ targetConcatenation word rest) (word.take finish)
exact htri.trans (Nat.add_le_add hstep (ih state.hi))
/-- The target-only part of the connection inequality. The prefix proof
accounts for overlaps and backwards connecting jumps automatically. -/
theorem target_connection_bound {α : Type u} (word : List α)
(states : List (TargetInterval word.length)) (parent : TargetInterval word.length) :
edit (targetConcatenation word states) (substring word parent.lo parent.hi) ≤
connectionBudget parent.lo states parent.hi := by
have h := edit_drop_nonexpansive
(word.take parent.lo ++ targetConcatenation word states)
(word.take parent.hi) parent.lo
have hlen : (word.take parent.lo).length = parent.lo := by
simp only [List.length_take, Nat.min_eq_left (parent.ordered.trans parent.valid)]
have hdrop : (word.take parent.lo ++ targetConcatenation word states).drop parent.lo =
targetConcatenation word states := by
simpa only [hlen] using List.drop_append_length
(l₁ := word.take parent.lo) (l₂ := targetConcatenation word states)
rw [hdrop] at h
exact h.trans (prefix_connection_bound word states parent.lo parent.hi)
theorem edit_flatten_pairs_le {α : Type u} (pairs : List (List α × List α)) :
edit (pairs.map Prod.fst).flatten (pairs.map Prod.snd).flatten ≤
(pairs.map fun pair => edit pair.1 pair.2).sum := by
induction pairs with
| nil => exact (cost_refl ([] : List α)).le
| cons pair rest ih =>
simp only [List.map_cons, List.flatten_cons, List.sum_cons]
exact (cost_append_le (s := true) pair.1 pair.2
(rest.map Prod.fst).flatten (rest.map Prod.snd).flatten).trans
(Nat.add_le_add_left ih _)
/-- Child editing plus endpoint connections bounds the parent cost, for
an actual concatenated partition of the source string. -/
theorem child_connection_bound {α : Type u} (word : List α)
(children : List (List α × TargetInterval word.length))
(parent : TargetInterval word.length) :
edit (children.map Prod.fst).flatten (substring word parent.lo parent.hi) ≤
(children.map fun child => edit child.1 (substring word child.2.lo child.2.hi)).sum +
connectionBudget parent.lo (children.map Prod.snd) parent.hi := by
have hchildren := edit_flatten_pairs_le
(children.map fun child => (child.1, substring word child.2.lo child.2.hi))
simp only [List.map_map, Function.comp_def] at hchildren
have htarget := target_connection_bound word (children.map Prod.snd) parent
have htri := cost_triangle (s := true) (children.map Prod.fst).flatten
(targetConcatenation word (children.map Prod.snd)) (substring word parent.lo parent.hi)
apply htri.trans
exact Nat.add_le_add (by simpa only [targetConcatenation, List.map_map,
Function.comp_def, cost, edit] using hchildren) htarget
def signedLengthGap {α : Type u} (source target : List α) : ℝ :=
(target.length : ℝ) - source.length
theorem signedLengthGap_append {α : Type u} (x y u v : List α) :
signedLengthGap (x ++ u) (y ++ v) = signedLengthGap x y + signedLengthGap u v := by
simp only [signedLengthGap, List.length_append, Nat.cast_add]
ring
theorem flattened_length_gap_le {α : Type u} (pairs : List (List α × List α)) :
|signedLengthGap (pairs.map Prod.fst).flatten (pairs.map Prod.snd).flatten| ≤
(pairs.map fun pair => |signedLengthGap pair.1 pair.2|).sum := by
induction pairs with
| nil => simp [signedLengthGap]
| cons pair rest ih =>
simp only [List.map_cons, List.flatten_cons, List.sum_cons, signedLengthGap_append]
exact (abs_add_le _ _).trans (add_le_add le_rfl ih)
/-- Length gaps obey the same arbitrary-gap connection inequality as
the edit costs. This is the second lower invariant of the Bellman operator. -/
theorem child_shift_connection_bound {α : Type u} (word : List α)
(children : List (List α × TargetInterval word.length))
(parent : TargetInterval word.length) :
|signedLengthGap (children.map Prod.fst).flatten (substring word parent.lo parent.hi)| ≤
(children.map fun child =>
|signedLengthGap child.1 (substring word child.2.lo child.2.hi)|).sum +
(connectionBudget parent.lo (children.map Prod.snd) parent.hi : ℝ) := by
let pairs := children.map fun child => (child.1, substring word child.2.lo child.2.hi)
have hflatten := flattened_length_gap_le pairs
have htarget := target_connection_bound word (children.map Prod.snd) parent
have hgap := abs_length_gap_le_edit (targetConcatenation word (children.map Prod.snd))
(substring word parent.lo parent.hi)
have hbudget : |signedLengthGap (targetConcatenation word (children.map Prod.snd))
(substring word parent.lo parent.hi)| ≤
(connectionBudget parent.lo (children.map Prod.snd) parent.hi : ℝ) :=
hgap.trans (by exact_mod_cast htarget)
have hid : signedLengthGap (children.map Prod.fst).flatten
(substring word parent.lo parent.hi) =
signedLengthGap (pairs.map Prod.fst).flatten (pairs.map Prod.snd).flatten +
signedLengthGap (targetConcatenation word (children.map Prod.snd))
(substring word parent.lo parent.hi) := by
simp only [pairs, List.map_map, Function.comp_def, targetConcatenation, signedLengthGap]
ring
rw [hid]
apply (abs_add_le _ _).trans
have hsum := add_le_add hflatten hbudget
simpa only [pairs, List.map_map, Function.comp_def] using hsum
end EditApproximation
end OAI

Xet Storage Details

Size:
10.6 kB
·
Xet hash:
fee8f87414af55f772b976523530a60a88d901e55832fe352c320684cac131a8

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.