File size: 17,622 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
import OAI.Computability.Scheduling.RequestCosts

namespace OAI

section

namespace ThreeMachine.StackCompiler.Packed
variable {k : ℕ}

def halt : Packed k := ofProgram (⟨(),fun _ _ => none⟩ : Program k Unit)

theorem halt_runs (s : Store k) : halt.Runs s s 0 :=
  ⟨0,le_rfl,(),Program.Exec.halt rfl⟩

def pushWord (dst : Fin (k+1)) : List Bool → Packed k
  | [] => halt
  | b :: bs => (pushWord dst bs).seq (ofProgram (Program.push dst b))

theorem pushWord_runs (dst : Fin (k+1)) (bs : List Bool) (s : Store k) :
    (pushWord dst bs).Runs s (Function.update s dst (bs ++ s dst)) (2*bs.length) := by
  induction bs with
  | nil => simpa only [pushWord,List.nil_append,Function.update_eq_self,List.length_nil,Nat.mul_zero] using halt_runs s
  | cons b bs ih =>
    have hp : (ofProgram (Program.push dst b)).Runs (Function.update s dst (bs ++ s dst))
        (Function.update (Function.update s dst (bs ++ s dst)) dst (b :: (bs ++ s dst))) 1 := by
      change (Program.push dst b).Runs _ _ _
      simpa only [Function.update_self] using (Program.push_exec dst b (Function.update s dst (bs ++ s dst))).runs
    have h := ih.seq hp
    simpa only [pushWord,List.length_cons,Function.update_self,Function.update_idem,
      List.cons_append,Nat.mul_add,Nat.mul_one,Nat.add_assoc] using h

def pop (src : Fin (k+1)) : Packed k := ofProgram (Program.instruction (fun _ => Program.popActions src))

theorem pop_runs (src : Fin (k+1)) (s : Store k) :
    (pop src).Runs s (Function.update s src (s src).tail) 1 :=
  (Program.pop_instruction src s).runs

def popN (src : Fin (k+1)) : ℕ → Packed k
  | 0 => halt
  | n+1 => (pop src).seq (popN src n)

theorem popN_runs (src : Fin (k+1)) (n : ℕ) (s : Store k) :
    (popN src n).Runs s (Function.update s src ((s src).drop n)) (2*n) := by
  induction n generalizing s with
  | zero => simpa only [popN,List.drop_zero,Function.update_eq_self,Nat.mul_zero] using halt_runs s
  | succ n ih =>
    have h := (pop_runs src s).seq (ih (Function.update s src (s src).tail))
    have h' := h.mono (show 1+2*n+1 ≤ 2*(n+1) by omega)
    simpa only [popN,Function.update_self,Function.update_idem,List.drop_tail] using h'

end ThreeMachine.StackCompiler.Packed

namespace ThreeMachine.StackCompiler.Raw
open Packed
variable {k : ℕ}

def boolChunk (b : Bool) : List Bool := true :: (enc b).encode

theorem encoded_cons (b : Bool) (xs : List Bool) :
    (enc (b :: xs)).encode = boolChunk b ++ (enc xs).encode := rfl

def encodeBody (src dst : Fin (k+1)) : Packed k :=
  Packed.branch (fun h => (h src).getD false)
    ((Packed.pop src).seq (Packed.pushWord dst (boolChunk true)))
    ((Packed.pop src).seq (Packed.pushWord dst (boolChunk false)))

theorem encodeBody_runs {src dst : Fin (k+1)} (hne : src ≠ dst)
    (b : Bool) (bs : List Bool) (s : Store k) (hs : s src = b :: bs) :
    (encodeBody src dst).Runs s
      (Function.update (Function.update s src bs) dst (boolChunk b ++ s dst)) 12 := by
  have h := (Packed.pop_runs src s).seq
    (Packed.pushWord_runs dst (boolChunk b) (Function.update s src (s src).tail))
  simp only [hs,List.tail_cons,Function.update_of_ne hne.symm] at h
  cases b with
  | false =>
    exact (Program.Runs.branch_right _ _ h (by simp [hs])).mono (by decide)
  | true =>
    exact (Program.Runs.branch_left _ _ h (by simp [hs])).mono (by decide)

def encodeLoop (src dst : Fin (k+1)) : Packed k :=
  Packed.loop (fun h => (h src).isSome) (encodeBody src dst)

