import Problems.PVersusNP.Millennium import Problems.PVersusNP.ForMathlib.Computable import Problems.PVersusNP.ForMathlib.StateTransition import Mathlib.Tactic.DeriveFintype /-! # A length guard for certificate checkers Scan the two blocks of the canonical input and save the word. Using unary stacks, start at one and multiply by the left length once for each unit of the fixed exponent. Compare the result with the right length. On acceptance, restore the saved word and start the original verifier. On rejection, discard it and return false. The guard needs only finitely many labels and does not assume closure of polynomial time under composition. -/ namespace Millennium.Machines.CertificateBound open Computability Turing StateTransition noncomputable section private inductive Aux | saved | base | temp | count | next | cert deriving DecidableEq, Fintype private inductive Phase (k : ℕ) | scan (right : Bool) | power (i : Fin (k + 1)) | mul (i : Fin (k + 1)) | copy (i : Fin (k + 1)) | putBack (i : Fin (k + 1)) | transfer (i : Fin (k + 1)) | clearBase | compare (ok : Bool) | drain (ok : Bool) | restore (ok : Bool) deriving Fintype private abbrev Alphabet (tm : FinTM2) : tm.K ⊕ Aux → Type := Sum.elim tm.Γ fun | .saved => tm.Γ tm.k₀ | _ => Unit private abbrev Register (tm : FinTM2) := Option (tm.Γ tm.k₀) × tm.σ private abbrev Stmt (tm : FinTM2) (k : ℕ) := TM2.Stmt (Alphabet tm) (Phase k ⊕ tm.Λ) (Register tm) private def liftStmt {tm : FinTM2} {k : ℕ} : tm.Stmt → Stmt tm k | .push j f q => .push (.inl j) (fun v => f v.2) (liftStmt q) | .peek j f q => .peek (.inl j) (fun v x => (v.1, f v.2 x)) (liftStmt q) | .pop j f q => .pop (.inl j) (fun v x => (v.1, f v.2 x)) (liftStmt q) | .load f q => .load (fun v => (v.1, f v.2)) (liftStmt q) | .branch f q r => .branch (fun v => f v.2) (liftStmt q) (liftStmt r) | .goto f => .goto (fun v => .inr (f v.2)) | .halt => .halt private def jump {tm : FinTM2} {k : ℕ} (label : Phase k ⊕ tm.Λ) : Stmt tm k := .load (fun _ => (none, tm.initialState)) (.goto fun _ => label) private def remember {tm : FinTM2} (_ : Register tm) (x : Option (tm.Γ tm.k₀)) : Register tm := (x, tm.initialState) private def mark {tm : FinTM2} (sep : tm.Γ tm.k₀) (_ : Register tm) (x : Option Unit) : Register tm := (x.map (fun _ => sep), tm.initialState) private def prev {k : ℕ} (i : Fin (k + 1)) : Fin (k + 1) := ⟨i.val - 1, by omega⟩ private def guard (tm : FinTM2) (sep : tm.Γ tm.k₀) (reject : tm.Γ tm.k₁) (k : ℕ) : Phase k → Stmt tm k := by classical exact fun | .scan right => .pop (.inl tm.k₀) remember (.branch (fun v => v.1.isSome) (.push (.inr .saved) (fun v => v.1.getD sep) (if right then .push (.inr .cert) (fun _ => ()) (jump (.inl (.scan true))) else .branch (fun v => decide (v.1 = some sep)) (.push (.inr .count) (fun _ => ()) (jump (.inl (.scan true)))) (.push (.inr .base) (fun _ => ()) (jump (.inl (.scan false)))))) (jump (.inl (.power ⟨k, by omega⟩)))) | .power i => if i.val = 0 then jump (.inl .clearBase) else jump (.inl (.mul i)) | .mul i => .pop (.inr .count) (mark sep) (.branch (fun v => v.1.isSome) (jump (.inl (.copy i))) (jump (.inl (.transfer i)))) | .copy i => .pop (.inr .base) (mark sep) (.branch (fun v => v.1.isSome) (.push (.inr .temp) (fun _ => ()) (.push (.inr .next) (fun _ => ()) (jump (.inl (.copy i))))) (jump (.inl (.putBack i)))) | .putBack i => .pop (.inr .temp) (mark sep) (.branch (fun v => v.1.isSome) (.push (.inr .base) (fun _ => ()) (jump (.inl (.putBack i)))) (jump (.inl (.mul i)))) | .transfer i => .pop (.inr .next) (mark sep) (.branch (fun v => v.1.isSome) (.push (.inr .count) (fun _ => ()) (jump (.inl (.transfer i)))) (jump (.inl (.power (prev i))))) | .clearBase => .pop (.inr .base) (mark sep) (.branch (fun v => v.1.isSome) (jump (.inl .clearBase)) (jump (.inl (.compare true)))) | .compare ok => .pop (.inr .cert) (mark sep) (.branch (fun v => v.1.isSome) (.pop (.inr .count) (mark sep) (.branch (fun v => v.1.isSome) (jump (.inl (.compare ok))) (jump (.inl (.compare false))))) (jump (.inl (.drain ok)))) | .drain ok => .pop (.inr .count) (mark sep) (.branch (fun v => v.1.isSome) (jump (.inl (.drain ok))) (jump (.inl (.restore ok)))) | .restore ok => .pop (.inr .saved) remember (.branch (fun v => v.1.isSome) (if ok then .push (.inl tm.k₀) (fun v => v.1.getD sep) (jump (.inl (.restore true))) else jump (.inl (.restore false))) (if ok then jump (.inr tm.main) else .push (.inl tm.k₁) (fun _ => reject) (.load (fun _ => (none, tm.initialState)) .halt))) private abbrev guarded (tm : FinTM2) (sep : tm.Γ tm.k₀) (reject : tm.Γ tm.k₁) (k : ℕ) : FinTM2 where K := tm.K ⊕ Aux kFin := by letI := tm.kFin; infer_instance k₀ := .inl tm.k₀ k₁ := .inl tm.k₁ Γ := Alphabet tm Λ := Phase k ⊕ tm.Λ ΛFin := by letI := tm.ΛFin; infer_instance main := .inl (.scan false) σ := Register tm σFin := by letI := tm.Γk₀Fin; letI := tm.σFin; infer_instance initialState := (none, tm.initialState) Γk₀Fin := tm.Γk₀Fin m := Sum.elim (guard tm sep reject k) (fun label => liftStmt (tm.m label)) private def liftStacks {tm : FinTM2} (st : ∀ j, List (tm.Γ j)) : ∀ j, List (Alphabet tm j) | .inl j => st j | .inr _ => [] private def liftCfg {tm : FinTM2} {k : ℕ} (c : tm.Cfg) : TM2.Cfg (Alphabet tm) (Phase k ⊕ tm.Λ) (Register tm) := ⟨c.l.map Sum.inr, (none, c.var), liftStacks c.stk⟩ private theorem liftStacks_update {tm : FinTM2} (st : ∀ j, List (tm.Γ j)) (j : tm.K) (xs : List (tm.Γ j)) : Function.update (liftStacks st) (.inl j) xs = liftStacks (Function.update st j xs) := by funext h cases h with | inl h => by_cases eq : h = j · subst h; simp [liftStacks] · have different : (Sum.inl h : tm.K ⊕ Aux) ≠ .inl j := fun e => eq (Sum.inl.inj e) simp [liftStacks, Function.update_of_ne different, Function.update_of_ne eq] | inr h => simp [liftStacks, Function.update_of_ne] private theorem liftStmt_correct {tm : FinTM2} {k : ℕ} (p : tm.Stmt) (v : tm.σ) (st : ∀ j, List (tm.Γ j)) : TM2.stepAux (liftStmt (k := k) p) (none, v) (liftStacks st) = liftCfg (TM2.stepAux p v st) := by induction p generalizing v st with | push j f q ih => change TM2.stepAux (liftStmt q) (none, v) (Function.update (liftStacks st) (.inl j) (f v :: st j)) = _ rw [liftStacks_update] exact ih v _ | pop j f q ih => change TM2.stepAux (liftStmt q) (none, f v (st j).head?) (Function.update (liftStacks st) (.inl j) (st j).tail) = _ rw [liftStacks_update] exact ih _ _ | peek j f q ih => exact ih _ _ | load f q ih => exact ih _ _ | branch f q r ih ir => cases hf : f v <;> simp only [liftStmt, TM2.stepAux, hf, cond_false, cond_true, ih, ir] | goto f => rfl | halt => rfl private theorem liftStep {tm : FinTM2} {sep : tm.Γ tm.k₀} {reject : tm.Γ tm.k₁} {k : ℕ} (c : tm.Cfg) : (guarded tm sep reject k).step (liftCfg c) = (tm.step c).map liftCfg := by rcases c with ⟨l, v, st⟩ cases l with | none => rfl | some label => exact congrArg some (liftStmt_correct (tm.m label) v st) private def liftRun {tm : FinTM2} {sep : tm.Γ tm.k₀} {reject : tm.Γ tm.k₁} {k : ℕ} {a b : tm.Cfg} (run : EvalsTo tm.step a (some b)) : EvalsTo (guarded tm sep reject k).step (liftCfg a) (some (liftCfg b)) := by let send : Option tm.Cfg → Option (guarded tm sep reject k).Cfg := Option.map liftCfg have commutes : Function.Semiconj send (flip bind tm.step) (flip bind (guarded tm sep reject k).step) := by intro o cases o with | none => rfl | some c => exact (liftStep c).symm refine ⟨run.steps, ?_⟩ exact ((commutes.iterate_right run.steps) (some a)).symm.trans (congrArg send run.evals_in_steps) private structure Work (A : Type) where input : List A := [] saved : List A := [] base : ℕ := 0 temp : ℕ := 0 count : ℕ := 0 next : ℕ := 0 cert : ℕ := 0 private def checkpoint {tm : FinTM2} {k : ℕ} (p : Phase k) (w : Work (tm.Γ tm.k₀)) : TM2.Cfg (Alphabet tm) (Phase k ⊕ tm.Λ) (Register tm) where l := some (.inl p) var := (none, tm.initialState) stk | .inl j => (initList tm w.input).stk j | .inr .saved => w.saved | .inr .base => List.replicate w.base () | .inr .temp => List.replicate w.temp () | .inr .count => List.replicate w.count () | .inr .next => List.replicate w.next () | .inr .cert => List.replicate w.cert () variable {tm : FinTM2} {sep : tm.Γ tm.k₀} {reject : tm.Γ tm.k₁} {k : ℕ} private abbrev Run (tm : FinTM2) (sep : tm.Γ tm.k₀) (reject : tm.Γ tm.k₁) (k : ℕ) (a b : (guarded tm sep reject k).Cfg) := EvalsTo (guarded tm sep reject k).step a (some b) private def beginStep {a b c : (guarded tm sep reject k).Cfg} (step : (guarded tm sep reject k).step a = some b) (rest : Run tm sep reject k b c) : Run tm sep reject k a c := EvalsTo.trans _ _ _ _ (EvalsToInTime.of_step step).toEvalsTo rest private def oneStep {a b : (guarded tm sep reject k).Cfg} (step : (guarded tm sep reject k).step a = some b) : Run tm sep reject k a b := (EvalsToInTime.of_step step).toEvalsTo macro "certificate_step" : tactic => do let machine := Lean.mkIdent `tm `(tactic| (simp [FinTM2.step, TM2.step, TM2.stepAux, guarded, guard, jump, mark, remember, checkpoint, liftCfg, liftStacks, List.replicate_succ, *] <;> (try apply congrArg some) <;> congr 1 <;> (funext s cases s with | inl j => by_cases hj : j = ($machine).k₀ <;> by_cases ho : j = ($machine).k₁ <;> simp_all [initList, haltList, Function.update_of_ne] | inr j => cases j <;> simp_all [Function.update_of_ne]))) private def scanRight (ys saved : List (tm.Γ tm.k₀)) (base cert : ℕ) : Run tm sep reject k (checkpoint (.scan true) { input := ys, saved := saved, base := base, count := 1, cert := cert }) (checkpoint (.power ⟨k, by omega⟩) { saved := ys.reverse ++ saved, base := base, count := 1, cert := cert + ys.length }) := by induction ys generalizing saved cert with | nil => apply oneStep; certificate_step | cons y ys ih => refine beginStep ?_ (by simpa [List.reverse_cons, List.append_assoc, Nat.add_assoc, Nat.add_comm 1] using ih (y :: saved) (cert + 1)) certificate_step end end Millennium.Machines.CertificateBound