File size: 18,118 Bytes
5bdeca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.NumberTheory.SingleFold.CounterCompilation

namespace OAI

namespace SingleFold.Counter
variable {Q J : Type} [DecidableEq Q] [DecidableEq J]
def step (M : Machine Q J) (c : Cfg Q J) : Option (Cfg Q J) :=
  match M.instr c.state with | .halt=>none | _=>some (next M c)
end SingleFold.Counter

namespace SingleFold.Macro

section
open Counter StateTransition Relation
inductive Instr (Q J : Type)
  | halt
  | goto (p : Q)
  | inc (j : J) (p : Q)
  | test (j : J) (pz pp : Q)
  | muladd (j : J) (e : Fin 5) (p : Q)
  | div (j : J) (binary : Bool) (p : Fin 5 → Q)

def radix (binary : Bool) : ℕ := if binary then 2 else 5
structure Cfg (Q J : Type) where
  label : Option Q
  value : J → ℕ
variable {Q J : Type}
def next [DecidableEq J] (M : Q → Instr Q J) (q : Q) (v : J → ℕ) : Cfg Q J :=
  match M q with
  | .halt => ⟨none,v⟩
  | .goto p => ⟨some p,v⟩
  | .inc j p => ⟨some p,Function.update v j (v j+1)⟩
  | .test j pz pp => ⟨some (if v j=0 then pz else pp),v⟩
  | .muladd j e p => ⟨some p,Function.update v j (5*v j+e)⟩
  | .div j b p => ⟨some (p ⟨v j%radix b,by have h := Nat.mod_lt (v j) (show 0 < radix b by cases b <;> decide); cases b <;> simp_all [radix]; omega⟩),Function.update v j (v j/radix b)⟩
def step [DecidableEq J] (M : Q → Instr Q J) (c : Cfg Q J) : Option (Cfg Q J) :=
  c.label.map (fun q=>next M q c.value)

inductive State (Q : Type)
  | main (q : Q)
  | grow (q : Q) (i : Fin 5)
  | copy (q : Q) (r : Fin 5)
  | copyInc (q : Q) (r : Fin 5)
  | add (q : Q) (i : Fin 5)
  | quotient (q : Q) (r : Fin 5)
  | carry (q : Q)
  | restore (q : Q)
  | halt
  deriving DecidableEq, Fintype
abbrev Reg (J : Type) := J ⊕ Bool
def scratch : Reg J := .inr false
def zeroReg : Reg J := .inr true

def succ5 (i : Fin 5) : Fin 5 := ⟨(i.val+1)%5,Nat.mod_lt _ (by omega)⟩
def go (s : State Q) : Counter.Instr (State Q) (Reg J) := .test zeroReg s s

def finish (M : Q → Instr Q J) (q : Q) (r : Fin 5) : State Q :=
  match M q with
  | .muladd _ _ _ => .add q 0
  | .div _ _ p => .main (p r)
  | _ => .halt

def compileInstr (M : Q → Instr Q J) : State Q → Counter.Instr (State Q) (Reg J)
  | .main q => match M q with
    | .halt => go .halt
    | .goto p => go (.main p)
    | .inc j p => .inc (.inl j) (.main p)
    | .test j pz _ => .test (.inl j) (.main pz) (.restore q)
    | .muladd j _ _ => .test (.inl j) (.copy q 0) (.grow q 0)
    | .div _ _ _ => go (.quotient q 0)
  | .grow q i => .inc scratch (if i.val=4 then .main q else .grow q (succ5 i))
  | .copy q r => .test scratch (finish M q r) (.copyInc q r)
  | .copyInc q r => match M q with
    | .muladd j _ _ | .div j _ _ => .inc (.inl j) (.copy q r)
    | _ => go .halt
  | .add q i => match M q with
    | .muladd j e p => if i.val<e.val then .inc (.inl j) (.add q (succ5 i)) else go (.main p)
    | _ => go .halt
  | .quotient q r => match M q with
    | .div j b _ => .test (.inl j) (.copy q r)
        (if r.val+1=radix b then .carry q else .quotient q (succ5 r))
    | _ => go .halt
  | .carry q => .inc scratch (.quotient q 0)
  | .restore q => match M q with
    | .test j _ p => .inc (.inl j) (.main p)
    | _ => go .halt
  | .halt => .halt

