File size: 5,895 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
import OAI.Combinatorics.EditApproximation.Geometry.AlignmentSplitting
import OAI.Combinatorics.EditApproximation.Geometry.Leaves
import OAI.Combinatorics.EditApproximation.Definitions


namespace OAI

/-! An exact suffix dynamic program for the bounded initial input range.
Each source row is computed once; its cells minimize over target cuts.
The singleton prefix cost is the executable occurrence formula. -/

namespace EditApproximation

open EditDistortion



theorem natListMinimum_le_fallback (fallback : ℕ) (values : List ℕ) :
    natListMinimum fallback values ≤ fallback := by
  induction values with
  | nil => exact le_rfl
  | cons first rest ih => exact (min_le_right _ _).trans ih

theorem natListMinimum_le_member (fallback value : ℕ) (values : List ℕ) (h : value ∈ values) :
    natListMinimum fallback values ≤ value := by
  induction values with
  | nil => simp only [List.not_mem_nil] at h
  | cons first rest ih =>
    rcases List.mem_cons.mp h with rfl | h
    · exact min_le_left _ _
    · exact (min_le_right _ _).trans (ih h)

theorem natListMinimum_lower (fallback lower : ℕ) (values : List ℕ)
    (hfallback : lower ≤ fallback) (hvalues : ∀ value ∈ values, lower ≤ value) :
    lower ≤ natListMinimum fallback values := by
  induction values with
  | nil => exact hfallback
  | cons first rest ih =>
    exact le_min (hvalues first (List.mem_cons_self))
      (ih (fun value h => hvalues value (List.mem_cons_of_mem _ h)))



theorem exactSuffixDP_length {α : Type*} [DecidableEq α] (source target : List α) :
    (exactSuffixDP source target).length = target.length + 1 := by
  cases source <;> simp only [exactSuffixDP, List.length_ofFn]

theorem ofFn_nat_getD {n j : ℕ} (f : Fin (n + 1) → ℕ) (hj : j ≤ n) :
    (List.ofFn f).getD j 0 = f ⟨j, Nat.lt_succ_of_le hj⟩ := by
  have hlt : j < (List.ofFn f).length := by simpa only [List.length_ofFn] using Nat.lt_succ_of_le hj
  rw [List.getD_eq_getElem _ _ hlt, List.getElem_ofFn]

theorem edit_nil_left_exact {α : Type*} (target : List α) : edit ([] : List α) target = target.length := by
  have hlo := (cost_length_bounds (s := true) ([] : List α) target).2
  have hup := edit_le_max_length ([] : List α) target
  simp only [List.length_nil, zero_add, max_eq_right (Nat.zero_le _)] at hlo hup
  exact Nat.le_antisymm hup hlo

theorem exactSuffixDP_spec {α : Type*} [DecidableEq α] (source target : List α) :
    ∀ j ≤ target.length, (exactSuffixDP source target).getD j 0 = edit source (target.drop j) := by
  induction source with
  | nil =>
    intro j hj
    rw [exactSuffixDP, ofFn_nat_getD _ hj, edit_nil_left_exact, List.length_drop]
  | cons a rest ih =>
    intro j hj
    rw [exactSuffixDP, ofFn_nat_getD _ hj]
    dsimp only
    have hcandidate (k : ℕ) (hk : k ≤ target.length - j) :
        edit (a :: rest) (target.drop j) ≤
          singletonCost a ((target.drop j).take k) + (exactSuffixDP rest target).getD (j + k) 0 := by
      rw [singletonCost_eq_edit, ih (j + k) (by omega)]
      have h := cost_append_le (s := true) [a] ((target.drop j).take k) rest ((target.drop j).drop k)
      rw [List.take_append_drop] at h
      simpa only [cost, edit, List.singleton_append, List.drop_drop] using h
    apply Nat.le_antisymm
    · obtain ⟨left, right, hsplit, hcost⟩ := edit_append_split [a] rest (target.drop j)
      have hk : left.length ≤ target.length - j := by
        have hlen := congrArg List.length hsplit
        simp only [List.length_drop, List.length_append] at hlen
        omega
      have hleft : (target.drop j).take left.length = left := by
        rw [hsplit]
        simp only [List.take_left]
      have hright : target.drop (j + left.length) = right := by
        rw [← List.drop_drop, hsplit]
        simp only [List.drop_left]
      have hm := natListMinimum_le_member
        (singletonCost a [] + (exactSuffixDP rest target).getD j 0)
        (singletonCost a ((target.drop j).take left.length) +
          (exactSuffixDP rest target).getD (j + left.length) 0)
        ((List.range (target.length - j + 1)).map fun k =>
          singletonCost a ((target.drop j).take k) + (exactSuffixDP rest target).getD (j + k) 0)
        (List.mem_map.mpr ⟨left.length, List.mem_range.mpr (by omega), rfl⟩)
      have hvalue : singletonCost a ((target.drop j).take left.length) +
          (exactSuffixDP rest target).getD (j + left.length) 0 = edit [a] left + edit rest right := by
        rw [hleft, singletonCost_eq_edit, ih (j + left.length) (by omega), hright]
      simpa only [List.singleton_append] using hm.trans_eq (hvalue.trans hcost.symm)
    · apply natListMinimum_lower
      · simpa only [List.take_zero, Nat.add_zero] using hcandidate 0 (Nat.zero_le _)
      · intro value hvalue
        rcases List.mem_map.mp hvalue with ⟨k, hk, rfl⟩
        exact hcandidate k (by have := List.mem_range.mp hk; omega)



theorem exactSuffixAnswer_eq_edit {α : Type*} [DecidableEq α] (source target : List α) :
    exactSuffixAnswer source target = edit source target := by
  simpa only [exactSuffixAnswer, List.drop_zero] using exactSuffixDP_spec source target 0 (Nat.zero_le _)

theorem exactSuffixAnswer_equal {α : Type*} [DecidableEq α] (source target : List α)
    (heq : source = target) : exactSuffixAnswer source target = 0 := by
  rw [exactSuffixAnswer_eq_edit]
  exact (cost_eq_zero_iff (s := true)).mpr heq

theorem exactSuffixAnswer_approximation {α : Type*} [DecidableEq α] (source target : List α)
    (epsilon : ℝ) (hepsilon : 0 ≤ epsilon) :
    edit source target ≤ exactSuffixAnswer source target ∧
      (exactSuffixAnswer source target : ℝ) ≤ (1 + epsilon) * edit source target := by
  rw [exactSuffixAnswer_eq_edit]
  constructor
  · exact le_rfl
  · nlinarith [show (0 : ℝ) ≤ (edit source target : ℝ) from Nat.cast_nonneg _]

end EditApproximation

end OAI