import Problems.PVersusNP.Millennium /-! # Even parity in polynomial time An explicit two-stack machine reads each input bit once, toggling an even-parity flag on `true`, then writes that flag and halts. Its configuration-level run takes at most `input.length + 1` steps. The accepted language is stated using `List.count true`, and `evenTrueCount_in_P` requires no composition hypothesis. -/ namespace Millennium.Examples.Parity open Turing Computability StateTransition /-- Toggle the running parity flag exactly when the input bit is `true`. -/ def toggle (a v : Bool) : Bool := if a then !v else v /-- The even-parity answer, expressed independently of the machine's state. -/ def evenParity : List Bool → Bool | [] => true | a :: inp => if a then !(evenParity inp) else evenParity inp /-- The language of Boolean strings with an even number of `true` symbols. -/ def evenTrueCount (inp : List Bool) : Prop := inp.count true % 2 = 0 /-- The recursive Boolean answer agrees with counting the `true` symbols. -/ theorem evenParity_eq_true_iff (inp : List Bool) : evenParity inp = true ↔ evenTrueCount inp := by induction inp with | nil => simp [evenParity, evenTrueCount] | cons a inp ih => cases a with | false => simpa [evenParity, evenTrueCount] using ih | true => have hnot : (!(evenParity inp)) = true ↔ ¬ (evenParity inp = true) := by cases evenParity inp <;> decide change (!(evenParity inp)) = true ↔ (true :: inp).count true % 2 = 0 rw [hnot, ih] have hcount : (true :: inp).count true = inp.count true + 1 := by simp rw [hcount] unfold evenTrueCount omega /-- Run the parity update from an arbitrary initial flag. -/ def parityAfter : List Bool → Bool → Bool | [], v => v | a :: inp, v => parityAfter inp (toggle a v) /-- Starting with the opposite flag complements the final answer. -/ theorem parityAfter_eq (inp : List Bool) (v : Bool) : parityAfter inp v = if v then evenParity inp else !(evenParity inp) := by induction inp generalizing v with | nil => cases v <;> rfl | cons a inp ih => rw [parityAfter, ih] cases a <;> cases v <;> simp [toggle, evenParity] /-- The first state bit records whether the last pop succeeded, and the second is the running even-parity flag. Input and output both use the Boolean alphabet. -/ def parityMachine : FinTM2 where K := Bool k₀ := true k₁ := false Γ := fun _ => Bool Λ := Unit main := () σ := Bool × Bool initialState := (false, true) Γk₀Fin := inferInstanceAs (Fintype Bool) m _ := TM2.Stmt.pop true (fun v x => (x.isSome, match x with | none => v.2 | some a => toggle a v.2)) (TM2.Stmt.branch (fun v => v.1) (TM2.Stmt.goto fun _ => ()) (TM2.Stmt.push false (fun v => v.2) (TM2.Stmt.load (fun _ => (false, true)) TM2.Stmt.halt))) /-- Explicit configurations for the parity machine. -/ def cfg (l : Option Unit) (v : Bool × Bool) (inp out : List Bool) : parityMachine.Cfg where l := l var := v stk | true => inp | false => out macro "parity_step_tac" : tactic => `(tactic| (simp [FinTM2.step, TM2.step, TM2.stepAux, parityMachine, cfg] try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) /-- Reading a symbol updates the flag and returns to the same instruction. -/ theorem step_cons (v : Bool × Bool) (a : Bool) (inp out : List Bool) : parityMachine.step (cfg (some ()) v (a :: inp) out) = some (cfg (some ()) (true, toggle a v.2) inp out) := by parity_step_tac /-- The empty-input step outputs the accumulated flag, resets the state and halts. -/ theorem step_nil (v : Bool × Bool) (out : List Bool) : parityMachine.step (cfg (some ()) v [] out) = some (cfg none (false, true) [] (v.2 :: out)) := by parity_step_tac /-- Weaken a timed computation's upper bound. -/ private def timedMono {σ : Type} {f : σ → Option σ} {a : σ} {b : Option σ} {m m' : ℕ} (h : EvalsToInTime f a b m) (hm : m ≤ m') : EvalsToInTime f a b m' := ⟨h.toEvalsTo, le_trans h.steps_le_m hm⟩ /-- Package an explicit machine transition as a one-step timed run. -/ private def oneStep {a b : parityMachine.Cfg} (h : parityMachine.step a = some b) : EvalsToInTime parityMachine.step a (some b) 1 := { steps := 1 evals_in_steps := by simpa [flip, Option.bind] using h steps_le_m := le_rfl } /-- One transition per input symbol, followed by one transition writing the answer. -/ def run (inp : List Bool) (v : Bool × Bool) (out : List Bool) : EvalsToInTime parityMachine.step (cfg (some ()) v inp out) (some (cfg none (false, true) [] (parityAfter inp v.2 :: out))) (inp.length + 1) := by induction inp generalizing v with | nil => simpa [parityAfter] using oneStep (step_nil v out) | cons a inp ih => have h₁ := oneStep (step_cons v a inp out) have h₂ := ih (true, toggle a v.2) have h := EvalsToInTime.trans parityMachine.step 1 (inp.length + 1) _ _ _ h₁ h₂ exact timedMono h (by simp) /-- The canonical initial configuration starts with even parity. -/ theorem initList_eq (inp : List Bool) : initList parityMachine inp = cfg (some ()) (false, true) inp [] := by unfold initList cfg congr funext k cases k <;> rfl /-- The canonical halted configuration has the reset state and only its output stack. -/ theorem haltList_eq (out : List Bool) : haltList parityMachine out = cfg none (false, true) [] out := by unfold haltList cfg congr funext k cases k <;> rfl /-- The explicit parity machine computes the Boolean answer with time polynomial `X + 1`. -/ set_option pp.all true in noncomputable def parityDecider : TM2ComputableInPolyTime (fin_encoding_string Bool).encode finEncodingBoolBool.encode evenParity where tm := parityMachine inputAlphabet := Equiv.refl _ outputAlphabet := Equiv.refl _ time := Polynomial.X + 1 outputsFun inp := by show EvalsToInTime parityMachine.step (initList parityMachine (List.map id inp)) (some (haltList parityMachine (List.map id [evenParity inp]))) ((Polynomial.X + 1 : Polynomial ℕ).eval inp.length) rw [List.map_id, List.map_id, initList_eq, haltList_eq] simpa [parityAfter_eq] using run inp (false, true) [] /-- Even-`true`-count Boolean strings are in P, with no composition hypothesis. -/ theorem evenTrueCount_in_P : InPolynomialTime (fin_encoding_string Bool) evenTrueCount := ⟨evenParity, parityDecider, fun inp => (evenParity_eq_true_iff inp).symm⟩ #print axioms evenTrueCount_in_P end Millennium.Examples.Parity