File size: 17,915 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
319
320
321
322
323
324
325
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockCoefficientFrames

namespace OAI.Algorithms

/-! Actual composition of the coefficient, split, and table scans. -/

namespace SubexponentialSAT.Subexp.BlockSearchNested

open BitProgram StructuredCalls BlockSearchProgram BlockSearchLoop

abbrev Data := StructuredProgram.State 24 42

def clearState (a : Data) (src : Fin 42) : Data :=
  ⟨readRegs a.regs 21 22 none, StackRoutines.clearStacks src a.stacks⟩

theorem clear_eval (a : Data) (src : Fin 42) :
    Eval (clear src) (2 * (a.stacks src).length + 3) a (clearState a src) :=
  TupleProgram.clear_eval src 21 22 a

def finishDimension (a : Data) : Data :=
  (StructuredProgram.Atom.set (12 : Fin 24) true).eval
    ((StructuredProgram.Atom.set (11 : Fin 24) false).eval
      ((StructuredProgram.Atom.copy (13 : Fin 24) 16).eval a))

def coefficientScanCost (cost : List Bool → ℕ) (records : List (List Bool)) : ℕ :=
  9 * (ProgramEncoding.encodeRecords records).length +
    RecordPrograms.budget (fun bits => cost bits + 2) records + 14

def dimensionCost (oldLength : ℕ) (cost : List Bool → ℕ)
    (records : List (List Bool)) (split : List Bool) : ℕ :=
  2 * oldLength + 9 * split.length + coefficientScanCost cost records + 14

