roseking's picture
Add lean source and binary files (part 64)
92416a2 verified
Raw History Blame Contribute Delete
14.7 kB
import Mathlib.Computability.TuringMachine.Computable
namespace OAI
/-!
# Certified reversal and relabeling between finite alphabets
The actual TM2 machine transfers a list over a finite alphabet `α` to a second
stack over `β`, reverses its order, and applies a fixed map `f : α → β` to each
symbol. The trace has exactly `input.length + 1` transitions. An explicit
fallback symbol makes the push expression total, including the unused branch
where the internal register is empty.
This primitive supports machine composition when intermediate stack alphabets
differ. It does not yet implement the Unique Games reduction.
-/
namespace UniqueGamesTheorem.Reduction.MachineTransfer
open Turing
abbrev alphabet (α β : Type) : Bool → Type
| false => α
| true => β
variable {α β : Type} [Fintype α] [Fintype β]
def loop (f : α → β) (fallback : β) :
TM2.Stmt (alphabet α β) Unit (Option β) :=
.pop false (fun _ head => head.map f)
(.branch Option.isSome
(.push true (fun state => state.getD fallback) (.goto fun _ => ()))
.halt)
def machine (f : α → β) (fallback : β) : FinTM2 where
K := Bool
k₀ := false
k₁ := true
Γ := alphabet α β
Λ := Unit
main := ()
σ := Option β
initialState := none
Γk₀Fin := inferInstanceAs (Fintype α)
m _ := loop f fallback
def tapeStacks (input : List α) (output : List β) :
(side : Bool) → List (alphabet α β side)
| false => input
| true => output
def running (f : α → β) (fallback : β) (input : List α) (output : List β)
(state : Option β) : (machine f fallback).Cfg :=
⟨some (), state, tapeStacks input output⟩
def halted (f : α → β) (fallback : β) (output : List β) : (machine f fallback).Cfg :=
⟨none, none, tapeStacks (α := α) [] output⟩
omit [Fintype α] [Fintype β] in
private theorem update_input (input replacement : List α) (output : List β) :
Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by
funext side
cases side <;> rfl
omit [Fintype α] [Fintype β] in
private theorem update_output (input : List α) (output replacement : List β) :
Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by
funext side
cases side <;> rfl
theorem step_empty (f : α → β) (fallback : β) (output : List β) (state : Option β) :
(machine f fallback).step (running f fallback [] output state) =
some (halted f fallback output) := by
change some (TM2.stepAux (loop f fallback) state (tapeStacks (α := α) [] output)) = _
simp [loop, TM2.stepAux, tapeStacks, halted, Function.update]
rw [update_input]
rfl
theorem step_cons (f : α → β) (fallback : β) (head : α) (input : List α)
(output : List β) (state : Option β) :
(machine f fallback).step (running f fallback (head :: input) output state) =
some (running f fallback input (f head :: output) (some (f head))) := by
change some (TM2.stepAux (loop f fallback) state (tapeStacks (head :: input) output)) = _
simp [loop, TM2.stepAux, tapeStacks, running, Function.update]
rw [update_input, update_output]
rfl
def next (f : α → β) (fallback : β) (configuration : Option (machine f fallback).Cfg) :
Option (machine f fallback).Cfg := configuration.bind (machine f fallback).step
/-- Exact actual-machine trace, for any accumulator and internal register. -/
theorem transfer_steps (f : α → β) (fallback : β) (input : List α) (output : List β)
(state : Option β) :
(next f fallback)^[input.length + 1] (some (running f fallback input output state)) =
some (halted f fallback (input.reverse.map f ++ 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.map_nil, List.nil_append]
using step_empty f fallback output state
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (next f fallback)^[input.length + 1]
((machine f fallback).step (running f fallback (head :: input) output state)) = _
rw [step_cons, ih]
simp only [List.reverse_cons, List.map_append, List.map_singleton,
List.append_assoc, List.singleton_append]
theorem initList_eq (f : α → β) (fallback : β) (input : List α) :
initList (machine f fallback) input = running f fallback input [] none := by
unfold initList running
congr 1
funext side
cases side <;> rfl
theorem haltList_eq (f : α → β) (fallback : β) (output : List β) :
haltList (machine f fallback) output = halted f fallback output := by
unfold haltList halted
congr 1
funext side
cases side <;> rfl
theorem transfer_init_steps (f : α → β) (fallback : β) (input : List α) :
(next f fallback)^[input.length + 1] (some (initList (machine f fallback) input)) =
some (haltList (machine f fallback) (input.reverse.map f)) := by
calc
_ = (next f fallback)^[input.length + 1]
(some (running f fallback input [] none)) :=
congrArg (fun c => (next f fallback)^[input.length + 1] (some c))
(initList_eq f fallback input)
_ = some (halted f fallback (input.reverse.map f)) := by
simpa only [List.append_nil] using transfer_steps f fallback input [] none
_ = _ := congrArg some (haltList_eq f fallback (input.reverse.map f)).symm
def outputsInTime (f : α → β) (fallback : β) (input : List α) :
TM2OutputsInTime (machine f fallback) input (some (input.reverse.map f))
(input.length + 1) where
steps := input.length + 1
evals_in_steps := transfer_init_steps f fallback input
steps_le_m := Nat.le_refl _
@[simp] theorem outputsInTime_steps (f : α → β) (fallback : β) (input : List α) :
(outputsInTime f fallback input).steps = input.length + 1 := rfl
/-- A genuine finite-machine certificate with identity encodings and bound `X+1`. -/
noncomputable def computableInPolyTime (f : α → β) (fallback : β) :
TM2ComputableInPolyTime (id : List α → List α) (id : List β → List β)
(fun input => input.reverse.map f) where
tm := machine f fallback
inputAlphabet := Equiv.refl α
outputAlphabet := Equiv.refl β
time := Polynomial.X + 1
outputsFun input := by
change TM2OutputsInTime (machine f fallback) (input.map id)
(some ((input.reverse.map f).map id))
((Polynomial.X + 1 : Polynomial Nat).eval input.length)
have hi := @List.map_id ((machine f fallback).Γ (machine f fallback).k₀) input
have ho := @List.map_id ((machine f fallback).Γ (machine f fallback).k₁)
(input.reverse.map f)
erw [hi, ho]
simpa only [Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one]
using outputsInTime f fallback input
variable {K Λ σ : Type} {Γ : K → Type} [DecidableEq K]
def exitAt (dst : K) (exit : Option Λ) : TM2.Stmt Γ Λ (σ × Option (Γ dst)) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
def loopAt (src dst : K) (f : Γ src → Γ dst) (fallback : Γ dst)
(loopLabel : Λ) (exit : Option Λ) : TM2.Stmt Γ Λ (σ × Option (Γ dst)) :=
.pop src (fun state head => (state.1, head.map f))
(.branch (fun state => state.2.isSome)
(.push dst (fun state => state.2.getD fallback) (.goto fun _ => loopLabel))
(exitAt dst exit))
def tapesAt (src dst : K) (base : (k : K) → List (Γ k))
(input : List (Γ src)) (output : List (Γ dst)) : (k : K) → List (Γ k) :=
Function.update (Function.update base src input) dst output
@[simp] theorem tapesAt_src (src dst : K) (distinct : src ≠ dst)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) :
tapesAt src dst base input output src = input := by
simp [tapesAt, distinct]
@[simp] theorem tapesAt_dst (src dst : K)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) :
tapesAt src dst base input output dst = output := by
simp [tapesAt]
theorem tapesAt_other (src dst k : K) (notSrc : k ≠ src) (notDst : k ≠ dst)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) :
tapesAt src dst base input output k = base k := by
simp [tapesAt, notSrc, notDst]
@[simp] theorem tapesAt_self (src dst : K) (base : (k : K) → List (Γ k)) :
tapesAt src dst base (base src) (base dst) = base := by
simp [tapesAt]
private theorem update_tapesAt_src (src dst : K) (distinct : src ≠ dst)
(base : (k : K) → List (Γ k)) (input replacement : List (Γ src))
(output : List (Γ dst)) :
Function.update (tapesAt src dst base input output) src replacement =
tapesAt src dst base replacement output := by
funext k
by_cases hs : k = src
· subst k
simp [tapesAt, distinct]
· by_cases hd : k = dst
· subst k
simp [tapesAt, Ne.symm distinct]
· simp [tapesAt, hs, hd]
private theorem update_tapesAt_dst (src dst : K)
(base : (k : K) → List (Γ k)) (input : List (Γ src))
(output replacement : List (Γ dst)) :
Function.update (tapesAt src dst base input output) dst replacement =
tapesAt src dst base input replacement := by
funext k
by_cases hd : k = dst
· subst k
simp [tapesAt]
· simp [tapesAt, hd]
def nextAt (dst : K) (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(configuration : Option (TM2.Cfg Γ Λ (σ × Option (Γ dst)))) :
Option (TM2.Cfg Γ Λ (σ × Option (Γ dst))) :=
configuration.bind (TM2.step program)
theorem stepAt_empty (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (output : List (Γ dst))
(ambient : σ) (register : Option (Γ dst)) :
TM2.step program ⟨some loopLabel, (ambient, register), tapesAt src dst base [] output⟩ =
some ⟨exit, (ambient, none), tapesAt src dst base [] output⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(tapesAt src dst base [] output)) = _
rw [atLoop]
cases exit <;>
simp [loopAt, exitAt, TM2.stepAux, tapesAt_src, distinct, update_tapesAt_src]
theorem stepAt_cons (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (head : Γ src) (input : List (Γ src))
(output : List (Γ dst)) (ambient : σ) (register : Option (Γ dst)) :
TM2.step program
⟨some loopLabel, (ambient, register), tapesAt src dst base (head :: input) output⟩ =
some ⟨some loopLabel, (ambient, some (f head)),
tapesAt src dst base input (f head :: output)⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(tapesAt src dst base (head :: input) output)) = _
rw [atLoop]
simp [loopAt, TM2.stepAux, tapesAt_src, tapesAt_dst, distinct,
update_tapesAt_src, update_tapesAt_dst]
/-- Placement-aware exact transfer, preserving all ambient state and other tapes.
The optional exit label is reached in the same final empty-pop transition. -/
theorem transferAt_steps (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst))
(ambient : σ) (register : Option (Γ dst)) :
(nextAt dst program)^[input.length + 1]
(some ⟨some loopLabel, (ambient, register), tapesAt src dst base input output⟩) =
some ⟨exit, (ambient, none), tapesAt src dst base [] (input.reverse.map f ++ output)⟩ := by
induction input generalizing output register with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, nextAt,
Option.bind_some, List.reverse_nil, List.map_nil, List.nil_append]
using stepAt_empty src dst distinct f fallback loopLabel exit program atLoop base output
ambient register
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (nextAt dst program)^[input.length + 1]
(TM2.step program
⟨some loopLabel, (ambient, register), tapesAt src dst base (head :: input) output⟩) = _
rw [stepAt_cons src dst distinct f fallback loopLabel exit program atLoop, ih]
simp only [List.reverse_cons, List.map_append, List.map_singleton,
List.append_assoc, List.singleton_append]
/-- The same trace starting with an arbitrary actual ambient tape family. -/
theorem transferAt_fromTapes (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (ambient : σ) (register : Option (Γ dst)) :
(nextAt dst program)^[(base src).length + 1]
(some ⟨some loopLabel, (ambient, register), base⟩) =
some ⟨exit, (ambient, none),
tapesAt src dst base [] ((base src).reverse.map f ++ base dst)⟩ := by
simpa only [tapesAt_self] using
transferAt_steps src dst distinct f fallback loopLabel exit program atLoop base
(base src) (base dst) ambient register
/-- A timed execution witness suitable for composing this bridge with other
phases of the same actual transition system. -/
def transferAtInTime (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (ambient : σ) (register : Option (Γ dst)) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some loopLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none),
tapesAt src dst base [] ((base src).reverse.map f ++ base dst)⟩)
((base src).length + 1) where
steps := (base src).length + 1
evals_in_steps := transferAt_fromTapes src dst distinct f fallback loopLabel exit
program atLoop base ambient register
steps_le_m := Nat.le_refl _
end UniqueGamesTheorem.Reduction.MachineTransfer
end OAI