{"repo":"pvsnp","rev":"68ee862ecf840b5859cdbb47a9a45c1bbaebcb39","blob":{"commit":"68ee862ecf840b5859cdbb47a9a45c1bbaebcb39","path":"PvsNP/Machine/Compose.lean","oid":"84b888a3cf5140a407a8a9f4ba61393ecd3bc657","size":4336,"range_start":0,"range_end":4336,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Machine.Defs\n\n/-!\n# Running one machine after another\n\n`M₁.seq M₂` runs `M₁`; when `M₁` halts it spends one step switching to `M₂`'s start state, touching no\ntape, and then runs `M₂` on the tapes `M₁` left behind. Its states are `M₁.Q ⊕ M₂.Q`, still finite.\n\n`Machine.run_seq` is the composition theorem: if `M₁` halts within `t₁` steps and `M₂`, started on what\n`M₁` left, halts within `t₂` steps, then the composite halts within `t₁ + 1 + t₂` steps in the\nconfiguration `M₂` reaches.\n-/\n\nnamespace PvsNP\n\nnamespace Machine\n\nvariable {α : Type} {k : Nat}\n\n/-- Run `M₁`, then `M₂` on the tapes `M₁` halts with. -/\ndef seq (M₁ M₂ : Machine α k) : Machine α k where\n  Q := M₁.Q ⊕ M₂.Q\n  finite := @IsFinite.instSum _ _ M₁.finite M₂.finite\n  start := .inl M₁.start\n  halted\n    | .inl _ => false\n    | .inr q => M₂.halted q\n  accepting\n    | .inl _ => false\n    | .inr q => M₂.accepting q\n  δ\n    | .inl q, r =>\n      if M₁.halted q then (.inr M₂.start, r, fun _ => .stay)\n      else let o := M₁.δ q r; (.inl o.1, o.2.1, o.2.2)\n    | .inr q, r => let o := M₂.δ q r; (.inr o.1, o.2.1, o.2.2)\n\nvariable (M₁ M₂ : Machine α k)\n\n/-- The composite's configuration while it is running `M₁`. -/\ndef inlCfg (c : Cfg α k M₁.Q) : Cfg α k (M₁.seq M₂).Q := ⟨.inl c.state, c.tapes⟩\n\n/-- The composite's configuration once it is running `M₂`. -/\ndef inrCfg (c : Cfg α k M₂.Q) : Cfg α k (M₁.seq M₂).Q := ⟨.inr c.state, c.tapes⟩\n\ntheorem apply_stay (T : Fin (k + 1) → Tape α) : apply T (reads T) (fun _ => .stay) = T := by\n  funext i; simp [apply, reads]\n\ntheorem step_seq_inl_running (c : Cfg α k M₁.Q) (h : M₁.halted c.state = false) :\n    (M₁.seq M₂).step (inlCfg M₁ M₂ c) = inlCfg M₁ M₂ (M₁.step c) := by\n  simp [step, seq, inlCfg, h]\n\ntheorem step_seq_inl_halted (c : Cfg α k M₁.Q) (h : M₁.halted c.state = true) :\n    (M₁.seq M₂).step (inlCfg M₁ M₂ c) = inrCfg M₁ M₂ (M₂.init c.tapes) := by\n  simp [step, seq, inlCfg, inrCfg, init, h, apply_stay]\n\ntheorem step_seq_inr (c : Cfg α k M₂.Q) :\n    (M₁.seq M₂).step (inrCfg M₁ M₂ c) = inrCfg M₁ M₂ (M₂.step c) := by\n  by_cases h : M₂.halted c.state = true\n  · simp [step, seq, inrCfg, h]\n  · simp [step, seq, inrCfg, h]\n\ntheorem run_seq_inr (c : Cfg α k M₂.Q) (t : Nat) :\n    (M₁.seq M₂).run (inrCfg M₁ M₂ c) t = inrCfg M₁ M₂ (M₂.run c t) := by\n  induction t generalizing c with\n  | zero => rfl\n  | succ t ih => rw [run_succ, step_seq_inr, ih, run_succ]\n\n/-- If `M₁` has halted by step `t`, the composite has switched to `M₂` by step `t + 1`. -/\ntheorem run_seq_switch (c : Cfg α k M₁.Q) (t : Nat) (h : M₁.halted (M₁.run c t).state = true) :\n    ∃ s, s ≤ t + 1 ∧\n      (M₁.seq M₂).run (inlCfg M₁ M₂ c) s = inrCfg M₁ M₂ (M₂.init (M₁.run c t).tapes) := by\n  induction t generalizing c with\n  | zero => exact ⟨1, Nat.le_refl 1, by rw [run_succ, step_seq_inl_halted M₁ M₂ c h]; rfl⟩\n  | succ t ih =>\n    cases hc : M₁.halted c.state\n    · rw [run_succ] at h\n      obtain ⟨s, hs, hrun⟩ := ih (M₁.step c) h\n      refine ⟨s + 1, by omega, ?_⟩\n      rw [run_succ, step_seq_inl_running M₁ M₂ c hc, hrun, run_succ]\n    · refine ⟨1, by omega, ?_⟩\n      rw [run_succ, step_seq_inl_halted M₁ M₂ c hc, run_zero, run_of_halted M₁ hc]\n\n/-- **Composition.** `M₁` halts within `t₁` steps; `M₂`, started on the tapes `M₁` leaves, halts within\n`t₂`. Then the composite halts within `t₁ + 1 + t₂` steps, where `M₂` does. -/\ntheorem run_seq (c : Cfg α k M₁.Q) (t₁ t₂ : Nat) (h₁ : M₁.halted (M₁.run c t₁).state = true)\n    (h₂ : M₂.halted (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂).state = true) :\n    (M₁.seq M₂).run (inlCfg M₁ M₂ c) (t₁ + 1 + t₂) =\n      inrCfg M₁ M₂ (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂) := by\n  obtain ⟨s, hs, hrun⟩ := run_seq_switch M₁ M₂ c t₁ h₁\n  have hmid : (M₁.seq M₂).run (inlCfg M₁ M₂ c) (s + t₂) =\n      inrCfg M₁ M₂ (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂) := by\n    rw [run_add, hrun, run_seq_inr]\n  rw [run_stable (M₁.seq M₂) (s := s + t₂) (by rw [hmid]; exact h₂) (by omega), hmid]\n\nend Machine\n\nend PvsNP\n"},"raw":"/r/pvsnp/raw/68ee862ecf840b5859cdbb47a9a45c1bbaebcb39/PvsNP/Machine/Compose.lean","jit":null,"api":"/v1/repos/pvsnp/blob?rev=68ee862ecf840b5859cdbb47a9a45c1bbaebcb39&path=PvsNP%2FMachine%2FCompose.lean","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=68ee862ecf840b5859cdbb47a9a45c1bbaebcb39&path=PvsNP%2FMachine%2FCompose.lean"}