theorem encodeLoop_runs {src dst : Fin (k+1)} (hne : src ≠ dst)
    (bs ys : List Bool) (s : Store k) (hs : s src = bs) (hd : s dst = (enc ys).encode) :
    (encodeLoop src dst).Runs s
      (Function.update (Function.update s src []) dst (enc (bs.reverse ++ ys)).encode)
      (14*bs.length) := by
  induction bs generalizing ys s with
  | nil =>
    have he : Function.update (Function.update s src []) dst (enc ([] ++ ys)).encode = s := by
      rw [List.nil_append,← hd]; ext i
      by_cases hi : i = dst <;> by_cases hj : i = src <;> simp_all
    rw [List.reverse_nil,he]
    exact Program.Runs.loop_stop _ _ s (by simp [hs])
  | cons b bs ih =>
    let t := Function.update (Function.update s src bs) dst (boolChunk b ++ s dst)
    have hb : (encodeBody src dst).Runs s t 12 := encodeBody_runs hne b bs s hs
    have ht : t src = bs := by simp [t,hne]
    have hd' : t dst = (enc (b :: ys)).encode := by simp only [t,Function.update_self,hd,encoded_cons]
    have hr := ih (b :: ys) t ht hd'
    have he : Function.update (Function.update t src []) dst (enc (bs.reverse ++ b :: ys)).encode =
        Function.update (Function.update s src []) dst (enc ((b :: bs).reverse ++ ys)).encode := by
      simp only [List.reverse_cons,List.append_assoc,List.singleton_append]
      ext i
      by_cases hi : i = dst <;> by_cases hj : i = src <;> simp_all [t]
    rw [he] at hr
    exact (Program.Runs.loop_iterate _ hb hr (by simp [hs])).mono (by simp only [List.length_cons]; omega)

end ThreeMachine.StackCompiler.Raw

namespace ThreeMachine.StackCompiler.Raw
open Packed
variable {k : ℕ}

def decodeAfterTag (src dst : Fin (k+1)) : Packed k :=
  Packed.branch (fun h => (h src).getD false)
    ((Packed.popN src 3).seq (Packed.ofProgram (Program.push dst true)))
    ((Packed.popN src 1).seq (Packed.ofProgram (Program.push dst false)))

def decodeBody (src dst : Fin (k+1)) : Packed k :=
  (Packed.pop src).seq (decodeAfterTag src dst)

theorem decodeAfterTag_runs {src dst : Fin (k+1)} (hne : src ≠ dst)
    (b : Bool) (rest : List Bool) (s : Store k) (hs : s src = (enc b).encode ++ rest) :
    (decodeAfterTag src dst).Runs s
      (Function.update (Function.update s src rest) dst (b :: s dst)) 10 := by
  cases b with
  | false =>
    have h := Program.Runs.seq (Packed.popN_runs src 1 s)
      (Program.push_exec dst false (Function.update s src ((s src).drop 1))).runs
    have he : (enc false).encode = [false] := rfl
    simp only [hs,he,List.singleton_append,List.drop_succ_cons,List.drop_zero,
      Function.update_of_ne hne.symm] at h
    exact (Program.Runs.branch_right _ _ h (by simp [hs])).mono (by omega)
  | true =>
    have h := Program.Runs.seq (Packed.popN_runs src 3 s)
      (Program.push_exec dst true (Function.update s src ((s src).drop 3))).runs
    have he : (enc true).encode = [true,false,false] := rfl
    simp only [hs,he,List.cons_append,List.nil_append,List.drop_succ_cons,List.drop_zero,
      Function.update_of_ne hne.symm] at h
    exact (Program.Runs.branch_left _ _ h (by simp [hs])).mono (by omega)

theorem decodeBody_runs {src dst : Fin (k+1)} (hne : src ≠ dst)
    (b : Bool) (bs : List Bool) (s : Store k) (hs : s src = (enc (b :: bs)).encode) :
    (decodeBody src dst).Runs s
      (Function.update (Function.update s src (enc bs).encode) dst (b :: s dst)) 12 := by
  have ht : (Function.update s src (s src).tail) src = (enc b).encode ++ (enc bs).encode := by
    simp only [Function.update_self,hs,encoded_cons,boolChunk,List.cons_append,List.tail_cons]
  have h := (Packed.pop_runs src s).seq (decodeAfterTag_runs hne b (enc bs).encode _ ht)
  simpa only [decodeBody,Function.update_idem,Function.update_of_ne hne.symm,Nat.reduceAdd] using h

