# PvsNP/Complexity/P.lean @ f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da

````
import PvsNP.Basic.Poly
import PvsNP.Complexity.Time

/-!
# The class P

`InP L`: some multi-tape machine decides `L` within a polynomial number of steps.

Proved here: the full and the empty language are in P, and P is closed under complement.
-/

namespace PvsNP

variable {α : Type}

/-- `L ∈ P`: a machine decides `L` in polynomial time. -/
def InP (L : Language α) : Prop :=
  ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ DecidesIn M L T

/-- A machine that halts at once, accepting or not as `b` says, touching nothing. -/
def Machine.const (α : Type) (k : Nat) (b : Bool) : Machine α k where
  Q := Unit
  finite := inferInstance
  start := ()
  halted _ := true
  accepting _ := b
  δ q r := (q, r, fun _ => .stay)

namespace InP

/-- The time bound of a language in P can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/
theorem polyBound {L : Language α} (h : InP L) :
    ∃ (k : Nat) (M : Machine α k) (c d : Nat), DecidesIn M L (PvsNP.polyBound c d) := by
  obtain ⟨k, M, T, ⟨c, d, hT⟩, hM⟩ := h
  exact ⟨k, M, c, d, hM.mono hT⟩

theorem univ : InP (Language.univ : Language α) :=
  ⟨0, Machine.const α 0 true, fun _ => 0, PolyBounded.const 0, fun _ =>
    ⟨rfl, by simp [Machine.const, Language.univ, Machine.init]⟩⟩

theorem empty : InP (Language.empty : Language α) :=
  ⟨0, Machine.const α 0 false, fun _ => 0, PolyBounded.const 0, fun _ =>
    ⟨rfl, by simp [Machine.const, Language.empty, Machine.init]⟩⟩

/-- **P is closed under complement**: swap accepting and rejecting halted states. -/
theorem compl {L : Language α} (h : InP L) : InP L.compl := by
  obtain ⟨k, M, T, hT, hM⟩ := h
  refine ⟨k, M.withAccepting (fun q => !M.accepting q), T, hT, fun x => ?_⟩
  have e : (M.withAccepting (fun q => !M.accepting q)).init (inputTapes x) = M.init (inputTapes x) :=
    rfl
  rw [e, Machine.run_withAccepting]
  refine ⟨(hM x).1, ?_⟩
  simp only [Machine.withAccepting, Bool.not_eq_eq_eq_not, Bool.not_true, Language.compl_apply,
    ← (hM x).2, Bool.not_eq_true]

theorem of_compl {L : Language α} (h : InP L.compl) : InP L := by
  obtain ⟨k, M, T, hT, hM⟩ := h.compl
  exact ⟨k, M, T, hT, fun x => ⟨(hM x).1, by rw [(hM x).2, Language.compl_compl]⟩⟩

theorem compl_iff {L : Language α} : InP L.compl ↔ InP L := ⟨of_compl, compl⟩

end InP

end PvsNP

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da&path=PvsNP%2FComplexity%2FP.lean`