def compile (M : Q → Instr Q J) (start : Q) : Counter.Machine (State Q) (Reg J) where
  instr := compileInstr M
  start := .main start
  halt := .halt
  halt_iff q := by
    cases q with
    | main q => cases h : M q <;> simp [compileInstr, h, go]
    | grow q i => simp [compileInstr]
    | copy q r => simp [compileInstr]
    | copyInc q r => cases h : M q <;> simp [compileInstr, h, go]
    | add q i => cases h : M q <;> simp [compileInstr, h, go]; split <;> simp
    | quotient q r => cases h : M q <;> simp [compileInstr, h, go]
    | carry q => simp [compileInstr]
    | restore q => cases h : M q <;> simp [compileInstr, h, go]
    | halt => simp [compileInstr]

variable [DecidableEq J]
def cfg (s : State Q) (v : J → ℕ) (a : ℕ) : Counter.Cfg (State Q) (Reg J) :=
  ⟨s,Sum.elim v (fun b=>if b then 0 else a)⟩
def encode (c : Cfg Q J) : Counter.Cfg (State Q) (Reg J) :=
  cfg (c.label.elim .halt State.main) c.value 0
variable [DecidableEq Q]

omit [DecidableEq Q] in
lemma inc_reg (M : Q → Instr Q J) (start : Q) (s t : State Q) (v : J → ℕ) (a : ℕ) (j : J)
    (hi : compileInstr M s=.inc (.inl j) t) :
    Counter.step (compile M start) (cfg s v a)=some (cfg t (Function.update v j (v j+1)) a) := by
  simp only [Counter.step,compile,cfg,hi,Counter.next,Sum.elim_inl]
  congr 1
  congr 1
  funext i
  cases i <;> simp [Function.update_apply]
omit [DecidableEq Q] in
lemma inc_scratch (M : Q → Instr Q J) (start : Q) (s t : State Q) (v : J → ℕ) (a : ℕ)
    (hi : compileInstr M s=.inc scratch t) :
    Counter.step (compile M start) (cfg s v a)=some (cfg t v (a+1)) := by
  simp only [Counter.step,compile,cfg,hi,Counter.next,scratch,Sum.elim_inr,Bool.false_eq_true,ite_false]
  congr 1
  congr 1
  funext i
  rcases i with j|b
  · simp
  · cases b <;> simp
omit [DecidableEq Q] in
lemma test_reg (M : Q → Instr Q J) (start : Q) (s t u : State Q) (v : J → ℕ) (a : ℕ) (j : J)
    (hi : compileInstr M s=.test (.inl j) t u) :
    Counter.step (compile M start) (cfg s v a)=
      some (if v j=0 then cfg t v a else cfg u (Function.update v j (v j-1)) a) := by
  simp only [Counter.step,compile,cfg,hi,Counter.next,Sum.elim_inl]
  by_cases h : v j=0
  · simp [h]
  · simp only [h, ite_false]; congr 1; congr 1; funext i; cases i <;> simp [Function.update_apply]
omit [DecidableEq Q] in
lemma test_scratch (M : Q → Instr Q J) (start : Q) (s t u : State Q) (v : J → ℕ) (a : ℕ)
    (hi : compileInstr M s=.test scratch t u) :
    Counter.step (compile M start) (cfg s v a)=
      some (if a=0 then cfg t v a else cfg u v (a-1)) := by
  simp only [Counter.step,compile,cfg,hi,Counter.next,scratch,Sum.elim_inr,Bool.false_eq_true,ite_false]
  split_ifs
  · rfl
  · congr 1; congr 1; funext i
    rcases i with j|b
    · simp
    · cases b <;> simp