def decodeLoop (src dst : Fin (k+1)) : Packed k :=
  Packed.loop (fun h => (h src).getD false) (decodeBody src dst)

theorem decodeLoop_runs {src dst : Fin (k+1)} (hne : src ≠ dst)
    (bs : List Bool) (s : Store k) (hs : s src = (enc bs).encode) :
    (decodeLoop src dst).Runs s
      (Function.update (Function.update s src [false]) dst (bs.reverse ++ s dst))
      (14*bs.length) := by
  induction bs generalizing s with
  | nil =>
    have he : Function.update (Function.update s src [false]) dst ([] ++ s dst) = s := by
      rw [List.nil_append]; ext i
      by_cases hi : i = dst <;> by_cases hj : i = src <;> simp_all
    rw [List.reverse_nil,he]
    exact Program.Runs.loop_stop _ _ s (by change (s src).head?.getD false = _; rw [hs]; rfl)
  | cons b bs ih =>
    let t := Function.update (Function.update s src (enc bs).encode) dst (b :: s dst)
    have hb : (decodeBody src dst).Runs s t 12 := decodeBody_runs hne b bs s hs
    have hr := ih t (by simp [t,hne])
    have he : Function.update (Function.update t src [false]) dst (bs.reverse ++ t dst) =
        Function.update (Function.update s src [false]) dst ((b :: bs).reverse ++ s dst) := by
      simp only [List.reverse_cons,List.append_assoc,List.singleton_append]
      ext i
      by_cases hi : i = dst <;> by_cases hj : i = src <;> simp_all [t]
    rw [he] at hr
    exact (Program.Runs.loop_iterate _ hb hr (by change (s src).head?.getD false = _; rw [hs]; rfl)).mono
      (by simp only [List.length_cons]; omega)

end ThreeMachine.StackCompiler.Raw

namespace ThreeMachine.StackCompiler.Tree
variable {V W : Type} [DecidableEq V] [DecidableEq W]

omit [DecidableEq V] [DecidableEq W] in
theorem overlay_zero (f : V → W) : overlay f (zeroStore V) (zeroStore W) = zeroStore W :=
  overlay_of_agree f _ _ (fun _ => rfl)

theorem overlay_start {f : V → W} (hf : Function.Injective f) (input : V) (x : Data) :
    overlay f (start input x) (zeroStore W) = start (f input) x := by
  rw [start,overlay_update hf,overlay_zero]
  rfl

theorem overlay_finish {f : V → W} (hf : Function.Injective f) (a b : V) (x y : Data) :
    overlay f (finish a b x y) (zeroStore W) = finish (f a) (f b) x y := by
  rw [finish,overlay_update hf,overlay_start hf]
  rfl

end ThreeMachine.StackCompiler.Tree

namespace ThreeMachine.StackCompiler.Routine
open ThreeMachine.StackCompiler.Tree
variable {f : Data → Data} {charge : Data → ℕ}
variable (R : Routine f charge)

noncomputable def number (i : R.Vars) : Fin (Fintype.card R.Vars+1) :=
  (Fintype.equivFin R.Vars i).succ

theorem number_injective : Function.Injective R.number :=
  (Fin.succ_injective _).comp (Fintype.equivFin R.Vars).injective

@[simp] theorem number_ne_zero (i : R.Vars) : R.number i ≠ 0 := Fin.succ_ne_zero _

noncomputable def numberedCode : Code (Fin (Fintype.card R.Vars+1)) := R.code.rename R.number

theorem numberedCode_runs (x : Data) :
    R.numberedCode.compile.Runs (memory (start (R.number R.input) x))
      (memory (finish (R.number R.input) (R.number R.output) x (f x))) (150*charge x) := by
  obtain ⟨n,hn,he⟩ := R.correct x
  have hh := (he.rename R.number_injective (zeroStore (Fin (Fintype.card R.Vars+1)))).compile
  rw [overlay_start R.number_injective,overlay_finish R.number_injective] at hh
  exact hh.mono (Nat.mul_le_mul_left 150 hn)

noncomputable def initRegs : Packed (Fintype.card R.Vars+4) :=
  Packed.ofProgram (Program.instruction (fun _ j =>
    if j.val < Fintype.card R.Vars+1 then .push false else .stay))

