File size: 12,260 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
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
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.SubexpScanPrimitives
import OAI.Algorithms.SubexponentialSAT.SubexpQuarticRing

namespace OAI.Algorithms

/-! A fixed streaming array loop, parameterized by a finite word program
and an actual trace theorem for one word. The concrete modular-word
instantiation is supplied in DensePolynomialPrograms. -/

namespace SubexponentialSAT.DensePolynomialLoop

open BitProgram StructuredCalls SubexpScanPrimitives
open SubexpLiftRing SubexpQuarticRing

abbrev Data := State 8 20

def stackArray (x y modulus left right width counter tmp packed : List Bool)
    (frame : Fin 6 β†’ List Bool) : Fin 20 β†’ List Bool :=
  fun i => Fin.addCases (m := 14) (n := 6) (motive := fun _ => List Bool)
    (![x,y,[],[],[],modulus,[],[],left,right,width,counter,tmp,packed] : Fin 14 β†’ List Bool) frame i

def state (regs : Fin 8 β†’ Bool)
    (x y modulus left right width counter tmp packed : List Bool)
    (frame : Fin 6 β†’ List Bool) : Data :=
  ⟨regs, stackArray x y modulus left right width counter tmp packed frame⟩

def ready (regs : Fin 8 β†’ Bool) (modulus left right width packed : List Bool)
    (frame : Fin 6 β†’ List Bool) : Data :=
  state regs [] [] modulus left right width [] [] packed frame

abbrev guard : Cmd 8 20 :=
  .seq (.base (.atom (.peek 8 0 1)))
    (.seq (.base (.atom (.peek 9 2 3))) (.base (.atom (.band 4 1 3))))

def guardRegs (regs : Fin 8 β†’ Bool) (left right : List Bool) : Fin 8 β†’ Bool :=
  let r := readRegs (readRegs regs 0 1 left.head?) 2 3 right.head?
  Function.update r 4 (r 1 && r 3)

@[simp] theorem guardRegs_test (regs : Fin 8 β†’ Bool) (left right : List Bool) :
    guardRegs regs left right 4 = (left.head?.isSome && right.head?.isSome) := by
  simp [guardRegs, readRegs]

theorem guard_eval (regs : Fin 8 β†’ Bool) (modulus left right width packed : List Bool)
    (frame : Fin 6 β†’ List Bool) :
    Eval guard 3 (ready regs modulus left right width packed frame)
      (ready (guardRegs regs left right) modulus left right width packed frame) := by
  let d := ready regs modulus left right width packed frame
  have h₁ := Eval.base (StructuredProgram.Eval.atom (r := 8) (s := 20) (.peek 8 0 1) d)
  have hβ‚‚ := Eval.base (StructuredProgram.Eval.atom (r := 8) (s := 20) (.peek 9 2 3)
    ((StructuredProgram.Atom.peek 8 0 1).eval d))
  have h₃ := Eval.base (StructuredProgram.Eval.atom (r := 8) (s := 20) (.band 4 1 3)
    ((StructuredProgram.Atom.peek 9 2 3).eval ((StructuredProgram.Atom.peek 8 0 1).eval d)))
  exact Eval.seq h₁ (Eval.seq hβ‚‚ h₃)

abbrev addLoop (step : Cmd 8 20) : Cmd 8 20 := .while 4 (.seq step guard)

abbrev flush : Cmd 8 20 :=
  .seq (.call (StackRoutines.transferProgram 8 13 (0 : Fin 8) 1))
    (.seq (.call (StackRoutines.transferProgram 9 13 (0 : Fin 8) 1))
      (.call (StackRoutines.transferProgram 13 8 (0 : Fin 8) 1)))

abbrev addCmd (step : Cmd 8 20) : Cmd 8 20 := .seq guard (.seq (addLoop step) flush)

abbrev program (step : Cmd 8 20) : Program 8 20 (StructuredCalls.size (addCmd step) + 1) :=
  StructuredCalls.compiled (addCmd step) 8 8

