3fc07c69 / PvsNP / Examples / FirstSymbol.lean

1import PvsNP.Complexity.P
2
3/-!
4# Example: languages decided by the first symbol
5
6A 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
10Two instances over `Bool`: the words that start with `true`, and the empty word.
11-/
12
13namespace PvsNP.Examples
14
15variable {α : Type}
16
17/-- The machine's three states. -/
18inductive FirstSymbolState where
19 | start
20 | accept
21 | reject
22 deriving DecidableEq, Repr
23
24instance : 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`. -/
28def 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. -/
31def 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. -/
44theorem 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. -/
51theorem 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`. -/
57def StartsWithTrue : Language Bool := FirstSymbol (· == some true)
58
59theorem startsWithTrue_iff (x : List Bool) : StartsWithTrue x ↔ x.head? = some true := by
60 simp [StartsWithTrue, FirstSymbol]
61
62theorem startsWithTrue_inP : InP StartsWithTrue := firstSymbol_inP _
63
64/-- The language holding only the empty word. -/
65def EmptyWord : Language Bool := FirstSymbol Option.isNone
66
67theorem emptyWord_iff (x : List Bool) : EmptyWord x ↔ x = [] := by
68 cases x <;> simp [EmptyWord, FirstSymbol]
69
70theorem emptyWord_inP : InP EmptyWord := firstSymbol_inP _
71
72/-- Its complement, the non-empty words, is in P too, by closure under complement. -/
73theorem nonEmpty_inP : InP EmptyWord.compl := emptyWord_inP.compl
74
75/-- The machine run in the kernel: on `[true, false]` it accepts after one step. -/
76example : ((firstSymbolMachine (· == some true)).run
77 ((firstSymbolMachine (· == some true)).init (inputTapes [true, false])) 1).state = .accept := by
78 rw [firstSymbolMachine_run]; rfl
79
80end PvsNP.Examples