Add concrete one-step machines for first-symbol languages

3fc07c69Claude · 6d agoparent 4186ab27files
firstSymbolMachine reads tape 0 once and halts, accepting when a test
on the first symbol passes, so every language decided by its first
symbol is in P. Instances over Bool: words starting with true, the empty
word, and (by closure under complement) the non-empty words. An example
runs the machine on [true, false] and checks that it accepts.

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

Diff

2 files changed +81 -0
M PvsNP.lean +1 -0
@@ -11,6 +11,7 @@
1111 import PvsNP.Complexity.NP
1212 import PvsNP.Complexity.Completeness
1313 import PvsNP.Open
14+import PvsNP.Examples.FirstSymbol
1415
1516 /-! # P versus NP in Lean 4
1617
@@ -0,0 +1,80 @@
1+import PvsNP.Complexity.P
2+
3+/-!
4+# Example: languages decided by the first symbol
5+
6+A concrete machine, not a construction from other machines: it reads the cell under the head of tape
7+`0` once and halts, accepting when a test `p` on that symbol passes. It decides
8+`{x | p x.head? = true}` in one step, so each such language is in P.
9+
10+Two instances over `Bool`: the words that start with `true`, and the empty word.
11+-/
12+
13+namespace PvsNP.Examples
14+
15+variable {α : Type}
16+
17+/-- The machine's three states. -/
18+inductive FirstSymbolState where
19+ | start
20+ | accept
21+ | reject
22+ deriving DecidableEq, Repr
23+
24+instance : IsFinite FirstSymbolState :=
25+ ⟨⟨[.start, .accept, .reject], by intro a; cases a <;> simp⟩⟩
26+
27+/-- The language of words whose first symbol (`none` for the empty word) passes `p`. -/
28+def FirstSymbol (p : Option α → Bool) : Language α := fun x => p x.head? = true
29+
30+/-- Read tape `0` once; accept if `p` holds of what is there. One step, one tape. -/
31+def firstSymbolMachine (p : Option α → Bool) : Machine α 0 where
32+ Q := FirstSymbolState
33+ finite := inferInstance
34+ start := .start
35+ halted
36+ | .start => false
37+ | _ => true
38+ accepting
39+ | .accept => true
40+ | _ => false
41+ δ _ r := (if p (r 0) then .accept else .reject, r, fun _ => .stay)
42+
43+/-- After one step the machine has halted, in `accept` exactly when `p` holds of the first symbol. -/
44+theorem firstSymbolMachine_run (p : Option α → Bool) (x : List α) :
45+ ((firstSymbolMachine p).run ((firstSymbolMachine p).init (inputTapes x)) 1).state =
46+ if p x.head? then .accept else .reject := by
47+ simp [Machine.run, Machine.step, Machine.init, firstSymbolMachine, Machine.reads, inputTapes,
48+ Tape.read_ofWord]
49+
50+/-- Every language decided by the first symbol is in P, within one step. -/
51+theorem firstSymbol_inP (p : Option α → Bool) : InP (FirstSymbol p) := by
52+ refine ⟨0, firstSymbolMachine p, fun _ => 1, PolyBounded.const 1, fun x => ?_⟩
53+ rw [firstSymbolMachine_run]
54+ cases h : p x.head? <;> simp [firstSymbolMachine, FirstSymbol, h]
55+
56+/-- Words over `Bool` that start with `true`. -/
57+def StartsWithTrue : Language Bool := FirstSymbol (· == some true)
58+
59+theorem startsWithTrue_iff (x : List Bool) : StartsWithTrue x ↔ x.head? = some true := by
60+ simp [StartsWithTrue, FirstSymbol]
61+
62+theorem startsWithTrue_inP : InP StartsWithTrue := firstSymbol_inP _
63+
64+/-- The language holding only the empty word. -/
65+def EmptyWord : Language Bool := FirstSymbol Option.isNone
66+
67+theorem emptyWord_iff (x : List Bool) : EmptyWord x ↔ x = [] := by
68+ cases x <;> simp [EmptyWord, FirstSymbol]
69+
70+theorem emptyWord_inP : InP EmptyWord := firstSymbol_inP _
71+
72+/-- Its complement, the non-empty words, is in P too, by closure under complement. -/
73+theorem nonEmpty_inP : InP EmptyWord.compl := emptyWord_inP.compl
74+
75+/-- The machine run in the kernel: on `[true, false]` it accepts after one step. -/
76+example : ((firstSymbolMachine (· == some true)).run
77+ ((firstSymbolMachine (· == some true)).init (inputTapes [true, false])) 1).state = .accept := by
78+ rw [firstSymbolMachine_run]; rfl
79+
80+end PvsNP.Examples