File size: 19,095 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.Counters
import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.StreamResources

namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.Enumerators

open BitProgram StructuredCalls ExecCNFTupleEnumeration

/-- The complete current tuple, including record delimiters, stays polynomial.
This is a bound on one word of data, not on the number of enumerated tuples. -/
theorem encoded_shape_length {words : List (List Bool)} {shape : List β„•}
    (hshape : words.map List.length = shape) :
    (Encoding.encodeList id words).length = 2 * shape.sum + 2 * shape.length + 1 := by
  have hn := congrArg List.length hshape
  have hb := congrArg List.sum hshape
  simp only [List.length_map] at hn
  rw [ExecCNFTupleOdometer.encoded_length, hn]
  simpa only [List.length_flatten] using congrArg (fun n => 2 * n + 2 * shape.length + 1) hb

/-- A local increment has polynomial workspace independently of how many
other increments have preceded it. Every caller stack is included. -/
theorem advance_stack_bound {R S : β„•} (p : CounterForPrograms.Ports R S)
    (words : List (List Bool)) (d : StructuredCalls.State R S)
    (hc : d.stacks p.counter = Encoding.encodeList id words)
    (hs : d.stacks p.scratch = []) (boundaryBound : β„•)
    (hb : βˆ€ i, (d.stacks i).length ≀ boundaryBound) :
    βˆƒ n, n ≀ 7 * (Encoding.encodeList id words).length + 4 ∧
      Eval (advanceCmd p) n d (advanceResult p d words) ∧
      βˆ€ prefixSteps intermediate, prefixSteps ≀ n β†’
        Trace (compiled (advanceCmd p) p.counter p.counter) prefixSteps
          (d.withPC (some (compiled (advanceCmd p) p.counter p.counter).entry)) intermediate β†’
        βˆ€ i, (intermediate.stacks i).length ≀
          S * boundaryBound + 7 * (Encoding.encodeList id words).length + 4 := by
  obtain ⟨n, hn, he⟩ := advance_eval p words d hc hs
  refine ⟨n, hn, he, ?_⟩
  intro prefixSteps intermediate hle htrace i
  have hb' : dataBits
      (d.withPC (some (compiled (advanceCmd p) p.counter p.counter).entry)) ≀
      S * boundaryBound := StreamResources.dataBits_le_mul _ hb
  have hp := StructuredSpace.local_stack_bound
    (compiled (advanceCmd p) p.counter p.counter)
    (steps := 7 * (Encoding.encodeList id words).length + 4)
    hb' prefixSteps intermediate (hle.trans hn) htrace i
  simpa only [Nat.add_assoc] using hp

