File size: 13,632 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
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
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.StructuredCalls
import OAI.Algorithms.SubexponentialSAT.ParserProgram

namespace OAI.Algorithms

/-! A fixed bit program testing whether all payload bits in a sequence of
frames are zero. Frame delimiters are ignored; no scalar arithmetic is hidden
inside this program. -/

namespace SubexponentialSAT.Subexp.BlockZeroProgram

open BitProgram

private theorem update_eq {n : ℕ} {A : Type*} (f g : Fin n → A) (i : Fin n) (a : A)
    (hi : g i = a) (ho : ∀ j, j ≠ i → g j = f j) : Function.update f i a = g := by
  funext j
  by_cases h : j = i
  · subst j; simp [hi]
  · simp [h, ho j h]

@[simp] private theorem update3_0 {A : Type*} (a b c x : A) :
    Function.update ![a,b,c] (0 : Fin 3) x = ![x,b,c] := by
  funext i; fin_cases i <;> simp

@[simp] private theorem update3_1 {A : Type*} (a b c x : A) :
    Function.update ![a,b,c] (1 : Fin 3) x = ![a,x,c] := by
  funext i; fin_cases i <;> simp

@[simp] private theorem update3_2 {A : Type*} (a b c x : A) :
    Function.update ![a,b,c] (2 : Fin 3) x = ![a,b,x] := by
  funext i; fin_cases i <;> simp

@[simp] private theorem update2_0 {A : Type*} (a b x : A) :
    Function.update ![a,b] (0 : Fin 2) x = ![x,b] := by
  funext i; fin_cases i <;> simp

@[simp] private theorem update2_1 {A : Type*} (a b x : A) :
    Function.update ![a,b] (1 : Fin 2) x = ![a,x] := by
  funext i; fin_cases i <;> simp

@[simp] private theorem update1 {A : Type*} (a x : A) :
    Function.update (fun _ : Fin 1 => a) 0 x = fun _ => x := by
  funext i; fin_cases i; simp

@[simp] private theorem update5_4 {A : Type*} (a b c d e x : A) :
    Function.update ![a,b,c,d,e] (4 : Fin 5) x = ![a,b,c,d,x] := by
  funext i; fin_cases i <;> simp

abbrev program : Program 3 1 8 where
  inputStack := 0
  outputStack := 0
  entry := 0
  code := ![.set 2 true 1, .pop 0 0 1 2, .branch 1 3 7, .branch 0 4 1,
    .pop 0 0 1 5, .branch 0 6 1, .set 2 false 1, .halt]

def cfg (pc : Option (Fin 8)) (v present zero : Bool) (input : List Bool) : Cfg 3 1 8 :=
  ⟨pc, ![v, present, zero], fun _ => input⟩

def frameCost : List Bool → ℕ
  | [] => 3
  | b :: bits => (if b then 6 else 5) + frameCost bits

def frames (words : List (List Bool)) : List Bool := words.flatMap Encoding.frame

def isZero (words : List (List Bool)) : Bool := words.all (fun bits => bits.all (! ·))

@[simp] theorem isZero_nil : isZero [] = true := rfl

