File size: 20,010 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
import OAI.Computability.WeisfeilerLeman.TapeCompiler

namespace OAI

universe uK uA uS uT

namespace WLTime.TypedStack

inductive Command (K : Type uK) (A : Type uA) (S : Type uS) where
  | next (q : S)
  | push (k : K) (x : A) (q : S)
  | pop (k : K) (q : S)
  | halt (b : Bool)

structure Machine (K : Type uK) (A : Type uA) (S : Type uS) where
  inputStack : K
  inputLetter : Bool → A
  initial : S
  transition : S → (K → Option A) → Command K A S

structure Config (K : Type uK) (A : Type uA) (S : Type uS) where
  state : Bool ⊕ S
  stack : K → List A

namespace Machine
variable {K : Type uK} {A : Type uA} {S : Type uS} [DecidableEq K] (M : Machine K A S)

 def perform (_M : Machine K A S) (C : Config K A S) (a : Command K A S) : Config K A S := match a with
  | .next q => { C with state := .inr q }
  | .push k x q => ⟨.inr q,Function.update C.stack k (x::C.stack k)⟩
  | .pop k q => ⟨.inr q,Function.update C.stack k (C.stack k).tail⟩
  | .halt b => { C with state := .inl b }
 def step (C : Config K A S) : Config K A S := match C.state with
  | .inl _ => C
  | .inr q => M.perform C (M.transition q (fun k => (C.stack k).head?))
 def initialConfig (w : List Bool) : Config K A S :=
  ⟨.inr M.initial,fun k => if k=M.inputStack then w.map M.inputLetter else []⟩
 def run (w : List Bool) (t : ℕ) := M.step^[t] (M.initialConfig w)

variable [Fintype K] [Fintype A] [Fintype S]

noncomputable def registerEquiv : K ≃ Fin (Fintype.card K) :=
  (Equiv.swap M.inputStack ((Fintype.equivFin K).symm ⟨0,Fintype.card_pos_iff.mpr ⟨M.inputStack⟩⟩)).trans
    (Fintype.equivFin K)
omit [Fintype A] [Fintype S] in
@[simp] theorem register_input : (M.registerEquiv M.inputStack).val=0 := by
  simp [registerEquiv]

noncomputable def decodeSymbol (x : Fin (2+Fintype.card A)) : A :=
  match finSumFinEquiv.symm x with
  | .inl b => M.inputLetter (b.val=1)
  | .inr a => (Fintype.equivFin A).symm a

noncomputable def encodeSymbol (_M : Machine K A S) (x : A) : Fin (2+Fintype.card A) :=
  finSumFinEquiv (.inr (Fintype.equivFin A x))

omit [Fintype K] [Fintype S] [DecidableEq K] in
@[simp] theorem decode_encode (x : A) : M.decodeSymbol (M.encodeSymbol x)=x := by
  simp [decodeSymbol,encodeSymbol]

noncomputable def encodeCommand : Command K A S →
    StackMachine.Command (Fintype.card K) (2+Fintype.card A) (Fintype.card S)
  | .next q => .next (Fintype.equivFin S q)
  | .push k x q => .push (M.registerEquiv k) (M.encodeSymbol x) (Fintype.equivFin S q)
  | .pop k q => .pop (M.registerEquiv k) (Fintype.equivFin S q)
  | .halt b => .halt b

noncomputable abbrev compile : StackMachine.Machine where
  count := Fintype.card K
  alphabet := 2+Fintype.card A
  states := Fintype.card S
  count_pos := Fintype.card_pos_iff.mpr ⟨M.inputStack⟩
  alphabet_ge := by omega
  initial := Fintype.equivFin S M.initial
  transition q v := M.encodeCommand
    (M.transition ((Fintype.equivFin S).symm q) (fun k => (v (M.registerEquiv k)).map M.decodeSymbol))

