import Problems.PVersusNP.Millennium import Mathlib.Tactic.DeriveFintype /-! # A linear-time palindrome decider Copy the input into two reversed work stacks. Move the scratch copy back onto the input stack, then drain the input and the retained reversed copy in parallel. The output alphabet is Boolean, independently of the finite input alphabet. -/ namespace Millennium.Examples.Palindrome open Turing Computability StateTransition inductive Stack where | input | scratch | backwards | output deriving DecidableEq, Fintype inductive Phase where | copy | restore | compare deriving DecidableEq, Fintype abbrev Alphabet (α : Type) : Stack → Type | .input => α | .scratch | .backwards => Option α | .output => Bool abbrev Register (α : Type) := Option α × Bool /-- In the successful restore branch a symbol exists. Keeping the fallback inside this instruction lets the machine work for the empty alphabet too. -/ private noncomputable def putInput (α : Type) : TM2.Stmt (Alphabet α) Phase (Register α) := by classical exact if h : Nonempty α then .push .input (fun v => v.1.getD (Classical.choice h)) (.goto fun _ => .restore) else .halt noncomputable def machine (α : Type) [Fintype α] [DecidableEq α] : FinTM2 where K := Stack k₀ := .input k₁ := .output Γ := Alphabet α Λ := Phase main := .copy σ := Register α initialState := (none, true) Γk₀Fin := inferInstanceAs (Fintype α) m | .copy => .pop .input (fun v x => (x, v.2)) (.branch (fun v => v.1.isSome) (.push .scratch (fun v => v.1) (.push .backwards (fun v => v.1) (.goto fun _ => .copy))) (.goto fun _ => .restore)) | .restore => .pop .scratch (fun v x => (x.join, v.2)) (.branch (fun v => v.1.isSome) (putInput α) (.goto fun _ => .compare)) | .compare => .pop .input (fun v x => (x, v.2)) (.branch (fun v => v.1.isSome) (.pop .backwards (fun v x => (none, v.2 && decide (v.1 = x.join))) (.goto fun _ => .compare)) (.push .output (fun v => v.2) (.load (fun _ => (none, true)) .halt))) variable {α : Type} [Fintype α] [DecidableEq α] /-- Reachable configurations store only real symbols on the work stacks. -/ def cfg (l : Option Phase) (v : Register α) (inp scratch backwards : List α) (out : List Bool) : (machine α).Cfg where l := l var := v stk | .input => inp | .scratch => scratch.map some | .backwards => backwards.map some | .output => out private def Go (phase : Phase) (v : Register α) (inp s r : List α) (out : List Bool) := cfg (some phase) v inp s r out macro "pal_step" : tactic => `(tactic| (simp [Go, FinTM2.step, TM2.step, TM2.stepAux, machine, cfg, putInput, *] try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) theorem copy_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : (machine α).step (Go .copy v (a :: inp) s r out) = some (Go .copy (some a, v.2) inp (a :: s) (a :: r) out) := by pal_step theorem copy_nil (v : Register α) (s r : List α) (out : List Bool) : (machine α).step (Go .copy v [] s r out) = some (Go .restore (none, v.2) [] s r out) := by pal_step theorem restore_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : (machine α).step (Go .restore v inp (a :: s) r out) = some (Go .restore (some a, v.2) (a :: inp) s r out) := by have hex : Nonempty α := ⟨a⟩ pal_step theorem restore_nil (v : Register α) (inp r : List α) (out : List Bool) : (machine α).step (Go .restore v inp [] r out) = some (Go .compare (none, v.2) inp [] r out) := by pal_step theorem compare_cons (v : Register α) (a b : α) (xs ys : List α) (out : List Bool) : (machine α).step (Go .compare v (a :: xs) [] (b :: ys) out) = some (Go .compare (none, v.2 && decide (a = b)) xs [] ys out) := by pal_step theorem compare_nil (v : Register α) (out : List Bool) : (machine α).step (Go .compare v [] [] [] out) = some (cfg none (none, true) ([] : List α) [] [] (v.2 :: out)) := by pal_step end Millennium.Examples.Palindrome