File size: 9,130 Bytes
180bc03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.NumberTheory.TwoPoint.Walks.ForestPathCounting

namespace OAI

/-!
# Decoding complete column patterns

Three binary streams merge regular segments with omitted runs, mark new
runs, and mark imperfect positions. Representative indices supply omitted
labels and imperfect attachments. All decoding is independent of numerical
primes, quotient coordinates, and padding coefficients.
-/

namespace TwoPointCorrelations

/-- A valid path code determines one family; invalid codes decode to empty
lists. The proven uniqueness makes this choice independent of witnesses. -/
noncomputable def decodeForestPaths {N segments : ℕ}
    (code : ForestPathData.Code N segments) : Fin segments → List ℕ := by
  classical
  exact if h : ∃ d : ForestPathData N segments, d.code = code then h.choose.pattern else fun _ => []

theorem decodeForestPaths_code {N segments : ℕ} (d : ForestPathData N segments) :
    decodeForestPaths d.code = d.pattern := by
  classical
  unfold decodeForestPaths
  split_ifs with h
  · exact ForestPathData.pattern_eq_of_code_eq _ _ h.choose_spec
  · exact (h ⟨d, rfl⟩).elim

inductive CanonicalColumnLabel
  | regular : ℕ → CanonicalColumnLabel
  | omitted : ℕ → CanonicalColumnLabel
  | freshImperfect : ℕ → CanonicalColumnLabel
  deriving DecidableEq

/-- Incidence supports start with a line and alternate line/point. -/
def evenEntries {α : Type*} : List α → List α
  | [] => []
  | [a] => [a]
  | a :: _ :: rest => a :: evenEntries rest

/-- A true bit consumes a regular segment; a false bit consumes one
omitted run. Unused trailing bits have no effect. -/
def mergeColumnRuns {α : Type*} : List Bool → List (List α) → List α → List α
  | [], _, _ => []
  | true :: mask, regular, omitted =>
      regular.headD [] ++ mergeColumnRuns mask regular.tail omitted
  | false :: mask, regular, omitted =>
      omitted.head?.toList ++ mergeColumnRuns mask regular omitted.tail

lemma mergeColumnRuns_empty {α : Type*} (mask : List Bool) :
    mergeColumnRuns mask ([] : List (List α)) [] = [] := by
  induction mask with
  | nil => rfl
  | cons b mask ih => cases b <;> simp [mergeColumnRuns, ih]

/-- The binary merge data recover any interleaving of regular segments
and omitted single runs, including padding to a fixed code length. -/
theorem mergeColumnRuns_recover {α : Type*} (pieces : List (List α ⊕ α))
    (padding : List Bool) :
    mergeColumnRuns
      (pieces.map (fun x => match x with | .inl _ => true | .inr _ => false) ++ padding)
      (pieces.filterMap (fun x => match x with | .inl a => some a | .inr _ => none))
      (pieces.filterMap (fun x => match x with | .inl _ => none | .inr a => some a)) =
        pieces.flatMap (fun x => match x with | .inl a => a | .inr a => [a]) := by
  induction pieces with
  | nil => exact mergeColumnRuns_empty padding
  | cons piece pieces ih => cases piece <;> simp [mergeColumnRuns, ih]

/-- The first bit marks an imperfect position; the second begins a new
perfect run. Repetition consumes no extra run label. -/
def expandColumnRuns : List (Bool × Bool) → List CanonicalColumnLabel →
    Option CanonicalColumnLabel → List (Option CanonicalColumnLabel)
  | [], _, _ => []
  | (true, _) :: flags, runs, _ => none :: expandColumnRuns flags runs none
  | (false, true) :: flags, runs, _ =>
      runs.head? :: expandColumnRuns flags runs.tail runs.head?
  | (false, false) :: flags, runs, previous =>
      previous :: expandColumnRuns flags runs previous

theorem expandColumnRuns_repeat (n : ℕ) (flags : List (Bool × Bool))
    (runs : List CanonicalColumnLabel) (previous : Option CanonicalColumnLabel) :
    expandColumnRuns (List.replicate n (false, false) ++ flags) runs previous =
      List.replicate n previous ++ expandColumnRuns flags runs previous := by
  induction n with
  | zero => rfl
  | succ n ih => simp [List.replicate_succ, expandColumnRuns, ih]

/-- Positive constant-run lengths are recovered from their boundary bits. -/
theorem expandColumnRuns_one_run (n : ℕ) (a : CanonicalColumnLabel)
    (flags : List (Bool × Bool)) (runs : List CanonicalColumnLabel)
    (previous : Option CanonicalColumnLabel) :
    expandColumnRuns ((false, true) :: (List.replicate n (false, false) ++ flags))
        (a :: runs) previous =
      List.replicate (n + 1) (some a) ++ expandColumnRuns flags runs (some a) := by
  simp only [expandColumnRuns, List.head?_cons, List.tail_cons, expandColumnRuns_repeat,
    List.replicate_succ, List.cons_append]

@[simp] theorem expandColumnRuns_length (flags : List (Bool × Bool))
    (runs : List CanonicalColumnLabel) (previous : Option CanonicalColumnLabel) :
    (expandColumnRuns flags runs previous).length = flags.length := by
  induction flags generalizing runs previous with
  | nil => rfl
  | cons flag flags ih => cases flag with
    | mk imperfect newRun =>
      cases imperfect <;> cases newRun <;> simp [expandColumnRuns, ih]

