File size: 23,667 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import OAI.Computability.WeisfeilerLeman.Transducer

namespace OAI

universe uK uA

namespace WLTime.BitWords
 def value (xs : List Bool) : ℕ := Nat.ofDigits 2 (xs.map Bool.toNat)
 @[simp] theorem value_nil : value []=0 := rfl
 @[simp] theorem value_cons (b : Bool) (xs : List Bool) : value (b::xs)=b.toNat+2*value xs := rfl
 theorem value_ofFn {w : ℕ} (f : Fin w → Bool) : value (List.ofFn f)=Nat.ofBits f := by
  induction w with
  | zero => simp [value]
  | succ w ih =>
    rw [List.ofFn_succ,value_cons,Nat.ofBits_succ,ih]
    dsimp only [Function.comp_def]
    omega
 theorem value_lt (xs : List Bool) : value xs<2^xs.length := by
  rw [←List.ofFn_get xs,value_ofFn]
  simpa only [List.length_ofFn] using Nat.ofBits_lt_two_pow xs.get
 theorem value_word {w : ℕ} (a : Word w) : value (wordBits a)=a.val := by
  rw [wordBits,value_ofFn,Nat.ofBits_testBit,Nat.mod_eq_of_lt a.isLt]
 theorem testBit_value (xs : List Bool) (i : ℕ) : (value xs).testBit i=xs.getD i false := by
  rw [←List.ofFn_get xs,value_ofFn,Nat.testBit_ofBits]
  simp only [List.ofFn_get]
  split_ifs with h
  · change xs[i]=xs.getD i false
    exact (List.getD_eq_getElem _ _ h).symm
  · exact (List.getD_eq_default xs false (Nat.le_of_not_gt h)).symm
 theorem word_of_value (xs : List Bool) : wordBits (⟨value xs,value_lt xs⟩ : Word xs.length)=xs := by
  apply List.ext_getElem
  · simp [wordBits]
  · intro i hi hj
    simp only [wordBits,List.getElem_ofFn,testBit_value]
    exact List.getD_eq_getElem _ _ hj
 def inc : List Bool → List Bool
  | [] => []
  | false::xs => true::xs
  | true::xs => false::inc xs
 @[simp] theorem length_inc (xs : List Bool) : (inc xs).length=xs.length := by
  induction xs with
  | nil => rfl
  | cons b xs ih => cases b <;> simp [inc,ih]
 theorem inc_value (xs : List Bool) : value (inc xs)=(value xs+1)%2^xs.length := by
  induction xs with
  | nil => simp [inc]
  | cons b xs ih =>
    cases b
    · have hx := value_lt xs
      simp only [inc,value_cons,Bool.toNat_false,Bool.toNat_true,List.length_cons,pow_succ]
      rw [Nat.mod_eq_of_lt (by omega)]
      omega
    · simp only [inc,value_cons,Bool.toNat_false,Bool.toNat_true,List.length_cons,pow_succ,ih,zero_add]
      have he : 1+2*value xs+1=2*(value xs+1) := by omega
      rw [he,Nat.mul_comm (2^xs.length) 2,Nat.mul_mod_mul_left]
 theorem word_injective {w : ℕ} : Function.Injective (wordBits (w:=w)) := by
  intro a b h
  apply Fin.ext
  simpa only [value_word] using congrArg value h
end WLTime.BitWords

namespace WLTime.BitWords
 def carry : List Bool → ℕ
  | true::xs => carry xs+1
  | _ => 0
 def rest : List Bool → List Bool
  | true::xs => rest xs
  | xs => xs
 def raise : List Bool → List Bool
  | [] => []
  | _::xs => true::xs
 theorem carry_le (xs : List Bool) : carry xs≤xs.length := by
  induction xs with
  | nil => rfl
  | cons b xs ih => cases b <;> simp [carry]; omega
 theorem inc_decompose (xs : List Bool) : inc xs=List.replicate (carry xs) false++raise (rest xs) := by
  induction xs with
  | nil => rfl
  | cons b xs ih => cases b <;> simp [inc,carry,rest,raise,ih,List.replicate_succ]
end WLTime.BitWords
namespace WLTime.TypedStack.Code
open BitWords
variable {K : Type uK} [DecidableEq K]
def carryCode (a b : K) : Code K Bool :=
  .loop (fun v => v a==some true) (.seq (.pop a) (.push b false))