/-- One split callback installs its split, runs the actual complete scalar
scan, and restores the enclosing iterator's physical boundary. -/
theorem dimensionCallback_eval (matrixPair : Cmd 24 42) (ctx : Context)
    (table split : List Bool) (test : List Bool → Bool) (cost : List Bool → ℕ)
    (valid : List Bool → Prop)
    (contract : CoefficientContract matrixPair ctx table split test cost valid)
    (records : List (List Bool)) (base : Data)
    (hv : ∀ bits ∈ records, valid bits)
    (hb : CoefficientEntry ctx table (base.stacks 8) base)
    (h4 : base.stacks 4 = split)
    (hs : base.stacks 14 = ProgramEncoding.encodeRecords records)
    (h6 : base.stacks 6 = []) (h7 : base.stacks 7 = []) :
    ∃ n next,
      n ≤ dimensionCost (base.stacks 8).length cost records split ∧
      next.stacks = Function.update (Function.update base.stacks 8 split) 4 [] ∧
      (∀ j : Fin 24, 8 ≤ j.val → j.val ≤ 10 → next.regs j = base.regs j) ∧
      next.regs 11 = false ∧ next.regs 12 = true ∧ next.regs 13 = records.all test ∧
      Eval (dimensionCallback matrixPair) n base next := by
  let a1 := clearState base 8
  let a2 := BlockCoefficientFrames.copyState a1 4 8
  have e1 := clear_eval base 8
  have h9 : base.stacks 9 = [] := hb.2.2.2.2.2.2.2.1
  have e2 := BlockCoefficientFrames.copy_eval a1 4 8 (by decide) (by decide)
    (by decide) (by simpa [a1, clearState, StackRoutines.clearStacks] using h9)
  have a2st : a2.stacks = Function.update base.stacks 8 split := by
    funext j
    by_cases h : j = 8
    · subst j; simp [a2, a1, BlockCoefficientFrames.copyState, clearState,
        StackRoutines.copyStacks, StackRoutines.clearStacks, h4]
    · simp [a2, a1, BlockCoefficientFrames.copyState, clearState,
        StackRoutines.copyStacks, StackRoutines.clearStacks, h]
  have ha2 : CoefficientEntry ctx table split a2 := by
    rcases hb with ⟨hm, hz, hM, hr, hd, ht, _, hw⟩
    refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
    · simpa [a2st] using hm
    · simpa [a2st] using hz
    · simpa [a2st] using hM
    · simpa [a2st] using hr
    · simpa [a2st] using hd
    · simpa [a2st] using ht
    · simp [a2st]
    · refine ⟨?_, ?_⟩
      · simpa [a2st] using hw.1
      · intro j hj h27 h36 h38 h39 h41
        have h8 : j ≠ 8 := by intro h; subst j; change 22 ≤ 8 at hj; omega
        simpa [a2st, h8] using hw.2 j hj h27 h36 h38 h39 h41
  obtain ⟨n, a3, hn, hst, hreg, hresult, e3⟩ := coefficientScan_eval matrixPair ctx
    table split test cost valid contract records a2 hv ha2
    (by simpa [a2st] using hs) (by simpa [a2st] using h6) (by simpa [a2st] using h7)
  let a4 := clearState a3 4
  let next := finishDimension a4
  have e4 := clear_eval a3 4
  have e5 : Eval (sequence [atom (.copy 13 16), atom (.set 11 false), atom (.set 12 true)])
      (1 + (1 + (1 + 0))) a4 next :=
    (Eval.base (.atom _ _)).seq ((Eval.base (.atom _ _)).seq
      ((Eval.base (.atom _ _)).seq (Eval.base (.skip _))))
  have hlen1 : (a1.stacks 4).length = split.length := by
    simp [a1, clearState, StackRoutines.clearStacks, h4]
  have hlen3 : (a3.stacks 4).length = split.length := by simp [hst, a2st, h4]
  refine ⟨_, next, ?_, ?_, ?_, ?_, ?_, ?_, e1.seq (e2.seq (e3.seq (e4.seq e5)))⟩
  · rw [hlen1, hlen3]
    change n ≤ coefficientScanCost cost records at hn
    unfold dimensionCost
    omega
  · change next.stacks = _
    simp [next, finishDimension, a4, clearState, StackRoutines.clearStacks,
      hst, a2st, StructuredProgram.Atom.eval, StructuredProgram.Atom.instr,
      StructuredProgram.State.ofCfg, execInstr]
  · intro j hj hk
    have h11 : j ≠ 11 := by intro h; subst j; change 11 ≤ 10 at hk; omega
    have h12 : j ≠ 12 := by intro h; subst j; change 12 ≤ 10 at hk; omega
    have h13 : j ≠ 13 := by intro h; subst j; change 13 ≤ 10 at hk; omega
    have h21 : j ≠ 21 := by intro h; subst j; change 21 ≤ 10 at hk; omega
    have h22 : j ≠ 22 := by intro h; subst j; change 22 ≤ 10 at hk; omega
    have hr3 := hreg j hj (by omega)
    simpa [next, finishDimension, a4, clearState, a2, a1,
      BlockCoefficientFrames.copyState, readRegs,
      StructuredProgram.Atom.eval, StructuredProgram.Atom.instr,
      StructuredProgram.State.ofCfg, StructuredProgram.State.withPC, execInstr,
      h11, h12, h13, h21, h22] using hr3
  · simp [next, finishDimension, StructuredProgram.Atom.eval, StructuredProgram.Atom.instr,
      StructuredProgram.State.ofCfg, execInstr]
  · simp [next, finishDimension, StructuredProgram.Atom.eval, StructuredProgram.Atom.instr,
      StructuredProgram.State.ofCfg, execInstr]
  · simpa [next, finishDimension, a4, clearState, readRegs,
      StructuredProgram.Atom.eval, StructuredProgram.Atom.instr,
      StructuredProgram.State.ofCfg, StructuredProgram.State.withPC, execInstr] using hresult

def DimensionEntry (ctx : Context) (table : List Bool) (records : List (List Bool))
    (limit : ℕ) (a : Data) : Prop :=
  CoefficientEntry ctx table (a.stacks 8) a ∧
  a.stacks 14 = ProgramEncoding.encodeRecords records ∧
  a.stacks 6 = [] ∧ a.stacks 7 = [] ∧ (a.stacks 8).length ≤ limit