theorem frame_trace (bits rest : List Bool) (v present zero : Bool) :
    Trace program (frameCost bits)
      (cfg (some 1) v present zero (Encoding.frame bits ++ rest))
      (cfg (some 1) false true (zero && bits.all (! ·)) rest) := by
  induction bits generalizing v present zero with
  | nil =>
      refine .cons (b := cfg (some 2) false true zero rest) ?_ ?_
      · simp [step, cfg, execInstr, readRegs, Encoding.frame]
      refine .cons (b := cfg (some 3) false true zero rest) ?_ ?_
      · simp [step, cfg, execInstr]
      refine .cons ?_ (.refl _)
      simp [step, cfg, execInstr]
  | cons b bits ih =>
      cases b with
      | false =>
          have cycle : Trace program 5
              (cfg (some 1) v present zero (Encoding.frame (false :: bits) ++ rest))
              (cfg (some 1) false true zero (Encoding.frame bits ++ rest)) := by
            refine .cons (b := cfg (some 2) true true zero
              (false :: (Encoding.frame bits ++ rest))) ?_ ?_
            · simp [step, cfg, execInstr, readRegs, Encoding.frame]
            refine .cons (b := cfg (some 3) true true zero
              (false :: (Encoding.frame bits ++ rest))) ?_ ?_
            · simp [step, cfg, execInstr]
            refine .cons (b := cfg (some 4) true true zero
              (false :: (Encoding.frame bits ++ rest))) ?_ ?_
            · simp [step, cfg, execInstr]
            refine .cons (b := cfg (some 5) false true zero (Encoding.frame bits ++ rest)) ?_ ?_
            · simp [step, cfg, execInstr, readRegs]
            refine .cons ?_ (.refl _)
            simp [step, cfg, execInstr]
          simpa [frameCost, Nat.add_comm] using cycle.trans (ih false true zero)
      | true =>
          have cycle : Trace program 6
              (cfg (some 1) v present zero (Encoding.frame (true :: bits) ++ rest))
              (cfg (some 1) true true false (Encoding.frame bits ++ rest)) := by
            refine .cons (b := cfg (some 2) true true zero
              (true :: (Encoding.frame bits ++ rest))) ?_ ?_
            · simp [step, cfg, execInstr, readRegs, Encoding.frame]
            refine .cons (b := cfg (some 3) true true zero
              (true :: (Encoding.frame bits ++ rest))) ?_ ?_
            · simp [step, cfg, execInstr]
            refine .cons (b := cfg (some 4) true true zero
              (true :: (Encoding.frame bits ++ rest))) ?_ ?_
            · simp [step, cfg, execInstr]
            refine .cons (b := cfg (some 5) true true zero (Encoding.frame bits ++ rest)) ?_ ?_
            · simp [step, cfg, execInstr, readRegs]
            refine .cons (b := cfg (some 6) true true zero (Encoding.frame bits ++ rest)) ?_ ?_
            · simp [step, cfg, execInstr]
            refine .cons ?_ (.refl _)
            simp [step, cfg, execInstr]
          simpa [frameCost, Nat.add_comm] using cycle.trans (ih true true false)

theorem frames_trace (words : List (List Bool)) (v present zero : Bool) :
    Trace program ((words.map frameCost).sum + 3)
      (cfg (some 1) v present zero (frames words))
      (cfg none false false (zero && isZero words) []) := by
  induction words generalizing v present zero with
  | nil =>
      refine .cons (b := cfg (some 2) false false zero []) ?_ ?_
      · simp [step, cfg, execInstr, readRegs, frames]
      refine .cons (b := cfg (some 7) false false zero []) ?_ ?_
      · simp [step, cfg, execInstr]
      refine .cons ?_ (.refl _)
      simp [step, cfg, execInstr]
  | cons bits words ih =>
      have h := (frame_trace bits (frames words) v present zero).trans
        (ih false true (zero && bits.all (! ·)))
      simpa [frames, isZero, Bool.and_assoc, Nat.add_assoc] using h

theorem program_trace (words : List (List Bool)) (v present zero : Bool) :
    Trace program ((words.map frameCost).sum + 4)
      (cfg (some program.entry) v present zero (frames words))
      (cfg none false false (isZero words) []) := by
  have h := frames_trace words v present true
  have first : Trace program 1
      (cfg (some program.entry) v present zero (frames words))
      (cfg (some 1) v present true (frames words)) := by
    refine .cons ?_ (.refl _)
    simp [step, cfg, execInstr]
  simpa [Nat.add_comm, Nat.add_left_comm, Nat.add_assoc] using first.trans h

