File size: 17,770 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Combinatorics.MatchingCount.Model

namespace OAI

namespace MatchingFPRAS.MachineCost
open Turing

def advance {α : Type*} (f : α → Option α) (x : α) : α := (f x).getD x
def run {α : Type*} (f : α → Option α) : ℕ → α → α
  | 0,x => x
  | n+1,x => run f n (advance f x)

lemma run_add {α : Type*} (f : α → Option α) (n m : ℕ) (x : α) :
    run f (n+m) x=run f m (run f n x) := by
  induction n generalizing x with
  | zero => simp only [Nat.zero_add,run]
  | succ n ih => simpa only [Nat.succ_add,run] using ih (advance f x)

lemma run_step_eq {α : Type*} (f : α → Option α) {x y : α}
    (h : advance f x=advance f y) {n : ℕ} (hn : 0<n) : run f n x=run f n y := by
  cases n with
  | zero => omega
  | succ n => simp only [run,h]

def statementCost {Γ Λ σ : Type*} : TM1.Stmt Γ Λ σ → ℕ
  | .move _ q => statementCost q+1
  | .write _ q => statementCost q+1
  | .load _ q => statementCost q
  | .branch _ q r => max (statementCost q) (statementCost r)
  | .goto _ => 1
  | .halt => 1
lemma statementCost_pos {Γ Λ σ : Type*} (q : TM1.Stmt Γ Λ σ) : 0<statementCost q := by
  induction q with
  | move _ q ih => simp only [statementCost]; omega
  | write _ q ih => simp only [statementCost]; omega
  | load _ q ih => exact ih
  | branch _ q r ihq ihr => exact lt_of_lt_of_le ihq (le_max_left _ _)
  | goto _ => exact Nat.zero_lt_one
  | halt => exact Nat.zero_lt_one

variable {Γ Λ σ : Type*} [Inhabited Γ] [Inhabited Λ] [Inhabited σ]
variable (M : Λ → TM1.Stmt Γ Λ σ)

theorem statement_translation_time (q : TM1.Stmt Γ Λ σ) (v : σ) (T : Tape Γ) :
    ∃ k, 0 < k ∧ k ≤ statementCost q ∧
      run (TM0.step (TM1to0.tr M)) k ⟨(some q,v),T⟩ =
        TM1to0.trCfg M (TM1.stepAux q v T) := by
  induction q generalizing v T with
  | move d q ih =>
    obtain ⟨k,hk,hc,he⟩ := ih v (T.move d)
    refine ⟨k+1,by omega,by simpa only [statementCost] using Nat.add_le_add_right hc 1,?_⟩
    simpa only [run,advance,TM0.step,TM1to0.tr,TM1to0.trAux,Option.map_some,
      Option.getD_some,TM1.stepAux] using he
  | write a q ih =>
    obtain ⟨k,hk,hc,he⟩ := ih v (T.write (a T.head v))
    refine ⟨k+1,by omega,by simpa only [statementCost] using Nat.add_le_add_right hc 1,?_⟩
    simpa only [run,advance,TM0.step,TM1to0.tr,TM1to0.trAux,Option.map_some,
      Option.getD_some,TM1.stepAux] using he
  | load a q ih =>
    obtain ⟨k,hk,hc,he⟩ := ih (a T.head v) T
    refine ⟨k,hk,hc,?_⟩
    rw [run_step_eq (TM0.step (TM1to0.tr M)) (x:=⟨(some (.load a q),v),T⟩)
      (y:=⟨(some q,a T.head v),T⟩) rfl hk]
    exact he
  | branch p q r ihq ihr =>
    cases hp : p T.head v with
    | false =>
      obtain ⟨k,hk,hc,he⟩ := ihr v T
      refine ⟨k,hk,hc.trans (le_max_right _ _),?_⟩
      have hs : advance (TM0.step (TM1to0.tr M)) ⟨(some (.branch p q r),v),T⟩ =
          advance (TM0.step (TM1to0.tr M)) ⟨(some r,v),T⟩ := by
        simp only [advance,TM0.step,TM1to0.tr,TM1to0.trAux,hp,Bool.cond_false,Option.map_some,Option.getD_some]
      rw [run_step_eq _ hs hk]
      simpa only [TM1.stepAux,hp,Bool.cond_false] using he
    | true =>
      obtain ⟨k,hk,hc,he⟩ := ihq v T
      refine ⟨k,hk,hc.trans (le_max_left _ _),?_⟩
      have hs : advance (TM0.step (TM1to0.tr M)) ⟨(some (.branch p q r),v),T⟩ =
          advance (TM0.step (TM1to0.tr M)) ⟨(some q,v),T⟩ := by
        simp only [advance,TM0.step,TM1to0.tr,TM1to0.trAux,hp,Bool.cond_true,Option.map_some,Option.getD_some]
      rw [run_step_eq _ hs hk]
      simpa only [TM1.stepAux,hp,Bool.cond_true] using he
  | goto l =>
    refine ⟨1,by decide,le_rfl,?_⟩
    simp only [run,advance,TM0.step,TM1to0.tr,TM1to0.trAux,Option.map_some,
      Option.getD_some,TM1.stepAux,TM1to0.trCfg,Tape.write_self]
  | halt =>
    refine ⟨1,by decide,le_rfl,?_⟩
    simp only [run,advance,TM0.step,TM1to0.tr,TM1to0.trAux,Option.map_some,
      Option.getD_some,TM1.stepAux,TM1to0.trCfg,Tape.write_self,Option.map_none]

