118eb9c9 / 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
10It needs a finite alphabet, like every class here: over `Nat`, a test on one symbol could be the
11halting problem. Two instances over `Bool`: the words that start with `true`, and the empty word.
12-/
13
14namespace PvsNP.Examples
15
16variable {α : Type}
17
18/-- The machine's three states. -/
19inductive FirstSymbolState where
20 | start
21 | accept
22 | reject
23 deriving DecidableEq, Repr
24
25instance : IsFinite FirstSymbolState :=
26 ⟨⟨[.start, .accept, .reject], by intro a; cases a <;> simp⟩⟩
27
28/-- The language of words whose first symbol (`none` for the empty word) passes `p`. -/
29def FirstSymbol (p : Option α → Bool) : Language α := fun x => p x.head? = true
30
31/-- Read tape `0` once; accept if `p` holds of what is there. One step, one tape. -/
32def firstSymbolMachine (p : Option α → Bool) : Machine α 0 where
33 Q := FirstSymbolState
34 finite := inferInstance
35 start := .start
36 halted
37 | .start => false
38 | _ => true
39 accepting
40 | .accept => true
41 | _ => false
42 δ _ r := (if p (r 0) then .accept else .reject, r, fun _ => .stay)
43
44/-- After one step the machine has halted, in `accept` exactly when `p` holds of the first symbol. -/
45theorem firstSymbolMachine_run (p : Option α → Bool) (x : List α) :
46 ((firstSymbolMachine p).run ((firstSymbolMachine p).init (inputTapes x)) 1).state =
47 if p x.head? then .accept else .reject := by
48 simp [Machine.run, Machine.step, Machine.init, firstSymbolMachine, Machine.reads, inputTapes,
49 Tape.read_ofWord]
50
51/-- Every language decided by the first symbol is in P, within one step. -/
52theorem firstSymbol_inP [IsFinite α] (p : Option α → Bool) : InP (FirstSymbol p) := by
53 refine ⟨inferInstance, 0, firstSymbolMachine p, fun _ => 1, PolyBounded.const 1, fun x => ?_⟩
54 rw [firstSymbolMachine_run]
55 cases h : p x.head? <;> simp [firstSymbolMachine, FirstSymbol, h]
56
57/-- Words over `Bool` that start with `true`. -/
58def StartsWithTrue : Language Bool := FirstSymbol (· == some true)
59
60theorem startsWithTrue_iff (x : List Bool) : StartsWithTrue x ↔ x.head? = some true := by
61 simp [StartsWithTrue, FirstSymbol]
62
63theorem startsWithTrue_inP : InP StartsWithTrue := firstSymbol_inP _
64
65/-- The language holding only the empty word. -/
66def EmptyWord : Language Bool := FirstSymbol Option.isNone
67
68theorem emptyWord_iff (x : List Bool) : EmptyWord x ↔ x = [] := by
69 cases x <;> simp [EmptyWord, FirstSymbol]
70
71theorem emptyWord_inP : InP EmptyWord := firstSymbol_inP _
72
73/-- Its complement, the non-empty words, is in P too, by closure under complement. -/
74theorem nonEmpty_inP : InP EmptyWord.compl := emptyWord_inP.compl
75
76/-- The machine run in the kernel: on `[true, false]` it accepts after one step. -/
77example : ((firstSymbolMachine (· == some true)).run
78 ((firstSymbolMachine (· == some true)).init (inputTapes [true, false])) 1).state = .accept := by
79 rw [firstSymbolMachine_run]; rfl
80
81end PvsNP.Examples