File size: 11,893 Bytes
760f90e | 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | import OAI.Computability.Superstring.Counts
namespace OAI
namespace Superstring
namespace Input
variable {α : Type} [DecidableEq α]
/-- Quadratic substring enumeration, with boundaries represented explicitly. -/
def subwords (s : List α) : Finset (List α) :=
(Finset.range (s.length + 1)).biUnion fun i =>
(Finset.range (s.length + 1)).image fun n => (s.drop i).take n
theorem mem_subwords {w s : List α} : w ∈ subwords s ↔ w <:+: s := by
constructor
· intro hw
obtain ⟨i, _, hw⟩ := Finset.mem_biUnion.mp hw
obtain ⟨n, _, rfl⟩ := Finset.mem_image.mp hw
exact (List.take_prefix _ _).isInfix.trans (List.drop_suffix _ _).isInfix
· rintro ⟨a, b, rfl⟩
apply Finset.mem_biUnion.mpr
refine ⟨a.length, Finset.mem_range.mpr (by simp only [List.length_append]; omega), ?_⟩
apply Finset.mem_image.mpr
refine ⟨w.length, Finset.mem_range.mpr (by simp only [List.length_append]; omega), ?_⟩
simp
/-- Deleting empty, duplicate and properly contained input words. -/
def core (S : List (List α)) : Finset (List α) :=
S.toFinset.filter fun s => s ≠ [] ∧ ∀ t ∈ S, s <:+: t → t = s
theorem mem_core {S : List (List α)} {s : List α} :
s ∈ core S ↔ s ∈ S ∧ s ≠ [] ∧ ∀ t ∈ S, s <:+: t → t = s := by
simp only [core, Finset.mem_filter, List.mem_toFinset]
theorem core_cover {S : List (List α)} {s : List α} (hs : s ∈ S) (hne : s ≠ []) :
∃ t ∈ core S, s <:+: t := by
let F := S.toFinset.filter (fun t => s <:+: t)
have hF : F.Nonempty := ⟨s, Finset.mem_filter.mpr ⟨List.mem_toFinset.mpr hs, List.infix_refl s⟩⟩
obtain ⟨t, ht, hmax⟩ := Finset.exists_max_image F List.length hF
obtain ⟨htS, hst⟩ := Finset.mem_filter.mp ht
refine ⟨t, mem_core.mpr ⟨List.mem_toFinset.mp htS, ?_, ?_⟩, hst⟩
· intro ht0
subst t
exact hne (List.eq_nil_of_length_eq_zero (by have := hst.length_le; simpa using this))
· intro u hu htu
apply (htu.eq_of_length_le ?_).symm
exact hmax u (Finset.mem_filter.mpr ⟨List.mem_toFinset.mpr hu, hst.trans htu⟩)
def vertices (S : List (List α)) : Finset (List α) :=
insert [] (S.toFinset.biUnion subwords)
theorem mem_vertices {S : List (List α)} {w : List α} :
w ∈ vertices S ↔ w = [] ∨ ∃ s ∈ S, w <:+: s := by
simp only [vertices, Finset.mem_insert, Finset.mem_biUnion, List.mem_toFinset,
mem_subwords]
def alphabet (S : List (List α)) : Finset α := S.flatten.toFinset
def data (S : List (List α)) : Counts.Data α := ⟨vertices S, core S, alphabet S⟩
theorem closed (S : List (List α)) : Hierarchical.SubstringClosed (vertices S : Set (List α)) := by
intro s hs w hws
rcases mem_vertices.mp hs with rfl | ⟨t, ht, hst⟩
· apply mem_vertices.mpr
left
exact List.eq_nil_of_length_eq_zero (by have := hws.length_le; simpa using this)
· exact mem_vertices.mpr (Or.inr ⟨t, ht, hws.trans hst⟩)
theorem letter_mem {S : List (List α)} {w : List α} (hw : w ∈ vertices S)
{c : α} (hc : c ∈ w) : c ∈ alphabet S := by
rcases mem_vertices.mp hw with rfl | ⟨s, hs, hws⟩
· simp only [List.not_mem_nil] at hc
· exact List.mem_toFinset.mpr (List.mem_flatten.mpr ⟨s, hs, hws.sublist.subset hc⟩)
theorem required_mem {S : List (List α)} {w : List α} (hw : w ∈ core S) :
w ∈ vertices S := mem_vertices.mpr (Or.inr ⟨w, (mem_core.mp hw).1, List.infix_refl w⟩)
theorem core_superstring_iff (S : List (List α)) (T : List α) :
(∀ s ∈ core S, s <:+: T) ↔ ∀ s ∈ S, s <:+: T := by
constructor
· intro h s hs
by_cases hnil : s = []
· subst s
exact List.nil_infix
· obtain ⟨t, ht, hst⟩ := core_cover hs hnil
exact hst.trans (h t ht)
· intro h s hs
exact h s (mem_core.mp hs).1
end Input
theorem concatenation_superstring (S : Instance) : IsCommonSuperstring S S.flatten :=
fun _ hs => List.infix_of_mem_flatten hs
theorem feasible_lengths_nonempty (S : Instance) :
{n : ℕ | ∃ T : Word, IsCommonSuperstring S T ∧ T.length = n}.Nonempty :=
⟨S.flatten.length, S.flatten, concatenation_superstring S, rfl⟩
/-- The actual computed lower bound is bounded by the unrestricted optimum. -/
theorem weight_le_opt (S : Instance) : (Input.data S).weight ≤ opt S := by
unfold opt
apply le_csInf (feasible_lengths_nonempty S)
rintro n ⟨T, hT, rfl⟩
apply Counts.Data.weight_le_length
exact (Input.core_superstring_iff S T).mpr hT
theorem input_count_le_total (S : Instance) {w : Word} (hw : w ≠ []) :
(Input.data S).count w ≤ S.flatten.length :=
le_trans ((Input.data S).count_le_number S.flatten
((Input.core_superstring_iff S S.flatten).mpr (concatenation_superstring S)) w)
(Counts.number_le_length hw)
namespace Counts.Data
variable {α : Type} [DecidableEq α] [Inhabited α]
/-- The hypotheses supplied by the actual all-substrings input construction. -/
structure WellFormed (D : Counts.Data α) : Prop where
empty_mem : [] ∈ D.vertices
closed : Hierarchical.SubstringClosed (D.vertices : Set (List α))
letters : ∀ s ∈ D.vertices, ∀ c ∈ s, c ∈ D.alphabet
omit [Inhabited α] in
theorem input_wellFormed (S : List (List α)) : (Input.data S).WellFormed :=
⟨Finset.mem_insert_self _ _, Input.closed S, fun _ hs _ hc => Input.letter_mem hs hc⟩
theorem mem_of_count_ne_zero (D : Data α) {s : List α} (hs : D.count s ≠ 0) :
s ∈ D.vertices := by
by_contra h
exact hs (D.count_zero _ (Or.inl h))
theorem left_bound (D : Data α) (hD : D.WellFormed) {s : List α} (hs : s ≠ []) :
(∑ c ∈ D.alphabet, D.count (c :: s)) ≤ D.count s := by
by_cases hmem : s ∈ D.vertices
· rw [← D.left_count]
exact D.left_le_count hmem hs
· have hz : ∀ c, D.count (c :: s) = 0 := by
intro c
apply D.count_zero
left
intro hcs
exact hmem (hD.closed hcs (List.suffix_cons c s).isInfix)
simp only [hz, Finset.sum_const_zero, Nat.zero_le]
theorem right_bound (D : Data α) (hD : D.WellFormed) {s : List α} (hs : s ≠ []) :
(∑ c ∈ D.alphabet, D.count (s ++ [c])) ≤ D.count s := by
by_cases hmem : s ∈ D.vertices
· rw [← D.right_count]
exact D.right_le_count hmem hs
· have hz : ∀ c, D.count (s ++ [c]) = 0 := by
intro c
apply D.count_zero
left
intro hsc
exact hmem (hD.closed hsc (List.prefix_append s [c]).isInfix)
simp only [hz, Finset.sum_const_zero, Nat.zero_le]
def up (D : Data α) (s : List α) : ℕ :=
D.count s - ∑ c ∈ D.alphabet, D.count (c :: s)
def down (D : Data α) (s : List α) : ℕ :=
D.count s - ∑ c ∈ D.alphabet, D.count (s ++ [c])
theorem up_add (D : Data α) (hD : D.WellFormed) {s : List α} (hs : s ≠ []) :
D.up s + ∑ c ∈ D.alphabet, D.count (c :: s) = D.count s :=
Nat.sub_add_cancel (D.left_bound hD hs)
theorem down_add (D : Data α) (hD : D.WellFormed) {s : List α} (hs : s ≠ []) :
D.down s + ∑ c ∈ D.alphabet, D.count (s ++ [c]) = D.count s :=
Nat.sub_add_cancel (D.right_bound hD hs)
/-- Balance is an exact natural-number equality; no truncated subtraction is
silently cancelled without the extension inequalities. -/
theorem balanced (D : Data α) (hD : D.WellFormed) (s : List α) :
D.up s + ∑ c ∈ D.alphabet, D.down (c :: s) =
D.down s + ∑ c ∈ D.alphabet, D.up (s ++ [c]) := by
have hd := Finset.sum_congr rfl (fun c (_hc : c ∈ D.alphabet) =>
D.down_add hD (s := c :: s) (List.cons_ne_nil c s))
have hu := Finset.sum_congr rfl (fun c (_hc : c ∈ D.alphabet) =>
D.up_add hD (s := s ++ [c]) (by simp))
rw [Finset.sum_add_distrib] at hd hu
have heq : (∑ c ∈ D.alphabet, ∑ d ∈ D.alphabet, D.count ((c :: s) ++ [d])) =
∑ c ∈ D.alphabet, ∑ d ∈ D.alphabet, D.count (d :: (s ++ [c])) := by
rw [Finset.sum_comm]
rfl
rw [heq] at hd
by_cases hs : s = []
· subst s
have hz : D.count [] = 0 := D.count_zero _ (Or.inr rfl)
have huz : D.up [] = 0 := by simp only [up, hz, Nat.zero_sub]
have hdz : D.down [] = 0 := by simp only [down, hz, Nat.zero_sub]
rw [huz, hdz]
simp only [List.nil_append, zero_add] at hd hu ⊢
omega
· have hl := D.up_add hD hs
have hr := D.down_add hD hs
omega
/-- Reindex all left extensions by their unique first letter and suffix. -/
theorem sum_left_all (D : Data α) (hD : D.WellFormed) :
(∑ s ∈ D.vertices, ∑ c ∈ D.alphabet, D.count (c :: s)) =
∑ r ∈ D.vertices, D.count r := by
rw [← Finset.sum_product D.vertices D.alphabet (fun a => D.count (a.2 :: a.1))]
apply Finset.sum_bij_ne_zero (fun a _ _ => a.2 :: a.1)
· intro a _ ha
exact D.mem_of_count_ne_zero ha
· intro a _ _ b _ _ hab
obtain ⟨hc, hs⟩ := List.cons.inj hab
exact Prod.ext hs hc
· intro r hr hz
cases r with
| nil => exact (hz (D.count_zero _ (Or.inr rfl))).elim
| cons c s =>
have hs : s ∈ D.vertices := hD.closed hr (List.suffix_cons c s).isInfix
have hc : c ∈ D.alphabet := hD.letters _ hr c (List.mem_cons_self ..)
exact ⟨(s, c), Finset.mem_product.mpr ⟨hs, hc⟩, hz, rfl⟩
· intros
rfl
/-- The dual reindexing by prefix and final letter. -/
theorem sum_right_all (D : Data α) (hD : D.WellFormed) :
(∑ s ∈ D.vertices, ∑ c ∈ D.alphabet, D.count (s ++ [c])) =
∑ r ∈ D.vertices, D.count r := by
rw [← Finset.sum_product D.vertices D.alphabet (fun a => D.count (a.1 ++ [a.2]))]
apply Finset.sum_bij_ne_zero (fun a _ _ => a.1 ++ [a.2])
· intro a _ ha
exact D.mem_of_count_ne_zero ha
· intro a _ _ b _ _ hab
have hs := congrArg List.dropLast hab
simp only [List.dropLast_concat] at hs
have hc : a.2 = b.2 := by
rw [hs] at hab
simpa only [List.append_cancel_left_eq, List.cons.injEq, and_true] using hab
exact Prod.ext hs hc
· intro r hr hz
have hne : r ≠ [] := by intro he; subst r; exact hz (D.count_zero _ (Or.inr rfl))
let s := r.dropLast
let c := r.getLast hne
have hrsc : s ++ [c] = r := List.dropLast_append_getLast hne
have hs : s ∈ D.vertices := hD.closed hr (hrsc ▸ (List.prefix_append s [c]).isInfix)
have hc : c ∈ D.alphabet := hD.letters r hr c (hrsc ▸ (by simp))
refine ⟨(s, c), Finset.mem_product.mpr ⟨hs, hc⟩, ?_, hrsc⟩
simpa only [hrsc] using hz
· intros
rfl
/-- Telescoping of all actual up-edge multiplicities. -/
theorem total_up (D : Data α) (hD : D.WellFormed) :
(∑ s ∈ D.vertices, D.up s) = D.weight := by
have hm := Finset.sum_erase_add D.vertices D.count hD.empty_mem
have hl := Finset.sum_erase_add D.vertices
(fun s => ∑ c ∈ D.alphabet, D.count (c :: s)) hD.empty_mem
have hu := Finset.sum_erase_add D.vertices D.up hD.empty_mem
have hh := Finset.sum_congr rfl (fun s (hs : s ∈ D.vertices.erase []) =>
D.up_add hD (Finset.mem_erase.mp hs).1)
rw [Finset.sum_add_distrib] at hh
have hz : D.count [] = 0 := D.count_zero _ (Or.inr rfl)
have huz : D.up [] = 0 := by simp only [up, hz, Nat.zero_sub]
rw [D.sum_left_all hD] at hl
change _ + D.weight = _ at hl
omega
theorem total_down (D : Data α) (hD : D.WellFormed) :
(∑ s ∈ D.vertices, D.down s) = D.weight := by
have hm := Finset.sum_erase_add D.vertices D.count hD.empty_mem
have hl := Finset.sum_erase_add D.vertices
(fun s => ∑ c ∈ D.alphabet, D.count (s ++ [c])) hD.empty_mem
have hd := Finset.sum_erase_add D.vertices D.down hD.empty_mem
have hh := Finset.sum_congr rfl (fun s (hs : s ∈ D.vertices.erase []) =>
D.down_add hD (Finset.mem_erase.mp hs).1)
rw [Finset.sum_add_distrib] at hh
have hz : D.count [] = 0 := D.count_zero _ (Or.inr rfl)
have hdz : D.down [] = 0 := by simp only [down, hz, Nat.zero_sub]
rw [D.sum_right_all hD] at hl
change _ + D.weight = _ at hl
omega
end Counts.Data
end Superstring
end OAI
|