# PvsNP/Examples/FirstSymbol.lean @ 118eb9c9aec819f90f93b9ba0ecefa9f0d2d3b6a

````
import PvsNP.Complexity.P

/-!
# Example: languages decided by the first symbol

A concrete machine, not a construction from other machines: it reads the cell under the head of tape
`0` once and halts, accepting when a test `p` on that symbol passes. It decides
`{x | p x.head? = true}` in one step, so each such language is in P.

It needs a finite alphabet, like every class here: over `Nat`, a test on one symbol could be the
halting problem. Two instances over `Bool`: the words that start with `true`, and the empty word.
-/

namespace PvsNP.Examples

variable {α : Type}

/-- The machine's three states. -/
inductive FirstSymbolState where
  | start
  | accept
  | reject
  deriving DecidableEq, Repr

instance : IsFinite FirstSymbolState :=
  ⟨⟨[.start, .accept, .reject], by intro a; cases a <;> simp⟩⟩

/-- The language of words whose first symbol (`none` for the empty word) passes `p`. -/
def FirstSymbol (p : Option α → Bool) : Language α := fun x => p x.head? = true

/-- Read tape `0` once; accept if `p` holds of what is there. One step, one tape. -/
def firstSymbolMachine (p : Option α → Bool) : Machine α 0 where
  Q := FirstSymbolState
  finite := inferInstance
  start := .start
  halted
    | .start => false
    | _ => true
  accepting
    | .accept => true
    | _ => false
  δ _ r := (if p (r 0) then .accept else .reject, r, fun _ => .stay)

/-- After one step the machine has halted, in `accept` exactly when `p` holds of the first symbol. -/
theorem firstSymbolMachine_run (p : Option α → Bool) (x : List α) :
    ((firstSymbolMachine p).run ((firstSymbolMachine p).init (inputTapes x)) 1).state =
      if p x.head? then .accept else .reject := by
  simp [Machine.run, Machine.step, Machine.init, firstSymbolMachine, Machine.reads, inputTapes,
    Tape.read_ofWord]

/-- Every language decided by the first symbol is in P, within one step. -/
theorem firstSymbol_inP [IsFinite α] (p : Option α → Bool) : InP (FirstSymbol p) := by
  refine ⟨inferInstance, 0, firstSymbolMachine p, fun _ => 1, PolyBounded.const 1, fun x => ?_⟩
  rw [firstSymbolMachine_run]
  cases h : p x.head? <;> simp [firstSymbolMachine, FirstSymbol, h]

/-- Words over `Bool` that start with `true`. -/
def StartsWithTrue : Language Bool := FirstSymbol (· == some true)

theorem startsWithTrue_iff (x : List Bool) : StartsWithTrue x ↔ x.head? = some true := by
  simp [StartsWithTrue, FirstSymbol]

theorem startsWithTrue_inP : InP StartsWithTrue := firstSymbol_inP _

/-- The language holding only the empty word. -/
def EmptyWord : Language Bool := FirstSymbol Option.isNone

theorem emptyWord_iff (x : List Bool) : EmptyWord x ↔ x = [] := by
  cases x <;> simp [EmptyWord, FirstSymbol]

theorem emptyWord_inP : InP EmptyWord := firstSymbol_inP _

/-- Its complement, the non-empty words, is in P too, by closure under complement. -/
theorem nonEmpty_inP : InP EmptyWord.compl := emptyWord_inP.compl

/-- The machine run in the kernel: on `[true, false]` it accepts after one step. -/
example : ((firstSymbolMachine (· == some true)).run
    ((firstSymbolMachine (· == some true)).init (inputTapes [true, false])) 1).state = .accept := by
  rw [firstSymbolMachine_run]; rfl

end PvsNP.Examples

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=118eb9c9aec819f90f93b9ba0ecefa9f0d2d3b6a&path=PvsNP%2FExamples%2FFirstSymbol.lean`
