File size: 7,597 Bytes
e63eb7f | 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | import OAI.Combinatorics.EditApproximation.Geometry.Geometry
namespace OAI
/-!
# Splitting an optimal edit script at a source cut
An arbitrary unrestricted edit script transports a cut through its
intermediate strings. Each edit belongs to one side of that cut. Applied
to a shortest script, this gives an exact optimal target split, including
empty source or target pieces.
-/
namespace EditApproximation
open EditDistortion
universe u
theorem step_split {α : Type u} {x y : List α} (step : Step true x y)
(left right : List α) (hx : x = left ++ right) :
∃ left' right', y = left' ++ right' ∧
((Step true left left' ∧ right = right') ∨
(left = left' ∧ Step true right right')) := by
cases step with
| insert p q a =>
rcases List.append_eq_append_iff.mp hx.symm with ⟨mid, hp, hr⟩ | ⟨mid, hl, hq⟩
· refine ⟨left, mid ++ a :: q, ?_, Or.inr ⟨rfl, ?_⟩⟩
· simp only [hp, List.append_assoc]
· rw [hr]
exact Step.insert mid q a
· refine ⟨p ++ a :: mid, right, ?_, Or.inl ⟨?_, rfl⟩⟩
· simp only [hq, List.append_assoc, List.cons_append]
· rw [hl]
exact Step.insert p mid a
| delete p q a =>
rcases List.append_eq_append_iff.mp hx.symm with ⟨mid, hp, hr⟩ | ⟨mid, hl, hq⟩
· refine ⟨left, mid ++ q, ?_, Or.inr ⟨rfl, ?_⟩⟩
· simp only [hp, List.append_assoc]
· rw [hr]
exact Step.delete mid q a
· cases mid with
| nil =>
simp only [List.append_nil] at hl
simp only [List.nil_append] at hq
refine ⟨left, q, ?_, Or.inr ⟨rfl, ?_⟩⟩
· rw [hl]
· rw [← hq]
exact Step.delete [] q a
| cons b mid =>
simp only [List.cons_append, List.cons.injEq] at hq
rcases hq with ⟨rfl, hq⟩
refine ⟨p ++ mid, right, ?_, Or.inl ⟨?_, rfl⟩⟩
· simp only [hq, List.append_assoc]
· rw [hl]
exact Step.delete p mid a
| substitute _ p q a b =>
rcases List.append_eq_append_iff.mp hx.symm with ⟨mid, hp, hr⟩ | ⟨mid, hl, hq⟩
· refine ⟨left, mid ++ b :: q, ?_, Or.inr ⟨rfl, ?_⟩⟩
· simp only [hp, List.append_assoc]
· rw [hr]
exact Step.substitute rfl mid q a b
· cases mid with
| nil =>
simp only [List.append_nil] at hl
simp only [List.nil_append] at hq
refine ⟨left, b :: q, ?_, Or.inr ⟨rfl, ?_⟩⟩
· rw [hl]
· rw [← hq]
exact Step.substitute rfl [] q a b
| cons c mid =>
simp only [List.cons_append, List.cons.injEq] at hq
rcases hq with ⟨rfl, hq⟩
refine ⟨p ++ b :: mid, right, ?_, Or.inl ⟨?_, rfl⟩⟩
· simp only [hq, List.append_assoc, List.cons_append]
· rw [hl]
exact Step.substitute rfl p mid a b
theorem script_split {α : Type u} {x y : List α} {n : ℕ}
(script : Script true n x y) (left right : List α) (hx : x = left ++ right) :
∃ left' right' nleft nright,
y = left' ++ right' ∧ nleft + nright = n ∧
Script true nleft left left' ∧ Script true nright right right' := by
induction script generalizing left right with
| nil x => exact ⟨left, right, 0, 0, hx, rfl, .nil _, .nil _⟩
| @cons n x y z step rest ih =>
obtain ⟨midleft, midright, hy, hs⟩ := step_split step left right hx
obtain ⟨lastleft, lastright, nl, nr, hz, hn, hleft, hright⟩ :=
ih midleft midright hy
rcases hs with ⟨hstep, rfl⟩ | ⟨rfl, hstep⟩
· exact ⟨lastleft, lastright, nl + 1, nr, hz, by omega,
.cons hstep hleft, hright⟩
· exact ⟨lastleft, lastright, nl, nr + 1, hz, by omega,
hleft, .cons hstep hright⟩
/-- A shortest unrestricted edit script admits an exactly optimal split
at every source cut. -/
theorem edit_append_split {α : Type u} (left right target : List α) :
∃ targetLeft targetRight, target = targetLeft ++ targetRight ∧
edit (left ++ right) target = edit left targetLeft + edit right targetRight := by
obtain ⟨tl, tr, nl, nr, ht, hn, hl, hr⟩ :=
script_split (cost_spec (s := true) (left ++ right) target) left right rfl
refine ⟨tl, tr, ht, Nat.le_antisymm ?_ ?_⟩
· rw [ht]
exact cost_append_le (s := true) left tl right tr
· have hleft := cost_le hl
have hright := cost_le hr
change edit left tl ≤ nl at hleft
change edit right tr ≤ nr at hright
change nl + nr = edit (left ++ right) target at hn
omega
/-- Repeated source cuts have a common ordered target partition whose
piecewise edit costs sum exactly to the parent optimum. Source pieces
may be empty. A nonempty list of pieces is needed to assign insertions. -/
theorem edit_partition_split {α : Type u} (parts : List (List α))
(hne : parts ≠ []) (target : List α) :
∃ targetParts : List (List α), targetParts.length = parts.length ∧
targetParts.flatten = target ∧
(List.zipWith edit parts targetParts).sum = edit parts.flatten target := by
induction parts generalizing target with
| nil => exact False.elim (hne rfl)
| cons first rest ih =>
cases rest with
| nil =>
refine ⟨[target], rfl, ?_, ?_⟩
· simp
· simp
| cons second rest =>
obtain ⟨tl, tr, ht, hc⟩ := edit_append_split first (second :: rest).flatten target
obtain ⟨targets, hlen, hflatten, hcost⟩ := ih (by simp) tr
refine ⟨tl :: targets, by simp only [List.length_cons, hlen], ?_, ?_⟩
· simp only [List.flatten_cons, hflatten]
exact ht.symm
· simp only [List.flatten_cons, List.zipWith_cons_cons, List.sum_cons, hcost]
exact hc.symm
/-- Prefixes of an ordered source/target partition use at most the
partition's total edit cost. This bounds every alignment anchor shift. -/
theorem edit_partition_prefix_le {α : Type u}
(parts targets : List (List α)) (hlen : parts.length = targets.length) (k : ℕ) :
edit (parts.take k).flatten (targets.take k).flatten ≤
(List.zipWith edit parts targets).sum := by
induction parts generalizing targets k with
| nil =>
have ht : targets = [] := List.length_eq_zero_iff.mp hlen.symm
subst targets
simp only [List.take_nil, List.flatten_nil, List.zipWith_nil_left, List.sum_nil]
exact (cost_refl ([] : List α)).le
| cons first rest ih =>
cases targets with
| nil => simp at hlen
| cons targetFirst targetRest =>
have hrest : rest.length = targetRest.length := by simpa using hlen
cases k with
| zero =>
simp only [List.take_zero, List.flatten_nil]
exact (cost_refl ([] : List α)).le.trans (Nat.zero_le _)
| succ k =>
simp only [List.take_succ_cons, List.flatten_cons, List.zipWith_cons_cons, List.sum_cons]
exact (cost_append_le (s := true) first targetFirst
(rest.take k).flatten (targetRest.take k).flatten).trans
(Nat.add_le_add_left (ih targetRest hrest k) _)
theorem optimal_partition_anchor_shift_bound {α : Type u}
(parts targets : List (List α)) (target : List α)
(hlen : parts.length = targets.length)
(hcost : (List.zipWith edit parts targets).sum = edit parts.flatten target) (k : ℕ) :
|((targets.take k).flatten.length : ℝ) - (parts.take k).flatten.length| ≤
(edit parts.flatten target : ℝ) := by
apply (abs_length_gap_le_edit (parts.take k).flatten (targets.take k).flatten).trans
exact_mod_cast (edit_partition_prefix_le parts targets hlen k).trans_eq hcost
end EditApproximation
end OAI
|