/-- Every actual callback and controller prefix has one common storage bound.
The number of visits does not appear in the bound. -/
theorem visits_stack_bound {R S : β„•} (p : CounterForPrograms.Ports R S)
    (body : StructuredCalls.Cmd R S) (shape : List β„•)
    (I J : StructuredCalls.State R S β†’ Prop) (boundaryBound callbackBound : β„•)
    (hinitial : βˆ€ a, I a β†’ βˆ€ i, (a.stacks i).length ≀ boundaryBound)
    (hmiddle : βˆ€ a, J a β†’ βˆ€ i, (a.stacks i).length ≀ boundaryBound)
    (hcallback : βˆ€ words a b nb, words.map List.length = shape β†’
      a.stacks p.counter = Encoding.encodeList id words β†’ Eval body nb a b β†’ I a β†’
      a.regs (p.register 2) = true β†’ J b ∧ b.stacks p.scratch = [] ∧
      βˆ€ prefixSteps intermediate, prefixSteps ≀ nb β†’
        Trace (compiled body p.counter p.counter) prefixSteps
          (a.withPC (some (compiled body p.counter p.counter).entry)) intermediate β†’
        βˆ€ i, (intermediate.stacks i).length ≀ callbackBound)
    (hnext : βˆ€ words a, words.map List.length = shape β†’
      a.stacks p.counter = Encoding.encodeList id words β†’ J a β†’
      I (advanceResult p a words))
    {ranks : List β„•} {steps : β„•} {a b : StructuredCalls.State R S}
    (hvis : Visits p body shape ranks steps a b) (hI : I a) :
    βˆ€ prefixSteps intermediate, prefixSteps ≀ steps β†’
      Trace (compiled (loopCmd p body) p.counter p.counter) prefixSteps
        (a.withPC (some (compiled (loopCmd p body) p.counter p.counter).entry)) intermediate β†’
      βˆ€ i, (intermediate.stacks i).length ≀
        max callbackBound (S * boundaryBound + 7 * (2 * shape.sum + 2 * shape.length + 1) + 4) := by
  let bound := max callbackBound
    (S * boundaryBound + 7 * (2 * shape.sum + 2 * shape.length + 1) + 4)
  have hboundary : boundaryBound ≀ bound := by
    have hS : 1 ≀ S := by have := p.counter.isLt; omega
    have hmul : boundaryBound ≀ S * boundaryBound := by
      simpa using Nat.mul_le_mul_right boundaryBound hS
    exact hmul.trans ((Nat.le_add_right _ _).trans
      ((Nat.le_add_right _ _).trans (Nat.le_max_right _ _)))
  induction hvis with
  | done a hf =>
      exact StructuredSpace.while_false_stack_bound _ _ a hf
        (fun i => (hinitial a hI i).trans hboundary)
        _ _ _ (compiled_site _ p.counter p.counter)
  | @step words a middle b nb na nt ranks hf hw hc hb he ha tail ih =>
      obtain ⟨hmid, hscratch, hbody⟩ := hcallback words a middle nb hw hc hb hI hf
      have hafter := hnext words middle hw he hmid
      obtain ⟨n, _, hn, hlocal⟩ := advance_stack_bound p words middle he hscratch
        boundaryBound (hmiddle middle hmid)
      have htime : na = n := StreamResources.eval_steps_eq p.counter p.counter ha hn
      have hcontrol : βˆ€ prefixSteps intermediate, prefixSteps ≀ na β†’
          Trace (compiled (advanceCmd p) p.counter p.counter) prefixSteps
            (middle.withPC (some (compiled (advanceCmd p) p.counter p.counter).entry))
            intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀ bound := by
        intro prefixSteps intermediate hle htrace i
        rw [htime] at hle
        have h := hlocal prefixSteps intermediate hle htrace i
        rw [encoded_shape_length hw] at h
        exact h.trans (Nat.le_max_right _ _)
      have hcallbackBound : βˆ€ prefixSteps intermediate, prefixSteps ≀ nb β†’
          Trace (compiled body p.counter p.counter) prefixSteps
            (a.withPC (some (compiled body p.counter p.counter).entry)) intermediate β†’
          βˆ€ i, (intermediate.stacks i).length ≀ bound := by
        intro prefixSteps intermediate hle htrace i
        exact (hbody prefixSteps intermediate hle htrace i).trans (Nat.le_max_left _ _)
      have hsequence := StructuredSpace.sequence_stack_bound hb ha p.counter p.counter
        hcallbackBound hcontrol
        (compiled (.seq body (advanceCmd p)) p.counter p.counter)
        (siteAddress (.seq body (advanceCmd p))) (exitAddress (.seq body (advanceCmd p)))
        (compiled_site _ p.counter p.counter)
      exact StructuredSpace.while_true_stack_bound hf (.seq hb ha) tail.eval
        p.counter p.counter hsequence (ih hafter)
        _ _ _ (compiled_site _ p.counter p.counter)

