{"ok":true,"data":{"commit":"118eb9c9aec819f90f93b9ba0ecefa9f0d2d3b6a","path":"PvsNP/Complexity/P.lean","oid":"a8fe3b6b5d49468b3a9c8e7adaaaa3b93cbafd3c","size":2908,"range_start":0,"range_end":2908,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Basic.Poly\nimport PvsNP.Complexity.Time\n\n/-!\n# The class P\n\n`InP L`: the alphabet is finite and some multi-tape machine decides `L` within a polynomial number\nof steps. Finiteness of the alphabet is part of the definition: over an infinite alphabet a single\nsymbol carries unbounded information (a machine reading one `Nat` could test membership in the\nhalting set), so P is empty there by definition rather than meaningless.\n\nProved here: the full and the empty language are in P, and P is closed under complement.\n-/\n\nnamespace PvsNP\n\nvariable {α : Type}\n\n/-- `L ∈ P`: over a finite alphabet, a machine decides `L` in polynomial time. -/\ndef InP (L : Language α) : Prop :=\n  IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ DecidesIn M L T\n\n/-- A machine that halts at once, accepting or not as `b` says, touching nothing. -/\ndef Machine.const (α : Type) (k : Nat) (b : Bool) : Machine α k where\n  Q := Unit\n  finite := inferInstance\n  start := ()\n  halted _ := true\n  accepting _ := b\n  δ q r := (q, r, fun _ => .stay)\n\nnamespace InP\n\n/-- The time bound of a language in P can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/\ntheorem polyBound {L : Language α} (h : InP L) :\n    ∃ (k : Nat) (M : Machine α k) (c d : Nat), DecidesIn M L (PvsNP.polyBound c d) := by\n  obtain ⟨_, k, M, T, ⟨c, d, hT⟩, hM⟩ := h\n  exact ⟨k, M, c, d, hM.mono hT⟩\n\n/-- Only languages over finite alphabets are in P. -/\ntheorem isFinite {L : Language α} (h : InP L) : IsFinite α := h.1\n\ntheorem univ [IsFinite α] : InP (Language.univ : Language α) :=\n  ⟨inferInstance, 0, Machine.const α 0 true, fun _ => 0, PolyBounded.const 0, fun _ =>\n    ⟨rfl, by simp [Machine.const, Language.univ, Machine.init]⟩⟩\n\ntheorem empty [IsFinite α] : InP (Language.empty : Language α) :=\n  ⟨inferInstance, 0, Machine.const α 0 false, fun _ => 0, PolyBounded.const 0, fun _ =>\n    ⟨rfl, by simp [Machine.const, Language.empty, Machine.init]⟩⟩\n\n/-- **P is closed under complement**: swap accepting and rejecting halted states. -/\ntheorem compl {L : Language α} (h : InP L) : InP L.compl := by\n  obtain ⟨hα, k, M, T, hT, hM⟩ := h\n  refine ⟨hα, k, M.withAccepting (fun q => !M.accepting q), T, hT, fun x => ?_⟩\n  have e : (M.withAccepting (fun q => !M.accepting q)).init (inputTapes x) = M.init (inputTapes x) :=\n    rfl\n  rw [e, Machine.run_withAccepting]\n  refine ⟨(hM x).1, ?_⟩\n  simp only [Machine.withAccepting, Bool.not_eq_eq_eq_not, Bool.not_true, Language.compl_apply,\n    ← (hM x).2, Bool.not_eq_true]\n\ntheorem of_compl {L : Language α} (h : InP L.compl) : InP L := by\n  obtain ⟨hα, k, M, T, hT, hM⟩ := h.compl\n  exact ⟨hα, k, M, T, hT, fun x => ⟨(hM x).1, by rw [(hM x).2, Language.compl_compl]⟩⟩\n\ntheorem compl_iff {L : Language α} : InP L.compl ↔ InP L := ⟨of_compl, compl⟩\n\nend InP\n\nend PvsNP\n"},"meta":{"request_id":"18959848-3b0","took_us":118,"limits":{"limit":600,"remaining":151,"window_ms":60000,"reset_ms":44832,"cost":1,"streams":{"limit":16,"open":0}}}}