theorem coefficientEntry_dimension_state (ctx : Context) (table split : List Bool)
    (base : Data) (v pr f : Bool) (src out : List Bool)
    (hb : CoefficientEntry ctx table split base) :
    CoefficientEntry ctx table split
      (RecordPrograms.state dimensionPorts.toPorts base v pr f src out) := by
  rcases hb with ⟨hm, hz, hM, hr, hd, ht, hs, hw⟩
  refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  · simpa [RecordPrograms.state, dimensionPorts] using hm
  · simpa [RecordPrograms.state, dimensionPorts] using hz
  · simpa [RecordPrograms.state, dimensionPorts] using hM
  · simpa [RecordPrograms.state, dimensionPorts] using hr
  · simpa [RecordPrograms.state, dimensionPorts] using hd
  · simpa [RecordPrograms.state, dimensionPorts] using ht
  · simpa [RecordPrograms.state, dimensionPorts] using hs
  · refine ⟨?_, ?_⟩
    · simpa [RecordPrograms.state, dimensionPorts] using hw.1
    · intro j hj h27 h36 h38 h39 h41
      have h3 : j ≠ 3 := by intro h; subst j; change 22 ≤ 3 at hj; omega
      have h4 : j ≠ 4 := by intro h; subst j; change 22 ≤ 4 at hj; omega
      simpa [RecordPrograms.state, dimensionPorts, h3, h4] using
        hw.2 j hj h27 h36 h38 h39 h41

/-- A split record is executable unary data. Its length supplies the bound;
the predicate family is implemented by the supplied coefficient contract. -/
theorem dimensionContract (matrixPair : Cmd 24 42) (ctx : Context)
    (table : List Bool) (records : List (List Bool)) (limit : ℕ)
    (test : List Bool → List Bool → Bool) (cost : List Bool → List Bool → ℕ)
    (validSplit : List Bool → Prop) (validCoefficient : List Bool → Prop)
    (hreversal : ∀ bits, validSplit bits → bits.reverse = bits)
    (hlimit : ∀ bits, validSplit bits → bits.length ≤ limit)
    (hrecords : ∀ bits ∈ records, validCoefficient bits)
    (contract : ∀ split, validSplit split →
      CoefficientContract matrixPair ctx table split (test split) (cost split) validCoefficient) :
    RecordPrograms.CallbackContractWithOn dimensionPorts.toPorts
      (dimensionCallback matrixPair) (fun split => records.all (test split))
      (fun split => dimensionCost limit (cost split) records split)
      (DimensionEntry ctx table records limit)
      (fun split a b => b.stacks = Function.update a.stacks 8 split ∧
        ∀ j : Fin 24, 8 ≤ j.val → j.val ≤ 10 → b.regs j = a.regs j) validSplit := by
  intro split tail base hsplit hb
  let start := RecordPrograms.state dimensionPorts.toPorts base false true false tail split.reverse
  have he : CoefficientEntry ctx table (start.stacks 8) start := by
    simpa [start, RecordPrograms.state, dimensionPorts] using
      coefficientEntry_dimension_state ctx table (base.stacks 8) base false true false
        tail split.reverse hb.1
  obtain ⟨n, out, hn, hst, hreg, h11, h12, h13, run⟩ :=
    dimensionCallback_eval matrixPair ctx table split (test split) (cost split)
      validCoefficient (contract split hsplit) records start hrecords he
      (by simp [start, RecordPrograms.state, dimensionPorts, hreversal split hsplit])
      (by simpa [start, RecordPrograms.state, dimensionPorts] using hb.2.1)
      (by simpa [start, RecordPrograms.state, dimensionPorts] using hb.2.2.1)
      (by simpa [start, RecordPrograms.state, dimensionPorts] using hb.2.2.2.1)
  let next : Data := ⟨out.regs, Function.update base.stacks 8 split⟩
  have hnext : DimensionEntry ctx table records limit next := by
    rcases hb.1 with ⟨hm, hz, hM, hr, hd, ht, _, hw⟩
    refine ⟨⟨?_, ?_, ?_, ?_, ?_, ?_, rfl, ?_⟩, ?_, ?_, ?_, ?_⟩
    · simpa [next] using hm
    · simpa [next] using hz
    · simpa [next] using hM
    · simpa [next] using hr
    · simpa [next] using hd
    · simpa [next] using ht
    · refine ⟨?_, ?_⟩
      · simpa [next] using hw.1
      · intro j hj h27 h36 h38 h39 h41
        have h8 : j ≠ 8 := by intro h; subst j; change 22 ≤ 8 at hj; omega
        simpa [next, h8] using hw.2 j hj h27 h36 h38 h39 h41
    · simpa [next] using hb.2.1
    · simpa [next] using hb.2.2.1
    · simpa [next] using hb.2.2.2.1
    · simpa [next] using hlimit split hsplit
  have hstate : RecordPrograms.state dimensionPorts.toPorts next false true
      (records.all (test split)) tail [] = out := by
    have hregs : (RecordPrograms.state dimensionPorts.toPorts next false true
        (records.all (test split)) tail []).regs = out.regs := by
      funext j
      by_cases hj11 : j = 11
      · subst j; simpa [RecordPrograms.state, dimensionPorts] using h11.symm
      by_cases hj12 : j = 12
      · subst j; simpa [RecordPrograms.state, dimensionPorts] using h12.symm
      by_cases hj13 : j = 13
      · subst j; simpa [RecordPrograms.state, dimensionPorts] using h13.symm
      simp [RecordPrograms.state, dimensionPorts, next, hj11, hj12, hj13]
    have hstacks : (RecordPrograms.state dimensionPorts.toPorts next false true
        (records.all (test split)) tail []).stacks = out.stacks := by
      rw [hst]
      funext j
      by_cases hj3 : j = 3 <;> by_cases hj4 : j = 4 <;> by_cases hj8 : j = 8 <;>
        subst_vars <;> simp_all [RecordPrograms.state, dimensionPorts, next, start]
    cases out
    exact congrArg₂ StructuredProgram.State.mk hregs hstacks
  refine ⟨n, next, ?_, hnext, ⟨rfl, ?_⟩, ?_⟩
  · have hold : (start.stacks 8).length ≤ limit := by
      simpa [start, RecordPrograms.state, dimensionPorts] using hb.2.2.2.2
    change n ≤ dimensionCost limit (cost split) records split
    unfold dimensionCost at hn ⊢
    omega
  · intro j hj hk
    have h11 : j ≠ 11 := by intro h; subst j; change 11 ≤ 10 at hk; omega
    have h12 : j ≠ 12 := by intro h; subst j; change 12 ≤ 10 at hk; omega
    have h13 : j ≠ 13 := by intro h; subst j; change 13 ≤ 10 at hk; omega
    simpa [next, start, RecordPrograms.state, dimensionPorts, h11, h12, h13] using hreg j hj hk
  · simpa only [hstate] using run