theorem flush_eval (regs : Fin 8 β†’ Bool) (modulus left right width packed : List Bool)
    (frame : Fin 6 β†’ List Bool) :
    Eval flush (6 * left.length + 6 * right.length + 3 * packed.length + 9)
      (ready regs modulus left right width packed frame)
      (ready (readRegs regs 0 1 none) modulus (packed.reverse ++ left ++ right) [] width [] frame) := by
  let dβ‚€ := ready regs modulus left right width packed frame
  let d₁ := ready (readRegs regs 0 1 none) modulus [] right width (left.reverse ++ packed) frame
  let dβ‚‚ := ready (readRegs regs 0 1 none) modulus [] [] width
    (right.reverse ++ left.reverse ++ packed) frame
  have h₁ := transfer_eval dβ‚€ 8 13 0 1 (by decide) (by decide)
  have e₁ : afterTransfer dβ‚€ 8 13 0 1 = d₁ := by
    apply state_ext
    Β· rfl
    Β· funext i; fin_cases i <;>
        simp [afterTransfer, dβ‚€, d₁, ready, state, stackArray, Fin.addCases, Fin.coe_ofNat_eq_mod,
          StackRoutines.transferStacks]
  have hβ‚‚ := transfer_eval d₁ 9 13 0 1 (by decide) (by decide)
  have eβ‚‚ : afterTransfer d₁ 9 13 0 1 = dβ‚‚ := by
    apply state_ext
    Β· simp [afterTransfer, d₁, dβ‚‚, ready, state, StackRoutines.readRegs_reset]
    Β· funext i; fin_cases i <;>
        simp [afterTransfer, d₁, dβ‚‚, ready, state, stackArray, Fin.addCases, Fin.coe_ofNat_eq_mod,
          StackRoutines.transferStacks,
          List.append_assoc]
  have h₃ := transfer_eval dβ‚‚ 13 8 0 1 (by decide) (by decide)
  have e₃ : afterTransfer dβ‚‚ 13 8 0 1 =
      ready (readRegs regs 0 1 none) modulus (packed.reverse ++ left ++ right) [] width [] frame := by
    apply state_ext
    Β· simp [afterTransfer, dβ‚‚, ready, state, StackRoutines.readRegs_reset]
    Β· funext i; fin_cases i <;>
        simp [afterTransfer, dβ‚‚, ready, state, stackArray, Fin.addCases, Fin.coe_ofNat_eq_mod,
          StackRoutines.transferStacks,
          List.reverse_append, List.append_assoc]
  rw [e₁] at h₁
  rw [eβ‚‚] at hβ‚‚
  rw [e₃] at h₃
  have h := Eval.seq h₁ (Eval.seq hβ‚‚ h₃)
  convert h using 1
  all_goals simp [ dβ‚€, d₁, dβ‚‚, ready, state, stackArray, Fin.addCases,
    Fin.coe_ofNat_eq_mod, List.length_append]
  omega

def flatten {B : β„•} (xs : List (Word B)) : List Bool := (xs.map Subtype.val).flatten

@[simp] theorem flatten_length {B : β„•} (xs : List (Word B)) :
    (flatten xs).length = xs.length * B := by
  induction xs with
  | nil => simp [flatten]
  | cons x xs ih =>
      change (x.val ++ flatten xs).length = (xs.length + 1) * B
      rw [List.length_append, x.property, ih]
      ring

theorem flatten_cons_ne_nil {B : β„•} (hB : 0 < B) (x : Word B) (xs : List (Word B)) :
    flatten (x :: xs) β‰  [] := by
  intro h
  have hh := congrArg List.length h
  rw [flatten_length] at hh
  simp only [List.length_cons, List.length_nil] at hh
  nlinarith

theorem head_isSome_of_ne_nil {Ξ± : Type*} {xs : List Ξ±} (h : xs β‰  []) : xs.head?.isSome = true := by
  cases xs with
  | nil => exact (h rfl).elim
  | cons a xs => rfl

