theoremcompileInstr_correct {r s l : ℕ} (p : Program r s l)
(i : Instr r s l) (c : Cfg r s l) :
Turing.TM2.stepAux (compileInstr i) c.regs c.stacks = tmCfg p (execInstr i c) :=by
proof · 2
lines
cases i <;> simp [compileInstr, execInstr, tmCfg]
all_goals first | rfl | (split <;> rfl)
A source instruction is exactly one finite TM2 block execution.
theoremtm_step {r s l : ℕ} (p : Program r s l) (c : Cfg r s l) :
(tm p).step (tmCfg p c) = (step p c).map (tmCfg p) :=by
proof · 6
lines
rcases c with ⟨pc, regs, stk⟩
cases pc with
| none => rfl
| some label =>
change some (Turing.TM2.stepAux (compileInstr (p.code label)) regs stk) = _
exact congrArg some (compileInstr_correct p (p.code label) ⟨some label, regs, stk⟩)
theoremtm_trace {r s l : ℕ} (p : Program r s l) {a b : Cfg r s l} {n : ℕ}
(h : Trace p n a b) :
Computation.Trace (tm p).step n (tmCfg p a) (tmCfg p b) :=by
proof · 6
lines
induction h with
| refl a => exact .refl _
| cons hs ht ih =>
apply Computation.Trace.cons _ ih
rw [tm_step, hs]
rfl
theoremtm_runsIn {r s l : ℕ} (p : Program r s l) {a b : Cfg r s l} {n : ℕ}
(h : RunsIn p n a b) :
Computation.RunsIn (tm p).step n (tmCfg p a) (tmCfg p b) :=by
proof · 2
lines
obtain ⟨steps, hsteps, htrace⟩ := h
exact ⟨steps, hsteps, tm_trace p htrace⟩
theorembitMachine_outputs {r s l : ℕ} (p : Program r s l)
{input output : ListBool} {n : ℕ}
(h : RunsIn p n (initial p input) (terminal p output)) :
(bitMachine p).OutputsInTime input output n :=by
proof · 5
lines
rw [BitMachine.outputsInTime_iff_runsIn]
rw [bitMachine_input_map, bitMachine_output_map]
change Computation.RunsIn (tm p).step n
(Turing.initList (tm p) input) (Turing.haltList (tm p) output)
simpa only [tm_initial, tm_terminal] using tm_runsIn p h
The output is a fully specified primitive tape machine, obtained by the
previously verified finite-control and stack-block compilers.
noncomputableabbrevprimitiveCfg {r s l : ℕ} (p : Program r s l) (c : Cfg r s l) :
(primitive p).Cfg:=StackAdapter.compiledSourceCfg (bitMachine p) (tmCfg p c)
theoremprimitive_execution {r s l : ℕ} (p : Program r s l)
{a b : Cfg r s l} {n : ℕ} (h : RunsIn p n a b) :
Computation.RunsIn (primitive p).step (primitiveOverhead p * n)
(primitiveCfg p a) (primitiveCfg p b) :=
proof · 1
line
StackAdapter.compiled_execution (bitMachine p) (tm_runsIn p h)
theoremprimitive_trace {r s l : ℕ} (p : Program r s l)
{a b : Cfg r s l} {n : ℕ} (h : Trace p n a b) :
Computation.RunsIn (primitive p).step (primitiveOverhead p * n)
(primitiveCfg p a) (primitiveCfg p b) :=
@[simp]theoremprimitive_initial {r s l : ℕ} (p : Program r s l) (input : ListBool) :
primitiveCfg p (initial p input) = (primitive p).init input :=by
proof · 6
lines
change StackAdapter.compiledSourceCfg (bitMachine p) (tmCfg p (initial p input)) =
(StackAdapter.compiledMachine (bitMachine p)).init input
rw [tm_initial]
have h :=StackAdapter.compiled_initial (bitMachine p) input
rw [bitMachine_input_map] at h
exact h
theoremprimitive_terminal {r s l : ℕ} (p : Program r s l) (answer : Bool) :
(primitiveCfg p (terminal p [answer])).state = .inl answer :=by
proof · 6
lines
change (StackAdapter.compiledSourceCfg (bitMachine p)
(tmCfg p (terminal p [answer]))).state = .inl answer
rw [tm_terminal]
have h :=StackAdapter.compiled_terminal (bitMachine p) answer
rw [bitMachine_output_map] at h
exact h
theoremprimitive_outputs {r s l : ℕ} (p : Program r s l)
{input : ListBool} {answer : Bool} {n : ℕ}
(h : RunsIn p n (initial p input) (terminal p [answer])) :
(primitive p).OutputsInTime input answer (primitiveOverhead p * n) :=by
proof · 2
lines
refine ⟨primitiveCfg p (terminal p [answer]), ?_, primitive_terminal p answer⟩
simpa using primitive_execution p h
Halting exposes the first output bit, without a cleanup precondition on
the remaining stacks or the finite Boolean registers.
theoremprimitive_halted {r s l : ℕ} (p : Program r s l) (c : Cfg r s l)
(halted : c.pc = none) :
(primitiveCfg p c).state = .inl ((c.stacks p.outputStack).head?.getD false) :=by
proof · 5
lines
rcases c with ⟨pc, regs, stk⟩
simp only at halted
subst pc
change Sum.inl (StackAdapter.outputBit (bitMachine p) (stk p.outputStack).head?) = _
cases (stk p.outputStack).head? <;> rfl
theoremprimitive_outputs_of_halted {r s l : ℕ} (p : Program r s l)
{input : ListBool} {answer : Bool} {n : ℕ} {final : Cfg r s l}
(h : RunsIn p n (initial p input) final) (halted : final.pc = none)
(output : (final.stacks p.outputStack).head? = some answer) :
(primitive p).OutputsInTime input answer (primitiveOverhead p * n) :=by
proof · 4
lines
refine ⟨primitiveCfg p final, ?_, ?_⟩
· simpa using primitive_execution p h
· rw [primitive_halted p final halted, output]
rfl