{"ok":true,"data":{"commit":"118eb9c9aec819f90f93b9ba0ecefa9f0d2d3b6a","path":"PvsNP/Examples/FirstSymbol.lean","oid":"260bb82a47fa5c653e5517c54acbc905a742f301","size":3232,"range_start":0,"range_end":3232,"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\nIt needs a finite alphabet, like every class here: over `Nat`, a test on one symbol could be the\nhalting 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"},"meta":{"request_id":"18959848-3a2","took_us":106,"limits":{"limit":600,"remaining":152,"window_ms":60000,"reset_ms":44706,"cost":1,"streams":{"limit":16,"open":0}}}}