noncomputable def programCost [Fintype Λ] : ℕ := Finset.univ.sup (fun l=>statementCost (M l))
omit [Inhabited Γ] [Inhabited Λ] [Inhabited σ] in
lemma statementCost_le_programCost [Fintype Λ] (l : Λ) :
    statementCost (M l)≤programCost M := Finset.le_sup (f:=fun l=>statementCost (M l)) (Finset.mem_univ l)
omit [Inhabited Γ] [Inhabited σ] in
lemma programCost_pos [Fintype Λ] : 0<programCost M :=
  (statementCost_pos (M default)).trans_le (statementCost_le_programCost M default)

theorem run_translation_time [Fintype Λ] (n : ℕ) (c : TM1.Cfg Γ Λ σ) :
    ∃ k≤programCost M*n,
      run (TM0.step (TM1to0.tr M)) k (TM1to0.trCfg M c)=
        TM1to0.trCfg M (run (TM1.step M) n c) := by
  induction n generalizing c with
  | zero => exact ⟨0,by omega,rfl⟩
  | succ n ih =>
    rcases c with ⟨l,v,T⟩
    cases l with
    | none =>
      have he : run (TM1.step M) (n+1) ⟨none,v,T⟩=⟨none,v,T⟩ := by
        have h : ∀ k, run (TM1.step M) k ⟨none,v,T⟩=⟨none,v,T⟩ := by
          intro k; induction k with
          | zero => simp only [run]
          | succ k ihk => simpa only [run,advance,TM1.step,Option.getD_none] using ihk
        exact h _
      exact ⟨0,by omega,by rw [run,he]⟩
    | some l =>
      obtain ⟨j,hj,hjc,hje⟩ := statement_translation_time M (M l) v T
      obtain ⟨k,hk,hke⟩ := ih (TM1.stepAux (M l) v T)
      refine ⟨j+k,?_,?_⟩
      · have := hjc.trans (statementCost_le_programCost M l)
        simpa only [Nat.mul_succ] using Nat.add_le_add this hk |>.trans_eq (Nat.add_comm _ _)
      · rw [run_add]
        change run (TM0.step (TM1to0.tr M)) k
          (run (TM0.step (TM1to0.tr M)) j ⟨(some (M l),v),T⟩)=_
        rw [hje,hke]
        rfl
end MatchingFPRAS.MachineCost

namespace MatchingFPRAS.PostCompiler
open Turing MachineCost
open scoped Classical

