# PvsNP/Complexity/Time.lean @ 3fc07c697bd3c08bd25c3bfbd35fedc0aa5234d1

````
import PvsNP.Language
import PvsNP.Machine.Lift
import PvsNP.Machine.Compose

/-!
# Time-bounded computation

Three things a machine can do within a time bound `T : Nat → Nat`, always measured in the length of the
input `x`:

- `DecidesIn M L T`: on input `x`, `M` has halted after `T |x|` steps, accepting exactly when `x ∈ L`.
- `ComputesIn M f T`: on input `x`, `M` has halted after `T |x|` steps, leaving `f x` on tape `0` with
  every other tape blank and every head back at `0`. This *clean* output convention is what lets a
  second machine start right where the first one stopped.
- `VerifiesIn M L T`: on input `x` with any certificate `w` on tape `1`, `M` has halted after `T |x|`
  steps; and `x ∈ L` exactly when some certificate makes it accept. The time bound depends on `|x|`
  only, so a verifier can read at most `T |x|` symbols of any certificate (Sipser's definition).

Halting is absorbing, so each of these is monotone in `T`.
-/

namespace PvsNP

variable {α : Type} {k : Nat}

/-- `M` decides `L` within `T |x|` steps. -/
def DecidesIn (M : Machine α k) (L : Language α) (T : Nat → Nat) : Prop :=
  ∀ x, M.halted (M.run (M.init (inputTapes x)) (T x.length)).state = true ∧
    (M.accepting (M.run (M.init (inputTapes x)) (T x.length)).state = true ↔ L x)

/-- `M` computes `f` within `T |x|` steps, leaving its output in the start position. -/
def ComputesIn (M : Machine α k) (f : List α → List α) (T : Nat → Nat) : Prop :=
  ∀ x, M.halted (M.run (M.init (inputTapes x)) (T x.length)).state = true ∧
    (M.run (M.init (inputTapes x)) (T x.length)).tapes = inputTapes (f x)

/-- `M` verifies `L` within `T |x|` steps, the certificate on tape `1`. -/
def VerifiesIn (M : Machine α k) (L : Language α) (T : Nat → Nat) : Prop :=
  (∀ x w, M.halted (M.run (M.init (verifierTapes x w)) (T x.length)).state = true) ∧
    ∀ x, L x ↔ ∃ w, M.accepting (M.run (M.init (verifierTapes x w)) (T x.length)).state = true

/-! ## More time never hurts -/

theorem DecidesIn.mono {M : Machine α k} {L : Language α} {T T' : Nat → Nat} (h : DecidesIn M L T)
    (hT : ∀ n, T n ≤ T' n) : DecidesIn M L T' := by
  intro x
  rw [M.run_stable (h x).1 (hT x.length)]
  exact h x

theorem ComputesIn.mono {M : Machine α k} {f : List α → List α} {T T' : Nat → Nat}
    (h : ComputesIn M f T) (hT : ∀ n, T n ≤ T' n) : ComputesIn M f T' := by
  intro x
  rw [M.run_stable (h x).1 (hT x.length)]
  exact h x

theorem VerifiesIn.mono {M : Machine α k} {L : Language α} {T T' : Nat → Nat} (h : VerifiesIn M L T)
    (hT : ∀ n, T n ≤ T' n) : VerifiesIn M L T' := by
  refine ⟨fun x w => ?_, fun x => ?_⟩
  · rw [M.run_stable (h.1 x w) (hT x.length)]; exact h.1 x w
  · rw [h.2 x]
    exact exists_congr fun w => by rw [M.run_stable (h.1 x w) (hT x.length)]

/-! ## More tapes never hurt -/

theorem DecidesIn.lift {M : Machine α k} {L : Language α} {T : Nat → Nat} {k' : Nat} (hk : k ≤ k')
    (h : DecidesIn M L T) : DecidesIn (M.lift (Embedding.castLE hk)) L T := by
  intro x
  have e : (M.lift (Embedding.castLE hk)).init (inputTapes x) =
      M.liftCfg (Embedding.castLE hk) (inputTapes x) (M.init (inputTapes x)) := by
    simp only [Machine.init, Machine.liftCfg, liftTapes_castLE_inputTapes]; rfl
  rw [e, Machine.run_lift]
  exact h x

theorem ComputesIn.lift {M : Machine α k} {f : List α → List α} {T : Nat → Nat} {k' : Nat}
    (hk : k ≤ k') (h : ComputesIn M f T) : ComputesIn (M.lift (Embedding.castLE hk)) f T := by
  intro x
  have e : (M.lift (Embedding.castLE hk)).init (inputTapes x) =
      M.liftCfg (Embedding.castLE hk) (inputTapes x) (M.init (inputTapes x)) := by
    simp only [Machine.init, Machine.liftCfg, liftTapes_castLE_inputTapes]; rfl
  rw [e, Machine.run_lift]
  refine ⟨(h x).1, ?_⟩
  simp only [Machine.liftCfg, (h x).2, liftTapes_castLE_inputTapes]

theorem VerifiesIn.lift {M : Machine α k} {L : Language α} {T : Nat → Nat} {k' : Nat} (hk : k ≤ k')
    (h : VerifiesIn M L T) : VerifiesIn (M.lift (Embedding.castLE hk)) L T := by
  have e : ∀ x w, (M.lift (Embedding.castLE hk)).init (verifierTapes x w) =
      M.liftCfg (Embedding.castLE hk) (verifierTapes x w) (M.init (verifierTapes x w)) := by
    intro x w
    simp only [Machine.init, Machine.liftCfg, liftTapes_castLE_verifierTapes]; rfl
  refine ⟨fun x w => ?_, fun x => ?_⟩
  · rw [e, Machine.run_lift]; exact h.1 x w
  · rw [h.2 x]
    exact exists_congr fun w => by rw [e, Machine.run_lift]; rfl

end PvsNP

````

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