File size: 9,086 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockSearchEncoding
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockBasisProgram
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockCoefficientContext
import OAI.Algorithms.SubexponentialSAT.Subexp.GatherProgram

namespace OAI.Algorithms

/-!
# Gathering a searched basis table into vector-major order

The local search stores ambient coordinates as rows and basis vectors as
columns. Its scalar frames have fixed width, so the checked gather program
can transpose those frames without decoding or changing a field element.
The result is exactly the kernel/complement table consumed by block copying.

This adapter's runtime inputs are the searched table, its unary frame width,
and the explicit transpose read plan. It proves the physical reordering and
the plan's polynomial size; producing the read-plan bytes is a separate stage.
-/

namespace SubexponentialSAT.Subexp.BasisTransposeProgram

open StackEncoding BlockSearchEncoding

def framedCode {A : Type*} (code : BlockCode A) : BlockCode A where
  width := 2 * code.width + 1
  width_pos := by omega
  block a := Encoding.frame (code.block a)
  length_block a := by simp [code.length_block]
  injective_block := by
    intro a b h
    apply code.injective_block
    exact (Encoding.frame_append_injective (bits := code.block a) (bits' := code.block b)
      (rest := []) (rest' := []) (by simpa using h)).1

def transposeIndex {N : β„•} (i : Fin (N * N)) : Fin (N * N) :=
  finProdFinEquiv ((finProdFinEquiv.symm i).2, (finProdFinEquiv.symm i).1)

def readPlan (N : β„•) : List β„• :=
  List.ofFn fun i : Fin (N * N) => (transposeIndex i).val

@[simp] theorem readPlan_length (N : β„•) : (readPlan N).length = N * N := by
  simp [readPlan]

theorem readPlan_valid (N : β„•) : βˆ€ i ∈ readPlan N, i < N * N := by
  intro i hi
  obtain ⟨j, rfl⟩ := List.mem_ofFn.mp hi
  exact (transposeIndex j).isLt

theorem readPlan_eq_nested (N : β„•) :
    readPlan N = (List.ofFn fun col : Fin N =>
      List.ofFn fun row : Fin N => row.val * N + col.val).flatten := by
  rw [readPlan, List.ofFn_mul]
  apply congrArg List.flatten
  apply congrArg List.ofFn
  funext col
  apply congrArg List.ofFn
  funext row
  have hv : (finProdFinEquiv (col, row)).val = col.val * N + row.val := by
    simp [finProdFinEquiv, Nat.mul_comm, Nat.add_comm]
  have hb : col.val * N + row.val < N * N := by
    rw [← hv]
    exact (finProdFinEquiv (col, row)).isLt
  change (transposeIndex (⟨col.val * N + row.val, hb⟩ : Fin (N * N))).val = _
  rw [show (⟨col.val * N + row.val, hb⟩ : Fin (N * N)) = finProdFinEquiv (col, row) from
    Fin.ext hv.symm]
  simp only [transposeIndex, Equiv.symm_apply_apply]
  simp [finProdFinEquiv, Nat.mul_comm, Nat.add_comm]

theorem readPlan_eq_ranges (N : β„•) :
    readPlan N = (List.range N).flatMap fun col =>
      (List.range N).map fun row => row * N + col := by
  rw [readPlan_eq_nested]
  simp only [List.flatMap_def, List.ofFn_eq_map, ← List.map_coe_finRange N,
    List.map_map, Function.comp_def]

def rows {A : Type*} (code : BlockCode A) {N : β„•} (T : Fin N β†’ Fin N β†’ A) :
    List (List Bool) :=
  List.ofFn fun i => (framedCode code).block (flattenTable T i)

@[simp] theorem rows_length {A : Type*} (code : BlockCode A) {N : β„•}
    (T : Fin N β†’ Fin N β†’ A) : (rows code T).length = N * N := by
  simp [rows]

theorem rows_width {A : Type*} (code : BlockCode A) {N : β„•}
    (T : Fin N β†’ Fin N β†’ A) :
    βˆ€ row ∈ rows code T, row.length = (framedCode code).width := by
  intro row hr
  obtain ⟨i, rfl⟩ := List.mem_ofFn.mp hr
  exact (framedCode code).length_block _

theorem rows_flatten {A : Type*} (code : BlockCode A) {N : β„•}
    (T : Fin N β†’ Fin N β†’ A) :
    (rows code T).flatten = encodeDataStack (framedCode code) (tableEntries T) := by
  simp only [rows, encodeDataStack_eq_flatMap, tableEntries, List.flatMap_def, List.map_ofFn]
  rfl

theorem gathered_eq_transpose {A : Type*} (code : BlockCode A) {N : β„•}
    (T : Fin N β†’ Fin N β†’ A) :
    GatherProgram.gathered (rows code T) (readPlan N) =
      encodeDataStack (framedCode code) (tableEntries fun col row => T row col) := by
  simp only [GatherProgram.gathered, readPlan, encodeDataStack_eq_flatMap,
    tableEntries, List.flatMap_def, List.map_ofFn]
  apply congrArg List.flatten
  apply congrArg List.ofFn
  funext i
  change (rows code T).getD (transposeIndex i).val [] =
    (framedCode code).block (flattenTable (fun col row => T row col) i)
  rw [List.getD_eq_getElem _ _ (by simp)]
  simp only [rows, List.getElem_ofFn]
  change (framedCode code).block (flattenTable T (transposeIndex i)) = _
  rw [transposeIndex, flattenTable_product]
  rfl

theorem pairEntries_splitPair {A : Type*} {N : β„•} (d : Fin (N + 1))
    (T : Fin N β†’ Fin N β†’ A) :
    BlockBasisProgram.pairEntries (splitPair d T) =
      tableEntries (fun col row => T row col) := by
  have hn : d.val + (N - d.val) = N := by have := d.isLt; omega
  rw [tableEntries_eq_flatten_rows]
  unfold tableRows
  rw [List.ofFn_congr hn.symm, List.ofFn_add, List.flatten_append]
  rfl

theorem gathered_eq_pairBits {A : Type*} (code : BlockCode A) {N : β„•}
    (d : Fin (N + 1)) (T : Fin N β†’ Fin N β†’ A) :
    GatherProgram.gathered (rows code T) (readPlan N) =
      BlockBasisProgram.pairBits (framedCode code) (splitPair d T) := by
  rw [BlockBasisProgram.pairBits, pairEntries_splitPair]
  exact gathered_eq_transpose code T

abbrev program := GatherProgram.program

def widthBits {A : Type*} (code : BlockCode A) : List Bool :=
  List.replicate (framedCode code).width true

def inputState {A : Type*} (code : BlockCode A) {N : β„•} (T : Fin N β†’ Fin N β†’ A) :=
  GatherProgram.inputState (rows code T) (widthBits code) (readPlan N)

def outputState {A : Type*} (code : BlockCode A) {N : β„•} (T : Fin N β†’ Fin N β†’ A) :=
  GatherProgram.outputState (rows code T) (widthBits code) (readPlan N)

theorem output_pairBits {A : Type*} (code : BlockCode A) {N : β„•}
    (d : Fin (N + 1)) (T : Fin N β†’ Fin N β†’ A) :
    (outputState code T).stacks 1 =
      BlockBasisProgram.pairBits (framedCode code) (splitPair d T) := by
  rw [outputState, GatherProgram.outputState_output]
  exact gathered_eq_pairBits code d T

def bound (N W : β„•) : β„• :=
  7 + (N * N) * ((N * N) * (14 * (2 * W + 1) + 16) + 21 * (2 * W + 1) + 27)

theorem program_run {A : Type*} (code : BlockCode A) {N : β„•}
    (T : Fin N β†’ Fin N β†’ A) :
    BitProgram.RunsIn program (bound N code.width)
      ((inputState code T).withPC (some program.entry))
      ((outputState code T).withPC none) := by
  have hw : βˆ€ row ∈ rows code T, row.length = (widthBits code).length := by
    simpa only [widthBits, List.length_replicate] using rows_width code T
  have hp : βˆ€ i ∈ readPlan N, i < (rows code T).length := by
    simpa only [rows_length] using readPlan_valid N
  obtain ⟨n, hn, ht⟩ := GatherProgram.program_run _ _ _ hw hp
  refine ⟨n, hn.trans ?_, ht⟩
  have h := GatherProgram.traceCost_le (rows code T) (widthBits code).length (readPlan N) hw hp
  simpa only [bound, rows_length, readPlan_length, widthBits, List.length_replicate, framedCode] using h

theorem primitive_run {A : Type*} (code : BlockCode A) {N : β„•}
    (T : Fin N β†’ Fin N β†’ A) :
    Computation.RunsIn (BitProgram.primitive program).step
      (BitProgram.primitiveOverhead program * bound N code.width)
      (BitProgram.primitiveCfg program ((inputState code T).withPC (some program.entry)))
      (BitProgram.primitiveCfg program ((outputState code T).withPC none)) :=
  BitProgram.primitive_execution program (program_run code T)

theorem readPlan_encoded_length (N : β„•) :
    (ProgramEncoding.encodeRecords (GatherProgram.indexRecords (readPlan N))).length ≀
      2 * (N * N) * (N * N) + 2 * (N * N) + 1 := by
  rw [GatherProgram.encoded_plan_length, readPlan_length]
  have hs := GatherProgram.plan_sum_le (readPlan N) (N * N) (readPlan_valid N)
  rw [readPlan_length] at hs
  nlinarith

theorem words_eq_flatMap (ws : List (List Bool)) :
    MatrixProgramSupport.words ws = ws.flatMap Encoding.frame := by
  induction ws with
  | nil => rfl
  | cons w ws ih => simp [MatrixProgramSupport.words, ih]

/-- The actual table bytes retained by the encoded basis search are exactly
the physical source array of this gather call. -/
theorem input_tableBits (p : β„•) [Fact p.Prime] {N : β„•}
    (T : Fin N β†’ Fin N β†’ ZMod p) :
    (inputState (MatrixWordEncoding.code p) T).stacks 0 =
      BlockCoefficientProgram.tableBits p T := by
  rw [inputState, GatherProgram.inputState_array, rows_flatten,
    encodeDataStack_eq_flatMap, tableEntries_eq_flatten_rows]
  simp [BlockCoefficientProgram.tableBits, BlockCoefficientProgram.matrixBits,
    BlockCoefficientProgram.matrixRows, BlockCoefficientProgram.vectorWords,
    tableRows, framedCode, MatrixWordEncoding.code, words_eq_flatMap,
    List.flatMap_def, List.map_flatten, List.map_ofFn, Function.comp_def]

end SubexponentialSAT.Subexp.BasisTransposeProgram

end OAI.Algorithms