/-- The iterator initialization and final halt preserve the loop's common
bound, so the theorem covers every prefix of the complete compiled program. -/
theorem enumerate_stack_bound {R S : β„•} (p : CounterForPrograms.Ports R S)
    (body : StructuredCalls.Cmd R S) {shape ranks : List β„•} {steps bound : β„•}
    {a b : StructuredCalls.State R S}
    (hvis : Visits p body shape ranks steps (startResult p a) b)
    (hloop : βˆ€ prefixSteps intermediate, prefixSteps ≀ steps β†’
      Trace (compiled (loopCmd p body) p.counter p.counter) prefixSteps
        ((startResult p a).withPC
          (some (compiled (loopCmd p body) p.counter p.counter).entry)) intermediate β†’
      βˆ€ i, (intermediate.stacks i).length ≀ bound) :
    βˆ€ prefixSteps intermediate, prefixSteps ≀ 1 + steps + 1 β†’
      Trace (program p body) prefixSteps
        (a.withPC (some (program p body).entry)) intermediate β†’
      βˆ€ i, (intermediate.stacks i).length ≀ bound := by
  have hstart : Eval (startCmd p) 1 a (startResult p a) := .base (.atom _ _)
  have hinit : βˆ€ i, (a.stacks i).length ≀ bound :=
    hloop 0 _ (Nat.zero_le _) (.refl _)
  have hprefix : βˆ€ prefixSteps intermediate, prefixSteps ≀ 1 β†’
      Trace (compiled (startCmd p) p.counter p.counter) prefixSteps
        (a.withPC (some (compiled (startCmd p) p.counter p.counter).entry)) intermediate β†’
      βˆ€ i, (intermediate.stacks i).length ≀ bound := by
    intro prefixSteps intermediate hle htrace i
    have ht : prefixSteps = 0 ∨ prefixSteps = 1 := by omega
    rcases ht with rfl | rfl
    Β· cases htrace
      exact hinit i
    Β· have heq := trace_same_length_unique htrace (compiled_trace hstart p.counter p.counter)
      rw [heq]
      exact hinit i
  have hseq := StructuredSpace.sequence_stack_bound hstart hvis.eval p.counter p.counter
    hprefix hloop (program p body) (siteAddress (enumerate p body))
    (exitAddress (enumerate p body)) (compiled_site _ p.counter p.counter)
  exact StructuredSpace.compiled_halt_stack_bound (.seq hstart hvis.eval)
    p.counter p.counter hseq

/-- The binary countdown controller also retains a uniform stack bound for
arbitrarily many callbacks. Counter width is measured, never its numeric value. -/
theorem countdown_visits_stack_bound {R S : β„•} (p : CounterForPrograms.Ports R S)
    (body : StructuredCalls.Cmd R S) (I J : StructuredCalls.State R S β†’ Prop)
    (boundaryBound callbackBound : β„•)
    (hinitial : βˆ€ a, I a β†’ βˆ€ i, (a.stacks i).length ≀ boundaryBound)
    (hmiddle : βˆ€ a, J a β†’ βˆ€ i, (a.stacks i).length ≀ boundaryBound)
    (hcallback : βˆ€ a b nb, Eval body nb a b β†’ I a β†’
      a.regs (p.register 2) = true β†’ 0 < CounterPrograms.value (a.stacks p.counter) β†’
      J b ∧ βˆ€ prefixSteps intermediate, prefixSteps ≀ nb β†’
        Trace (compiled body p.counter p.counter) prefixSteps
          (a.withPC (some (compiled body p.counter p.counter).entry)) intermediate β†’
        βˆ€ i, (intermediate.stacks i).length ≀ callbackBound)
    (hnext : βˆ€ a, J a β†’ 0 < CounterPrograms.value (a.stacks p.counter) β†’
      I (CounterForPrograms.nextState p a))
    {costs : List β„•} {steps : β„•} {a b : StructuredCalls.State R S}
    (hvis : CounterForPrograms.Visits p body costs steps a b) (hI : I a) :
    βˆ€ prefixSteps intermediate, prefixSteps ≀ steps β†’
      Trace (compiled (CounterForPrograms.loopCmd p body) p.counter p.counter) prefixSteps
        (a.withPC
          (some (compiled (CounterForPrograms.loopCmd p body) p.counter p.counter).entry))
        intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀
          max callbackBound (S * boundaryBound + 16 * boundaryBound + 16) := by
  let bound := max callbackBound (S * boundaryBound + 16 * boundaryBound + 16)
  have hboundary : boundaryBound ≀ bound := by
    have hS : 1 ≀ S := by have := p.counter.isLt; omega
    have hmul : boundaryBound ≀ S * boundaryBound := by
      simpa using Nat.mul_le_mul_right boundaryBound hS
    exact hmul.trans ((Nat.le_add_right _ _).trans
      ((Nat.le_add_right _ _).trans (Nat.le_max_right _ _)))
  induction hvis with
  | done a hf hz =>
      exact StructuredSpace.while_false_stack_bound _ _ a hf
        (fun i => (hinitial a hI i).trans hboundary)
        _ _ _ (compiled_site _ p.counter p.counter)
  | @step a middle b nb nt costs hf hp hb hc hs tail ih =>
      obtain ⟨hmid, hbody⟩ := hcallback a middle nb hb hI hf hp
      have hpositive : 0 < CounterPrograms.value (middle.stacks p.counter) := by
        simpa only [hc] using hp
      have hafter := hnext middle hmid hpositive
      have ha := CounterForPrograms.advance_eval p middle hs
      have hcost := CounterForPrograms.advanceCost_le p middle hpositive
      have hwidth := hmiddle middle hmid p.counter
      have hcontrol : βˆ€ prefixSteps intermediate,
          prefixSteps ≀ CounterForPrograms.advanceCost p middle β†’
          Trace (compiled (CounterForPrograms.advanceCmd p) p.counter p.counter) prefixSteps
            (middle.withPC
              (some (compiled (CounterForPrograms.advanceCmd p) p.counter p.counter).entry))
            intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀ bound := by
        intro prefixSteps intermediate hle htrace i
        have hb' : dataBits (middle.withPC
            (some (compiled (CounterForPrograms.advanceCmd p) p.counter p.counter).entry)) ≀
            S * boundaryBound := StreamResources.dataBits_le_mul _ (hmiddle middle hmid)
        have h := StructuredSpace.local_stack_bound
          (compiled (CounterForPrograms.advanceCmd p) p.counter p.counter)
          (steps := 16 * boundaryBound + 16) hb' prefixSteps intermediate
          (by omega) htrace i
        exact h.trans (by
          simpa only [bound, Nat.add_assoc] using
            (Nat.le_max_right callbackBound (S * boundaryBound + 16 * boundaryBound + 16)))
      have hcallbackBound : βˆ€ prefixSteps intermediate, prefixSteps ≀ nb β†’
          Trace (compiled body p.counter p.counter) prefixSteps
            (a.withPC (some (compiled body p.counter p.counter).entry)) intermediate β†’
          βˆ€ i, (intermediate.stacks i).length ≀ bound := by
        intro prefixSteps intermediate hle htrace i
        exact (hbody prefixSteps intermediate hle htrace i).trans (Nat.le_max_left _ _)
      have hsequence := StructuredSpace.sequence_stack_bound hb ha p.counter p.counter
        hcallbackBound hcontrol
        (compiled (.seq body (CounterForPrograms.advanceCmd p)) p.counter p.counter)
        (siteAddress (.seq body (CounterForPrograms.advanceCmd p)))
        (exitAddress (.seq body (CounterForPrograms.advanceCmd p)))
        (compiled_site _ p.counter p.counter)
      exact StructuredSpace.while_true_stack_bound hf (.seq hb ha) tail.eval
        p.counter p.counter hsequence (ih hafter)
        _ _ _ (compiled_site _ p.counter p.counter)