def dimensionScanCost (limit : ℕ) (cost : List Bool → List Bool → ℕ)
    (coefficients dimensions : List (List Bool)) : ℕ :=
  9 * (ProgramEncoding.encodeRecords dimensions).length +
    RecordPrograms.budget (fun split => dimensionCost limit (cost split) coefficients split)
      dimensions + 12

/-- The actual saved split scan retains a successful split, while framing
the table iterator and every stack except the selected split. -/
theorem dimensionScan_eval (matrixPair : Cmd 24 42) (ctx : Context)
    (table : List Bool) (coefficients dimensions : List (List Bool)) (limit : ℕ)
    (test : List Bool → List Bool → Bool) (cost : List Bool → List Bool → ℕ)
    (validSplit : List Bool → Prop) (validCoefficient : List Bool → Prop)
    (hreversal : ∀ bits, validSplit bits → bits.reverse = bits)
    (hlimit : ∀ bits, validSplit bits → bits.length ≤ limit)
    (hcoefficients : ∀ bits ∈ coefficients, validCoefficient bits)
    (hdimensions : ∀ bits ∈ dimensions, validSplit bits)
    (contract : ∀ split, validSplit split →
      CoefficientContract matrixPair ctx table split (test split) (cost split) validCoefficient)
    (base : Data) (hb : DimensionEntry ctx table coefficients limit base)
    (hs : base.stacks 12 = ProgramEncoding.encodeRecords dimensions)
    (h3 : base.stacks 3 = []) (h4 : base.stacks 4 = []) :
    ∃ n next,
      n ≤ dimensionScanCost limit cost coefficients dimensions ∧
      DimensionEntry ctx table coefficients limit next ∧
      (∀ j : Fin 42, j ≠ 8 → next.stacks j = base.stacks j) ∧
      (∀ j : Fin 24, 8 ≤ j.val → j.val ≤ 10 → next.regs j = base.regs j) ∧
      next.regs 13 = dimensions.any (fun split => coefficients.all (test split)) ∧
      (dimensions.any (fun split => coefficients.all (test split)) = true →
        ∃ split ∈ dimensions, coefficients.all (test split) = true ∧ next.stacks 8 = split) ∧
      Eval (savedAnyCommand dimensionPorts (dimensionCallback matrixPair)) n base next := by
  let pred := fun split => coefficients.all (test split)
  let inv := fun a : Data => DimensionEntry ctx table coefficients limit a ∧
    (∀ j : Fin 42, j ≠ 8 → a.stacks j = base.stacks j) ∧
    ∀ j : Fin 24, 8 ≤ j.val → j.val ≤ 10 → a.regs j = base.regs j
  have hc : RecordPrograms.CallbackContractWithOn dimensionPorts.toPorts
      (dimensionCallback matrixPair) pred
      (fun split => dimensionCost limit (cost split) coefficients split) inv
      (fun split _ b => b.stacks 8 = split) validSplit := by
    intro split tail a hv ha
    obtain ⟨n, b, hn, hi, heffect, heval⟩ := dimensionContract matrixPair ctx table coefficients
      limit test cost validSplit validCoefficient hreversal hlimit hcoefficients contract
      split tail a hv ha.1
    refine ⟨n, b, hn, ⟨hi, ?_, ?_⟩, ?_, heval⟩
    · intro j hj
      rw [heffect.1, Function.update_of_ne hj]
      exact ha.2.1 j hj
    · intro j hj hk
      exact (heffect.2 j hj hk).trans (ha.2.2 j hj hk)
    · simp [heffect.1]
  have h9 : base.stacks dimensionPorts.temporary = [] := hb.1.2.2.2.2.2.2.2.1
  obtain ⟨n, b, hn, hi, history, he⟩ := savedAnyCommand_eval_with dimensionPorts
    (dimensionCallback matrixPair) pred
    (fun split => dimensionCost limit (cost split) coefficients split) inv
    (fun split _ b => b.stacks 8 = split) validSplit hc dimensions base hdimensions
    ⟨hb, fun _ _ => rfl, fun _ _ _ => rfl⟩ hs h9
    (base.regs 11) (base.regs 12) (base.regs 13)
  let next := RecordPrograms.state dimensionPorts.toPorts b false true
    (dimensions.any pred) [] []
  have hstart : RecordPrograms.state dimensionPorts.toPorts base (base.regs 11)
      (base.regs 12) (base.regs 13) [] [] = base := by
    apply RecordPrograms.state_eq_self <;> simp [dimensionPorts, h3, h4]
  have hst8 : next.stacks 8 = b.stacks 8 := by
    simp [next, RecordPrograms.state, dimensionPorts]
  have hentry : DimensionEntry ctx table coefficients limit next := by
    refine ⟨?_, ?_, ?_, ?_, ?_⟩
    · simpa only [hst8] using coefficientEntry_dimension_state ctx table (b.stacks 8) b
        false true (dimensions.any pred) [] [] hi.1.1
    · simpa [next, RecordPrograms.state, dimensionPorts] using hi.1.2.1
    · simpa [next, RecordPrograms.state, dimensionPorts] using hi.1.2.2.1
    · simpa [next, RecordPrograms.state, dimensionPorts] using hi.1.2.2.2.1
    · simpa only [hst8] using hi.1.2.2.2.2
  refine ⟨n, next, hn, hentry, ?_, ?_, ?_, ?_, ?_⟩
  · intro j hj
    by_cases hjs : j = 3
    · subst j; simp [next, RecordPrograms.state, dimensionPorts, h3]
    by_cases hjp : j = 4
    · subst j; simp [next, RecordPrograms.state, dimensionPorts, h4]
    simpa [next, RecordPrograms.state, dimensionPorts, hjs, hjp] using hi.2.1 j hj
  · intro j hj hk
    have h11 : j ≠ 11 := by intro h; subst j; change 11 ≤ 10 at hk; omega
    have h12 : j ≠ 12 := by intro h; subst j; change 12 ≤ 10 at hk; omega
    have h13 : j ≠ 13 := by intro h; subst j; change 13 ≤ 10 at hk; omega
    simpa [next, RecordPrograms.state, dimensionPorts, h11, h12, h13] using hi.2.2 j hj hk
  · exact RecordPrograms.state_found dimensionPorts.toPorts b false true
      (dimensions.any pred) [] []
  · intro success
    obtain ⟨split, hmem, ht, hout⟩ := selected_of_visits 8 pred history success
    exact ⟨split, hmem, ht, hst8.trans hout⟩
  · simpa only [hstart] using he

end SubexponentialSAT.Subexp.BlockSearchNested

end OAI.Algorithms