File size: 17,567 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import OAI.Computability.Superstring.Periods

namespace OAI

namespace Superstring

namespace Counts.Data

variable {α : Type} [DecidableEq α] [Inhabited α]

/-- Prefix-subtree telescoping, stronger than the total-cost identity. -/
theorem down_descendants (D : Data α) (hD : D.WellFormed) {s : List α} (hs : s ≠ []) :
    (∑ r ∈ D.vertices with s <+: r, D.down r) = D.count s := by
  classical
  by_cases hsm : s ∈ D.vertices
  · let T := D.vertices.filter (fun r => s <+: r)
    have hsT : s ∈ T := Finset.mem_filter.mpr ⟨hsm, List.prefix_refl s⟩
    have hext : (∑ r ∈ T, ∑ c ∈ D.alphabet, D.count (r ++ [c])) =
        ∑ r ∈ T.erase s, D.count r := by
      rw [← Finset.sum_product T 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 hz
        obtain ⟨haT, _⟩ := Finset.mem_product.mp ha
        have hpre := (Finset.mem_filter.mp haT).2
        have hlen := hpre.length_le
        refine Finset.mem_erase.mpr ⟨?_, Finset.mem_filter.mpr
          ⟨D.mem_of_count_ne_zero hz, hpre.trans (List.prefix_append _ _)⟩⟩
        intro he
        have := congrArg List.length he
        simp only [List.length_append, List.length_singleton] at this
        omega
      · intro a _ _ b _ _ hab
        have he := congrArg List.dropLast hab
        simp only [List.dropLast_concat] at he
        have hc : a.2 = b.2 := by
          rw [he] at hab
          simpa only [List.append_cancel_left_eq, List.cons.injEq, and_true] using hab
        exact Prod.ext he hc
      · intro r hr hz
        obtain ⟨hrs, hrT⟩ := Finset.mem_erase.mp hr
        obtain ⟨hrV, hpre⟩ := Finset.mem_filter.mp hrT
        have hrne : r ≠ [] := by
          intro he
          have := hpre.length_le
          have hslen : 0 < s.length := List.length_pos_iff.mpr hs
          simp only [he, List.length_nil] at this
          omega
        have hsl : s.length < r.length := by
          have := hpre.length_le
          by_contra hn
          exact hrs (hpre.eq_of_length (by omega)).symm
        have hpl : s <+: r.dropLast :=
          List.prefix_of_prefix_length_le hpre (List.dropLast_prefix r)
            (by simp only [List.length_dropLast]; omega)
        have heq := List.dropLast_append_getLast hrne
        have hpT : r.dropLast ∈ T := Finset.mem_filter.mpr
          ⟨hD.closed hrV (List.dropLast_prefix r).isInfix, hpl⟩
        have hc : r.getLast hrne ∈ D.alphabet := hD.letters r hrV _
          (List.getLast_mem hrne)
        refine ⟨(r.dropLast, r.getLast hrne), Finset.mem_product.mpr ⟨hpT, hc⟩, ?_, heq⟩
        simpa only [heq] using hz
      · intros
        rfl
    have hadd := Finset.sum_congr rfl (fun r (hr : r ∈ T) =>
      D.down_add hD (s := r) (by
        intro he
        have hh := (Finset.mem_filter.mp hr).2.length_le
        have hslen : 0 < s.length := List.length_pos_iff.mpr hs
        simp only [he, List.length_nil] at hh
        omega))
    rw [Finset.sum_add_distrib, hext] at hadd
    have hsum := Finset.sum_erase_add T D.count hsT
    change (∑ r ∈ T, D.down r) = D.count s
    rw [← hsum] at hadd
    exact Nat.add_right_cancel (hadd.trans (Nat.add_comm _ _))
  · have he : D.vertices.filter (fun r => s <+: r) = ∅ := by
      apply Finset.eq_empty_iff_forall_notMem.mpr
      intro r hr
      obtain ⟨hr, hp⟩ := Finset.mem_filter.mp hr
      exact hsm (hD.closed hr hp.isInfix)
    rw [he, Finset.sum_empty, D.count_zero s (Or.inl hsm)]

/-- Each copy of an actual base down edge is a distinct token. -/
abbrev DownToken (D : Data α) := Σ s : D.vertices, Fin (D.down s.val)

namespace DownToken

variable (D : Data α)

def word (i : D.DownToken) : List α := i.1.val

theorem mem (i : D.DownToken) : word D i ∈ D.vertices := i.1.property

theorem nonempty (i : D.DownToken) : word D i ≠ [] := by
  intro he
  have hi := i.2.isLt
  change i.2.val < D.down (word D i) at hi
  rw [he] at hi
  have hz := D.count_zero [] (Or.inr rfl)
  simp only [down, hz, Nat.zero_sub, Nat.not_lt_zero] at hi

theorem histogram (P : List α → Prop) [DecidablePred P] :
    (Finset.univ.filter (fun i : D.DownToken => P (word D i))).card =
      ∑ s ∈ D.vertices, if P s then D.down s else 0 := by
  classical
  rw [Finset.card_filter, Fintype.sum_sigma]
  change (∑ x : D.vertices, ∑ _ : Fin (D.down x.val), if P x.val then 1 else 0) = _
  simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin,
    nsmul_eq_mul, mul_ite, mul_one, mul_zero]
  exact Finset.sum_coe_sort D.vertices (fun s => if P s then D.down s else 0)