omit [DecidableEq Q] in
lemma goto (M : Q → Instr Q J) (start : Q) (s t : State Q) (v : J → ℕ) (a : ℕ)
    (hi : compileInstr M s=go t) :
    Counter.step (compile M start) (cfg s v a)=some (cfg t v a) := by
  simp [Counter.step,compile,cfg,hi,go,Counter.next,zeroReg]
end

section
open Counter StateTransition Relation
variable {Q J : Type} [DecidableEq Q] [DecidableEq J]
variable (M : Q → Instr Q J) (start : Q)
local notation "F" => Counter.step (compile M start)
omit [DecidableEq Q] in
lemma prepend {c d e : Counter.Cfg (State Q) (Reg J)}
    (ht : Reaches₁ F d e) (h : F c=some d) : Reaches₁ F c e :=
  TransGen.head (r:=fun c d=>d∈F c) h ht
omit [DecidableEq Q] in
lemma inc_at (s t : State Q) (v : J → ℕ) (a n : ℕ) (j : J)
    (hi : compileInstr M s=.inc (.inl j) t) :
    F (cfg s (Function.update v j n) a)=some (cfg t (Function.update v j (n+1)) a) := by
  simpa using inc_reg M start s t (Function.update v j n) a j hi
omit [DecidableEq Q] in
lemma test_at_zero (s t u : State Q) (v : J → ℕ) (a : ℕ) (j : J)
    (hi : compileInstr M s=.test (.inl j) t u) :
    F (cfg s (Function.update v j 0) a)=some (cfg t (Function.update v j 0) a) := by
  simpa using test_reg M start s t u (Function.update v j 0) a j hi
omit [DecidableEq Q] in
lemma test_at_succ (s t u : State Q) (v : J → ℕ) (a n : ℕ) (j : J)
    (hi : compileInstr M s=.test (.inl j) t u) :
    F (cfg s (Function.update v j (n+1)) a)=some (cfg u (Function.update v j n) a) := by
  simpa using test_reg M start s t u (Function.update v j (n+1)) a j hi

omit [DecidableEq Q] in
lemma grow_loop (q : Q) (i : Fin 5) (v : J → ℕ) (a : ℕ) :
    Reaches₁ F (cfg (.grow q i) v a) (cfg (.main q) v (a+(5-i.val))) := by
  induction i using Fin.reverseInduction generalizing a with
  | last =>
    exact TransGen.single (by simpa using inc_scratch M start (.grow q 4) (.main q) v a rfl)
  | cast i ih =>
    have hi : i.val < 4 := i.isLt
    have hs : succ5 i.castSucc=i.succ := by ext; simp [succ5]; omega
    have hh : compileInstr M (.grow q i.castSucc)=.inc scratch (.grow q i.succ) := by
      simp only [compileInstr,Fin.val_castSucc]
      rw [ite_eq_right (by omega),hs]
    have he := inc_scratch M start (.grow q i.castSucc) (.grow q i.succ) v a hh
    have ht := prepend M start (ih (a+1)) he
    convert ht using 1 ; congr 1 ; simp only [Fin.val_castSucc,Fin.val_succ] ; omega

omit [DecidableEq Q] in
lemma multiply_loop (q : Q) (j : J) (e : Fin 5) (p : Q) (hm : M q=.muladd j e p)
    (v : J → ℕ) (n a : ℕ) :
    Reaches₁ F (cfg (.main q) (Function.update v j n) a)
      (cfg (.copy q 0) (Function.update v j 0) (a+5*n)) := by
  induction n generalizing a with
  | zero =>
    apply TransGen.single
    simpa using test_at_zero M start (.main q) (.copy q 0) (.grow q 0) v a j (by simp [compileInstr,hm])
  | succ n ih =>
    have h1 := test_at_succ M start (.main q) (.copy q 0) (.grow q 0) v a n j (by simp [compileInstr,hm])
    have h2 := grow_loop M start q 0 (Function.update v j n) a
    have h3 := prepend M start (h2.trans (ih (a+5))) h1
    convert h3 using 1 ; congr 1 ; omega

