# PvsNP/Machine/Compose.lean @ 7598bc4245baed6bd68ea2defb9aa40da8e0070d

````
import PvsNP.Machine.Defs

/-!
# Running one machine after another

`M₁.seq M₂` runs `M₁`; when `M₁` halts it spends one step switching to `M₂`'s start state, touching no
tape, and then runs `M₂` on the tapes `M₁` left behind. Its states are `M₁.Q ⊕ M₂.Q`, still finite.

`Machine.run_seq` is the composition theorem: if `M₁` halts within `t₁` steps and `M₂`, started on what
`M₁` left, halts within `t₂` steps, then the composite halts within `t₁ + 1 + t₂` steps in the
configuration `M₂` reaches.
-/

namespace PvsNP

namespace Machine

variable {α : Type} {k : Nat}

/-- Run `M₁`, then `M₂` on the tapes `M₁` halts with. -/
def seq (M₁ M₂ : Machine α k) : Machine α k where
  Q := M₁.Q ⊕ M₂.Q
  finite := @IsFinite.instSum _ _ M₁.finite M₂.finite
  start := .inl M₁.start
  halted
    | .inl _ => false
    | .inr q => M₂.halted q
  accepting
    | .inl _ => false
    | .inr q => M₂.accepting q
  δ
    | .inl q, r =>
      if M₁.halted q then (.inr M₂.start, r, fun _ => .stay)
      else let o := M₁.δ q r; (.inl o.1, o.2.1, o.2.2)
    | .inr q, r => let o := M₂.δ q r; (.inr o.1, o.2.1, o.2.2)

variable (M₁ M₂ : Machine α k)

/-- The composite's configuration while it is running `M₁`. -/
def inlCfg (c : Cfg α k M₁.Q) : Cfg α k (M₁.seq M₂).Q := ⟨.inl c.state, c.tapes⟩

/-- The composite's configuration once it is running `M₂`. -/
def inrCfg (c : Cfg α k M₂.Q) : Cfg α k (M₁.seq M₂).Q := ⟨.inr c.state, c.tapes⟩

theorem apply_stay (T : Fin (k + 1) → Tape α) : apply T (reads T) (fun _ => .stay) = T := by
  funext i; simp [apply, reads]

theorem step_seq_inl_running (c : Cfg α k M₁.Q) (h : M₁.halted c.state = false) :
    (M₁.seq M₂).step (inlCfg M₁ M₂ c) = inlCfg M₁ M₂ (M₁.step c) := by
  simp [step, seq, inlCfg, h]

theorem step_seq_inl_halted (c : Cfg α k M₁.Q) (h : M₁.halted c.state = true) :
    (M₁.seq M₂).step (inlCfg M₁ M₂ c) = inrCfg M₁ M₂ (M₂.init c.tapes) := by
  simp [step, seq, inlCfg, inrCfg, init, h, apply_stay]

theorem step_seq_inr (c : Cfg α k M₂.Q) :
    (M₁.seq M₂).step (inrCfg M₁ M₂ c) = inrCfg M₁ M₂ (M₂.step c) := by
  by_cases h : M₂.halted c.state = true
  · simp [step, seq, inrCfg, h]
  · simp [step, seq, inrCfg, h]

theorem run_seq_inr (c : Cfg α k M₂.Q) (t : Nat) :
    (M₁.seq M₂).run (inrCfg M₁ M₂ c) t = inrCfg M₁ M₂ (M₂.run c t) := by
  induction t generalizing c with
  | zero => rfl
  | succ t ih => rw [run_succ, step_seq_inr, ih, run_succ]

/-- If `M₁` has halted by step `t`, the composite has switched to `M₂` by step `t + 1`. -/
theorem run_seq_switch (c : Cfg α k M₁.Q) (t : Nat) (h : M₁.halted (M₁.run c t).state = true) :
    ∃ s, s ≤ t + 1 ∧
      (M₁.seq M₂).run (inlCfg M₁ M₂ c) s = inrCfg M₁ M₂ (M₂.init (M₁.run c t).tapes) := by
  induction t generalizing c with
  | zero => exact ⟨1, Nat.le_refl 1, by rw [run_succ, step_seq_inl_halted M₁ M₂ c h]; rfl⟩
  | succ t ih =>
    cases hc : M₁.halted c.state
    · rw [run_succ] at h
      obtain ⟨s, hs, hrun⟩ := ih (M₁.step c) h
      refine ⟨s + 1, by omega, ?_⟩
      rw [run_succ, step_seq_inl_running M₁ M₂ c hc, hrun, run_succ]
    · refine ⟨1, by omega, ?_⟩
      rw [run_succ, step_seq_inl_halted M₁ M₂ c hc, run_zero, run_of_halted M₁ hc]

/-- **Composition.** `M₁` halts within `t₁` steps; `M₂`, started on the tapes `M₁` leaves, halts within
`t₂`. Then the composite halts within `t₁ + 1 + t₂` steps, where `M₂` does. -/
theorem run_seq (c : Cfg α k M₁.Q) (t₁ t₂ : Nat) (h₁ : M₁.halted (M₁.run c t₁).state = true)
    (h₂ : M₂.halted (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂).state = true) :
    (M₁.seq M₂).run (inlCfg M₁ M₂ c) (t₁ + 1 + t₂) =
      inrCfg M₁ M₂ (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂) := by
  obtain ⟨s, hs, hrun⟩ := run_seq_switch M₁ M₂ c t₁ h₁
  have hmid : (M₁.seq M₂).run (inlCfg M₁ M₂ c) (s + t₂) =
      inrCfg M₁ M₂ (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂) := by
    rw [run_add, hrun, run_seq_inr]
  rw [run_stable (M₁.seq M₂) (s := s + t₂) (by rw [hmid]; exact h₂) (by omega), hmid]

end Machine

end PvsNP

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=7598bc4245baed6bd68ea2defb9aa40da8e0070d&path=PvsNP%2FMachine%2FCompose.lean`
