Compose machines sequentially

6e7066ddClaude · 6d agoparent ca084059files
M₁.seq M₂ runs M₁, switches to M₂ in one step without touching a tape,
then runs M₂ on the tapes M₁ left. run_seq: if M₁ halts within t₁ steps
and M₂ then halts within t₂, the composite halts within t₁ + 1 + t₂ in
the configuration M₂ reaches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Diff

2 files changed +98 -0
M PvsNP.lean +1 -0
@@ -4,6 +4,7 @@
44 import PvsNP.Machine.Tape
55 import PvsNP.Machine.Defs
66 import PvsNP.Machine.Lift
7+import PvsNP.Machine.Compose
78
89 /-! # P versus NP in Lean 4
910
@@ -0,0 +1,97 @@
1+import PvsNP.Machine.Defs
2+
3+/-!
4+# Running one machine after another
5+
6+`M₁.seq M₂` runs `M₁`; when `M₁` halts it spends one step switching to `M₂`'s start state, touching no
7+tape, and then runs `M₂` on the tapes `M₁` left behind. Its states are `M₁.Q ⊕ M₂.Q`, still finite.
8+
9+`Machine.run_seq` is the composition theorem: if `M₁` halts within `t₁` steps and `M₂`, started on what
10+`M₁` left, halts within `t₂` steps, then the composite halts within `t₁ + 1 + t₂` steps in the
11+configuration `M₂` reaches.
12+-/
13+
14+namespace PvsNP
15+
16+namespace Machine
17+
18+variable {α : Type} {k : Nat}
19+
20+/-- Run `M₁`, then `M₂` on the tapes `M₁` halts with. -/
21+def seq (M₁ M₂ : Machine α k) : Machine α k where
22+ Q := M₁.Q ⊕ M₂.Q
23+ finite := @IsFinite.instSum _ _ M₁.finite M₂.finite
24+ start := .inl M₁.start
25+ halted
26+ | .inl _ => false
27+ | .inr q => M₂.halted q
28+ accepting
29+ | .inl _ => false
30+ | .inr q => M₂.accepting q
31+ δ
32+ | .inl q, r =>
33+ if M₁.halted q then (.inr M₂.start, r, fun _ => .stay)
34+ else let o := M₁.δ q r; (.inl o.1, o.2.1, o.2.2)
35+ | .inr q, r => let o := M₂.δ q r; (.inr o.1, o.2.1, o.2.2)
36+
37+variable (M₁ M₂ : Machine α k)
38+
39+/-- The composite's configuration while it is running `M₁`. -/
40+def inlCfg (c : Cfg α k M₁.Q) : Cfg α k (M₁.seq M₂).Q := ⟨.inl c.state, c.tapes⟩
41+
42+/-- The composite's configuration once it is running `M₂`. -/
43+def inrCfg (c : Cfg α k M₂.Q) : Cfg α k (M₁.seq M₂).Q := ⟨.inr c.state, c.tapes⟩
44+
45+theorem apply_stay (T : Fin (k + 1) → Tape α) : apply T (reads T) (fun _ => .stay) = T := by
46+ funext i; simp [apply, reads]
47+
48+theorem step_seq_inl_running (c : Cfg α k M₁.Q) (h : M₁.halted c.state = false) :
49+ (M₁.seq M₂).step (inlCfg M₁ M₂ c) = inlCfg M₁ M₂ (M₁.step c) := by
50+ simp [step, seq, inlCfg, h]
51+
52+theorem step_seq_inl_halted (c : Cfg α k M₁.Q) (h : M₁.halted c.state = true) :
53+ (M₁.seq M₂).step (inlCfg M₁ M₂ c) = inrCfg M₁ M₂ (M₂.init c.tapes) := by
54+ simp [step, seq, inlCfg, inrCfg, init, h, apply_stay]
55+
56+theorem step_seq_inr (c : Cfg α k M₂.Q) :
57+ (M₁.seq M₂).step (inrCfg M₁ M₂ c) = inrCfg M₁ M₂ (M₂.step c) := by
58+ by_cases h : M₂.halted c.state = true
59+ · simp [step, seq, inrCfg, h]
60+ · simp [step, seq, inrCfg, h]
61+
62+theorem run_seq_inr (c : Cfg α k M₂.Q) (t : Nat) :
63+ (M₁.seq M₂).run (inrCfg M₁ M₂ c) t = inrCfg M₁ M₂ (M₂.run c t) := by
64+ induction t generalizing c with
65+ | zero => rfl
66+ | succ t ih => rw [run_succ, step_seq_inr, ih, run_succ]
67+
68+/-- If `M₁` has halted by step `t`, the composite has switched to `M₂` by step `t + 1`. -/
69+theorem run_seq_switch (c : Cfg α k M₁.Q) (t : Nat) (h : M₁.halted (M₁.run c t).state = true) :
70+ ∃ s, s ≤ t + 1 ∧
71+ (M₁.seq M₂).run (inlCfg M₁ M₂ c) s = inrCfg M₁ M₂ (M₂.init (M₁.run c t).tapes) := by
72+ induction t generalizing c with
73+ | zero => exact ⟨1, Nat.le_refl 1, by rw [run_succ, step_seq_inl_halted M₁ M₂ c h]; rfl⟩
74+ | succ t ih =>
75+ cases hc : M₁.halted c.state
76+ · rw [run_succ] at h
77+ obtain ⟨s, hs, hrun⟩ := ih (M₁.step c) h
78+ refine ⟨s + 1, by omega, ?_⟩
79+ rw [run_succ, step_seq_inl_running M₁ M₂ c hc, hrun, run_succ]
80+ · refine ⟨1, by omega, ?_⟩
81+ rw [run_succ, step_seq_inl_halted M₁ M₂ c hc, run_zero, run_of_halted M₁ hc]
82+
83+/-- **Composition.** `M₁` halts within `t₁` steps; `M₂`, started on the tapes `M₁` leaves, halts within
84+`t₂`. Then the composite halts within `t₁ + 1 + t₂` steps, where `M₂` does. -/
85+theorem run_seq (c : Cfg α k M₁.Q) (t₁ t₂ : Nat) (h₁ : M₁.halted (M₁.run c t₁).state = true)
86+ (h₂ : M₂.halted (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂).state = true) :
87+ (M₁.seq M₂).run (inlCfg M₁ M₂ c) (t₁ + 1 + t₂) =
88+ inrCfg M₁ M₂ (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂) := by
89+ obtain ⟨s, hs, hrun⟩ := run_seq_switch M₁ M₂ c t₁ h₁
90+ have hmid : (M₁.seq M₂).run (inlCfg M₁ M₂ c) (s + t₂) =
91+ inrCfg M₁ M₂ (M₂.run (M₂.init (M₁.run c t₁).tapes) t₂) := by
92+ rw [run_add, hrun, run_seq_inr]
93+ rw [run_stable (M₁.seq M₂) (s := s + t₂) (by rw [hmid]; exact h₂) (by omega), hmid]
94+
95+end Machine
96+
97+end PvsNP