theorem initRegs_runs (s : Store (Fintype.card R.Vars+4)) :
    R.initRegs.Runs s (fun j => if j.val < Fintype.card R.Vars+1 then false :: s j else s j) 1 := by
  have he : (fun j => (if j.val < Fintype.card R.Vars+1 then Action.push false else Action.stay).apply (s j)) =
      (fun j => if j.val < Fintype.card R.Vars+1 then false :: s j else s j) := by
    funext j
    split_ifs <;> rfl
  simpa only [initRegs,Packed.Runs,Packed.ofProgram,he] using
    (Program.instruction_exec (fun _ j =>
      if j.val < Fintype.card R.Vars+1 then Action.push false else .stay) s).runs

noncomputable def inputProgram : Packed (Fintype.card R.Vars+4) :=
  (Packed.ofProgram (Program.transfer 0 (temp 0))).seq
    (R.initRegs.seq (Raw.encodeLoop (temp 0) (reg (R.number R.input))))

@[simp] theorem temp_ne_zero : (temp (r := Fintype.card R.Vars+1) 0) ≠ 0 := by
  change temp 0 ≠ reg (0 : Fin (Fintype.card R.Vars+1))
  simp

@[simp] theorem reg_number_ne_zero (i : R.Vars) : reg (R.number i) ≠ 0 := by
  change reg (R.number i) ≠ reg (0 : Fin (Fintype.card R.Vars+1))
  simp

theorem inputProgram_runs (bs : List Bool) :
    R.inputProgram.Runs (initialStore bs)
      (memory (start (R.number R.input) (enc bs))) (17*bs.length+3) := by
  let s : Store (Fintype.card R.Vars+4) := Function.update (fun _ => []) (temp 0) bs.reverse
  have ht : (Packed.ofProgram (Program.transfer 0 (temp 0))).Runs
      (initialStore bs) s (3*bs.length) := by
    have h := (Program.transfer_exec (Ne.symm R.temp_ne_zero) (initialStore bs)).runs
    have he : Program.transferResult 0 (temp 0) (initialStore bs) = s := by
      funext i
      by_cases hi : i = temp 0 <;> by_cases hz : i = 0 <;>
        simp_all [Program.transferResult,initialStore,s,R.temp_ne_zero,Ne.symm R.temp_ne_zero]
    simpa only [Packed.Runs,Packed.ofProgram,initialStore,ite_true,he] using h
  let t : Store (Fintype.card R.Vars+4) :=
    fun j => if j.val < Fintype.card R.Vars+1 then false :: s j else s j
  have hi : R.initRegs.Runs s t 1 := R.initRegs_runs s
  have hts : t (temp 0) = bs.reverse := by simp [t,s,temp]
  have htd : t (reg (R.number R.input)) = (enc ([] : List Bool)).encode := by
    have hv : (reg (R.number R.input)).val < Fintype.card R.Vars+1 := (R.number R.input).isLt
    simp only [t,ite_eq_left hv,s,Function.update_of_ne (reg_ne_temp _ _)]
    rfl
  have he := Raw.encodeLoop_runs (temp_ne_reg _ _) bs.reverse [] t hts htd
  have hfinal : Function.update (Function.update t (temp 0) [])
      (reg (R.number R.input)) (enc (bs.reverse.reverse ++ [])).encode =
      memory (start (R.number R.input) (enc bs)) := by
    simp only [List.reverse_reverse,List.append_nil]
    funext j
    refine Fin.addCases (m := Fintype.card R.Vars+1) (n := 4) (fun i => ?_) (fun i => ?_) j
    · change Function.update (Function.update t (temp 0) []) (reg (R.number R.input))
        (enc bs).encode (reg i) = memory (start (R.number R.input) (enc bs)) (reg i)
      by_cases hh : i = R.number R.input
      · subst i; simp
      · have hv : (reg i).val < Fintype.card R.Vars+1 := i.isLt
        simp only [Function.update_of_ne (show reg i ≠ reg (R.number R.input) by simpa using hh),
          memory_reg,start_other _ hh,t,ite_eq_left hv,s,
          Function.update_of_ne (reg_ne_temp _ _)]
        rfl
    · change Function.update (Function.update t (temp 0) []) (reg (R.number R.input))
        (enc bs).encode (temp i) = memory (start (R.number R.input) (enc bs)) (temp i)
      by_cases hh : i = 0
      · subst i; simp
      · have hv : ¬(temp (r := Fintype.card R.Vars+1) i).val < Fintype.card R.Vars+1 := by
          simp only [temp,Fin.val_natAdd]; omega
        simp only [Function.update_of_ne (temp_ne_reg _ _),memory_temp,
          Function.update_of_ne (show (temp i : Fin (Fintype.card R.Vars+1+4)) ≠ temp 0 by simpa using hh),
          t,ite_eq_right hv,s]
  rw [hfinal] at he
  exact (ht.seq (hi.seq he)).mono (by simp only [List.length_reverse]; omega)