/-- The initial zero test and final halt are included in the counted loop's
prefix bound. The test scans only the current bounded-length counter word. -/
theorem countdown_for_stack_bound {R S : β„•} (p : CounterForPrograms.Ports R S)
    (body : StructuredCalls.Cmd R S) {costs : List β„•} {steps loopBound boundaryBound : β„•}
    {a b : StructuredCalls.State R S} (hs : a.stacks p.scratch = [])
    (hinitial : βˆ€ i, (a.stacks i).length ≀ boundaryBound)
    (hvis : CounterForPrograms.Visits p body costs steps (CounterForPrograms.testResult p a) b)
    (hloop : βˆ€ prefixSteps intermediate, prefixSteps ≀ steps β†’
      Trace (compiled (CounterForPrograms.loopCmd p body) p.counter p.counter) prefixSteps
        ((CounterForPrograms.testResult p a).withPC
          (some (compiled (CounterForPrograms.loopCmd p body) p.counter p.counter).entry))
        intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀ loopBound) :
    βˆ€ prefixSteps intermediate,
      prefixSteps ≀ 8 * (a.stacks p.counter).length + 7 + steps + 1 β†’
      Trace (CounterForPrograms.program p body p.counter p.counter) prefixSteps
        (a.withPC (some (CounterForPrograms.program p body p.counter p.counter).entry))
        intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀
          max loopBound (S * boundaryBound + 8 * boundaryBound + 7) := by
  let bound := max loopBound (S * boundaryBound + 8 * boundaryBound + 7)
  have htest := CounterForPrograms.test_eval p a hs
  have hprefix : βˆ€ prefixSteps intermediate,
      prefixSteps ≀ 8 * (a.stacks p.counter).length + 7 β†’
      Trace (compiled (CounterForPrograms.testCmd p) p.counter p.counter) prefixSteps
        (a.withPC (some (compiled (CounterForPrograms.testCmd p) p.counter p.counter).entry))
        intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀ bound := by
    intro prefixSteps intermediate hle htrace i
    have hb : dataBits (a.withPC
        (some (compiled (CounterForPrograms.testCmd p) p.counter p.counter).entry)) ≀
        S * boundaryBound := StreamResources.dataBits_le_mul _ hinitial
    have hc := hinitial p.counter
    have h := StructuredSpace.local_stack_bound
      (compiled (CounterForPrograms.testCmd p) p.counter p.counter)
      (steps := 8 * boundaryBound + 7) hb prefixSteps intermediate (by omega) htrace i
    exact h.trans (by
      simpa only [bound, Nat.add_assoc] using
        (Nat.le_max_right loopBound (S * boundaryBound + 8 * boundaryBound + 7)))
  have hloop' : βˆ€ prefixSteps intermediate, prefixSteps ≀ steps β†’
      Trace (compiled (CounterForPrograms.loopCmd p body) p.counter p.counter) prefixSteps
        ((CounterForPrograms.testResult p a).withPC
          (some (compiled (CounterForPrograms.loopCmd p body) p.counter p.counter).entry))
        intermediate β†’ βˆ€ i, (intermediate.stacks i).length ≀ bound := by
    intro prefixSteps intermediate hle htrace i
    exact (hloop prefixSteps intermediate hle htrace i).trans (Nat.le_max_left _ _)
  have hseq := StructuredSpace.sequence_stack_bound htest hvis.eval p.counter p.counter
    hprefix hloop' (CounterForPrograms.program p body p.counter p.counter)
    (siteAddress (CounterForPrograms.forCmd p body))
    (exitAddress (CounterForPrograms.forCmd p body)) (compiled_site _ p.counter p.counter)
  exact StructuredSpace.compiled_halt_stack_bound (.seq htest hvis.eval)
    p.counter p.counter hseq


