Documentation

Mathlib.Computability.NFA

Nondeterministic Finite Automata #

This file contains the definition of a Nondeterministic Finite Automaton (NFA), a state machine which determines whether a string (implemented as a list over an arbitrary alphabet) is in a regular set by evaluating the string over every possible path. We show that DFA's are equivalent to NFA's however the construction from NFA to DFA uses an exponential number of states. Note that this definition allows for Automaton with infinite states; a Fintype instance must be supplied for true NFA's.

structure NFA (α : Type u) (σ : Type v) :
Type (max u v)

An NFA is a set of states (σ), a transition function from state to state labelled by the alphabet (step), a set of starting states (start) and a set of acceptance states (accept). Note the transition function sends a state to a Set of states. These are the states that it may be sent to.

  • step : σαSet σ
  • start : Set σ
  • accept : Set σ
Instances For
    instance NFA.instInhabitedNFA {α : Type u} {σ : Type v} :
    Inhabited (NFA α σ)
    Equations
    • NFA.instInhabitedNFA = { default := { step := fun (x : σ) (x : α) => , start := , accept := } }
    def NFA.stepSet {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (a : α) :
    Set σ

    M.stepSet S a is the union of M.step s a for all s ∈ S.

    Equations
    Instances For
      theorem NFA.mem_stepSet {α : Type u} {σ : Type v} (M : NFA α σ) (s : σ) (S : Set σ) (a : α) :
      s NFA.stepSet M S a ∃ t ∈ S, s M.step t a
      @[simp]
      theorem NFA.stepSet_empty {α : Type u} {σ : Type v} (M : NFA α σ) (a : α) :
      def NFA.evalFrom {α : Type u} {σ : Type v} (M : NFA α σ) (start : Set σ) :
      List αSet σ

      M.evalFrom S x computes all possible paths though M with input x starting at an element of S.

      Equations
      Instances For
        @[simp]
        theorem NFA.evalFrom_nil {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) :
        NFA.evalFrom M S [] = S
        @[simp]
        theorem NFA.evalFrom_singleton {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (a : α) :
        NFA.evalFrom M S [a] = NFA.stepSet M S a
        @[simp]
        theorem NFA.evalFrom_append_singleton {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (x : List α) (a : α) :
        NFA.evalFrom M S (x ++ [a]) = NFA.stepSet M (NFA.evalFrom M S x) a
        def NFA.eval {α : Type u} {σ : Type v} (M : NFA α σ) :
        List αSet σ

        M.eval x computes all possible paths though M with input x starting at an element of M.start.

        Equations
        Instances For
          @[simp]
          theorem NFA.eval_nil {α : Type u} {σ : Type v} (M : NFA α σ) :
          NFA.eval M [] = M.start
          @[simp]
          theorem NFA.eval_singleton {α : Type u} {σ : Type v} (M : NFA α σ) (a : α) :
          NFA.eval M [a] = NFA.stepSet M M.start a
          @[simp]
          theorem NFA.eval_append_singleton {α : Type u} {σ : Type v} (M : NFA α σ) (x : List α) (a : α) :
          NFA.eval M (x ++ [a]) = NFA.stepSet M (NFA.eval M x) a
          def NFA.accepts {α : Type u} {σ : Type v} (M : NFA α σ) :

          M.accepts is the language of x such that there is an accept state in M.eval x.

          Equations
          Instances For
            theorem NFA.mem_accepts {α : Type u} {σ : Type v} (M : NFA α σ) {x : List α} :
            x NFA.accepts M ∃ S ∈ M.accept, S NFA.evalFrom M M.start x
            def NFA.toDFA {α : Type u} {σ : Type v} (M : NFA α σ) :
            DFA α (Set σ)

            M.toDFA is a DFA constructed from an NFA M using the subset construction. The states is the type of Sets of M.state and the step function is M.stepSet.

            Equations
            Instances For
              @[simp]
              theorem NFA.toDFA_correct {α : Type u} {σ : Type v} (M : NFA α σ) :
              theorem NFA.pumping_lemma {α : Type u} {σ : Type v} (M : NFA α σ) [Fintype σ] {x : List α} (hx : x NFA.accepts M) (hlen : Fintype.card (Set σ) List.length x) :
              ∃ (a : List α) (b : List α) (c : List α), x = a ++ b ++ c List.length a + List.length b Fintype.card (Set σ) b [] {a} * KStar.kstar {b} * {c} NFA.accepts M
              @[simp]
              theorem DFA.toNFA_accept {α : Type u} {σ' : Type v} (M : DFA α σ') :
              (DFA.toNFA M).accept = M.accept
              @[simp]
              theorem DFA.toNFA_step {α : Type u} {σ' : Type v} (M : DFA α σ') (s : σ') (a : α) :
              (DFA.toNFA M).step s a = {M.step s a}
              @[simp]
              theorem DFA.toNFA_start {α : Type u} {σ' : Type v} (M : DFA α σ') :
              (DFA.toNFA M).start = {M.start}
              def DFA.toNFA {α : Type u} {σ' : Type v} (M : DFA α σ') :
              NFA α σ'

              M.toNFA is an NFA constructed from a DFA M by using the same start and accept states and a transition function which sends s with input a to the singleton M.step s a.

              Equations
              • DFA.toNFA M = { step := fun (s : σ') (a : α) => {M.step s a}, start := {M.start}, accept := M.accept }
              Instances For
                @[simp]
                theorem DFA.toNFA_evalFrom_match {α : Type u} {σ : Type v} (M : DFA α σ) (start : σ) (s : List α) :
                NFA.evalFrom (DFA.toNFA M) {start} s = {DFA.evalFrom M start s}
                @[simp]
                theorem DFA.toNFA_correct {α : Type u} {σ : Type v} (M : DFA α σ) :