| .skip | .inc _ | .dec _ | .write _ | .discard | .rewind =>Unit
| .seq a b =>Label a ⊕ Label b
| .test _ a b =>Unit ⊕ Label a ⊕ Label b
| .read a b c =>Unit ⊕ Label a ⊕ Label b ⊕ Label c
| .loop _ b =>Unit ⊕ Label b
noncomputableinstancelabelFintype (c : CommandR) : Fintype (Label c) :=by
body · 6
lines
induction c with
| skip | inc | dec | write | discard | rewind => exact inferInstanceAs (FintypeUnit)
| seq a b ia ib => exact inferInstanceAs (Fintype (Label a ⊕ Label b))
| test r a b ia ib => exact inferInstanceAs (Fintype (Unit ⊕ Label a ⊕ Label b))
| read a b c ia ib ic => exact inferInstanceAs (Fintype (Unit ⊕ Label a ⊕ Label b ⊕ Label c))
| loop r b ib => exact inferInstanceAs (Fintype (Unit ⊕ Label b))
| skip (d) : Evaluates .skip d d 1
| inc (d r) : Evaluates (.inc r) d (incData d r) 1
| dec (d r) : Evaluates (.dec r) d (decData d r) 1
| write (d b) : Evaluates (.write b) d (writeData d b) 1
| discard (d) : Evaluates .discard d (discardData d) 1
| rewind_empty {d} : d.backup = [] →Evaluates .rewind d d 1
| rewind_cons {d e n b rest} : d.backup = b :: rest →Evaluates .rewind (backData d) e n →Evaluates .rewind d e (1 + n)
| seq {a b d e f n m} : Evaluates a d e n →Evaluates b e f m →Evaluates (.seq a b) d f (n + m)
| test_zero {r a b d e n} : d.reg r = 0→Evaluates a d e n →Evaluates (.test r a b) d e (1 + n)
| test_pos {r a b d e n} : d.reg r ≠0→Evaluates b d e n →Evaluates (.test r a b) d e (1 + n)
| read_one {a b c d e n rest} : d.input = true :: rest →Evaluates a (readData d) e n →Evaluates (.read a b c) d e (1 + n)
| read_zero {a b c d e n rest} : d.input = false :: rest →Evaluates b (readData d) e n →Evaluates (.read a b c) d e (1 + n)
| read_empty {a b c d e n} : d.input = [] →Evaluates c (readData d) e n →Evaluates (.read a b c) d e (1 + n)
| loop_zero {r b d} : d.reg r = 0→Evaluates (.loop r b) d d 1
| loop_pos {r b d e f n m} : d.reg r ≠0→Evaluates b (decData d r) e n →Evaluates (.loop r b) e f m →Evaluates (.loop r b) d f (1 + n + m)
theoremRuns.trans {p : L→InstructionRL} {n m : ℕ} {l l' l'' : L} {d e f : DataR}
(h : Runs p n l d l' e) (h' : Runs p m l' e l'' f) : Runs p (n + m) l d l'' f :=by
proof · 2
lines
unfold Runs at *
rw [Nat.add_comm n m, Function.iterate_add_apply, h, h']
theoremtrace {c : CommandR} {d d' : DataR} {n : ℕ} (h : Evaluates c d d' n)
(p : L→InstructionRL) (labels : c.Label→L) (exit : L)
(atLabels : ∀ l, p (labels l) = c.code labels exit l) :
Runs p n (labels c.entry) d exit d' :=by
proof · 61
lines
induction h generalizing Lwith
| skip d =>
apply runs_one
rw [atLabels]; rfl
| inc d r =>
apply runs_one
rw [atLabels]; rfl
| dec d r =>
apply runs_one
rw [atLabels]
simp [code, Instruction.execute, decData]
| write d b =>
apply runs_one
rw [atLabels]; rfl
| discard d =>
apply runs_one
rw [atLabels]; rfl
| @rewind_empty d hd =>
apply runs_one
rw [atLabels]
simp [code, Instruction.execute, hd]
cases d; simp_all
| @rewind_cons d e n b rest hd he ih =>
refine Runs.trans (runs_one p _ _ _ _ ?_) (ih p labels exit atLabels)
rw [atLabels]
simp [code, entry, Instruction.execute, backData, hd]
| @seq a b d e f n m h h' ih ih' =>
exact Runs.trans
(ih p (fun l => labels (.inl l)) (labels (.inr b.entry)) (fun l => atLabels (.inl l)))
(ih' p (fun l => labels (.inr l)) exit (fun l => atLabels (.inr l)))
| @test_zero r a b d e n hr h ih =>
refine Runs.trans (runs_one p _ _ _ _ ?_) (ih p _ exit (fun l => atLabels (.inr (.inl l))))
rw [atLabels]
simp [code, entry, Instruction.execute, hr]
| @test_pos r a b d e n hr h ih =>
refine Runs.trans (runs_one p _ _ _ _ ?_) (ih p _ exit (fun l => atLabels (.inr (.inr l))))
rw [atLabels]
simp [code, entry, Instruction.execute, hr]
| @read_one a b c d e n rest hin h ih =>
refine Runs.trans (runs_one p _ _ _ _ ?_) (ih p _ exit (fun l => atLabels (.inr (.inl l))))
rw [atLabels]
simp [code, entry, Instruction.execute, readData, hin]
| @read_zero a b c d e n rest hin h ih =>
refine Runs.trans (runs_one p _ _ _ _ ?_) (ih p _ exit (fun l => atLabels (.inr (.inr (.inl l)))))
rw [atLabels]
simp [code, entry, Instruction.execute, readData, hin]
| @read_empty a b c d e n hin h ih =>
refine Runs.trans (runs_one p _ _ _ _ ?_) (ih p _ exit (fun l => atLabels (.inr (.inr (.inr l)))))
rw [atLabels]
simp [code, entry, Instruction.execute, readData, hin]
| @loop_zero r b d hr =>
apply runs_one
rw [atLabels]
simp [code, entry, Instruction.execute, hr]
rw [← hr, Function.update_eq_self]
| @loop_pos r b d e f n m hr h h' ih ih' =>
refine Runs.trans (Runs.trans (runs_one p _ _ _ _ ?_)
(ih p (fun l => labels (.inr l)) (labels (.inl ())) (fun l => atLabels (.inr l))))
(ih' p labels exit atLabels)
rw [atLabels]
simp [code, entry, Instruction.execute, decData, hr]