variable {Λ : Type*} [Inhabited Λ]
variable (M : Λ → Symbol → Option (Λ × TM0.Stmt Symbol)) (S : Finset Λ) (hS : TM0.Supports M (S : Set Λ))

noncomputable def index : S ≃ Fin S.card := Fintype.equivFinOfCardEq (by simp)
noncomputable def pack (q : S) : Fin (S.card+1) := (index S q).succ
noncomputable def unpack (i : Fin (S.card+1)) : S :=
  if h : i.val=0 then ⟨default,hS.1⟩ else
    (index S).symm ⟨i.val-1,by have := i.isLt; omega⟩
lemma unpack_pack (q : S) : unpack M S hS (pack S q)=q := by
  simp [unpack,pack]
omit [Inhabited Λ] in
lemma pack_ne_zero (q : S) : pack S q≠0 := by simp [pack]

noncomputable def packState (q : Λ) : Fin (S.card+1) :=
  if h : q∈S then pack S ⟨q,h⟩ else 0
omit [Inhabited Λ] in
lemma packState_of_mem (q : Λ) (hq : q∈S) : packState S q=pack S ⟨q,hq⟩ := by
  simp [packState,hq]

noncomputable def sealedMachine : RandomMachine where
  states := S.card
  transition i a _ := if i=0 then some (pack S ⟨default,hS.1⟩,.write a) else
    (M (unpack M S hS i).val a).map (fun (q,s)=>(packState S q,s))

abbrev Phase (Λ : Type*) := TM0.Cfg Symbol Λ × Bool

def perform (s : TM0.Stmt Symbol) (T : Tape Symbol) : Tape Symbol := match s with
  | .move d => T.move d
  | .write a => T.write a

noncomputable def phaseStep (x : Phase Λ) : Phase Λ :=
  if x.2 then
    match M x.1.q x.1.Tape.head with
    | none => (x.1,false)
    | some (q,s) => (⟨q,perform s x.1.Tape⟩,true)
  else x

def well (x : Phase Λ) : Prop := x.1.q∈S ∧ (x.2=false → TM0.step M x.1=none)
noncomputable def embed (x : Phase Λ) : Configuration (sealedMachine M S hS) :=
  ⟨if x.2 then some (packState S x.1.q) else none,x.1.Tape⟩

include hS

lemma phaseStep_well (x : Phase Λ) (hx : well M S x) : well M S (phaseStep M x) := by
  rcases x with ⟨⟨q,T⟩,b⟩
  cases b with
  | false => exact hx
  | true =>
    cases he : M q T.head with
    | none => simpa [well,phaseStep,he,TM0.step] using hx.1
    | some ys =>
      rcases ys with ⟨y,s⟩
      simpa [well,phaseStep,he] using hS.2 he hx.1

omit hS in
lemma phaseStep_first (_hS : TM0.Supports M (S : Set Λ)) (x : Phase Λ) (hx : well M S x) :
    (phaseStep M x).1=advance (TM0.step M) x.1 := by
  rcases x with ⟨⟨q,T⟩,b⟩
  cases b with
  | false => simp [phaseStep,advance,hx.2 rfl]
  | true =>
    cases he : M q T.head with
    | none => simp [phaseStep,advance,TM0.step,he]
    | some ys => rcases ys with ⟨y,s⟩; cases s <;> simp [phaseStep,advance,TM0.step,he,perform]

lemma embed_step (x : Phase Λ) (hx : well M S x) (b : Bool) :
    tick (sealedMachine M S hS) (embed M S hS x) b=embed M S hS (phaseStep M x) := by
  rcases x with ⟨⟨q,T⟩,live⟩
  cases live with
  | false => rfl
  | true =>
    have hp := pack_ne_zero S (⟨q,hx.1⟩ : S)
    cases he : M q T.head with
    | none => simp [embed,tick,sealedMachine,phaseStep,packState_of_mem S q hx.1,hp,unpack_pack,he]
    | some ys =>
      rcases ys with ⟨y,s⟩
      cases s <;> simp [embed,tick,sealedMachine,phaseStep,packState_of_mem S q hx.1,hp,unpack_pack,he,perform]