noncomputable def project (C : M.compile.Config) : Config K A S where
  state := C.state.map id (Fintype.equivFin S).symm
  stack k := (C.stack (M.registerEquiv k)).map M.decodeSymbol

 theorem project_perform (C : M.compile.Config) (a : Command K A S) :
    M.project (M.compile.perform C (M.encodeCommand a))=M.perform (M.project C) a := by
  cases a with
  | next q => simp only [encodeCommand,StackMachine.Machine.perform,project,Sum.map_inr,
      Equiv.symm_apply_apply,perform]
  | halt b => rfl
  | push k x q =>
    simp only [encodeCommand,StackMachine.Machine.perform,project,Sum.map_inr,
      Equiv.symm_apply_apply,perform]
    congr 1
    funext j
    by_cases hj : j=k
    · subst j
      simp only [Function.update_self,List.map_cons,M.decode_encode]
    · have hn : M.registerEquiv j≠M.registerEquiv k := fun hh => hj (M.registerEquiv.injective hh)
      simp only [Function.update_of_ne hj,Function.update_of_ne hn]
  | pop k q =>
    simp only [encodeCommand,StackMachine.Machine.perform,project,Sum.map_inr,
      Equiv.symm_apply_apply,perform]
    congr 1
    funext j
    by_cases hj : j=k
    · subst j
      simp only [Function.update_self,List.map_tail]
    · have hn : M.registerEquiv j≠M.registerEquiv k := fun hh => hj (M.registerEquiv.injective hh)
      simp only [Function.update_of_ne hj,Function.update_of_ne hn]

 theorem project_step (C : M.compile.Config) : M.project (M.compile.step C)=M.step (M.project C) := by
  obtain ⟨p,s⟩ := C
  cases p with
  | inl b => rfl
  | inr q =>
    change M.project (M.compile.perform _ (M.encodeCommand _))=_
    rw [M.project_perform]
    simp only [step,project,Sum.map_inr,List.head?_map]

 theorem project_initial (w : List Bool) : M.project (M.compile.initialConfig w)=M.initialConfig w := by
  have hi (b : Bool) : M.decodeSymbol (M.compile.inputLetter b)=M.inputLetter b := by
    cases b <;> rfl
  simp only [project,StackMachine.Machine.initialConfig,Sum.map_inr,Equiv.symm_apply_apply,initialConfig]
  congr 1
  funext k
  have hk : (M.registerEquiv k).val=0 ↔ k=M.inputStack := by
    rw [← M.register_input]
    exact Fin.ext_iff.symm.trans (M.registerEquiv.injective.eq_iff)
  simp only [hk]
  split_ifs
  · simp only [List.map_map]
    exact List.map_congr_left (fun b _ => hi b)
  · rfl

 theorem project_run (w : List Bool) (t : ℕ) : M.project (M.compile.run w t)=M.run w t := by
  induction t with
  | zero => exact M.project_initial w
  | succ t ih =>
    simp only [run,StackMachine.Machine.run,Function.iterate_succ_apply'] at *
    rw [M.project_step,ih]

 theorem compile_halt (w : List Bool) (t : ℕ) (b : Bool)
    (h : (M.run w t).state=.inl b) : (M.compile.run w t).state=.inl b := by
  rw [← M.project_run] at h
  cases he : (M.compile.run w t).state with
  | inl b' => simpa only [project,he,Sum.map_inl,id_eq,Sum.inl.injEq] using h
  | inr q => simp only [project,he,Sum.map_inr,reduceCtorEq] at h

 theorem oneTape_correct (w : List Bool) (t : ℕ) (b : Bool)
    (h : (M.run w t).state=.inl b) :
    M.compile.oneTape.toTM.halts w ∧
      M.compile.oneTape.toTM.time w ≤ 8*(Fintype.card K+1)*(w.length+t+1)^2 ∧
      (M.compile.oneTape.toTM.accepts w ↔ b=true) :=
  M.compile.oneTape_correct w t b (M.compile_halt w t b h)

end Machine
end WLTime.TypedStack

namespace WLTime.TypedStack
namespace Command
variable {K : Type uK} {A : Type uA} {S : Type uS} {T : Type uT}
 def rename (f : S → T) : Command K A S → Command K A T
  | .next q => .next (f q)
  | .push k x q => .push k x (f q)
  | .pop k q => .pop k (f q)
  | .halt b => .halt b
 def redirect (f : S → T) (r : T) : Command K A S → Command K A T
  | .next q => .next (f q)
  | .push k x q => .push k x (f q)
  | .pop k q => .pop k (f q)
  | .halt _ => .next r
end Command

variable {K : Type uK} {A : Type uA} {S : Type uS} {T : Type uT} [DecidableEq K]
abbrev Stacks (K : Type uK) (A : Type uA) := K → List A
abbrev Tops (K : Type uK) (A : Type uA) := K → Option A
 def tops (s : Stacks K A) : Tops K A := fun k => (s k).head?

inductive Execution (tr : S → Tops K A → Command K A S) :
    S → Stacks K A → ℕ → Bool → Stacks K A → Prop
  | halt {q s b} (h : tr q (tops s)=.halt b) : Execution tr q s 1 b s
  | next {q s q' t b out} (h : tr q (tops s)=.next q')
      (tail : Execution tr q' s t b out) : Execution tr q s (t+1) b out
  | push {q s k x q' t b out} (h : tr q (tops s)=.push k x q')
      (tail : Execution tr q' (Function.update s k (x::s k)) t b out) :
      Execution tr q s (t+1) b out
  | pop {q s k q' t b out} (h : tr q (tops s)=.pop k q')
      (tail : Execution tr q' (Function.update s k (s k).tail) t b out) :
      Execution tr q s (t+1) b out

namespace Execution
 theorem iterate (M : Machine K A S) {q s t b out} (h : Execution M.transition q s t b out) :
    M.step^[t] ⟨.inr q,s⟩=⟨.inl b,out⟩ := by
  induction h with
  | @halt q s b h =>
    change M.perform ⟨.inr q,s⟩ (M.transition q (tops s))=⟨.inl b,s⟩
    rw [h]
    rfl
  | @next q s q' t b out h tail ih =>
    rw [Function.iterate_succ_apply]
    change M.step^[t] (M.perform ⟨.inr q,s⟩ (M.transition q (tops s)))=⟨.inl b,out⟩
    rw [h]
    exact ih
  | @push q s k x q' t b out h tail ih =>
    rw [Function.iterate_succ_apply]
    change M.step^[t] (M.perform ⟨.inr q,s⟩ (M.transition q (tops s)))=⟨.inl b,out⟩
    rw [h]
    exact ih
  | @pop q s k q' t b out h tail ih =>
    rw [Function.iterate_succ_apply]
    change M.step^[t] (M.perform ⟨.inr q,s⟩ (M.transition q (tops s)))=⟨.inl b,out⟩
    rw [h]
    exact ih

 theorem rename {tr : S → Tops K A → Command K A S} {tr' : T → Tops K A → Command K A T}
    (f : S → T) (hf : ∀ q v,tr' (f q) v=Command.rename f (tr q v))
    {q s t b out} (h : Execution tr q s t b out) : Execution tr' (f q) s t b out := by
  induction h with
  | halt h => exact .halt (by rw [hf,h]; rfl)
  | next h tail ih => exact .next (by rw [hf,h]; rfl) ih
  | push h tail ih => exact .push (by rw [hf,h]; rfl) ih
  | pop h tail ih => exact .pop (by rw [hf,h]; rfl) ih

 theorem plug {tr : S → Tops K A → Command K A S} {tr' : T → Tops K A → Command K A T}
    (f : S → T) (r : T) (hf : ∀ q v,tr' (f q) v=Command.redirect f r (tr q v))
    {q s t b mid} (h : Execution tr q s t b mid)
    {u c out} (cont : Execution tr' r mid u c out) : Execution tr' (f q) s (t+u) c out := by
  induction h with
  | halt h =>
    simpa only [Nat.add_comm] using Execution.next (by rw [hf,h]; rfl) cont
  | next h tail ih =>
    simpa only [Nat.add_right_comm] using Execution.next (by rw [hf,h]; rfl) (ih cont)
  | push h tail ih =>
    simpa only [Nat.add_right_comm] using Execution.push (by rw [hf,h]; rfl) (ih cont)
  | pop h tail ih =>
    simpa only [Nat.add_right_comm] using Execution.pop (by rw [hf,h]; rfl) (ih cont)
end Execution

inductive Code (K : Type uK) (A : Type uA) where
  | skip
  | push (k : K) (x : A)
  | pushDyn (k : K) (letter : Tops K A → A)
  | pop (k : K)
  | seq (left right : Code K A)
  | branch (test : Tops K A → Bool) (yes no : Code K A)
  | loop (test : Tops K A → Bool) (body : Code K A)

namespace Code
 @[reducible] def State : Code K A → Type
  | .skip => Unit
  | .push _ _ => Fin 2
  | .pushDyn _ _ => Fin 2
  | .pop _ => Fin 2
  | .seq a b => a.State ⊕ b.State
  | .branch _ a b => Unit ⊕ (a.State ⊕ b.State)
  | .loop _ a => Unit ⊕ a.State

noncomputable instance stateFintype (c : Code K A) : Fintype c.State := by
  induction c with
  | skip => exact inferInstanceAs (Fintype Unit)
  | push k x => exact inferInstanceAs (Fintype (Fin 2))
  | pushDyn k f => exact inferInstanceAs (Fintype (Fin 2))
  | pop k => exact inferInstanceAs (Fintype (Fin 2))
  | seq a b ha hb => exact inferInstanceAs (Fintype (a.State ⊕ b.State))
  | branch p a b ha hb => exact inferInstanceAs (Fintype (Unit ⊕ (a.State ⊕ b.State)))
  | loop p a ha => exact inferInstanceAs (Fintype (Unit ⊕ a.State))

 def entry : (c : Code K A) → c.State
  | .skip => ()
  | .push _ _ => 0
  | .pushDyn _ _ => 0
  | .pop _ => 0
  | .seq a _ => .inl a.entry
  | .branch _ _ _ => .inl ()
  | .loop _ _ => .inl ()

 def transition : (c : Code K A) → c.State → Tops K A → Command K A c.State
  | .skip,_,_ => .halt false
  | .push k x,q,_ => if q=0 then .push k x 1 else .halt false
  | .pushDyn k f,q,v => if q=0 then .push k (f v) 1 else .halt false
  | .pop k,q,_ => if q=0 then .pop k 1 else .halt false
  | .seq a b,.inl q,v => Command.redirect Sum.inl (.inr b.entry) (a.transition q v)
  | .seq _ b,.inr q,v => Command.rename Sum.inr (b.transition q v)
  | .branch p a b,.inl _,v => .next (if p v then .inr (.inl a.entry) else .inr (.inr b.entry))
  | .branch _ a _,.inr (.inl q),v => Command.rename (Sum.inr ∘ Sum.inl) (a.transition q v)
  | .branch _ _ b,.inr (.inr q),v => Command.rename (Sum.inr ∘ Sum.inr) (b.transition q v)
  | .loop p a,.inl _,v => if p v then .next (.inr a.entry) else .halt false
  | .loop _ a,.inr q,v => Command.redirect Sum.inr (.inl ()) (a.transition q v)

 def machine (c : Code K A) (k : K) (bits : Bool → A) : Machine K A c.State :=
  ⟨k,bits,c.entry,c.transition⟩

inductive Exec : Code K A → Stacks K A → ℕ → Stacks K A → Prop
  | skip (s) : Exec .skip s 1 s
  | push (s k x) : Exec (.push k x) s 2 (Function.update s k (x::s k))
  | pushDyn (s k f) : Exec (.pushDyn k f) s 2 (Function.update s k (f (tops s)::s k))
  | pop (s k) : Exec (.pop k) s 2 (Function.update s k (s k).tail)
  | seq {a b s mid out t u} (first : Exec a s t mid) (second : Exec b mid u out) :
      Exec (.seq a b) s (t+u) out
  | branch_true {p a b s t out} (test : p (tops s)=true) (body : Exec a s t out) :
      Exec (.branch p a b) s (t+1) out
  | branch_false {p a b s t out} (test : p (tops s)=false) (body : Exec b s t out) :
      Exec (.branch p a b) s (t+1) out
  | loop_false {p a s} (test : p (tops s)=false) : Exec (.loop p a) s 1 s
  | loop_true {p a s mid out t u} (test : p (tops s)=true)
      (body : Exec a s t mid) (rest : Exec (.loop p a) mid u out) :
      Exec (.loop p a) s (t+u+1) out

 theorem exec_finite {c : Code K A} {s t out} (h : c.Exec s t out) :
    Execution c.transition c.entry s t false out := by
  induction h with
  | skip s => exact .halt rfl
  | push s k x =>
    exact .push (q' := (1 : Fin 2)) (by simp [entry,transition]) (.halt (by simp [transition]))
  | pushDyn s k f =>
    exact .push (q' := (1 : Fin 2)) (by simp [entry,transition]) (.halt (by simp [transition]))
  | pop s k =>
    exact .pop (q' := (1 : Fin 2)) (by simp [entry,transition]) (.halt (by simp [transition]))
  | @seq a b s mid out t u first second ih1 ih2 =>
    exact ih1.plug Sum.inl (.inr b.entry) (fun _ _ => rfl) (ih2.rename Sum.inr (fun _ _ => rfl))
  | branch_true hp body ih =>
    exact .next (by simp [entry,transition,hp]) (ih.rename (Sum.inr ∘ Sum.inl) (fun _ _ => rfl))
  | branch_false hp body ih =>
    exact .next (by simp [entry,transition,hp]) (ih.rename (Sum.inr ∘ Sum.inr) (fun _ _ => rfl))
  | loop_false hp => exact .halt (by simp [entry,transition,hp])
  | loop_true hp body rest ih1 ih2 =>
    exact .next (by simp [entry,transition,hp])
      (ih1.plug Sum.inr (.inl ()) (fun _ _ => rfl) ih2)

 theorem exec_iterate (c : Code K A) (k : K) (bits : Bool → A) {s t out} (h : c.Exec s t out) :
    (c.machine k bits).step^[t] ⟨.inr c.entry,s⟩=⟨.inl false,out⟩ :=
  (exec_finite h).iterate (c.machine k bits)

end Code
end WLTime.TypedStack

namespace WLTime.TypedStack.Code
variable {K : Type uK} {A : Type uA} [DecidableEq K]

 def nonempty (k : K) (v : Tops K A) : Bool := (v k).isSome
 def clear (k : K) : Code K A := .loop (nonempty k) (.pop k)
 def transfer (a b : K) (zero : A) : Code K A :=
  .loop (nonempty a) (.seq (.pushDyn b (fun v => (v a).getD zero)) (.pop a))

 theorem clear_exec (k : K) (s : Stacks K A) :
    (clear k).Exec s (3*(s k).length+1) (Function.update s k []) := by
  generalize hx : s k=xs
  induction xs generalizing s with
  | nil =>
    have hs : Function.update s k []=s := by rw [← hx]; exact Function.update_eq_self k s
    rw [hs]
    exact .loop_false (by simp [nonempty,tops,hx])
  | cons x xs ih =>
    have hstep := ih (Function.update s k xs) (by simp)
    have he : (Function.update (Function.update s k xs) k [])=Function.update s k [] := by simp
    rw [he] at hstep
    have first : Exec (.pop k) s 2 (Function.update s k xs) := by
      simpa only [hx,List.tail_cons] using Exec.pop s k
    have hh := Exec.loop_true (p:=nonempty k) (by simp [nonempty,tops,hx]) first hstep
    convert hh using 1 <;> simp [clear]
    omega

 theorem transfer_exec (a b : K) (hab : a≠b) (zero : A) (s : Stacks K A) :
    (transfer a b zero).Exec s (5*(s a).length+1)
      (Function.update (Function.update s a []) b ((s a).reverse++s b)) := by
  generalize hx : s a=xs
  induction xs generalizing s with
  | nil =>
    have hout : Function.update (Function.update s a []) b (([] : List A).reverse++s b)=s := by
      simp only [List.reverse_nil,List.nil_append]
      rw [← hx,Function.update_eq_self]
      exact Function.update_eq_self b s
    rw [hout]
    exact .loop_false (by simp [nonempty,tops,hx])
  | cons x xs ih =>
    let s₁ := Function.update s b (x::s b)
    let s₂ := Function.update s₁ a xs
    have hs₁a : s₁ a=x::xs := by simp [s₁,Function.update_of_ne hab,hx]
    have hs₂a : s₂ a=xs := by simp [s₂]
    have body : Exec (.seq (.pushDyn b (fun v => (v a).getD zero)) (.pop a)) s 4 s₂ := by
      have hp : Exec (.pushDyn b (fun v => (v a).getD zero)) s 2 s₁ := by
        simpa [tops,hx,s₁] using Exec.pushDyn s b (fun v => (v a).getD zero)
      have hq : Exec (.pop a) s₁ 2 s₂ := by
        simpa [hs₁a,s₂] using Exec.pop s₁ a
      exact .seq hp hq
    have hrest := ih s₂ hs₂a
    have hout : Function.update (Function.update s₂ a []) b (xs.reverse++s₂ b)=
        Function.update (Function.update s a []) b ((x::xs).reverse++s b) := by
      funext k
      by_cases hb : k=b
      · subst k
        simp [s₂,s₁,hab.symm,List.reverse_cons,List.append_assoc]
      · by_cases ha : k=a
        · subst k
          simp [hb]
        · simp [s₂,s₁,hb,ha]
    rw [hout] at hrest
    have hh := Exec.loop_true (p:=nonempty a) (by simp [nonempty,tops,hx]) body hrest
    convert hh using 1 <;> simp [transfer]
    omega

 def distribute (a b c : K) (zero : A) : Code K A :=
  .loop (nonempty a) (.seq (.pushDyn b (fun v => (v a).getD zero))
    (.seq (.pushDyn c (fun v => (v a).getD zero)) (.pop a)))

 theorem distribute_exec (a b c : K) (hab : a≠b) (hac : a≠c) (hbc : b≠c)
    (zero : A) (s : Stacks K A) :
    (distribute a b c zero).Exec s (7*(s a).length+1)
      (Function.update (Function.update (Function.update s a []) b ((s a).reverse++s b))
        c ((s a).reverse++s c)) := by
  generalize hx : s a=xs
  induction xs generalizing s with
  | nil =>
    have hout : Function.update (Function.update (Function.update s a []) b ([]++s b)) c ([]++s c)=s := by
      simp only [List.nil_append]
      rw [← hx,Function.update_eq_self,Function.update_eq_self,Function.update_eq_self]
    change Exec _ s 1 (Function.update (Function.update (Function.update s a []) b ([]++s b)) c ([]++s c))
    rw [hout]
    exact .loop_false (by simp [nonempty,tops,hx])
  | cons x xs ih =>
    let s₁ := Function.update s b (x::s b)
    let s₂ := Function.update s₁ c (x::s c)
    let s₃ := Function.update s₂ a xs
    have hs₁a : s₁ a=x::xs := by simp [s₁,hab,hx]
    have hs₁c : s₁ c=s c := by simp [s₁,hbc.symm]
    have hs₂a : s₂ a=x::xs := by simp [s₂,hac,hs₁a]
    have hs₃a : s₃ a=xs := by simp [s₃]
    have first : Exec (.pushDyn b (fun v => (v a).getD zero)) s 2 s₁ := by
      simpa [tops,hx,s₁] using Exec.pushDyn s b (fun v => (v a).getD zero)
    have second : Exec (.pushDyn c (fun v => (v a).getD zero)) s₁ 2 s₂ := by
      simpa [tops,hs₁a,hs₁c,s₂] using Exec.pushDyn s₁ c (fun v => (v a).getD zero)
    have third : Exec (.pop a) s₂ 2 s₃ := by
      simpa [hs₂a,s₃] using Exec.pop s₂ a
    have body := Exec.seq first (Exec.seq second third)
    have hrest := ih s₃ hs₃a
    have hout : Function.update (Function.update (Function.update s₃ a []) b (xs.reverse++s₃ b))
        c (xs.reverse++s₃ c)=Function.update (Function.update (Function.update s a [])
          b ((x::xs).reverse++s b)) c ((x::xs).reverse++s c) := by
      funext k
      by_cases hc : k=c
      · subst k
        simp [s₃,s₂,s₁,hac.symm,List.reverse_cons,List.append_assoc]
      · by_cases hb : k=b
        · subst k
          simp [s₃,s₂,s₁,hab.symm,hbc,List.reverse_cons,List.append_assoc]
        · by_cases ha : k=a
          · subst k
            simp [hc,hb]
          · simp [s₃,s₂,s₁,hc,hb,ha]
    rw [hout] at hrest
    have hh := Exec.loop_true (p:=nonempty a) (by simp [nonempty,tops,hx]) body hrest
    convert hh using 1 <;> simp [distribute]
    omega

end WLTime.TypedStack.Code

end OAI