{"repo":"pvsnp","rev":"examples/first-symbol","blob":{"commit":"68ee862ecf840b5859cdbb47a9a45c1bbaebcb39","path":"PvsNP/Examples/FirstSymbol.lean","oid":"b1d04d1714936efdc9f347033b647a21a0737d81","size":3285,"range_start":0,"range_end":3285,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Complexity.P\n\n/-!\n# Example: languages decided by the first symbol\n\nA concrete machine, not a construction from other machines: it reads the cell under the head of tape\n`0` once and halts, accepting when a test `p` on that symbol passes. It decides\n`{x | p x.head? = true}` in one step, so each such language is in P.\n\nThe machine and its one-step run work over any alphabet; only the conclusion `InP` needs a finite\none (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.\n-/\n\nnamespace PvsNP.Examples\n\nvariable {α : Type}\n\n/-- The machine's three states. -/\ninductive FirstSymbolState where\n  | start\n  | accept\n  | reject\n  deriving DecidableEq, Repr\n\ninstance : IsFinite FirstSymbolState :=\n  ⟨⟨[.start, .accept, .reject], by intro a; cases a <;> simp⟩⟩\n\n/-- The language of words whose first symbol (`none` for the empty word) passes `p`. -/\ndef FirstSymbol (p : Option α → Bool) : Language α := fun x => p x.head? = true\n\n/-- Read tape `0` once; accept if `p` holds of what is there. One step, one tape. -/\ndef firstSymbolMachine (p : Option α → Bool) : Machine α 0 where\n  Q := FirstSymbolState\n  finite := inferInstance\n  start := .start\n  halted\n    | .start => false\n    | _ => true\n  accepting\n    | .accept => true\n    | _ => false\n  δ _ r := (if p (r 0) then .accept else .reject, r, fun _ => .stay)\n\n/-- After one step the machine has halted, in `accept` exactly when `p` holds of the first symbol. -/\ntheorem firstSymbolMachine_run (p : Option α → Bool) (x : List α) :\n    ((firstSymbolMachine p).run ((firstSymbolMachine p).init (inputTapes x)) 1).state =\n      if p x.head? then .accept else .reject := by\n  simp [Machine.run, Machine.step, Machine.init, firstSymbolMachine, Machine.reads, inputTapes,\n    Tape.read_ofWord]\n\n/-- Every language decided by the first symbol is in P, within one step. -/\ntheorem firstSymbol_inP [IsFinite α] (p : Option α → Bool) : InP (FirstSymbol p) := by\n  refine ⟨inferInstance, 0, firstSymbolMachine p, fun _ => 1, PolyBounded.const 1, fun x => ?_⟩\n  rw [firstSymbolMachine_run]\n  cases h : p x.head? <;> simp [firstSymbolMachine, FirstSymbol, h]\n\n/-- Words over `Bool` that start with `true`. -/\ndef StartsWithTrue : Language Bool := FirstSymbol (· == some true)\n\ntheorem startsWithTrue_iff (x : List Bool) : StartsWithTrue x ↔ x.head? = some true := by\n  simp [StartsWithTrue, FirstSymbol]\n\ntheorem startsWithTrue_inP : InP StartsWithTrue := firstSymbol_inP _\n\n/-- The language holding only the empty word. -/\ndef EmptyWord : Language Bool := FirstSymbol Option.isNone\n\ntheorem emptyWord_iff (x : List Bool) : EmptyWord x ↔ x = [] := by\n  cases x <;> simp [EmptyWord, FirstSymbol]\n\ntheorem emptyWord_inP : InP EmptyWord := firstSymbol_inP _\n\n/-- Its complement, the non-empty words, is in P too, by closure under complement. -/\ntheorem nonEmpty_inP : InP EmptyWord.compl := emptyWord_inP.compl\n\n/-- The machine run in the kernel: on `[true, false]` it accepts after one step. -/\nexample : ((firstSymbolMachine (· == some true)).run\n    ((firstSymbolMachine (· == some true)).init (inputTapes [true, false])) 1).state = .accept := by\n  rw [firstSymbolMachine_run]; rfl\n\nend PvsNP.Examples\n"},"raw":"/r/pvsnp/raw/examples%2Ffirst-symbol/PvsNP/Examples/FirstSymbol.lean","jit":null,"api":"/v1/repos/pvsnp/blob?rev=examples%2Ffirst-symbol&path=PvsNP%2FExamples%2FFirstSymbol.lean","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=examples%2Ffirst-symbol&path=PvsNP%2FExamples%2FFirstSymbol.lean"}