| 1 | import PvsNP.Complexity.P |
| 2 | |
| 3 | /-! |
| 4 | # Example: languages decided by the first symbol |
| 5 | |
| 6 | A 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 | |
| 10 | Two instances over `Bool`: the words that start with `true`, and the empty word. |
| 11 | -/ |
| 12 | |
| 13 | namespace PvsNP.Examples |
| 14 | |
| 15 | variable {α : Type} |
| 16 | |
| 17 | |
| 18 | inductive FirstSymbolState where |
| 19 | | start |
| 20 | | accept |
| 21 | | reject |
| 22 | deriving DecidableEq, Repr |
| 23 | |
| 24 | instance : IsFinite FirstSymbolState := |
| 25 | ⟨⟨[.start, .accept, .reject], by intro a; cases a <;> simp⟩⟩ |
| 26 | |
| 27 | |
| 28 | def FirstSymbol (p : Option α → Bool) : Language α := fun x => p x.head? = true |
| 29 | |
| 30 | |
| 31 | def 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 | |
| 44 | theorem 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 | |
| 51 | theorem 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 | |
| 57 | def StartsWithTrue : Language Bool := FirstSymbol (· == some true) |
| 58 | |
| 59 | theorem startsWithTrue_iff (x : List Bool) : StartsWithTrue x ↔ x.head? = some true := by |
| 60 | simp [StartsWithTrue, FirstSymbol] |
| 61 | |
| 62 | theorem startsWithTrue_inP : InP StartsWithTrue := firstSymbol_inP _ |
| 63 | |
| 64 | |
| 65 | def EmptyWord : Language Bool := FirstSymbol Option.isNone |
| 66 | |
| 67 | theorem emptyWord_iff (x : List Bool) : EmptyWord x ↔ x = [] := by |
| 68 | cases x <;> simp [EmptyWord, FirstSymbol] |
| 69 | |
| 70 | theorem emptyWord_inP : InP EmptyWord := firstSymbol_inP _ |
| 71 | |
| 72 | |
| 73 | theorem nonEmpty_inP : InP EmptyWord.compl := emptyWord_inP.compl |
| 74 | |
| 75 | |
| 76 | example : ((firstSymbolMachine (· == some true)).run |
| 77 | ((firstSymbolMachine (· == some true)).init (inputTapes [true, false])) 1).state = .accept := by |
| 78 | rw [firstSymbolMachine_run]; rfl |
| 79 | |
| 80 | end PvsNP.Examples |