File size: 19,057 Bytes
760f90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Computability.Scheduling.TreeData

namespace OAI

section

namespace ThreeMachine.StackCompiler

structure Packed (k : ℕ) where
  Control : Type
  finiteControl : Fintype Control
  program : Program k Control

attribute [instance] Packed.finiteControl

namespace Packed
variable {k : ℕ}

def ofProgram {Q : Type} [Fintype Q] (p : Program k Q) : Packed k := ⟨Q,inferInstance,p⟩
def seq (P P' : Packed k) : Packed k := ofProgram (Program.seq P.program P'.program)
def branch (test : Heads k → Bool) (P P' : Packed k) : Packed k :=
  ofProgram (Program.branch test P.program P'.program)
def loop (test : Heads k → Bool) (P : Packed k) : Packed k :=
  ofProgram (Program.loop test P.program)

def Runs (P : Packed k) := P.program.Runs

theorem Runs.seq {P P' : Packed k} {s t u : Store k} {n m : ℕ}
    (h : P.Runs s t n) (h' : P'.Runs t u m) : (P.seq P').Runs s u (n+m+1) :=
  Program.Runs.seq h h'

theorem Runs.mono {P : Packed k} {s t : Store k} {n m : ℕ}
    (h : P.Runs s t n) (hnm : n ≤ m) : P.Runs s t m := Program.Runs.mono h hnm

end Packed

namespace Tree
variable {r : ℕ}
abbrev Registers (r : ℕ) := Fin r → Data

def reg (i : Fin r) : Fin (r+4) := i.castAdd 4
def temp (i : Fin 4) : Fin (r+4) := i.natAdd r

def memory (s : Registers r) : Store (r+3) :=
  Fin.append (m := r) (n := 4) (fun i : Fin r => (s i).encode) (fun _ : Fin 4 => [])

@[simp] theorem memory_reg (s : Registers r) (i : Fin r) : memory s (reg i) = (s i).encode :=
  Fin.append_left _ _ _
@[simp] theorem memory_temp (s : Registers r) (i : Fin 4) : memory s (temp i) = [] :=
  Fin.append_right _ _ _
@[simp] theorem reg_ne_temp (i : Fin r) (j : Fin 4) : reg i ≠ temp j := by
  intro he
  have hv := congrArg Fin.val he
  simp only [reg,temp,Fin.val_castAdd,Fin.val_natAdd] at hv
  omega
@[simp] theorem temp_ne_reg (i : Fin r) (j : Fin 4) : temp j ≠ reg i := (reg_ne_temp i j).symm
@[simp] theorem reg_eq_reg (i j : Fin r) : reg i = reg j ↔ i = j := Fin.castAdd_inj
@[simp] theorem temp_eq_temp (i j : Fin 4) : (temp i : Fin (r+4)) = temp j ↔ i = j := Fin.natAdd_inj r

theorem memory_update (s : Registers r) (dst : Fin r) (d : Data) :
    memory (Function.update s dst d) = Function.update (memory s) (reg dst) d.encode := by
  funext i
  refine Fin.addCases (m := r) (n := 4) (fun i => ?_) (fun i => ?_) i
  · change memory _ (reg i) = Function.update (memory s) (reg dst) d.encode (reg i)
    by_cases hi : i = dst <;> simp [hi]
  · change memory _ (temp i) = Function.update (memory s) (reg dst) d.encode (temp i)
    simp

def nil (dst : Fin r) : Packed (r+3) := Packed.ofProgram (Program.setNil (reg dst))
def pair (dst a b : Fin r) : Packed (r+3) :=
  Packed.ofProgram (Program.setPair (reg dst) (reg a) (reg b) (temp 0) (temp 1))

theorem nil_runs (dst : Fin r) (s : Registers r) :
    (nil dst).Runs (memory s) (memory (Function.update s dst .nil)) (3*(s dst).size+2) := by
  rw [memory_update]
  simpa only [nil,Packed.Runs,Packed.ofProgram,memory_reg,Data.size] using Program.setNil_runs (reg dst) (memory s)

theorem pair_runs (dst a b : Fin r) (s : Registers r) :
    (pair dst a b).Runs (memory s) (memory (Function.update s dst (.pair (s a) (s b))))
      (12*(s a).size+12*(s b).size+3*(s dst).size+14) := by
  rw [memory_update]
  simpa only [pair,Packed.Runs,Packed.ofProgram,memory_reg,Data.size,Data.encode_pair,List.cons_append] using
    Program.setPair_runs (dst := reg dst) (a := reg a) (b := reg b) (tmp := temp 0) (buf := temp 1)
      (by simp) (by simp) (by simp) (by simp) (by simp) (by simp) (by simp)
      (memory s) (by simp) (by simp)

end Tree
end ThreeMachine.StackCompiler

namespace ThreeMachine.StackCompiler.Tree
variable {r : ℕ}

def work (s : Registers r) (t : Fin 4 → List Bool) : Store (r+3) :=
  Fin.append (m := r) (n := 4) (fun i : Fin r => (s i).encode) t

@[simp] theorem work_reg (s : Registers r) (t : Fin 4 → List Bool) (i : Fin r) :
    work s t (reg i) = (s i).encode := Fin.append_left _ _ _
@[simp] theorem work_temp (s : Registers r) (t : Fin 4 → List Bool) (i : Fin 4) :
    work s t (temp i) = t i := Fin.append_right _ _ _

theorem work_update_temp (s : Registers r) (t : Fin 4 → List Bool) (i : Fin 4) (word : List Bool) :
    Function.update (work s t) (temp i) word = work s (Function.update t i word) := by
  funext j
  refine Fin.addCases (m := r) (n := 4) (fun j => ?_) (fun j => ?_) j
  · change Function.update (work s t) (temp i) word (reg j) = work _ _ (reg j)
    simp
  · change Function.update (work s t) (temp i) word (temp j) = work _ _ (temp j)
    by_cases hj : j = i <;> simp [hj]

theorem work_update_reg (s : Registers r) (t : Fin 4 → List Bool) (i : Fin r) (d : Data) :
    Function.update (work s t) (reg i) d.encode = work (Function.update s i d) t := by
  funext j
  refine Fin.addCases (m := r) (n := 4) (fun j => ?_) (fun j => ?_) j
  · change Function.update (work s t) (reg i) d.encode (reg j) = work _ _ (reg j)
    by_cases hj : j = i <;> simp [hj]
  · change Function.update (work s t) (reg i) d.encode (temp j) = work _ _ (temp j)
    simp

@[simp] theorem work_empty (s : Registers r) : work s ![[],[],[],[]] = memory s := by
  have he : (![[],[],[],[]] : Fin 4 → List Bool) = fun _ => [] := by
    funext i; fin_cases i <;> rfl
  simp only [work,memory,he]

def selectFinish (head : Bool) : Packed (r+3) :=
  if head then Packed.ofProgram (Program.seq (Program.clear (temp 0)) (Program.transfer (temp 2) (temp 0)))
  else Packed.ofProgram (Program.seq (Program.clear (temp 2)) (Program.clear (temp 2)))

def selectPair (head : Bool) (dst : Fin r) : Packed (r+3) :=
  (Packed.ofProgram (Program.instruction (fun _ => Program.popActions (temp 0)))).seq
  ((Packed.ofProgram (Program.push (temp 3) true)).seq
  ((Packed.ofProgram (Program.scan (temp 0) (temp 2) (temp 3))).seq
  ((selectFinish head).seq (Packed.ofProgram (Program.move (temp 0) (reg dst) (temp 1))))))

def selectAux (head : Bool) (dst : Fin r) : Packed (r+3) :=
  Packed.branch (fun h => (h (temp 0)).getD false) (selectPair head dst)
    (Packed.ofProgram (Program.move (temp 0) (reg dst) (temp 1)))

def select (head : Bool) (dst src : Fin r) : Packed (r+3) :=
  (Packed.ofProgram (Program.copy (reg src) (temp 0) (temp 1))).seq (selectAux head dst)

theorem selectFinish_runs (head : Bool) (s : Registers r) (a b : Data) :
    (selectFinish head).Runs (work s ![b.encode,[],a.encode.reverse,[]])
      (work s ![(if head then a else b).encode,[],[],[]]) (3*a.size+3*b.size+2) := by
  cases head with
  | false =>
    have h₁ := (Program.clear_exec (temp 2) (work s ![b.encode,[],a.encode.reverse,[]])).runs
    rw [work_update_temp] at h₁
    have he : Function.update ![b.encode,[],a.encode.reverse,[]] (2 : Fin 4) [] = ![b.encode,[],[],[]] := by
      funext i; fin_cases i <;> simp
    rw [he] at h₁
    have h₂ := (Program.clear_exec (temp 2) (work s ![b.encode,[],[],[]])).runs
    rw [work_update_temp] at h₂
    have he₂ : Function.update ![b.encode,[],[],[]] (2 : Fin 4) [] = ![b.encode,[],[],[]] := by
      funext i; fin_cases i <;> simp
    rw [he₂] at h₂
    have hx := h₁.seq h₂
    exact hx.mono (by simp [Data.size]; omega)
  | true =>
    have h₁ := (Program.clear_exec (temp 0) (work s ![b.encode,[],a.encode.reverse,[]])).runs
    rw [work_update_temp] at h₁
    have he : Function.update ![b.encode,[],a.encode.reverse,[]] (0 : Fin 4) [] = ![[],[],a.encode.reverse,[]] := by
      funext i; fin_cases i <;> simp
    rw [he] at h₁
    have h₂ := (Program.transfer_exec (show (temp 2 : Fin (r+4)) ≠ temp 0 by simp)
      (work s ![[],[],a.encode.reverse,[]])).runs
    have he₂ : Program.transferResult (temp 2) (temp 0) (work s ![[],[],a.encode.reverse,[]]) =
        work s ![a.encode,[],[],[]] := by
      simp only [Program.transferResult,work_temp,Matrix.cons_val_two,Matrix.cons_val_zero,
        List.append_nil,work_update_temp]
      congr 1
      funext i; fin_cases i <;> simp
    rw [he₂] at h₂
    exact (h₁.seq h₂).mono (by simp [Data.size]; omega)

end ThreeMachine.StackCompiler.Tree

namespace ThreeMachine.StackCompiler.Tree
variable {r : ℕ}

theorem selectPair_runs (head : Bool) (dst : Fin r) (s : Registers r) (a b : Data) :
    (selectPair head dst).Runs (work s ![(Data.pair a b).encode,[],[],[]])
      (memory (Function.update s dst (if head then a else b)))
      (30*(a.size+b.size+(s dst).size+1)) := by
  let s₀ := work s ![(Data.pair a b).encode,[],[],[]]
  let s₁ := work s ![a.encode++b.encode,[],[],[]]
  let s₂ := work s ![a.encode++b.encode,[],[],[true]]
  let s₃ := work s ![b.encode,[],a.encode.reverse,[]]
  let s₄ := work s ![(if head then a else b).encode,[],[],[]]
  have h₁ : (Packed.ofProgram (Program.instruction (fun _ => Program.popActions (temp 0)))).Runs s₀ s₁ 1 := by
    have hh := (Program.pop_instruction (temp 0) s₀).runs
    have he : Function.update s₀ (temp 0) (s₀ (temp 0)).tail = s₁ := by
      simp only [s₀,s₁,work_temp,Matrix.cons_val_zero,Data.encode_pair,work_update_temp]
      congr 1
      funext i; fin_cases i <;> simp
    rwa [he] at hh
  have h₂ : (Packed.ofProgram (Program.push (temp 3) true)).Runs s₁ s₂ 1 := by
    have hh := (Program.push_exec (temp 3) true s₁).runs
    have he : Function.update s₁ (temp 3) (true :: s₁ (temp 3)) = s₂ := by
      simp only [s₁,s₂,work_temp,Matrix.cons_val_three,work_update_temp]
      congr 1
      funext i; fin_cases i <;> simp
    rwa [he] at hh
  have h₃ : (Packed.ofProgram (Program.scan (temp 0) (temp 2) (temp 3))).Runs s₂ s₃ (3*a.size) := by
    have hh := (Program.scan_tree (src := temp 0) (dst := temp 2) (ct := temp 3)
      (by simp) (by simp) (by simp) a s₂ b.encode (by simp [s₂]) (by simp [s₂])).runs
    have he : Program.scanned (temp 0) (temp 2) (temp 3) s₂ b.encode a.encode 0 = s₃ := by
      simp only [Program.scanned,s₂,s₃,work_temp,Matrix.cons_val_two,
        List.replicate_zero,work_update_temp]
      congr 1
      funext i; fin_cases i <;> simp
    rwa [he] at hh
  have h₄ : (selectFinish head).Runs s₃ s₄ (3*a.size+3*b.size+2) :=
    selectFinish_runs head s a b
  have h₅ : (Packed.ofProgram (Program.move (temp 0) (reg dst) (temp 1))).Runs s₄
      (memory (Function.update s dst (if head then a else b)))
      (3*(s dst).size+6*(if head then a else b).size+2) := by
    have hh := (Program.move_exec (src := temp 0) (dst := reg dst) (tmp := temp 1)
      (by simp) (by simp) (by simp) s₄ (by simp [s₄])).runs
    have he : Function.update s₄ (temp 0) [] = memory s := by
      simp only [s₄,work_update_temp]
      have hw : Function.update ![(if head then a else b).encode,[],[],[]] (0 : Fin 4) [] = ![[],[],[],[]] := by
        funext i; fin_cases i <;> simp
      rw [hw,work_empty]
    rw [he] at hh
    simpa only [Packed.Runs,Packed.ofProgram,s₄,work_reg,work_temp,Matrix.cons_val_zero,← memory_update,Data.size] using hh
  apply (h₁.seq (h₂.seq (h₃.seq (h₄.seq h₅)))).mono
  cases head <;> simp only [Bool.false_eq_true,ite_false,ite_true] <;> omega

theorem selectAux_runs (head : Bool) (dst : Fin r) (s : Registers r) (d : Data) :
    (selectAux head dst).Runs (work s ![d.encode,[],[],[]])
      (memory (Function.update s dst (if head then d.head else d.tail)))
      (40*(d.size+(s dst).size+1)) := by
  cases d with
  | nil =>
    have hh := (Program.move_exec (src := temp 0) (dst := reg dst) (tmp := temp 1)
      (by simp) (by simp) (by simp) (work s ![Data.nil.encode,[],[],[]]) (by simp)).runs
    have he : Function.update (work s ![Data.nil.encode,[],[],[]]) (temp 0) [] = memory s := by
      rw [work_update_temp]
      have hw : Function.update ![Data.nil.encode,[],[],[]] (0 : Fin 4) [] = ![[],[],[],[]] := by
        funext i; fin_cases i <;> simp
      rw [hw,work_empty]
    rw [he] at hh
    have hx := hh.branch_right (fun h => (h (temp 0)).getD false) (selectPair head dst).program (by simp)
    have hout : (if head then Data.nil.head else Data.nil.tail) = Data.nil := by cases head <;> rfl
    rw [hout,memory_update]
    simpa only [selectAux,Packed.Runs,Packed.branch,Packed.ofProgram,work_temp,Matrix.cons_val_zero] using
      hx.mono (m := 40*(Data.nil.size+(s dst).size+1)) (by simp [Data.size]; omega)
  | pair a b =>
    have hh := selectPair_runs head dst s a b
    have hx := Program.Runs.branch_left (fun h => (h (temp 0)).getD false)
      (Program.move (temp 0) (reg dst) (temp 1)) hh (by simp [Data.encode_pair])
    exact hx.mono (by simp [Data.size_pair]; omega)

theorem select_runs (head : Bool) (dst src : Fin r) (s : Registers r) :
    (select head dst src).Runs (memory s)
      (memory (Function.update s dst (if head then (s src).head else (s src).tail)))
      (50*((s src).size+(s dst).size+1)) := by
  have h₁ := (Program.copy_exec (src := reg src) (dst := temp 0) (tmp := temp 1)
    (by simp) (by simp) (by simp) (memory s) (by simp)).runs
  have he : Function.update (memory s) (temp 0) (memory s (reg src)) = work s ![(s src).encode,[],[],[]] := by
    rw [memory_reg,← work_empty,work_update_temp]
    congr 1
    funext i; fin_cases i <;> simp
  rw [he] at h₁
  have h₂ := selectAux_runs head dst s (s src)
  exact (h₁.seq h₂).mono (by simp [Data.size]; omega)

end ThreeMachine.StackCompiler.Tree

namespace ThreeMachine.StackCompiler.Program
variable {k : ℕ} {Q : Type}

theorem Runs.loop_stop (test : Heads k → Bool) (P : Program k Q) (s : Store k)
    (hs : test (fun i => (s i).head?) = false) : (loop test P).Runs s s 0 :=
  (Program.loop_stop test P s hs).runs

theorem Runs.loop_iterate (test : Heads k → Bool) {P : Program k Q}
    {s t u : Store k} {n m : ℕ} (h : P.Runs s t n) (h' : (loop test P).Runs t u m)
    (hs : test (fun i => (s i).head?) = true) : (loop test P).Runs s u (n+m+2) := by
  obtain ⟨n',hn,q,hq⟩ := h
  obtain ⟨m',hm,r,hr⟩ := h'
  have hnone : r = none := by
    have hh := hr.halted
    cases r with
    | none => rfl
    | some q =>
      unfold step at hh
      cases he : P.transition q (fun i => (u i).head?) <;> simp_all [loop]
  subst r
  exact (Program.loop_iterate test hs hq hr).runs.mono (by omega)

end ThreeMachine.StackCompiler.Program

namespace ThreeMachine.StackCompiler.Tree
variable {r : ℕ}

def skip : Packed (r+3) := Packed.ofProgram (Program.instruction (fun _ => Program.stayActions))

theorem skip_runs (s : Registers r) : skip.Runs (memory s) (memory s) 1 := by
  have hh := (Program.instruction_exec (k := r+3) (fun _ => Program.stayActions) (memory s)).runs
  simpa only [skip,Packed.Runs,Packed.ofProgram,Program.stayActions,Action.apply] using hh

def copy (dst src : Fin r) : Packed (r+3) :=
  if dst = src then skip else Packed.ofProgram (Program.copy (reg src) (reg dst) (temp 0))

theorem copy_runs (dst src : Fin r) (s : Registers r) :
    (copy dst src).Runs (memory s) (memory (Function.update s dst (s src)))
      (3*(s dst).size+6*(s src).size+2) := by
  by_cases he : dst = src
  · subst src
    simpa [copy] using (skip_runs s).mono (by omega : 1 ≤ 3*(s dst).size+6*(s dst).size+2)
  · rw [memory_update]
    unfold copy
    rw [ite_eq_right he]
    have hh := (Program.copy_exec (src := reg src) (dst := reg dst) (tmp := temp 0)
      (by simpa using Ne.symm he) (by simp) (by simp) (memory s) (by simp)).runs
    simpa only [memory_reg,Data.size,Packed.Runs,Packed.ofProgram] using hh

def weight (s : Registers r) : ℕ := 1 + ∑ i, (s i).size

theorem one_le_weight (s : Registers r) : 1 ≤ weight s := by unfold weight; omega

theorem size_le_weight (s : Registers r) (i : Fin r) : (s i).size ≤ weight s := by
  have hh : (s i).size ≤ ∑ j, (s j).size := Finset.single_le_sum (f := fun j : Fin r => (s j).size) (fun _ _ => Nat.zero_le _) (Finset.mem_univ i)
  unfold weight
  omega

inductive Code (V : Type)
  | skip
  | nil (dst : V)
  | pair (dst a b : V)
  | select (head : Bool) (dst src : V)
  | copy (dst src : V)
  | seq (first second : Code V)
  | branch (src : V) (nonempty empty : Code V)
  | loop (src : V) (body : Code V)

def _root_.OAI.ThreeMachine.StackCompiler.Data.nonempty : Data → Bool | .nil => false | .pair _ _ => true

@[simp] theorem _root_.OAI.ThreeMachine.StackCompiler.Data.encode_head (d : Data) : d.encode.head?.getD false = d.nonempty := by
  cases d <;> rfl

inductive Eval {V : Type} [DecidableEq V] : Code V → (V → Data) → (V → Data) → ℕ → Prop
  | skip (s) : Eval .skip s s 1
  | nil (dst) (s) : Eval (.nil dst) s (Function.update s dst .nil) (1+(s dst).size)
  | pair (dst a b) (s) : Eval (.pair dst a b) s (Function.update s dst (.pair (s a) (s b))) (1+(s dst).size+(s a).size+(s b).size)
  | select (head dst src) (s) :
      Eval (.select head dst src) s (Function.update s dst (if head then (s src).head else (s src).tail)) (1+(s dst).size+(s src).size)
  | copy (dst src) (s) : Eval (.copy dst src) s (Function.update s dst (s src)) (1+(s dst).size+(s src).size)
  | seq {p q s t u n m} : Eval p s t n → Eval q t u m → Eval (.seq p q) s u (n+m+1)
  | branch_true {src p q s t n} : (s src).nonempty = true → Eval p s t n → Eval (.branch src p q) s t (n+1)
  | branch_false {src p q s t n} : (s src).nonempty = false → Eval q s t n → Eval (.branch src p q) s t (n+1)
  | loop_stop {src p s} : (s src).nonempty = false → Eval (.loop src p) s s 1
  | loop_step {src p s t u n m} : (s src).nonempty = true → Eval p s t n →
      Eval (.loop src p) t u m → Eval (.loop src p) s u (n+m+1)

namespace Code

def compile : Code (Fin r) → Packed (r+3)
  | .skip => Tree.skip
  | .nil dst => Tree.nil dst
  | .pair dst a b => Tree.pair dst a b
  | .select head dst src => Tree.select head dst src
  | .copy dst src => Tree.copy dst src
  | .seq p q => p.compile.seq q.compile
  | .branch src p q => Packed.branch (fun h => (h (reg src)).getD false) p.compile q.compile
  | .loop src p => Packed.loop (fun h => (h (reg src)).getD false) p.compile

end Code

theorem Eval.compile {p : Code (Fin r)} {s t : Registers r} {n : ℕ} (h : Eval p s t n) :
    p.compile.Runs (memory s) (memory t) (150*n) := by
  induction h with
  | skip s => exact (skip_runs s).mono (by omega)
  | nil dst s =>
    apply (nil_runs dst s).mono
    omega
  | pair dst a b s =>
    apply (pair_runs dst a b s).mono
    omega
  | select head dst src s =>
    apply (select_runs head dst src s).mono
    omega
  | copy dst src s =>
    apply (copy_runs dst src s).mono
    omega
  | seq _ _ ih ih' => exact (ih.seq ih').mono (by omega)
  | @branch_true src p q s t n hs _ ih =>
    exact (Program.Runs.branch_left _ q.compile.program ih (by simpa using hs)).mono (by omega)
  | @branch_false src p q s t n hs _ ih =>
    exact (Program.Runs.branch_right _ p.compile.program ih (by simpa using hs)).mono (by omega)
  | @loop_stop src p s hs =>
    exact (Program.Runs.loop_stop _ p.compile.program (memory s) (by simpa using hs)).mono (by omega)
  | @loop_step src p s t u n m hs _ _ ih ih' =>
    exact (Program.Runs.loop_iterate _ ih ih' (by simpa using hs)).mono (by omega)

end ThreeMachine.StackCompiler.Tree

end

end OAI