omit [DecidableEq Q] in
lemma copy_loop (q : Q) (j : J) (r : Fin 5)
    (hm : (∃e p,M q=.muladd j e p) ∨ (∃b p,M q=.div j b p))
    (v : J → ℕ) (n a : ℕ) :
    Reaches₁ F (cfg (.copy q r) (Function.update v j n) a)
      (cfg (finish M q r) (Function.update v j (n+a)) 0) := by
  induction a generalizing n with
  | zero =>
    apply TransGen.single
    simpa using test_scratch M start (.copy q r) (finish M q r) (.copyInc q r) (Function.update v j n) 0 rfl
  | succ a ih =>
    have h1 := test_scratch M start (.copy q r) (finish M q r) (.copyInc q r) (Function.update v j n) (a+1) rfl
    simp only [Nat.add_eq_zero_iff, Nat.one_ne_zero, and_false, ite_false, Nat.add_sub_cancel] at h1
    have hh : compileInstr M (.copyInc q r)=.inc (.inl j) (.copy q r) := by
      rcases hm with ⟨e,p,h⟩|⟨b,p,h⟩ <;> simp [compileInstr,h]
    have h2 := inc_at M start (.copyInc q r) (.copy q r) v a n j hh
    simpa [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using prepend M start (prepend M start (ih (n+1)) h2) h1

omit [DecidableEq Q] in
lemma add_loop (q : Q) (j : J) (e : Fin 5) (p : Q) (hm : M q=.muladd j e p)
    (i : Fin 5) (hi : i.val ≤ e.val) (v : J → ℕ) (n a : ℕ) :
    Reaches₁ F (cfg (.add q i) (Function.update v j n) a)
      (cfg (.main p) (Function.update v j (n+(e.val-i.val))) a) := by
  induction i using Fin.reverseInduction generalizing n with
  | last =>
    have hh : e.val=4 := by have := e.isLt; simp only [Fin.val_last] at hi; omega
    apply TransGen.single
    simpa [hh] using goto M start (.add q 4) (.main p) (Function.update v j n) a (by simp [compileInstr,hm,hh])
  | cast i ih =>
    simp only [Fin.val_castSucc] at hi
    by_cases he : i.val=e.val
    · apply TransGen.single
      simpa [he] using goto M start (.add q i.castSucc) (.main p) (Function.update v j n) a (by simp [compileInstr,hm,he])
    · have hs : succ5 i.castSucc=i.succ := by ext; simp [succ5]; have := i.isLt; omega
      have h1 := inc_at M start (.add q i.castSucc) (.add q i.succ) v a n j (by
        simp only [compileInstr,hm,Fin.val_castSucc]; rw [ite_eq_left (by omega),hs])
      have h2 := prepend M start (ih (by simp only [Fin.val_succ]; omega) (n+1)) h1
      have har : n+(e.val-i.val)=(n+1)+(e.val-(i.val+1)) := by omega
      simpa only [Fin.val_castSucc,Fin.val_succ,har] using h2
end

open Counter StateTransition Relation
variable {Q J : Type} [DecidableEq Q] [DecidableEq J]
lemma radix_pos (b : Bool) : 0 < radix b := by cases b <;> decide
lemma radix_le (b : Bool) : radix b ≤ 5 := by cases b <;> decide
def mod5 (b : Bool) (n : ℕ) : Fin 5 := ⟨n%radix b,(Nat.mod_lt _ (radix_pos b)).trans_le (radix_le b)⟩
@[simp] lemma mod5_val (b : Bool) (n : ℕ) : (mod5 b n).val=n%radix b := rfl
lemma mod5_self (b : Bool) (r : Fin 5) (hr : r.val<radix b) : mod5 b r.val=r :=
  Fin.ext (Nat.mod_eq_of_lt hr)
variable (M : Q → Instr Q J) (start : Q)
local notation "F" => Counter.step (compile M start)
omit [DecidableEq Q] in
lemma quotient_loop (q : Q) (j : J) (b : Bool) (p : Fin 5 → Q) (hm : M q=.div j b p)
    (v : J → ℕ) (n : ℕ) (r : Fin 5) (hr : r.val<radix b) (a : ℕ) :
    Reaches₁ F (cfg (.quotient q r) (Function.update v j n) a)
      (cfg (.copy q (mod5 b (n+r.val))) (Function.update v j 0) (a+(n+r.val)/radix b)) := by
  induction n generalizing r a with
  | zero =>
    apply TransGen.single
    simpa [mod5_self b r hr, Nat.div_eq_of_lt hr] using test_at_zero M start (.quotient q r) (.copy q r)
      (if r.val+1=radix b then .carry q else .quotient q (succ5 r)) v a j (by simp [compileInstr,hm])
  | succ n ih =>
    have h1 := test_at_succ M start (.quotient q r) (.copy q r)
      (if r.val+1=radix b then .carry q else .quotient q (succ5 r)) v a n j (by simp [compileInstr,hm])
    by_cases he : r.val+1=radix b
    · simp only [he,ite_eq_left] at h1
      have h2 := inc_scratch M start (.carry q) (.quotient q 0) (Function.update v j n) a rfl
      have h3 := prepend M start (prepend M start (ih 0 (radix_pos b) (a+1)) h2) h1
      have har : n+1+r.val=n+radix b := by omega
      simpa [har,mod5,radix_pos b,Nat.add_div_right, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using h3
    · have hs : (succ5 r).val=r.val+1 := by
        apply Nat.mod_eq_of_lt
        have := radix_le b
        omega
      simp only [he,ite_false] at h1
      have h3 := prepend M start (ih (succ5 r) (by rw [hs]; omega) a) h1
      simpa only [hs, Nat.succ_eq_add_one, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using h3

omit [DecidableEq Q] in
lemma simulate (q : Q) (v : J → ℕ) :
    Reaches₁ F (cfg (.main q) v 0) (encode (next M q v)) := by
  cases hm : M q with
  | halt =>
    apply TransGen.single
    simpa [next,hm,encode] using goto M start (.main q) .halt v 0 (by simp [compileInstr,hm])
  | goto p =>
    apply TransGen.single
    simpa [next,hm,encode] using goto M start (.main q) (.main p) v 0 (by simp [compileInstr,hm])
  | inc j p =>
    apply TransGen.single
    simpa [next,hm,encode] using inc_reg M start (.main q) (.main p) v 0 j (by simp [compileInstr,hm])
  | test j pz pp =>
    have h1 := test_reg M start (.main q) (.main pz) (.restore q) v 0 j (by simp [compileInstr,hm])
    by_cases hz : v j=0
    · apply TransGen.single
      simpa [next,hm,encode,hz] using h1
    · simp only [hz,ite_false] at h1
      have h2 := inc_at M start (.restore q) (.main pp) v 0 (v j-1) j (by simp [compileInstr,hm])
      have har : v j-1+1=v j := by omega
      have ht : Reaches₁ F (cfg (.restore q) (Function.update v j (v j-1)) 0) (cfg (.main pp) v 0) :=
        TransGen.single (by simpa [har] using h2)
      simpa [next,hm,encode,hz] using prepend M start ht h1
  | muladd j e p =>
    have h1 := multiply_loop M start q j e p hm v (v j) 0
    have h2 := copy_loop M start q j 0 (Or.inl ⟨e,p,hm⟩) v 0 (5*v j)
    have h3 := add_loop M start q j e p hm 0 (Nat.zero_le _) v (5*v j) 0
    simp only [Nat.zero_add] at h1
    simp only [finish,hm,Nat.zero_add] at h2
    simp only [Fin.val_zero,Nat.sub_zero] at h3
    have ht := (h1.trans h2).trans h3
    simpa [next,hm,encode,Reaches₁] using ht
  | div j b p =>
    have h1 := goto M start (.main q) (.quotient q 0) v 0 (by simp [compileInstr,hm])
    have h2 := quotient_loop M start q j b p hm v (v j) 0 (radix_pos b) 0
    have h3 := copy_loop M start q j (mod5 b (v j)) (Or.inr ⟨b,p,hm⟩) v 0 (v j/radix b)
    simp only [Fin.val_zero,Nat.add_zero,Nat.zero_add,Function.update_eq_self] at h2
    simp only [finish,hm,Nat.zero_add] at h3
    simpa [next,hm,encode,mod5] using prepend M start (h2.trans h3) h1

omit [DecidableEq Q] in
lemma respects : Respects (step M) F (fun c d=>encode c=d) := by
  apply fun_respects.mpr
  intro c
  rcases c with ⟨label,v⟩
  cases label with
  | none => simp [step,encode,FRespects,Counter.step,compile,compileInstr,cfg]
  | some q => exact simulate M start q v
omit [DecidableEq Q] in
lemma eval_dom (c : Cfg Q J) :
    (eval F (encode c)).Dom ↔ (eval (step M) c).Dom :=
  tr_eval_dom (respects M start) rfl
end SingleFold.Macro

namespace SingleFold.Counter
open StateTransition Relation
variable {Q J : Type} [DecidableEq Q] [DecidableEq J]
omit [DecidableEq Q] in
lemma step_eq_none (M : Machine Q J) (c : Cfg Q J) : step M c=none ↔ c.state=M.halt := by
  rw [←M.halt_iff c.state]
  cases h : M.instr c.state <;> simp [step,h]
omit [DecidableEq Q] in
lemma reaches_iterate (M : Machine Q J) {c e : Cfg Q J} (h : Reaches (step M) c e) :
    ∃t,(next M)^[t] c=e := by
  induction h with
  | refl => exact ⟨0,rfl⟩
  | @tail d e h hd ih =>
    rcases ih with ⟨t,rfl⟩
    refine ⟨t+1,?_⟩
    rw [Function.iterate_succ_apply']
    change step M ((next M)^[t] c)=some e at hd
    unfold step at hd
    split at hd
    · contradiction
    · exact Option.some.inj hd
omit [DecidableEq Q] in
lemma reaches_run (M : Machine Q J) (d : J → ℕ) (t : ℕ) :
    Reaches (step M) ⟨M.start,d⟩ (run M d t) := by
  induction t with
  | zero => exact ReflTransGen.refl
  | succ t ih =>
    rw [run_succ]
    cases h : M.instr (run M d t).state with
    | halt => simpa only [next,h] using ih
    | inc j p => exact ih.tail (by simp [step,h])
    | test j pz pd => exact ih.tail (by simp [step,h])
lemma halts_iff_exists (M : Machine Q J) (d : J → ℕ) : Halts M d ↔ ∃t,(run M d t).state=M.halt := by
  constructor
  · rintro ⟨t,ht,_⟩; exact ⟨t,ht⟩
  · intro h
    exact ⟨Nat.find h,Nat.find_spec h,fun t ht=>Nat.find_min h ht⟩
lemma eval_dom_iff_halts (M : Machine Q J) (d : J → ℕ) :
    (eval (step M) ⟨M.start,d⟩).Dom ↔ Halts M d := by
  rw [halts_iff_exists]
  constructor
  · intro h
    obtain ⟨hr,hh⟩ := mem_eval.mp (Part.get_mem h)
    obtain ⟨t,ht⟩ := reaches_iterate M hr
    refine ⟨t,?_⟩
    have := (step_eq_none M _).mp hh
    simpa only [run,ht] using this
  · rintro ⟨t,ht⟩
    have h := mem_eval.mpr ⟨reaches_run M d t,(step_eq_none M _).mpr ht⟩
    exact Part.dom_iff_mem.mpr ⟨_,h⟩
end SingleFold.Counter

end OAI