theorem card (hD : D.WellFormed) : Fintype.card D.DownToken = D.weight := by
  have hh := histogram D (fun _ => True)
  simpa only [Finset.filter_true, Finset.card_univ, ite_true, D.total_down hD] using hh

theorem prefix_count (hD : D.WellFormed) {s : List α} (hs : s ≠ []) :
    (Finset.univ.filter (fun i : D.DownToken => s <+: word D i)).card = D.count s := by
  rw [histogram, ← Finset.sum_filter]
  exact D.down_descendants hD hs

/-- The capacity condition needed to thread the down tokens into passages. -/
theorem capacity (hD : D.WellFormed) (s : List α) :
    (Finset.univ.filter (fun i : D.DownToken => s <+: (word D i).tail)).card ≤
      (Finset.univ.filter (fun i : D.DownToken => s <+: word D i)).card := by
  classical
  by_cases hs : s = []
  · subst s
    simp only [List.nil_prefix, Finset.filter_true, le_refl]
  · let U := Finset.univ.filter (fun i : D.DownToken => s <+: (word D i).tail)
    have hf := Finset.sum_card_fiberwise_eq_card_filter U D.alphabet
      (fun i => (word D i).head (nonempty D i))
    have he : U.filter (fun i => (word D i).head (nonempty D i) ∈ D.alphabet) = U := by
      apply Finset.filter_eq_self.mpr
      intro i _
      exact hD.letters _ (mem D i) _ (List.head_mem (nonempty D i))
    rw [he] at hf
    have hterm (c : α) : U.filter (fun i => (word D i).head (nonempty D i) = c) =
        Finset.univ.filter (fun i : D.DownToken => c :: s <+: word D i) := by
      ext i
      simp only [U, Finset.mem_filter, Finset.mem_univ, true_and]
      have hpref (w : List α) (hn : w ≠ []) :
          (s <+: w.tail ∧ w.head hn = c) ↔ c :: s <+: w := by
        cases w with
        | nil => exact (hn rfl).elim
        | cons a w => simp only [List.tail_cons, List.head_cons, List.cons_prefix_cons]
                      tauto
      exact hpref _ (nonempty D i)
    simp_rw [hterm, prefix_count D hD (List.cons_ne_nil _ _)] at hf
    rw [prefix_count D hD hs]
    exact hf ▸ D.left_bound hD hs

end DownToken


theorem down_permutation (D : Data α) (hD : D.WellFormed) :
    ∃ f : Equiv.Perm D.DownToken,
      ∀ i, (DownToken.word D i).tail <+: DownToken.word D (f i) :=
  PrefixMatching.permutation _ _ (DownToken.capacity D hD)

end Counts.Data

namespace Hierarchical

variable {α ι : Type}

/-- Down-edge tokens threaded through the acyclic up-edge runs. -/
structure Threading (V : Set (List α)) (ι : Type) where
  word : ι → List α
  nonempty : ∀ i, word i ≠ []
  mem : ∀ i, word i ∈ V
  next : Equiv.Perm ι
  follows : ∀ i, (word i).tail <+: word (next i)

namespace Threading

variable {V : Set (List α)} (C : Threading V ι)