structure WordStep (B : β„•) where
  cmd : Cmd 8 20
  modulus : List Bool
  add : Word B β†’ Word B β†’ Word B
  cost : β„•
  run : βˆ€ (regs : Fin 8 β†’ Bool) (x y : Word B)
    (left right width packed : List Bool) (frame : Fin 6 β†’ List Bool),
    width.length = B β†’ βˆƒ n ≀ cost, βˆƒ regs',
      Eval cmd n (ready regs modulus (x.val ++ left) (y.val ++ right) width packed frame)
        (ready regs' modulus left right width ((add x y).val.reverse ++ packed) frame)

variable {B : β„•} (step : WordStep B)

def addWords : List (Word B) β†’ List (Word B) β†’ List (Word B)
  | x :: xs, y :: ys => step.add x y :: addWords xs ys
  | _, _ => []

theorem addWords_length (xs ys : List (Word (B))) :
    (addWords step xs ys).length = min xs.length ys.length := by
  induction xs generalizing ys with
  | nil => simp [addWords]
  | cons x xs ih => cases ys <;> simp [addWords, ih]

theorem addLoop_eval (hB : 0 < B) (regs : Fin 8 β†’ Bool)
    (xs ys : List (Word (B)))
    (width packed : List Bool) (frame : Fin 6 β†’ List Bool)
    (hwidth : width.length = B) :
    βˆƒ n ≀ min xs.length ys.length * (step.cost + 4) + 1, βˆƒ regs',
      Eval (addLoop step.cmd) n
        (ready (guardRegs regs (flatten xs) (flatten ys)) step.modulus
          (flatten xs) (flatten ys) width packed frame)
        (ready regs' step.modulus (flatten (xs.drop ys.length)) (flatten (ys.drop xs.length)) width
          ((flatten (addWords step xs ys)).reverse ++ packed) frame) := by
  induction xs generalizing ys regs packed with
  | nil =>
      refine ⟨1, by simp, guardRegs regs [] (flatten ys), ?_⟩
      simpa [addLoop, flatten, addWords] using
        (Eval.while_false (test := (4 : Fin 8)) (body := .seq step.cmd guard)
          (a := ready (guardRegs regs [] (flatten ys)) step.modulus [] (flatten ys) width packed frame)
          (by simp [ready, state]))
  | cons x xs ih =>
      cases ys with
      | nil =>
          refine ⟨1, by simp, guardRegs regs (flatten (x :: xs)) [], ?_⟩
          simpa [addLoop, flatten, addWords] using
            (Eval.while_false (test := (4 : Fin 8)) (body := .seq step.cmd guard)
              (a := ready (guardRegs regs (flatten (x :: xs)) []) step.modulus
                (flatten (x :: xs)) [] width packed frame) (by simp [ready, state]))
      | cons y ys =>
          let rg := guardRegs regs (flatten (x :: xs)) (flatten (y :: ys))
          have ht : (ready rg step.modulus (flatten (x :: xs)) (flatten (y :: ys)) width packed frame).regs 4 = true := by
            simp only [ready, state, rg, guardRegs_test,
              head_isSome_of_ne_nil (flatten_cons_ne_nil hB x xs),
              head_isSome_of_ne_nil (flatten_cons_ne_nil hB y ys), Bool.and_self]
          obtain ⟨n, hn, r₁, hβ‚βŸ© := step.run rg x y (flatten xs) (flatten ys)
            width packed frame hwidth
          let packed' := (step.add x y).val.reverse ++ packed
          have hg := guard_eval r₁ step.modulus (flatten xs) (flatten ys) width packed' frame
          obtain ⟨m, hm, rβ‚‚, hβ‚‚βŸ© := ih r₁ ys packed'
          refine ⟨n + 3 + m + 1, ?_, rβ‚‚, ?_⟩
          Β· simp only [List.length_cons]
            rw [show min (xs.length + 1) (ys.length + 1) = min xs.length ys.length + 1 by omega]
            calc
              _ ≀ step.cost + 3 +
                  (min xs.length ys.length * (step.cost + 4) + 1) + 1 := by omega
              _ = _ := by ring
          Β· have h := Eval.while_true ht (Eval.seq h₁ hg) hβ‚‚
            simpa [addLoop, flatten, addWords, rg, packed', List.reverse_append,
              List.append_assoc, Nat.add_assoc] using h

def addWordsTotal : List (Word (B)) β†’ List (Word (B)) β†’
    List (Word (B))
  | [], ys => ys
  | xs, [] => xs
  | x :: xs, y :: ys => (step.add x y) :: addWordsTotal xs ys

theorem addWordsTotal_decompose (xs ys : List (Word (B))) :
    addWordsTotal step xs ys = addWords step xs ys ++ xs.drop ys.length ++ ys.drop xs.length := by
  induction xs generalizing ys with
  | nil => simp [addWordsTotal, addWords]
  | cons x xs ih => cases ys <;> simp [addWordsTotal, addWords, ih]

@[simp] theorem flatten_append {B : β„•} (xs ys : List (Word B)) :
    flatten (xs ++ ys) = flatten xs ++ flatten ys := by simp [flatten]

theorem addCmd_eval (hB : 0 < B) (regs : Fin 8 β†’ Bool)
    (xs ys : List (Word (B))) (width : List Bool) (frame : Fin 6 β†’ List Bool)
    (hwidth : width.length = B) :
    βˆƒ n ≀ (xs.length + ys.length + 1) * (step.cost + 15 * B + 20), βˆƒ regs',
      Eval (addCmd step.cmd) n (ready regs step.modulus (flatten xs) (flatten ys) width [] frame)
        (ready regs' step.modulus (flatten (addWordsTotal step xs ys)) [] width [] frame) := by
  have hg := guard_eval regs step.modulus (flatten xs) (flatten ys) width [] frame
  obtain ⟨n, hn, r₁, hl⟩ := addLoop_eval step hB regs xs ys width [] frame hwidth
  simp only [List.append_nil] at hl
  let left := flatten (xs.drop ys.length)
  let right := flatten (ys.drop xs.length)
  let packed := (flatten (addWords step xs ys)).reverse
  have hf := flush_eval r₁ step.modulus left right width packed frame
  have h := Eval.seq hg (Eval.seq hl hf)
  have hleft : left.length ≀ (xs.length + ys.length) * B := by
    simp only [left, flatten_length, List.length_drop]
    exact Nat.mul_le_mul_right _ (by omega)
  have hright : right.length ≀ (xs.length + ys.length) * B := by
    simp only [right, flatten_length, List.length_drop]
    exact Nat.mul_le_mul_right _ (by omega)
  have hpacked : packed.length ≀ (xs.length + ys.length) * B := by
    simp only [packed, List.length_reverse, flatten_length, addWords_length]
    exact Nat.mul_le_mul_right _ (by omega)
  have hloop : n ≀ (xs.length + ys.length) * (step.cost + 4) + 1 :=
    hn.trans (by gcongr; omega)
  refine ⟨3 + (n + (6 * left.length + 6 * right.length + 3 * packed.length + 9)), ?_,
    readRegs r₁ 0 1 none, ?_⟩
  Β· nlinarith
  Β· simpa only [addCmd, List.append_nil, packed, left, right, List.reverse_reverse,
      addWordsTotal_decompose, flatten_append] using h


end SubexponentialSAT.DensePolynomialLoop

end OAI.Algorithms