/-- References to represented perfect labels reuse those labels. A
reference to an unrepresented imperfect class uses its first position. -/
def attachImperfectLabels (full : List (Option CanonicalColumnLabel)) :
    List (Option CanonicalColumnLabel) → List ℕ → List CanonicalColumnLabel
  | [], _ => []
  | some a :: rest, refs => a :: attachImperfectLabels full rest refs
  | none :: rest, refs =>
      let j := refs.headD 0
      ((full[j]?.join).getD (.freshImperfect j)) ::
        attachImperfectLabels full rest refs.tail

@[simp] theorem attachImperfectLabels_length (full rest : List (Option CanonicalColumnLabel))
    (refs : List ℕ) : (attachImperfectLabels full rest refs).length = rest.length := by
  induction rest generalizing refs with
  | nil => rfl
  | cons a rest ih => cases a <;> simp [attachImperfectLabels, ih]

abbrev ColumnDecoderCode (N segments omitted imperfect : ℕ) :=
  ForestPathData.Code N segments ×
    (Fin N → Bool) × (Fin N → Bool × Bool) ×
    (Fin omitted → Fin N) × (Fin imperfect → Fin N)

noncomputable def decodeColumnLabels {N segments omitted imperfect : ℕ}
    (code : ColumnDecoderCode N segments omitted imperfect) : List CanonicalColumnLabel :=
  let paths := List.ofFn (fun i => (evenEntries (decodeForestPaths code.1 i)).map
    CanonicalColumnLabel.regular)
  let omittedLabels := List.ofFn (fun i => CanonicalColumnLabel.omitted (code.2.2.2.1 i).val)
  let runs := mergeColumnRuns (List.ofFn code.2.1) paths omittedLabels
  let full := expandColumnRuns (List.ofFn code.2.2.1) runs none
  attachImperfectLabels full full (List.ofFn (fun i => (code.2.2.2.2 i).val))

theorem decodeColumnLabels_length {N segments omitted imperfect : ℕ}
    (code : ColumnDecoderCode N segments omitted imperfect) :
    (decodeColumnLabels code).length = N := by
  simp only [decodeColumnLabels, attachImperfectLabels_length, expandColumnRuns_length,
    List.length_ofFn]

/-- An equality pattern contains no numerical label names. -/
noncomputable def decodeColumnPattern {N segments omitted imperfect : ℕ}
    (code : ColumnDecoderCode N segments omitted imperfect) : Fin N → Fin N → Bool :=
  fun i j => decide ((decodeColumnLabels code).getD i (.freshImperfect 0) =
    (decodeColumnLabels code).getD j (.freshImperfect 0))

theorem card_columnDecoderCode_le (N segments omitted imperfect : ℕ) :
    Fintype.card (ColumnDecoderCode N segments omitted imperfect) ≤
      2 ^ (6 * N + 1) * N ^ (2 * segments + omitted + imperfect) := by
  have h := ForestPathData.card_code_le N segments
  calc
    _ = Fintype.card (ForestPathData.Code N segments) *
        (2 ^ N * ((2 * 2) ^ N * (N ^ omitted * N ^ imperfect))) := by
      simp only [ColumnDecoderCode, Fintype.card_prod, Fintype.card_fun, Fintype.card_fin,
        Fintype.card_bool]
    _ ≤ (2 ^ (3 * N + 1) * N ^ (2 * segments)) *
        (2 ^ N * ((2 * 2) ^ N * (N ^ omitted * N ^ imperfect))) :=
      Nat.mul_le_mul_right _ h
    _ = _ := by
      rw [mul_pow]
      calc
        _ = (2 ^ (3 * N + 1) * 2 ^ N * 2 ^ N * 2 ^ N) *
            (N ^ (2 * segments) * N ^ omitted * N ^ imperfect) := by ring
        _ = _ := by
          have hb : 2 ^ (3 * N + 1) * 2 ^ N * 2 ^ N * 2 ^ N =
              2 ^ (6 * N + 1) := by
            rw [← pow_add, ← pow_add, ← pow_add]
            congr 1
            omega
          rw [hb, ← pow_add, ← pow_add]

/-- The decoder's image is one universal, coefficient-independent family
of full column equality patterns. -/
theorem card_decoded_column_patterns (N segments omitted imperfect : ℕ) :
    Nat.card (Set.range (@decodeColumnPattern N segments omitted imperfect)) ≤
      2 ^ (6 * N + 1) * N ^ (2 * segments + omitted + imperfect) := by
  let f := @decodeColumnPattern N segments omitted imperfect
  have h : Nat.card (Set.range f) ≤ Nat.card (ColumnDecoderCode N segments omitted imperfect) :=
    Nat.card_le_card_of_surjective (fun c => (⟨f c, c, rfl⟩ : Set.range f)) (by
      rintro ⟨_, c, rfl⟩
      exact ⟨c, rfl⟩)
  exact h.trans (by
    simpa only [Nat.card_eq_fintype_card] using card_columnDecoderCode_le N segments omitted imperfect)

end TwoPointCorrelations

end OAI