noncomputable def outputProgram : Packed (Fintype.card R.Vars+4) :=
  (Raw.decodeLoop (reg (R.number R.output)) (temp 0)).seq
    ((Packed.pop 0).seq (Packed.ofProgram (Program.transfer (temp 0) 0)))

noncomputable def rawProgram : Packed (Fintype.card R.Vars+4) :=
  R.inputProgram.seq (R.numberedCode.compile.seq R.outputProgram)

theorem outputProgram_runs (bs out : List Bool) :
    ∃ t, R.outputProgram.Runs
      (memory (finish (R.number R.input) (R.number R.output) (enc bs) (enc out))) t
      (17*out.length+3) ∧ t 0 = out := by
  let s := memory (finish (R.number R.input) (R.number R.output) (enc bs) (enc out))
  have hzero : s 0 = [false] := by
    change memory _ (reg (0 : Fin (Fintype.card R.Vars+1))) = _
    simp only [memory_reg,finish,Function.update_of_ne (Ne.symm (R.number_ne_zero R.output)),
      start_other _ (Ne.symm (R.number_ne_zero R.input))]
    rfl
  have hd := Raw.decodeLoop_runs (reg_ne_temp _ 0) out s
    (show s (reg (R.number R.output)) = (enc out).encode by simp [s])
  let u := Function.update (Function.update s (reg (R.number R.output)) [false])
    (temp 0) (out.reverse ++ s (temp 0))
  have hu : u 0 = [false] := by
    simp only [u,Function.update_of_ne (Ne.symm R.temp_ne_zero),
      Function.update_of_ne (Ne.symm (R.reg_number_ne_zero R.output)),hzero]
  have hv : u (temp 0) = out.reverse := by simp [u,s]
  let v := Function.update u 0 []
  have hp : (Packed.pop 0).Runs u v 1 := by
    simpa only [hu,List.tail_cons,List.tail_nil] using Packed.pop_runs 0 u
  have ht := (Program.transfer_exec R.temp_ne_zero v).runs
  have hvt : v (temp 0) = out.reverse := by simp [v,R.temp_ne_zero,hv]
  have hvz : v 0 = [] := by simp [v]
  rw [hvt,List.length_reverse] at ht
  have ht' : (Packed.ofProgram (Program.transfer (temp 0) 0)).Runs v
      (Program.transferResult (temp 0) 0 v) (3*out.length) := ht
  refine ⟨Program.transferResult (temp 0) 0 v,?_,?_⟩
  · exact (hd.seq (hp.seq ht')).mono (by omega)
  · simp [Program.transferResult,hvt,hvz]

theorem rawProgram_runs (bs out : List Bool) (h : f (enc bs) = enc out) :
    ∃ t, R.rawProgram.Runs (initialStore bs) t
      (1000*(charge (enc bs)+bs.length+out.length+1)) ∧ t 0 = out := by
  have hi := R.inputProgram_runs bs
  have hc := R.numberedCode_runs (enc bs)
  rw [h] at hc
  obtain ⟨t,ho,ht⟩ := R.outputProgram_runs bs out
  exact ⟨t,(hi.seq (hc.seq ho)).mono (by omega),ht⟩

include R in
theorem exists_rawMachine : ∃ k q g, ∃ M : Machine k q g,
    ∀ bs out, f (enc bs) = enc out →
      M.Produces bs out (2000*(charge (enc bs)+bs.length+out.length+1)) := by
  refine ⟨_,_,_,compile R.rawProgram.program,?_⟩
  intro bs out h
  obtain ⟨t,ht,ho⟩ := R.rawProgram_runs bs out h
  have hh := ht.computes
  obtain ⟨n,hn,hs,hh⟩ := hh
  have he := compile_produces R.rawProgram.program bs out
    (1000*(charge (enc bs)+bs.length+out.length+1)) ⟨n,hn,hh,by rw [hs,ho]⟩
  simpa only [← Nat.mul_assoc] using he

end ThreeMachine.StackCompiler.Routine
end

end OAI