theorem frameCost_le (bits : List Bool) : frameCost bits ≤ 3 * (Encoding.frame bits).length := by
  induction bits with
  | nil => simp [frameCost, Encoding.frame_nil]
  | cons b bits ih =>
      have hl := Encoding.length_frame bits
      cases b <;> simp [frameCost, Encoding.frame_cons] <;> omega

theorem cost_le (words : List (List Bool)) :
    (words.map frameCost).sum + 4 ≤ 3 * (frames words).length + 4 := by
  suffices (words.map frameCost).sum ≤ 3 * (frames words).length by omega
  induction words with
  | nil => simp [frames]
  | cons bits words ih =>
      have hb := frameCost_le bits
      simp only [List.map_cons, List.sum_cons, frames, List.flatMap_cons, List.length_append]
      dsimp [frames] at ih
      omega

theorem program_runsIn (words : List (List Bool)) (v present zero : Bool) :
    RunsIn program (3 * (frames words).length + 4)
      (cfg (some program.entry) v present zero (frames words))
      (cfg none false false (isZero words) []) :=
  ⟨_, cost_le words, program_trace words v present zero⟩

theorem isZero_iff (words : List (List Bool)) :
    isZero words = true ↔ ∀ bits ∈ words, ∀ b ∈ bits, b = false := by
  simp [isZero, List.all_eq_true]

/-- Consume a runtime number of complete scalar frames. Stack 0 is the
unary count and stack 1 the framed scalar stream. -/
abbrev skipProgram : Program 3 2 6 where
  inputStack := 1
  outputStack := 1
  entry := 0
  code := ![.pop 0 0 1 1, .branch 1 2 5, .pop 1 0 2 3,
    .branch 0 4 0, .pop 1 0 2 2, .halt]

def skipCfg (pc : Option (Fin 6)) (v countPresent scalarPresent : Bool)
    (count input : List Bool) : Cfg 3 2 6 :=
  ⟨pc, ![v, countPresent, scalarPresent], ![count, input]⟩

theorem skipFrame_trace (bits rest count : List Bool) (v pr sp : Bool) :
    Trace skipProgram (3 * bits.length + 2)
      (skipCfg (some 2) v pr sp count (Encoding.frame bits ++ rest))
      (skipCfg (some 0) false pr true count rest) := by
  induction bits generalizing v sp with
  | nil =>
      refine .cons (b := skipCfg (some 3) false pr true count rest) ?_ ?_
      · simp [step, skipCfg, execInstr, readRegs, Encoding.frame]
      refine .cons ?_ (.refl _)
      simp [step, skipCfg, execInstr]
  | cons bit bits ih =>
      have first : Trace skipProgram 3
          (skipCfg (some 2) v pr sp count (Encoding.frame (bit :: bits) ++ rest))
          (skipCfg (some 2) bit pr true count (Encoding.frame bits ++ rest)) := by
        refine .cons (b := skipCfg (some 3) true pr true count
          (bit :: (Encoding.frame bits ++ rest))) ?_ ?_
        · simp [step, skipCfg, execInstr, readRegs, Encoding.frame]
        refine .cons (b := skipCfg (some 4) true pr true count
          (bit :: (Encoding.frame bits ++ rest))) ?_ ?_
        · simp [step, skipCfg, execInstr]
        refine .cons ?_ (.refl _)
        simp [step, skipCfg, execInstr, readRegs]
      convert first.trans (ih bit true) using 1 ; simp [List.length_cons] ; omega

def skipCost (words : List (List Bool)) : ℕ :=
  3 + (words.map (fun bits => 3 * bits.length + 4)).sum