/-- The letter removed by the down step at each integer start coordinate. -/
def text (i : ι) (x : ℤ) : α :=
  (C.word ((C.next ^ x) i)).head (C.nonempty _)

theorem text_zpow (i : ι) (d x : ℤ) :
    C.text ((C.next ^ d) i) x = C.text i (x + d) := by
  simp only [text, zpow_add, Equiv.Perm.mul_apply]

theorem text_next (i : ι) (x : ℤ) : C.text (C.next i) x = C.text i (x + 1) := by
  simpa only [zpow_one] using C.text_zpow i 1 x

theorem text_period [Fintype ι] (i : ι) :
    Function.Periodic (C.text i) (orderOf C.next : ℤ) := by
  intro x
  rw [← C.text_zpow, zpow_natCast, pow_orderOf_eq_one]
  rfl

theorem exists_primitive_text [Fintype ι] (i : ι) :
    ∃ p, Primitive (C.text i) p :=
  exists_primitive (orderOf_pos C.next) (C.text_period i)

/-- Following tails proves that every old vertex is spelled by the infinite
text, even if it is longer than an entire turn of the threaded circulation. -/
theorem word_spelled (i : ι) :
    C.word i = textWord (C.text i) 0 (C.word i).length := by
  have hget : ∀ k (j : ι) (hk : k < (C.word j).length),
      (C.word j)[k] = C.text j (k : ℤ) := by
    intro k
    induction k with
    | zero =>
      intro j hk
      simp only [text, Nat.cast_zero, zpow_zero, Equiv.Perm.one_apply]
      exact (List.head_eq_getElem (C.nonempty j)).symm
    | succ k ih =>
      intro j hk
      have hkt : k < (C.word j).tail.length := by
        rw [List.length_tail]
        omega
      have hkn : k < (C.word (C.next j)).length :=
        lt_of_lt_of_le hkt (C.follows j).length_le
      calc
        (C.word j)[k + 1] = (C.word j).tail[k] := (List.getElem_tail hkt).symm
        _ = (C.word (C.next j))[k] := (C.follows j).getElem hkt
        _ = C.text (C.next j) (k : ℤ) := ih _ hkn
        _ = C.text j ((k + 1 : ℕ) : ℤ) := by
          simpa only [Nat.cast_add, Nat.cast_one] using C.text_next j (k : ℤ)
  apply List.ext_getElem
  · simp only [length_textWord, sub_zero, Int.toNat_natCast]
  · intro k hk hk'
    simp only [textWord, List.getElem_ofFn, zero_add]
    exact hget k i hk

/-- The actual exit vertex seen at any translated token. -/
theorem translated_word (i : ι) (x : ℤ) :
    C.word ((C.next ^ x) i) =
      textWord (C.text i) x (x + (C.word ((C.next ^ x) i)).length) := by
  calc
    C.word ((C.next ^ x) i) =
        textWord (C.text ((C.next ^ x) i)) 0 (C.word ((C.next ^ x) i)).length :=
      C.word_spelled _
    _ = textWord (C.text i) x (x + (C.word ((C.next ^ x) i)).length) := by
      apply List.ext_getElem
      · simp only [length_textWord, sub_zero, add_sub_cancel_left]
      · intro k hk hk'
        simp only [textWord, List.getElem_ofFn, zero_add, C.text_zpow]
        congr 1
        omega

/-- Entry-exit monotonicity before sorting; all entry windows are nonnegative. -/
theorem exit_mono_one (i : ι) :
    (C.word i).length ≤ 1 + (C.word (C.next i)).length := by
  have hh := (C.follows i).length_le
  rw [List.length_tail] at hh
  omega

end Threading

end Hierarchical

namespace Counts.Data

variable {α : Type} [DecidableEq α] [Inhabited α]

/-- Realize the base down multiset as a periodic circulation on actual vertices. -/
noncomputable def threading (D : Data α) (hD : D.WellFormed) :
    Hierarchical.Threading (D.vertices : Set (List α)) D.DownToken where
  word := DownToken.word D
  nonempty := DownToken.nonempty D
  mem := DownToken.mem D
  next := Classical.choose (D.down_permutation hD)
  follows := Classical.choose_spec (D.down_permutation hD)

end Counts.Data

namespace OrderStats

variable {β : Type} [LinearOrder β] {n : ℕ}