lemma phaseRun (n : ℕ) (x : Phase Λ) (hx : well M S x) :
    well M S ((phaseStep M)^[n] x) ∧
      ((phaseStep M)^[n] x).1=MachineCost.run (TM0.step M) n x.1 := by
  induction n generalizing x with
  | zero => exact ⟨hx,rfl⟩
  | succ n ih =>
    have h := ih (phaseStep M x) (phaseStep_well M S hS x hx)
    rw [Function.iterate_succ_apply]
    exact ⟨h.1,by rw [h.2,phaseStep_first M S hS x hx]; rfl⟩

lemma phaseRun_stops (n : ℕ) (x : Phase Λ) (hx : well M S x)
    (hh : TM0.step M (MachineCost.run (TM0.step M) n x.1)=none) :
    ((phaseStep M)^[n+1] x).2=false ∧
      ((phaseStep M)^[n+1] x).1=MachineCost.run (TM0.step M) n x.1 := by
  obtain ⟨hw,he⟩ := phaseRun M S hS n x hx
  rw [Function.iterate_succ_apply']
  rw [←he] at hh ⊢
  generalize (phaseStep M)^[n] x=y at *
  rcases y with ⟨⟨q,T⟩,live⟩
  have hq0 : M q T.head=none := Option.map_eq_none_iff.mp hh
  cases live <;> simp [phaseStep,hq0]

lemma embed_fold (bits : List Bool) (x : Phase Λ) (hx : well M S x) :
    bits.foldl (tick (sealedMachine M S hS)) (embed M S hS x)=
      embed M S hS ((phaseStep M)^[bits.length] x) := by
  induction bits generalizing x with
  | nil => rfl
  | cons b bits ih =>
    rw [List.foldl_cons,embed_step M S hS x hx,ih _ (phaseStep_well M S hS x hx),
      List.length_cons,Function.iterate_succ_apply]

lemma entry (a : Symbol) (b : Bool) :
    (sealedMachine M S hS).transition 0 a b=some (pack S ⟨default,hS.1⟩,.write a) := by
  dsimp only [sealedMachine]
  split
  · rfl
  · rename_i h; exact False.elim (h rfl)

lemma initial_tick (input : List Symbol) (b : Bool) :
    tick (sealedMachine M S hS) (initial (sealedMachine M S hS) input) b=
      embed M S hS (TM0.init input,true) := by
  simp only [tick,initial,entry,Tape.write_self,embed,TM0.init,packState_of_mem S default hS.1,
    ↓reduceIte]

omit hS in
lemma halted_tick (A : RandomMachine) (c : Configuration A) (h : c.state=none) (b : Bool) : tick A c b=c := by
  rcases c with ⟨s,T⟩
  change s=none at h
  subst s
  rfl
omit hS in
lemma halted_fold (A : RandomMachine) (c : Configuration A) (h : c.state=none) (bits : List Bool) :
    bits.foldl (tick A) c=c := by
  induction bits with
  | nil => rfl
  | cons b bits ih => rw [List.foldl_cons,halted_tick A c h b,ih]

theorem seal_words (input output : List Symbol) (n : ℕ)
    (hhalt : TM0.step M (MachineCost.run (TM0.step M) n (TM0.init input))=none)
    (hout : (MachineCost.run (TM0.step M) n (TM0.init input)).Tape.right₀=
      ListBlank.mk output) (bits : Fin (n+2) → Bool) :
    (MatchingFPRAS.run (sealedMachine M S hS) input bits).state=none ∧
      (MatchingFPRAS.run (sealedMachine M S hS) input bits).tape.right₀=ListBlank.mk output := by
  let x : Phase Λ := (TM0.init input,true)
  have hx : well M S x := ⟨hS.1,by intro h; cases h⟩
  obtain ⟨hs,he⟩ := phaseRun_stops M S hS n x hx hhalt
  have hr : MatchingFPRAS.run (sealedMachine M S hS) input bits=embed M S hS ((phaseStep M)^[n+1] x) := by
    unfold MatchingFPRAS.run
    rw [List.ofFn_succ,List.foldl_cons,initial_tick,embed_fold M S hS _ x hx,List.length_ofFn]
  constructor
  · rw [hr]
    simp only [embed,hs,Bool.false_eq_true,↓reduceIte]
  · rw [hr]
    change ((phaseStep M)^[n+1] x).1.Tape.right₀=_
    rw [he]
    exact hout

theorem seal_outputs (input : List Symbol) (n : ℕ) (q : ℚ)
    (hhalt : TM0.step M (MachineCost.run (TM0.step M) n (TM0.init input))=none)
    (hout : (MachineCost.run (TM0.step M) n (TM0.init input)).Tape.right₀=
      ListBlank.mk (encodeRat q)) (bits : Fin (n+2) → Bool) :
    Outputs (sealedMachine M S hS) input bits q := by
  let x : Phase Λ := (TM0.init input,true)
  have hx : well M S x := ⟨hS.1,by intro h; cases h⟩
  obtain ⟨hs,he⟩ := phaseRun_stops M S hS n x hx hhalt
  have hr : MatchingFPRAS.run (sealedMachine M S hS) input bits=embed M S hS ((phaseStep M)^[n+1] x) := by
    unfold MatchingFPRAS.run
    rw [List.ofFn_succ,List.foldl_cons,initial_tick,embed_fold M S hS _ x hx,List.length_ofFn]
  constructor
  · rw [hr]
    simp only [embed,hs,Bool.false_eq_true,↓reduceIte]
  · rw [hr]
    change ((phaseStep M)^[n+1] x).1.Tape.right₀=_
    rw [he]
    exact hout

omit hS

open Turing MachineCost
open scoped Classical

theorem outputs_extend (A : RandomMachine) (input : List Symbol) (q : ℚ)
    {n t : ℕ} (hnt : n ≤ t) (bits : Fin t → Bool)
    (h : Outputs A input (fun i : Fin n=>bits (Fin.castLE hnt i)) q) :
    Outputs A input bits q := by
  obtain ⟨m,rfl⟩ := Nat.exists_eq_add_of_le hnt
  have hr : MatchingFPRAS.run A input bits=
      MatchingFPRAS.run A input (fun i : Fin n=>bits (Fin.castLE hnt i)) := by
    unfold MatchingFPRAS.run
    rw [List.ofFn_add,List.foldl_append]
    exact halted_fold A _ h.1 _
  constructor
  · rw [hr]; exact h.1
  · rw [hr]; exact h.2

theorem words_extend (A : RandomMachine) (input output : List Symbol)
    {n t : ℕ} (hnt : n ≤ t) (bits : Fin t → Bool)
    (h : (MatchingFPRAS.run A input (fun i : Fin n=>bits (Fin.castLE hnt i))).state=none ∧
      (MatchingFPRAS.run A input (fun i : Fin n=>bits (Fin.castLE hnt i))).tape.right₀=ListBlank.mk output) :
    (MatchingFPRAS.run A input bits).state=none ∧
      (MatchingFPRAS.run A input bits).tape.right₀=ListBlank.mk output := by
  obtain ⟨m,rfl⟩ := Nat.exists_eq_add_of_le hnt
  have hr : MatchingFPRAS.run A input bits=
      MatchingFPRAS.run A input (fun i : Fin n=>bits (Fin.castLE hnt i)) := by
    unfold MatchingFPRAS.run
    rw [List.ofFn_add,List.foldl_append]
    exact halted_fold A _ h.1 _
  rw [hr]
  exact h

variable {Λ σ : Type*} [Inhabited Λ] [Inhabited σ] [Fintype Λ] [Fintype σ]
variable (M : Λ → TM1.Stmt Symbol Λ σ)

omit [Inhabited σ] [Fintype σ] in
lemma finite_supported : TM1.Supports M Finset.univ := by
  constructor
  · exact Finset.mem_univ _
  · intro l hl
    induction M l with
    | move _ q ih => exact ih
    | write _ q ih => exact ih
    | load _ q ih => exact ih
    | branch _ q r ihq ihr => exact ⟨ihq,ihr⟩
    | goto _ => exact fun _ _=>Finset.mem_univ _
    | halt => trivial

noncomputable def compiled : RandomMachine :=
  sealedMachine (TM1to0.tr M) (TM1to0.trStmts M Finset.univ)
    (TM1to0.tr_supports M (finite_supported M))

theorem compiled_outputs (input : List Symbol) (n t : ℕ) (q : ℚ)
    (hhalt : (MachineCost.run (TM1.step M) n (TM1.init input)).l=none)
    (hout : (MachineCost.run (TM1.step M) n (TM1.init input)).Tape.right₀=
      ListBlank.mk (encodeRat q))
    (ht : programCost M*n+2 ≤ t) (bits : Fin t → Bool) : Outputs (compiled M) input bits q := by
  obtain ⟨k,hk,he⟩ := run_translation_time M n (TM1.init input)
  have hi : TM1to0.trCfg M (TM1.init input)=TM0.init input := rfl
  rw [hi] at he
  have hh : TM0.step (TM1to0.tr M)
      (MachineCost.run (TM0.step (TM1to0.tr M)) k (TM0.init input))=none := by
    rw [he]
    generalize hc : MachineCost.run (TM1.step M) n (TM1.init input)=c at hhalt ⊢
    rcases c with ⟨l,v,T⟩
    change l=none at hhalt
    subst l
    rfl
  have ho : (MachineCost.run (TM0.step (TM1to0.tr M)) k (TM0.init input)).Tape.right₀=
      ListBlank.mk (encodeRat q) := by
    rw [he]
    exact hout
  have hkt : k+2 ≤ t := (Nat.add_le_add_right hk 2).trans ht
  exact outputs_extend (compiled M) input q hkt bits
    (seal_outputs (TM1to0.tr M) (TM1to0.trStmts M Finset.univ)
      (TM1to0.tr_supports M (finite_supported M)) input k q hh ho _)

theorem compiled_words (input output : List Symbol) (n t : ℕ)
    (hhalt : (MachineCost.run (TM1.step M) n (TM1.init input)).l=none)
    (hout : (MachineCost.run (TM1.step M) n (TM1.init input)).Tape.right₀=
      ListBlank.mk output)
    (ht : programCost M*n+2 ≤ t) (bits : Fin t → Bool) :
    (MatchingFPRAS.run (compiled M) input bits).state=none ∧
      (MatchingFPRAS.run (compiled M) input bits).tape.right₀=ListBlank.mk output := by
  obtain ⟨k,hk,he⟩ := run_translation_time M n (TM1.init input)
  have hi : TM1to0.trCfg M (TM1.init input)=TM0.init input := rfl
  rw [hi] at he
  have hh : TM0.step (TM1to0.tr M)
      (MachineCost.run (TM0.step (TM1to0.tr M)) k (TM0.init input))=none := by
    rw [he]
    generalize hc : MachineCost.run (TM1.step M) n (TM1.init input)=c at hhalt ⊢
    rcases c with ⟨l,v,T⟩
    change l=none at hhalt
    subst l
    rfl
  have ho : (MachineCost.run (TM0.step (TM1to0.tr M)) k (TM0.init input)).Tape.right₀=
      ListBlank.mk output := by
    rw [he]
    exact hout
  have hkt : k+2 ≤ t := (Nat.add_le_add_right hk 2).trans ht
  exact words_extend (compiled M) input output hkt bits
    (seal_words (TM1to0.tr M) (TM1to0.trStmts M Finset.univ)
      (TM1to0.tr_supports M (finite_supported M)) input output k hh ho _)
end MatchingFPRAS.PostCompiler

end OAI