/-- A completed actual traversal resets every payload bit while retaining all
record widths. This lets successive scans reuse the same counter frame. -/
theorem visits_reset_counter {R S : β„•} {p : CounterForPrograms.Ports R S}
    {body : StructuredCalls.Cmd R S} {shape ranks : List β„•} {steps : β„•}
    {a b : StructuredCalls.State R S} (h : Visits p body shape ranks steps a b)
    (hstart : a.regs (p.register 2) = true) :
    b.stacks p.counter = Encoding.encodeList id
      (shape.map (fun width => List.replicate width false)) := by
  have hflat : (shape.map (fun width => List.replicate width false)).flatten =
      List.replicate shape.sum false := by
    clear h hstart
    induction shape with
    | nil => rfl
    | cons width shape ih => simp [ih]
  have haux : (a.regs (p.register 2) = false β†’ a.stacks p.counter =
      Encoding.encodeList id (shape.map (fun width => List.replicate width false))) β†’
      b.stacks p.counter = Encoding.encodeList id
        (shape.map (fun width => List.replicate width false)) := by
    clear hstart
    induction h with
    | done a hf => exact fun hz => hz hf
    | @step words a middle b nb na nt ranks hf hw hc hb he ha tail ih =>
        intro _
        apply ih
        intro hfalse
        have hcarry : (ExecCNFTupleOdometer.bumpWords words).1 = true := by
          simpa only [advanceResult_flag, Bool.not_eq_false_eq_eq_true] using hfalse
        have hv := ExecCNFTupleOdometer.bumpWords_value words
        simp only [hcarry, ite_true] at hv
        have hlt := ExecCNFTupleOdometer.value_lt_power_length words.flatten
        have hz : CounterPrograms.value
            (ExecCNFTupleOdometer.bumpWords words).2.flatten = 0 := by omega
        rw [advanceResult_counter]
        apply congrArg (Encoding.encodeList id)
        apply tuple_eq_of_shape_value
        Β· simpa only [ExecCNFTupleOdometer.bumpWords_lengths, List.map_map,
            Function.comp_def, List.length_replicate, List.map_id'] using hw
        Β· simpa only [hflat, value_replicate_false] using hz
  apply haux
  intro hfalse
  simp only [hstart, Bool.true_eq_false] at hfalse

end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.Enumerators