omit [LinearOrder β] in
theorem card_filter_perm (f : Fin n → β) (e : Equiv.Perm (Fin n))
    (P : β → Prop) [DecidablePred P] :
    (Finset.univ.filter (fun i => P (f (e i)))).card =
      (Finset.univ.filter (fun i => P (f i))).card := by
  rw [Finset.card_filter, Finset.card_filter]
  exact Equiv.sum_comp e (fun i => if P (f i) then 1 else 0)

theorem order_perm (f : Fin n → β) (e : Equiv.Perm (Fin n)) (i : Fin n) :
    order (fun j => f (e j)) i = order f i :=
  order_eq_of_counts (fun t => card_filter_perm f e (fun v => v ≤ t)) i

end OrderStats

namespace Hierarchical.Threading

variable {α ι : Type} {V : Set (List α)} [Fintype ι]

/-- Tokens with a specified full-text alignment, not just a matching finite word. -/
def Aligned (C : Threading V ι) (A : ℤ → α) := {i : ι // C.text i = A}

noncomputable instance (C : Threading V ι) (A : ℤ → α) : Fintype (C.Aligned A) := by
  classical
  exact inferInstanceAs (Fintype {i : ι // C.text i = A})

noncomputable def enumerate (C : Threading V ι) (A : ℤ → α) :
    Fin (Fintype.card (C.Aligned A)) ≃ C.Aligned A := (Fintype.equivFin _).symm

/-- A text period permutes, rather than fixes, the multiple aligned passages. -/
def periodPerm (C : Threading V ι) {A : ℤ → α} {p : ℕ}
    (hp : Function.Periodic A (p : ℤ)) : Equiv.Perm (C.Aligned A) :=
  (C.next ^ (p : ℤ)).subtypeEquiv (by
    intro i
    constructor
    · intro hi
      funext x
      rw [C.text_zpow, hi]
      exact hp x
    · intro hi
      funext x
      have hh := congrFun hi (x - p)
      rw [C.text_zpow, sub_add_cancel] at hh
      exact hh.trans (by simpa only [sub_eq_add_neg] using hp.neg x))

/-- Exit ends of the unsorted primitive passages. -/
noncomputable def exits (C : Threading V ι) (A : ℤ → α) (x : ℤ)
    (j : Fin (Fintype.card (C.Aligned A))) : ℤ :=
  x + (C.word ((C.next ^ x) (C.enumerate A j).val)).length

theorem exits_step (C : Threading V ι) (A : ℤ → α) (x : ℤ)
    (j : Fin (Fintype.card (C.Aligned A))) : C.exits A x j ≤ C.exits A (x + 1) j := by
  have he : (C.next ^ (x + 1)) (C.enumerate A j).val =
      C.next ((C.next ^ x) (C.enumerate A j).val) := by
    rw [show x + 1 = 1 + x by omega, zpow_add, zpow_one, Equiv.Perm.mul_apply]
  have hh := C.exit_mono_one ((C.next ^ x) (C.enumerate A j).val)
  simp only [exits, he]
  omega

theorem exits_lower (C : Threading V ι) (A : ℤ → α) (x : ℤ)
    (j : Fin (Fintype.card (C.Aligned A))) : x + 1 ≤ C.exits A x j := by
  have hh := List.length_pos_iff.mpr (C.nonempty ((C.next ^ x) (C.enumerate A j).val))
  simp only [exits]
  omega

/-- Sorting independently at every coordinate gives the monotone layer function. -/
noncomputable def sortedExit (C : Threading V ι) (A : ℤ → α)
    (j : Fin (Fintype.card (C.Aligned A))) (x : ℤ) : ℤ := OrderStats.order (C.exits A x) j

theorem sortedExit_mono (C : Threading V ι) (A : ℤ → α)
    (j : Fin (Fintype.card (C.Aligned A))) : Monotone (C.sortedExit A j) := by
  apply monotone_int_of_le_succ
  intro x
  exact OrderStats.order_le_order (C.exits_step A x) j

theorem sortedExit_lower (C : Threading V ι) (A : ℤ → α)
    (j : Fin (Fintype.card (C.Aligned A))) (x : ℤ) : x ≤ C.sortedExit A j (x - 1) := by
  obtain ⟨k, hk⟩ := OrderStats.order_mem (C.exits A (x - 1)) j
  change x ≤ OrderStats.order (C.exits A (x - 1)) j
  rw [← hk]
  have := C.exits_lower A (x - 1) k
  omega

theorem exits_period (C : Threading V ι) {A : ℤ → α} {p : ℕ}
    (hp : Function.Periodic A (p : ℤ)) (x : ℤ)
    (j : Fin (Fintype.card (C.Aligned A))) :
    C.exits A (x + p) j =
      C.exits A x ((C.enumerate A).symm (C.periodPerm hp (C.enumerate A j))) + p := by
  have he : (C.periodPerm hp (C.enumerate A j)).val =
      (C.next ^ (p : ℤ)) (C.enumerate A j).val := rfl
  simp only [exits, Equiv.apply_symm_apply, he, zpow_add, Equiv.Perm.mul_apply]
  omega

theorem sortedExit_shift (C : Threading V ι) {A : ℤ → α} {p : ℕ}
    (hp : Function.Periodic A (p : ℤ))
    (j : Fin (Fintype.card (C.Aligned A))) (x : ℤ) :
    C.sortedExit A j (x + p) = C.sortedExit A j x + p := by
  let e : Equiv.Perm (Fin (Fintype.card (C.Aligned A))) :=
    (C.enumerate A).trans ((C.periodPerm hp).trans (C.enumerate A).symm)
  have he : C.exits A (x + p) = fun k => C.exits A x (e k) + p := by
    funext k
    exact C.exits_period hp x k
  change OrderStats.order (C.exits A (x + p)) j =
    OrderStats.order (C.exits A x) j + (p : ℤ)
  rw [he, OrderStats.order_add, OrderStats.order_perm]

theorem exits_actual (C : Threading V ι) (A : ℤ → α) (x : ℤ)
    (j : Fin (Fintype.card (C.Aligned A))) : textWord A x (C.exits A x j) ∈ V := by
  have he := C.translated_word (C.enumerate A j).val x
  rw [(C.enumerate A j).property] at he
  exact he ▸ C.mem ((C.next ^ x) (C.enumerate A j).val)

/-- The sorted windows are genuine substring vertices. No abstract completion of
V or artificial layer windows are introduced. -/
theorem sortedExit_actual (C : Threading V ι) (closed : SubstringClosed V)
    (A : ℤ → α) (j : Fin (Fintype.card (C.Aligned A))) (x e : ℤ)
    (hlo : C.sortedExit A j (x - 1) ≤ e) (hhi : e ≤ C.sortedExit A j x) :
    textWord A x e ∈ V := by
  obtain ⟨k, hk⟩ := OrderStats.order_mem (C.exits A x) j
  have hh : e ≤ C.exits A x k := by
    change e ≤ OrderStats.order (C.exits A x) j at hhi
    rwa [hk]
  exact closed (C.exits_actual A x k)
    (textWord_infix A le_rfl ((C.sortedExit_lower A j x).trans hlo) hh)

/-- The ordered-layer construction for a specified primitive-text group. -/
noncomputable def layer (C : Threading V ι) (closed : SubstringClosed V)
    {A : ℤ → α} {p : ℕ} (hp : Function.Periodic A (p : ℤ))
    (j : Fin (Fintype.card (C.Aligned A))) : Layer V A p where
  z := C.sortedExit A j
  mono := C.sortedExit_mono A j
  lower := C.sortedExit_lower A j
  shift := C.sortedExit_shift hp j
  actual := C.sortedExit_actual closed A j

theorem layers_ordered (C : Threading V ι) (A : ℤ → α)
    (i j : Fin (Fintype.card (C.Aligned A))) (hij : i ≤ j) (x : ℤ) :
    C.sortedExit A i x ≤ C.sortedExit A j x := OrderStats.monotone_order _ hij

/-- Exact conservation of vertical unit edges under the sorting operation. -/
theorem sorted_edges (C : Threading V ι) (A : ℤ → α) (x e : ℤ) :
    (Finset.univ.filter (fun j => C.sortedExit A j (x - 1) ≤ e ∧
      e < C.sortedExit A j x)).card =
    (Finset.univ.filter (fun j => C.exits A (x - 1) j ≤ e ∧
      e < C.exits A x j)).card := by
  apply OrderStats.sorted_edge_count
  intro j
  have hh := C.exits_step A (x - 1) j
  simpa only [sub_add_cancel] using hh

end Hierarchical.Threading

end Superstring

end OAI