theorem skipProgram_trace (words : List (List Bool)) (rest count : List Bool)
    (hc : count.length = words.length) (v pr sp : Bool) :
    Trace skipProgram (skipCost words)
      (skipCfg (some skipProgram.entry) v pr sp count (frames words ++ rest))
      (skipCfg none false false (sp || !words.isEmpty) [] rest) := by
  induction words generalizing count v pr sp with
  | nil =>
      have hn : count = [] := List.length_eq_zero_iff.mp (by simpa using hc)
      subst count
      refine .cons (b := skipCfg (some 1) false false sp [] rest) ?_ ?_
      · simp [step, skipCfg, execInstr, readRegs, frames]
      refine .cons (b := skipCfg (some 5) false false sp [] rest) ?_ ?_
      · simp [step, skipCfg, execInstr]
      refine .cons ?_ (.refl _)
      simp [step, skipCfg, execInstr]
  | cons bits words ih =>
      cases count with
      | nil => simp at hc
      | cons bit count =>
          have ht : count.length = words.length := by simpa using hc
          have first : Trace skipProgram 2
              (skipCfg (some skipProgram.entry) v pr sp (bit :: count)
                (frames (bits :: words) ++ rest))
              (skipCfg (some 2) bit true sp count
                (Encoding.frame bits ++ (frames words ++ rest))) := by
            refine .cons (b := skipCfg (some 1) bit true sp count
              (Encoding.frame bits ++ (frames words ++ rest))) ?_ ?_
            · simp [step, skipCfg, execInstr, readRegs, frames, List.append_assoc]
            refine .cons ?_ (.refl _)
            simp [step, skipCfg, execInstr]
          have h := first.trans ((skipFrame_trace bits (frames words ++ rest) count
            bit true sp).trans (ih count ht false true true))
          convert h using 1 <;> simp [skipCost, Bool.or_true, Nat.add_assoc] ; omega

theorem skipCost_le (words : List (List Bool)) :
    skipCost words ≤ 4 * (frames words).length + 3 := by
  induction words with
  | nil => simp [skipCost, frames]
  | cons bits words ih =>
      simp only [skipCost, List.map_cons, List.sum_cons] at *
      simp only [frames, List.flatMap_cons, List.length_append, Encoding.length_frame] at *
      omega

/-- Combine the four verified zero predicates. Inputs are zero(y),
zero(coefficients), zero(tail coefficients), zero(M*y); output is register 4. -/
abbrev classifyProgram : Program 5 1 8 where
  inputStack := 0
  outputStack := 0
  entry := 0
  code := ![.branch 0 1 2, .branch 1 2 5, .branch 3 3 4, .branch 2 6 5,
    .branch 2 5 6, .set 4 false 7, .set 4 true 7, .halt]

def classifyResult (zeroY zeroCoefficients zeroTail zeroImage : Bool) : Bool :=
  ((!zeroY) || zeroCoefficients) && (zeroImage == zeroTail)

def classifySteps (zeroY zeroCoefficients : Bool) : ℕ :=
  if zeroY then (if zeroCoefficients then 6 else 4) else 5

def classifyCfg (pc : Option (Fin 8)) (zy zc zt zm output : Bool) (data : List Bool) :
    Cfg 5 1 8 := ⟨pc, ![zy, zc, zt, zm, output], fun _ => data⟩

theorem classifyProgram_trace (zy zc zt zm output : Bool) (data : List Bool) :
    Trace classifyProgram (classifySteps zy zc)
      (classifyCfg (some classifyProgram.entry) zy zc zt zm output data)
      (classifyCfg none zy zc zt zm (classifyResult zy zc zt zm) data) := by
  apply Computation.Trace.of_iterate
  cases zy <;> cases zc <;> cases zt <;> cases zm <;>
    simp [classifySteps, classifyResult, Function.iterate_succ_apply,      step, classifyProgram, classifyCfg, execInstr]

theorem classifyProgram_runsIn (zy zc zt zm output : Bool) (data : List Bool) :
    RunsIn classifyProgram 6
      (classifyCfg (some classifyProgram.entry) zy zc zt zm output data)
      (classifyCfg none zy zc zt zm (classifyResult zy zc zt zm) data) := by
  refine ⟨classifySteps zy zc, ?_, classifyProgram_trace zy zc zt zm output data⟩
  cases zy <;> cases zc <;> decide

end SubexponentialSAT.Subexp.BlockZeroProgram

end OAI.Algorithms