File size: 4,810 Bytes
92416a2 | 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 | import Mathlib.Computability.TuringMachine.Computable
namespace OAI
namespace UniqueGamesTheorem.Reduction.MachineReverse
open Turing
def loop : TM2.Stmt (fun _ : Bool => Bool) Unit (Option Bool) :=
.pop false (fun _ head => head)
(.branch Option.isSome
(.push true (fun state => state.getD false) (.goto fun _ => ()))
.halt)
def machine : FinTM2 where
K := Bool
k₀ := false
k₁ := true
Γ _ := Bool
Λ := Unit
main := ()
σ := Option Bool
initialState := none
m _ := loop
def tapeStacks (input output : List Bool) : Bool → List Bool :=
fun side => if side then output else input
def running (input output : List Bool) (state : Option Bool) : machine.Cfg :=
⟨some (), state, tapeStacks input output⟩
def halted (output : List Bool) : machine.Cfg :=
⟨none, none, tapeStacks [] output⟩
private theorem update_input (input output replacement : List Bool) :
Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by
funext side
cases side <;> simp [tapeStacks]
private theorem update_output (input output replacement : List Bool) :
Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by
funext side
cases side <;> simp [tapeStacks]
/-- Popping an empty input resets the internal state to `none` before halting. -/
theorem step_empty (output : List Bool) (state : Option Bool) :
machine.step (running [] output state) = some (halted output) := by
change some (TM2.stepAux loop state (tapeStacks [] output)) = some (halted output)
simp [loop, TM2.stepAux, tapeStacks, halted, Function.update]
rw [update_input]
rfl
/-- One transition moves the head symbol from the input onto the output. -/
theorem step_cons (head : Bool) (input output : List Bool) (state : Option Bool) :
machine.step (running (head :: input) output state) =
some (running input (head :: output) (some head)) := by
change some (TM2.stepAux loop state (tapeStacks (head :: input) output)) = _
simp [loop, TM2.stepAux, tapeStacks, running, Function.update]
rw [update_input, update_output]
rfl
def next (configuration : Option machine.Cfg) : Option machine.Cfg :=
configuration.bind machine.step
/-- An exact transition trace, with arbitrary initial accumulator and state. -/
theorem reverse_steps (input output : List Bool) (state : Option Bool) :
next^[input.length + 1] (some (running input output state)) =
some (halted (input.reverse ++ output)) := by
induction input generalizing output state with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, next,
Option.bind_some, List.reverse_nil, List.nil_append] using step_empty output state
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change next^[input.length + 1]
(machine.step (running (head :: input) output state)) = _
rw [step_cons, ih]
simp only [List.reverse_cons, List.append_assoc, List.singleton_append]
theorem initList_eq (input : List Bool) :
initList machine input = running input [] none := by
unfold initList running
congr 1
funext side
cases side <;> rfl
theorem haltList_eq (output : List Bool) : haltList machine output = halted output := by
unfold haltList halted
congr 1
/-- The exact trace starts and ends at the standard encoded TM2 configurations. -/
theorem reverse_init_steps (input : List Bool) :
next^[input.length + 1] (some (initList machine input)) =
some (haltList machine input.reverse) := by
rw [initList_eq, haltList_eq]
simpa only [List.append_nil] using reverse_steps input [] none
def outputsInTime (input : List Bool) :
TM2OutputsInTime machine input (some input.reverse) (input.length + 1) where
steps := input.length + 1
evals_in_steps := reverse_init_steps input
steps_le_m := Nat.le_refl _
@[simp] theorem outputsInTime_steps (input : List Bool) :
(outputsInTime input).steps = input.length + 1 := rfl
/-- Actual finite-machine polynomial-time computability, with identity encodings. -/
noncomputable def computableInPolyTime :
TM2ComputableInPolyTime (id : List Bool → List Bool) id List.reverse where
tm := machine
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := Polynomial.X + 1
outputsFun input := by
change TM2OutputsInTime machine (input.map id) (some (input.reverse.map id))
((Polynomial.X + 1 : Polynomial Nat).eval input.length)
have hi := @List.map_id (machine.Γ machine.k₀) input
have ho := @List.map_id (machine.Γ machine.k₁) input.reverse
rw [hi, ho]
simpa only [Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one]
using outputsInTime input
end UniqueGamesTheorem.Reduction.MachineReverse
end OAI
|