def raiseCode (a : K) : Code K Bool :=
  .branch (nonempty a) (.seq (.pop a) (.push a true)) .skip
def increment (a b : K) : Code K Bool :=
  .seq (carryCode a b) (.seq (raiseCode a) (.transfer b a false))
 theorem carryCode_exec (a b : K) (hab : a≠b) (s : Stacks K Bool) :
    (carryCode a b).Exec s (5*carry (s a)+1)
      (Function.update (Function.update s a (rest (s a))) b (List.replicate (carry (s a)) false++s b)) := by
  generalize hx : s a=xs
  induction xs generalizing s with
  | nil =>
    have hout : Function.update (Function.update s a (rest [])) b (List.replicate (carry []) false++s b)=s := by
      simp only [rest,carry,List.replicate_zero,List.nil_append]
      rw [←hx,Function.update_eq_self,Function.update_eq_self]
    rw [hout]
    exact .loop_false (by simp [tops,hx])
  | cons x xs ih =>
    cases x
    · have hout : Function.update (Function.update s a (rest (false::xs))) b (List.replicate (carry (false::xs)) false++s b)=s := by
        simp only [rest,carry,List.replicate_zero,List.nil_append]
        rw [←hx,Function.update_eq_self,Function.update_eq_self]
      rw [hout]
      exact .loop_false (by simp [tops,hx])
    · let mid := Function.update (Function.update s a xs) b (false::s b)
      have hb : (Code.seq (.pop a) (.push b false)).Exec s 4 mid := by
        simpa only [hx,List.tail_cons,Function.update_of_ne hab.symm,mid] using
          Exec.seq (Exec.pop s a) (Exec.push (Function.update s a (s a).tail) b false)
      have hs : mid a=xs := by simp [mid,hab]
      have hh := ih mid hs
      have hout : Function.update (Function.update mid a (rest xs)) b (List.replicate (carry xs) false++mid b)=
          Function.update (Function.update s a (rest (true::xs))) b (List.replicate (carry (true::xs)) false++s b) := by
        funext k
        by_cases hk : k=b
        · subst k
          simp only [Function.update_self,mid,carry]
          rw [List.replicate_succ']
          simp only [List.append_assoc,List.singleton_append]
        · by_cases ha : k=a
          · subst k; simp [mid,hk,rest]
          · simp [mid,hk,ha]
      rw [hout] at hh
      have he := Exec.loop_true (p:=fun v : Tops K Bool => v a==some true) (by simp [tops,hx]) hb hh
      convert he using 1
      · rfl
      · simp [carry]; omega
 theorem raiseCode_exec (a : K) (s : Stacks K Bool) :
    ∃u≤5,(raiseCode a).Exec s u (Function.update s a (raise (s a))) := by
  cases hx : s a with
  | nil =>
    refine ⟨2,by omega,?_⟩
    have ht : Function.update s a (raise (s a))=s := by rw [hx]; change Function.update s a []=s; rw [←hx]; exact Function.update_eq_self _ _
    rw [hx] at ht
    rw [ht]
    exact Exec.branch_false (by simp [nonempty,tops,hx]) (Exec.skip s)
  | cons b xs =>
    have he := Exec.seq (Exec.pop s a) (Exec.push (Function.update s a (s a).tail) a true)
    have hf := Exec.branch_true (b:=.skip) (p:=nonempty a) (by simp [nonempty,tops,hx]) he
    refine ⟨5,by omega,?_⟩
    simpa only [raiseCode,hx,List.tail_cons,Function.update_self,Function.update_idem,raise] using hf
 theorem increment_exec (a b : K) (hab : a≠b) (s : Stacks K Bool) (hb : s b=[]) :
    ∃u≤10*(s a).length+7,(increment a b).Exec s u (Function.update s a (inc (s a))) := by
  let mid := Function.update (Function.update s a (rest (s a))) b (List.replicate (carry (s a)) false)
  have hc : (carryCode a b).Exec s (5*carry (s a)+1) mid := by simpa only [hb,List.append_nil] using carryCode_exec a b hab s
  obtain ⟨u,hu,he⟩ := raiseCode_exec a mid
  have ht := transfer_exec b a hab.symm false (Function.update mid a (raise (mid a)))
  have hf := Exec.seq hc (Exec.seq he ht)
  have hout : Function.update (Function.update (Function.update mid a (raise (mid a))) b []) a
      (((Function.update mid a (raise (mid a))) b).reverse++(Function.update mid a (raise (mid a))) a)=
      Function.update s a (inc (s a)) := by
    funext k
    by_cases ha : k=a
    · subst k
      simp [mid,hab,hab.symm,inc_decompose]
    · by_cases hkb : k=b
      · subst k; simp [mid,ha,hb]
      · simp [mid,ha,hkb]
  rw [hout] at hf
  refine ⟨_,?_,hf⟩
  simp only [mid,Function.update_of_ne hab.symm,Function.update_self,List.length_replicate]
  have h := carry_le (s a)
  omega
end WLTime.TypedStack.Code

namespace WLTime.SparseMatch
open TypedStack
abbrev Reg := Fin 4
def state (a b f h : List Bool) : Stacks Reg Bool := ![a,b,f,h]
def body : Code Reg Bool :=
  .seq (.pushDyn 2 (fun v => (v 2).getD false && ((v 0).getD false == (v 1).getD false)))
    (.seq (.pushDyn 3 (fun v => (v 0).getD false)) (.seq (.pop 0) (.pop 1)))
def program : ℕ → Code Reg Bool
  | 0 => .skip
  | n+1 => .seq body (program n)
 theorem body_exec (x y q : Bool) (xs ys fs hs : List Bool) :
    body.Exec (state (x::xs) (y::ys) (q::fs) hs) 8
      (state xs ys ((q&&(x==y))::q::fs) (x::hs)) := by
  let s := state (x::xs) (y::ys) (q::fs) hs
  let s₁ := Function.update s 2 ((q&&(x==y))::q::fs)
  let s₂ := Function.update s₁ 3 (x::hs)
  let s₃ := Function.update s₂ 0 xs
  have h₁ : (Code.pushDyn 2 (fun v : Tops Reg Bool => (v 2).getD false && ((v 0).getD false == (v 1).getD false))).Exec s 2 s₁ := by
    simpa [s,s₁,state,tops] using Code.Exec.pushDyn s 2 (fun v => (v 2).getD false && ((v 0).getD false == (v 1).getD false))
  have h₂ : (Code.pushDyn 3 (fun v : Tops Reg Bool => (v 0).getD false)).Exec s₁ 2 s₂ := by
    simpa [s₂,s₁,s,state,tops] using Code.Exec.pushDyn s₁ 3 (fun v => (v 0).getD false)
  have h₃ : (Code.pop 0).Exec s₂ 2 s₃ := by simpa [s₃,s₂,s₁,s,state] using Code.Exec.pop s₂ 0
  have h₄ : (Code.pop 1).Exec s₃ 2 (state xs ys ((q&&(x==y))::q::fs) (x::hs)) := by
    have he := Code.Exec.pop s₃ 1
    convert he using 1
    funext i; fin_cases i <;> simp [s₃,s₂,s₁,s,state]
  exact Code.Exec.seq h₁ (Code.Exec.seq h₂ (Code.Exec.seq h₃ h₄))
 theorem program_exec (xs ys tail fs hs : List Bool) (q : Bool) (hlen : ys.length=xs.length) :
    ∃gs, gs.length=xs.length+fs.length+1 ∧ gs.headD false=(q && (xs==ys)) ∧
      (program xs.length).Exec (state (xs++tail) ys (q::fs) hs) (8*xs.length+1)
        (state tail [] gs (xs.reverse++hs)) := by
  induction xs generalizing ys fs hs q with
  | nil =>
    have hy : ys=[] := List.eq_nil_of_length_eq_zero hlen
    subst ys
    exact ⟨q::fs,by simp,by simp,by simpa [program,state] using Code.Exec.skip (state tail [] (q::fs) hs)⟩
  | cons x xs ih =>
    cases ys with
    | nil => simp at hlen
    | cons y ys =>
      have hy : ys.length=xs.length := by simpa using hlen
      obtain ⟨gs,hg,hhead,he⟩ := ih ys (q::fs) (x::hs) (q&&(x==y)) hy
      refine ⟨gs,by simp only [List.length_cons] at hg ⊢; omega,?_,?_⟩
      · rw [hhead]
        simp only [List.cons_beq_cons]
        cases q <;> cases hxy : (x==y) <;> simp
      · have h := Code.Exec.seq (body_exec x y q (xs++tail) ys fs hs) he
        simpa only [program,List.length_cons,List.cons_append,List.reverse_cons,List.append_assoc,List.singleton_append,List.nil_append,Nat.mul_add,Nat.mul_one,Nat.add_assoc,Nat.add_comm,Nat.add_left_comm] using h
 theorem program_size (n : ℕ) : (program n).controlSize=8*n+1 := by
  induction n with
  | zero => rfl
  | succ n ih => simp only [program,body,Code.controlSize,ih]; omega
end WLTime.SparseMatch
namespace WLTime.SparseTake
open TypedStack
abbrev Reg := Fin 3
def state (a b c : List Bool) : Stacks Reg Bool := ![a,b,c]
def body : Code Reg Bool :=
  .seq (.pushDyn 1 (fun v => (v 0).getD false))
    (.seq (.pushDyn 2 (fun v => (v 0).getD false)) (.pop 0))
def program : ℕ → Code Reg Bool
  | 0 => .skip
  | n+1 => .seq body (program n)
 theorem body_exec (x : Bool) (xs bs cs : List Bool) :
    body.Exec (state (x::xs) bs cs) 6 (state xs (x::bs) (x::cs)) := by
  let s := state (x::xs) bs cs
  let s₁ := Function.update s 1 (x::bs)
  let s₂ := Function.update s₁ 2 (x::cs)
  have he₁ : (Code.pushDyn 1 (fun v : Tops Reg Bool => (v 0).getD false)).Exec s 2 s₁ := by
    simpa [s₁,s,state,tops] using Code.Exec.pushDyn s 1 (fun v => (v 0).getD false)
  have he₂ : (Code.pushDyn 2 (fun v : Tops Reg Bool => (v 0).getD false)).Exec s₁ 2 s₂ := by
    simpa [s₂,s₁,s,state,tops] using Code.Exec.pushDyn s₁ 2 (fun v => (v 0).getD false)
  have he₃ : (Code.pop 0).Exec s₂ 2 (state xs (x::bs) (x::cs)) := by
    have he := Code.Exec.pop s₂ 0
    convert he using 1
    funext i; fin_cases i <;> simp [s₂,s₁,s,state]
  exact Code.Exec.seq he₁ (Code.Exec.seq he₂ he₃)
 theorem program_exec (xs tail bs cs : List Bool) :
    (program xs.length).Exec (state (xs++tail) bs cs) (6*xs.length+1)
      (state tail (xs.reverse++bs) (xs.reverse++cs)) := by
  induction xs generalizing bs cs with
  | nil => simpa [program,state] using Code.Exec.skip (state tail bs cs)
  | cons x xs ih =>
    have he := Code.Exec.seq (body_exec x (xs++tail) bs cs) (ih (x::bs) (x::cs))
    simpa only [program,List.length_cons,List.cons_append,List.reverse_cons,List.append_assoc,List.singleton_append,List.nil_append,Nat.mul_add,Nat.mul_one,Nat.add_assoc,Nat.add_comm,Nat.add_left_comm] using he
 theorem program_size (n : ℕ) : (program n).controlSize=6*n+1 := by
  induction n with
  | zero => rfl
  | succ n ih => simp only [program,body,Code.controlSize,ih]; omega
end WLTime.SparseTake

namespace WLTime.SparseLookup
open TypedStack
abbrev Reg := Fin 8
def state (log query out hist copy flags candidate tmp : List Bool) : Stacks Reg Bool :=
  ![log,query,out,hist,copy,flags,candidate,tmp]
def matchMap : Fin 4 → Reg := ![0,4,5,3]
def takeMap : Fin 3 → Reg := ![0,3,6]
 theorem matchMap_inj : Function.Injective matchMap := by decide
 theorem takeMap_inj : Function.Injective takeMap := by decide
def first (w : ℕ) : Code Reg Bool :=
  .seq (.copy 1 4 7 false) (.seq (.push 5 true)
    (.seq ((SparseMatch.program w).relabel matchMap) ((SparseTake.program w).relabel takeMap)))
def finish : Code Reg Bool :=
  .seq (.branch (fun v => (v 5).getD false) (.transfer 6 2 false) (.clear 6)) (.clear 5)
def one (w : ℕ) : Code Reg Bool := .seq (first w) finish
 theorem first_exec (key value query tail hist : List Bool)
    (hk : key.length=query.length) (hv : value.length=query.length) :
    ∃fs, fs.length=query.length+1 ∧ fs.headD false=(key==query) ∧
      (first query.length).Exec (state (key++value++tail) query [] hist [] [] [] [])
        (26*query.length+6)
        (state tail query [] (value.reverse++key.reverse++hist) [] fs value.reverse []) := by
  let s₀ := state (key++value++tail) query [] hist [] [] [] []
  let s₁ := state (key++value++tail) query [] hist query [] [] []
  let s₂ := state (key++value++tail) query [] hist query [true] [] []
  have h₁ : (Code.copy 1 4 7 false).Exec s₀ (12*query.length+2) s₁ := by
    have h := Code.copy_exec (1 : Reg) 4 7 (by decide) (by decide) (by decide) false s₀ rfl
    convert h using 1
    · rfl
    · funext i; fin_cases i <;> simp [s₁,s₀,state]
  have h₂ : (Code.push 5 true).Exec s₁ 2 s₂ := by
    have h := Code.Exec.push s₁ 5 true
    convert h using 1
    funext i; fin_cases i <;> simp [s₂,s₁,state]
  obtain ⟨fs,hlen,hhead,hm⟩ := SparseMatch.program_exec key query (value++tail) [] hist true hk.symm
  have h₃ : ((SparseMatch.program query.length).relabel matchMap).Exec s₂ (8*query.length+1)
      (state (value++tail) query [] (key.reverse++hist) [] fs [] []) := by
    rw [hk] at hm
    apply Code.relabel_exec_of_maps matchMap matchMap_inj hm
    · intro i; fin_cases i <;> simp [s₂,state,SparseMatch.state,matchMap,List.append_assoc]
    · intro i; fin_cases i <;> simp [state,SparseMatch.state,matchMap]
    · intro i hi; fin_cases i <;> simp_all [state,s₂,matchMap]
      <;> first | exact False.elim (hi 0 rfl) | exact False.elim (hi 1 rfl) | exact False.elim (hi 2 rfl) | exact False.elim (hi 3 rfl)
  have h₄ : ((SparseTake.program query.length).relabel takeMap).Exec
      (state (value++tail) query [] (key.reverse++hist) [] fs [] []) (6*query.length+1)
      (state tail query [] (value.reverse++key.reverse++hist) [] fs value.reverse []) := by
    have ht := SparseTake.program_exec value tail (key.reverse++hist) []
    rw [hv] at ht
    apply Code.relabel_exec_of_maps takeMap takeMap_inj ht
    · intro i; fin_cases i <;> simp [state,SparseTake.state,takeMap]
    · intro i; fin_cases i <;> simp [state,SparseTake.state,takeMap,List.append_assoc]
    · intro i hi; fin_cases i <;> simp_all [state,takeMap]
      <;> first | exact False.elim (hi 0 rfl) | exact False.elim (hi 1 rfl) | exact False.elim (hi 2 rfl)
  refine ⟨fs,by simpa [hk] using hlen,by simpa using hhead,?_⟩
  have he := Code.Exec.seq h₁ (Code.Exec.seq h₂ (Code.Exec.seq h₃ h₄))
  convert he using 1
  · rfl
  · omega
 theorem finish_exec (tail query hist fs value : List Bool) :
    ∃u ≤ 5*value.length+3*fs.length+3,
      finish.Exec (state tail query [] hist [] fs value.reverse []) u
        (state tail query (if fs.headD false then value else []) hist [] [] [] []) := by
  let s := state tail query [] hist [] fs value.reverse []
  let out := if fs.headD false then value else []
  let mid := state tail query out hist [] fs [] []
  have hb : ∃u ≤ 5*value.length+2,
      (Code.branch (fun v : Tops Reg Bool => (v 5).getD false) (.transfer 6 2 false) (.clear 6)).Exec s u mid := by
    cases hh : fs.headD false
    · have he := Code.clear_exec (6 : Reg) s
      have ho : out=[] := ite_eq_right (by rw [hh]; decide)
      have hm : Function.update s 6 []=mid := by
        funext i; fin_cases i <;> simp [s,mid,state,ho]
      rw [hm] at he
      refine ⟨_,?_,Code.Exec.branch_false (by simpa [tops,s,state] using hh) he⟩
      simp [s,state]; omega
    · have he := Code.transfer_exec (6 : Reg) 2 (by decide) false s
      have ho : out=value := ite_eq_left hh
      have hm : Function.update (Function.update s 6 []) 2 ((s 6).reverse++s 2)=mid := by
        funext i; fin_cases i <;> simp [s,mid,state,ho]
      rw [hm] at he
      refine ⟨_,?_,Code.Exec.branch_true (by simpa [tops,s,state] using hh) he⟩
      simp [s,state]
  obtain ⟨u,hu,he⟩ := hb
  have hc := Code.clear_exec (5 : Reg) mid
  have hf : Function.update mid 5 []=state tail query out hist [] [] [] [] := by
    funext i; fin_cases i <;> simp [mid,state]
  rw [hf] at hc
  refine ⟨_,?_,Code.Exec.seq he hc⟩
  change u+(3*fs.length+1) ≤ _
  omega
 theorem one_exec (key value query tail hist : List Bool)
    (hk : key.length=query.length) (hv : value.length=query.length) :
    ∃u ≤ 40*(query.length+1),
      (one query.length).Exec (state (key++value++tail) query [] hist [] [] [] []) u
        (state tail query (if key=query then value else []) (value.reverse++key.reverse++hist) [] [] [] []) := by
  obtain ⟨fs,hf,hh,he⟩ := first_exec key value query tail hist hk hv
  obtain ⟨u,hu,hg⟩ := finish_exec tail query (value.reverse++key.reverse++hist) fs value
  rw [hh] at hg
  simp only [beq_iff_eq] at hg
  refine ⟨_,?_,Code.Exec.seq he hg⟩
  rw [hf,hv] at hu
  omega
end WLTime.SparseLookup
namespace WLTime.TypedStack.Code
variable {K : Type uK} {A : Type uA} [DecidableEq K]
def pushList (k : K) : List A → Code K A
  | [] => .skip
  | x::xs => .seq (pushList k xs) (.push k x)
 theorem pushList_exec (k : K) (xs : List A) (s : Stacks K A) :
    (pushList k xs).Exec s (2*xs.length+1) (Function.update s k (xs++s k)) := by
  induction xs with
  | nil => simpa [pushList] using Exec.skip s
  | cons x xs ih =>
    have he := Exec.seq ih (Exec.push (Function.update s k (xs++s k)) k x)
    simpa only [pushList,List.length_cons,List.cons_append,Function.update_self,Function.update_idem,
      Nat.mul_add,Nat.mul_one,Nat.add_assoc] using he
 omit [DecidableEq K] in
 theorem pushList_size (k : K) (xs : List A) : (pushList k xs).controlSize=2*xs.length+1 := by
  induction xs with
  | nil => rfl
  | cons x xs ih => simp only [pushList,controlSize,List.length_cons,ih]; omega
end WLTime.TypedStack.Code
namespace WLTime.SparseLookup
open TypedStack
abbrev Entry := List Bool × List Bool
def flatten (l : List Entry) : List Bool := l.flatMap (fun e => e.1++e.2)
def WellFormed (w : ℕ) (l : List Entry) : Prop := ∀e∈l,e.1.length=w ∧ e.2.length=w
def consumed (q : List Bool) : List Entry → List Entry
  | [] => []
  | e::l => if e.1=q then [e] else e::consumed q l
def remaining (q : List Bool) : List Entry → List Entry
  | [] => []
  | e::l => if e.1=q then l else remaining q l
def found (q : List Bool) : List Entry → List Bool
  | [] => []
  | e::l => if e.1=q then e.2 else found q l
 theorem split_log (q : List Bool) (l : List Entry) : consumed q l++remaining q l=l := by
  induction l with
  | nil => rfl
  | cons e l ih => by_cases he : e.1=q <;> simp [consumed,remaining,he,ih]
 theorem flatten_append (l m : List Entry) : flatten (l++m)=flatten l++flatten m := by simp [flatten]
 theorem flatten_length (w : ℕ) (l : List Entry) (hw : WellFormed w l) : (flatten l).length=2*w*l.length := by
  induction l with
  | nil => simp [flatten]
  | cons e l ih =>
    have he := hw e (by simp)
    have hl : WellFormed w l := fun a ha => hw a (by simp [ha])
    have hh := ih hl
    simp only [flatten,List.flatMap_cons,List.length_append,List.length_cons] at *
    rw [he.1,he.2,hh]
    ring
 theorem consumed_length (q : List Bool) (l : List Entry) : (flatten (consumed q l)).length ≤ (flatten l).length := by
  have hh := congrArg (fun l => (flatten l).length) (split_log q l)
  rw [flatten_append,List.length_append] at hh
  omega
 theorem found_length (q : List Bool) (l : List Entry) (hw : WellFormed q.length l) :
    (found q l).length=0 ∨ (found q l).length=q.length := by
  induction l with
  | nil => exact Or.inl rfl
  | cons e l ih =>
    by_cases he : e.1=q
    · exact Or.inr (by simpa [found,he] using (hw e (by simp)).2)
    · simpa [found,he] using ih (fun a ha => hw a (by simp [ha]))
def keepGoing (v : Tops Reg Bool) : Bool := (Code.nonempty 0 v) && !(Code.nonempty 2 v)
def search (w : ℕ) : Code Reg Bool := .loop keepGoing (one w)
 theorem search_exec (q : List Bool) (l : List Entry) (hist : List Bool) (hq : 0<q.length) (hw : WellFormed q.length l) :
    ∃u ≤ 42*(q.length+1)*l.length+1,
      (search q.length).Exec (state (flatten l) q [] hist [] [] [] []) u
        (state (flatten (remaining q l)) q (found q l) ((flatten (consumed q l)).reverse++hist) [] [] [] []) := by
  induction l generalizing hist with
  | nil =>
    refine ⟨1,by simp,?_⟩
    have he : (search q.length).Exec (state [] q [] hist [] [] [] []) 1 (state [] q [] hist [] [] [] []) :=
      Code.Exec.loop_false (by simp [keepGoing,Code.nonempty,tops,state])
    simpa [flatten,consumed,remaining,found] using he
  | cons e l ih =>
    have he := hw e (by simp)
    have hl : WellFormed q.length l := fun a ha => hw a (by simp [ha])
    have hkne : e.1≠[] := by
      intro h
      have hh := he.1
      rw [h,List.length_nil] at hh
      omega
    obtain ⟨a,ha,hstep⟩ := one_exec e.1 e.2 q (flatten l) hist he.1 he.2
    by_cases h : e.1=q
    · have hval : e.2≠[] := by intro hh; rw [hh,List.length_nil] at he; omega
      simp only [ite_eq_left h] at hstep
      have hstop : (search q.length).Exec
          (state (flatten l) q e.2 (e.2.reverse++e.1.reverse++hist) [] [] [] []) 1
          (state (flatten l) q e.2 (e.2.reverse++e.1.reverse++hist) [] [] [] []) :=
        Code.Exec.loop_false (by simp [keepGoing,Code.nonempty,tops,state,hval])
      have hex := Code.Exec.loop_true (p:=keepGoing) (by simp [keepGoing,Code.nonempty,tops,state,hkne]) hstep hstop
      refine ⟨a+1+1,?_,?_⟩
      · simp only [List.length_cons]; nlinarith
      · simpa only [search,flatten,List.flatMap_cons,consumed,remaining,found,ite_eq_left h,List.flatMap_nil,List.append_nil,
          List.reverse_append,List.append_assoc] using hex
    · simp only [ite_eq_right h] at hstep
      obtain ⟨b,hb,hrest⟩ := ih (e.2.reverse++e.1.reverse++hist) hl
      have hex := Code.Exec.loop_true (p:=keepGoing) (by simp [keepGoing,Code.nonempty,tops,state,hkne]) hstep hrest
      refine ⟨a+b+1,?_,?_⟩
      · simp only [List.length_cons]; nlinarith
      · simpa only [search,flatten,List.flatMap_cons,consumed,remaining,found,ite_eq_right h,List.reverse_append,List.append_assoc] using hex
end WLTime.SparseLookup

end OAI