cfahlgren1/math / lean /OAI /Algorithms /SubexponentialSAT /BitProgramCompile.lean
cfahlgren1's picture
download
raw
9.04 kB
15
def compileInstr {r s l : ℕ} (i : Instr r s l) :
    Turing.TM2.Stmt (fun _ : Fin s => Bool) (Fin l) (Fin r → Bool) :=
body · 22 lines
  match i with
  | .halt => .halt
  | .goto next => .goto (fun _ => next)
  | .set dst value next =>
      .load (fun regs => Function.update regs dst value) (.goto (fun _ => next))
  | .copy dst src next =>
      .load (fun regs => Function.update regs dst (regs src)) (.goto (fun _ => next))
  | .bnot dst src next =>
      .load (fun regs => Function.update regs dst (!(regs src))) (.goto (fun _ => next))
  | .band dst a b next =>
      .load (fun regs => Function.update regs dst (regs a && regs b)) (.goto (fun _ => next))
  | .bor dst a b next =>
      .load (fun regs => Function.update regs dst (regs a || regs b)) (.goto (fun _ => next))
  | .bxor dst a b next =>
      .load (fun regs => Function.update regs dst (regs a ^^ regs b)) (.goto (fun _ => next))
  | .branch test yes no =>
      .branch (fun regs => regs test) (.goto (fun _ => yes)) (.goto (fun _ => no))
  | .push stack src next => .push stack (fun regs => regs src) (.goto (fun _ => next))
  | .peek stack value present next =>
      .peek stack (fun regs top => readRegs regs value present top) (.goto (fun _ => next))
  | .pop stack value present next =>
      .pop stack (fun regs top => readRegs regs value present top) (.goto (fun _ => next))

All three finite dimensions are determined by the fixed source program.

41
abbrev tm {r s l : ℕ} (p : Program r s l) : Turing.FinTM2 where
body · 9 lines
  K := Fin s
  k₀ := p.inputStack
  k₁ := p.outputStack
  Γ := fun _ => Bool
  Λ := Fin l
  main := p.entry
  σ := Fin r → Bool
  initialState := fun _ => false
  m := fun label => compileInstr (p.code label)
52
abbrev bitMachine {r s l : ℕ} (p : Program r s l) : BitMachine where
body · 4 lines
  tm := tm p
  inputAlphabet := Equiv.refl Bool
  outputAlphabet := Equiv.refl Bool
  finiteAlphabet := fun _ => (inferInstance : Finite Bool)
58
@[simp] theorem bitMachine_inputAlphabet_symm_apply {r s l : ℕ}
    (p : Program r s l) (b : Bool) :
    (bitMachine p).inputAlphabet.symm b = b := rfl
62
@[simp] theorem bitMachine_outputAlphabet_symm_apply {r s l : ℕ}
    (p : Program r s l) (b : Bool) :
    (bitMachine p).outputAlphabet.symm b = b := rfl
66
@[simp] theorem bitMachine_input_map {r s l : ℕ}
    (p : Program r s l) (input : List Bool) :
    input.map (bitMachine p).inputAlphabet.symm = input := by
proof · 4 lines
  induction input with
  | nil => rfl
  | cons b input ih =>
      rw [List.map_cons, bitMachine_inputAlphabet_symm_apply, ih]
74
@[simp] theorem bitMachine_output_map {r s l : ℕ}
    (p : Program r s l) (output : List Bool) :
    output.map (bitMachine p).outputAlphabet.symm = output := by
proof · 4 lines
  induction output with
  | nil => rfl
  | cons b output ih =>
      rw [List.map_cons, bitMachine_outputAlphabet_symm_apply, ih]
82
abbrev tmCfg {r s l : ℕ} (p : Program r s l) (c : Cfg r s l) : (tm p).Cfg :=
body · 1 line
  ⟨c.pc, c.regs, c.stacks⟩
85
theorem compileInstr_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.

92
theorem tm_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⟩)
101
theorem tm_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
111
theorem tm_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⟩
117
@[simp] theorem tm_initial {r s l : ℕ} (p : Program r s l) (input : List Bool) :
    tmCfg p (initial p input) = Turing.initList (tm p) input := by
proof · 1 line
  congr 1
121
@[simp] theorem tm_terminal {r s l : ℕ} (p : Program r s l) (output : List Bool) :
    tmCfg p (terminal p output) = Turing.haltList (tm p) output := by
proof · 1 line
  congr 1
125
theorem bitMachine_outputs {r s l : ℕ} (p : Program r s l)
    {input output : List Bool} {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.

137
noncomputable abbrev primitive {r s l : ℕ} (p : Program r s l) : PrimitiveMachine :=
body · 1 line
  StackAdapter.compiledMachine (bitMachine p)
140
noncomputable abbrev primitiveCfg {r s l : ℕ} (p : Program r s l) (c : Cfg r s l) :
    (primitive p).Cfg := StackAdapter.compiledSourceCfg (bitMachine p) (tmCfg p c)
143
noncomputable abbrev primitiveOverhead {r s l : ℕ} (p : Program r s l) : ℕ :=
body · 1 line
  StackAdapter.overhead (bitMachine p)
146
theorem primitiveOverhead_pos {r s l : ℕ} (p : Program r s l) :
    0 < primitiveOverhead p := StackAdapter.overhead_pos _
149
theorem primitive_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)
155
theorem primitive_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) :=
proof · 1 line
  primitive_execution p ⟨n, le_rfl, h⟩
161
@[simp] theorem primitive_initial {r s l : ℕ} (p : Program r s l) (input : List Bool) :
    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
170
theorem primitive_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
179
theorem primitive_outputs {r s l : ℕ} (p : Program r s l)
    {input : List Bool} {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.

188
theorem primitive_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
197
theorem primitive_outputs_of_halted {r s l : ℕ} (p : Program r s l)
    {input : List Bool} {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

Xet Storage Details

Size:
9.04 kB
·
Xet hash:
5aa8032e1d07095dac2cb824ab2125df091805dc403827f81b9e5e956f7f3830

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.