The areas / V32. Logic, model theory and computability
Mathlib.Computability.NFA
76 declarations. Mathlib docs · Source
Nondeterministic Finite Automata
A Nondeterministic Finite Automaton (NFA) is a state machine which
decides membership in a particular Language, by following every
possible path that describes an input string.
We show that DFAs and NFAs can decide the same languages, by constructing an equivalent DFA for every NFA, and vice versa.
As constructing a DFA from an NFA uses an exponential number of states,
we re-prove the pumping lemma instead of lifting DFA.pumping_lemma,
in order to obtain the optimal bound on the minimal length of the string.
Like DFA, this definition allows for automata with infinite states;
a Fintype instance must be supplied for true NFAs.
Main definitions
NFA α σ: automaton over alphabetαand set of statesσNFA.evalFrom M S x: set of possible ending states for an input wordxand set of initial statesSNFA.accepts M: the language accepted by the NFAMNFA.Path M s t x: a specific path fromstotfor an input wordxNFA.Path.supp p: set of states visited by the pathp
Main theorems
NFA.pumping_lemma: every sufficiently long string accepted by the NFA has a substring that can be repeated arbitrarily many times (and have the overall string still be accepted)
theorem NFA.Path.supp.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} {σ : Type u_2} {M : NFA α σ} (motive : (x : List α) → (t s : σ) → M.Path s t x → Sort u_3) (x : List α)
(t s : σ) (x_1 : M.Path s t x) (h_1 : (s : σ) → motive [] s s (NFA.Path.nil s))
(h_2 :
(t s t_1 : σ) →
(a : α) →
(x : List α) →
(a_1 : t_1 ∈ M.step s a) → (p : M.Path t_1 t x) → motive (a :: x) t s (NFA.Path.cons t_1 s t a x a_1 p))
(s_1 : σ),
x = [] →
t = s_1 →
s = s_1 →
x_1 ≍ NFA.Path.nil s_1 →
(match x, t, s, x_1 with
| .([]), s, .(s), NFA.Path.nil .(s) => h_1 s
| .(a :: x), t, s, NFA.Path.cons t_1 .(s) .(t) a x a_1 p => h_2 t s t_1 a x a_1 p) ≍
h_1 s_1
def NFA.ctorIdx E1 #
{α : Type u} → {σ : Type v} → NFA α σ → ℕ
theorem NFA.Path.brecOn.eq E1 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {motive : (a a_1 : σ) → (a_2 : List α) → M.Path a a_1 a_2 → Sort u_1}
{a a_1 : σ} {a_2 : List α} (t : M.Path a a_1 a_2)
(F_1 : (a a_3 : σ) → (a_4 : List α) → (t : M.Path a a_3 a_4) → NFA.Path.below t → motive a a_3 a_4 t),
NFA.Path.brecOn t F_1 = F_1 a a_1 a_2 t (NFA.Path.brecOn.go t F_1).2
theorem NFA.mk.injEq E0 #
∀ {α : Type u} {σ : Type v} (step : σ → α → Set σ) (start accept : Set σ) (step_1 : σ → α → Set σ)
(start_1 accept_1 : Set σ),
({ step := step, start := start, accept := accept } = { step := step_1, start := start_1, accept := accept_1 }) =
(step = step_1 ∧ start = start_1 ∧ accept = accept_1)
theorem NFA.Path.cons.injEq E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} (t s u : σ) (a : α) (x : List α) (a_1 : t ∈ M.step s a) (a_2 : M.Path t u x)
(t_1 : σ) (a_3 : t_1 ∈ M.step s a) (a_4 : M.Path t_1 u x),
(NFA.Path.cons t s u a x a_1 a_2 = NFA.Path.cons t_1 s u a x a_3 a_4) = (t = t_1 ∧ a_2 ≍ a_4)
abbrev NFA.Path.brecOn.go E1 #
{α : Type u} →
{σ : Type v} →
{M : NFA α σ} →
{motive : (a a_1 : σ) → (a_2 : List α) → M.Path a a_1 a_2 → Sort u_1} →
{a a_1 : σ} →
{a_2 : List α} →
(t : M.Path a a_1 a_2) →
((a a_3 : σ) → (a_4 : List α) → (t : M.Path a a_3 a_4) → NFA.Path.below t → motive a a_3 a_4 t) →
motive a a_1 a_2 t ×' NFA.Path.below t
theorem NFA.Path.supp.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} {σ : Type u_2} {M : NFA α σ} (motive : (x : List α) → (t s : σ) → M.Path s t x → Sort u_3) (x : List α)
(t s : σ) (x_1 : M.Path s t x) (h_1 : (s : σ) → motive [] s s (NFA.Path.nil s))
(h_2 :
(t s t_1 : σ) →
(a : α) →
(x : List α) →
(a_1 : t_1 ∈ M.step s a) → (p : M.Path t_1 t x) → motive (a :: x) t s (NFA.Path.cons t_1 s t a x a_1 p))
(t_1 s_1 t_2 : σ) (a : α) (x_2 : List α) (a_1 : t_2 ∈ M.step s_1 a) (p : M.Path t_2 t_1 x_2),
x = a :: x_2 →
t = t_1 →
s = s_1 →
x_1 ≍ NFA.Path.cons t_2 s_1 t_1 a x_2 a_1 p →
(match x, t, s, x_1 with
| .([]), s, .(s), NFA.Path.nil .(s) => h_1 s
| .(a :: x), t, s, NFA.Path.cons t_3 .(s) .(t) a x a_2 p => h_2 t s t_3 a x a_2 p) ≍
h_2 t_1 s_1 t_2 a x_2 a_1 p
theorem NFA.Path.cons.inj E1 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {t s u : σ} {a : α} {x : List α} {a_1 : t ∈ M.step s a} {a_2 : M.Path t u x}
{t_1 : σ} {a_3 : t_1 ∈ M.step s a} {a_4 : M.Path t_1 u x},
NFA.Path.cons t s u a x a_1 a_2 = NFA.Path.cons t_1 s u a x a_3 a_4 → t = t_1 ∧ a_2 ≍ a_4
theorem NFA.mk.inj E1 #
∀ {α : Type u} {σ : Type v} {step : σ → α → Set σ} {start accept : Set σ} {step_1 : σ → α → Set σ}
{start_1 accept_1 : Set σ},
{ step := step, start := start, accept := accept } = { step := step_1, start := start_1, accept := accept_1 } →
step = step_1 ∧ start = start_1 ∧ accept = accept_1
abbrev NFA.Path.ctorElimType E1 #
{α : Type u} →
{σ : Type v} →
{M : NFA α σ} →
{motive : (a a_1 : σ) → (a_2 : List α) → M.Path a a_1 a_2 → Sort u_1} →
ℕ → Sort (max 1 (imax (v + 1) u_1) (imax (v + 1) (v + 1) (v + 1) (u + 1) (u + 1) (max (u + 1) (v + 1)) u_1))
def NFA.Path.ctorIdx E1 #
{α : Type u} → {σ : Type v} → {M : NFA α σ} → {a a_1 : σ} → {a_2 : List α} → M.Path a a_1 a_2 → ℕ
structure NFA E3 #
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.
ctor NFA.mk E0 #
{α : Type u} → {σ : Type v} → (σ → α → Set σ) → Set σ → Set σ → NFA α σ
abbrev NFA.step E1 #
{α : Type u} → {σ : Type v} → NFA α σ → σ → α → Set σ
The NFA's transition function
abbrev NFA.start E1 #
{α : Type u} → {σ : Type v} → NFA α σ → Set σ
Set of starting states
abbrev NFA.accept E1 #
{α : Type u} → {σ : Type v} → NFA α σ → Set σ
Set of accepting states
instance NFA.instInhabited E0 #
{α : Type u} → {σ : Type v} → Inhabited (NFA α σ)
def NFA.stepSet E1 #
{α : Type u} → {σ : Type v} → NFA α σ → Set σ → α → Set σ
M.stepSet S a is the union of M.step s a for all s ∈ S.
theorem NFA.mem_stepSet E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {s : σ} {S : Set σ} {a : α}, s ∈ M.stepSet S a ↔ ∃ t ∈ S, s ∈ M.step t a
theorem NFA.stepSet_empty E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (a : α), M.stepSet ∅ a = ∅
theorem NFA.stepSet_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (s : σ) (a : α), M.stepSet {s} a = M.step s a
theorem NFA.stepSet_union E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S T : Set σ} {a : α}, M.stepSet (S ∪ T) a = M.stepSet S a ∪ M.stepSet T a
def NFA.evalFrom E1 #
{α : Type u} → {σ : Type v} → NFA α σ → Set σ → List α → Set σ
M.evalFrom S x computes all possible paths through M with input x starting at an element
of S.
theorem NFA.evalFrom_nil E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ), M.evalFrom S [] = S
theorem NFA.evalFrom_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (a : α), M.evalFrom S [a] = M.stepSet S a
theorem NFA.evalFrom_cons E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (a : α) (x : List α),
M.evalFrom S (a :: x) = M.evalFrom (M.stepSet S a) x
theorem NFA.evalFrom_append E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (x y : List α),
M.evalFrom S (x ++ y) = M.evalFrom (M.evalFrom S x) y
theorem NFA.evalFrom_union E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (S T : Set σ) (x : List α),
M.evalFrom (S ∪ T) x = M.evalFrom S x ∪ M.evalFrom T x
theorem NFA.evalFrom_iUnion E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {ι : Sort u_1} (s : ι → Set σ) (x : List α),
M.evalFrom (⋃ i, s i) x = ⋃ i, M.evalFrom (s i) x
theorem NFA.evalFrom_iUnion₂ E1 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {ι : Sort u_1} {κ : ι → Sort u_2} (f : (i : ι) → κ i → Set σ) (x : List α),
M.evalFrom (⋃ i, ⋃ j, f i j) x = ⋃ i, ⋃ j, M.evalFrom (f i j) x
theorem NFA.evalFrom_eq_biUnion_singleton E1 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (S : Set σ) (x : List α), M.evalFrom S x = ⋃ s ∈ S, M.evalFrom {s} x
theorem NFA.mem_evalFrom_iff_exists E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {s : σ} {S : Set σ} {x : List α},
s ∈ M.evalFrom S x ↔ ∃ t ∈ S, s ∈ M.evalFrom {t} x
def NFA.acceptsFrom E1 #
{α : Type u} → {σ : Type v} → NFA α σ → Set σ → Language α
M.acceptsFrom S is the language of x such that there is an accept state
in M.evalFrom S x.
theorem NFA.mem_acceptsFrom E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {S : Set σ} {x : List α},
x ∈ M.acceptsFrom S ↔ ∃ s ∈ M.accept, s ∈ M.evalFrom S x
theorem NFA.nil_mem_acceptsFrom E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S : Set σ}, [] ∈ M.acceptsFrom S ↔ ∃ s ∈ S, s ∈ M.accept
theorem NFA.cons_mem_acceptsFrom E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S : Set σ} {a : α} {x : List α},
a :: x ∈ M.acceptsFrom S ↔ x ∈ M.acceptsFrom (M.stepSet S a)
theorem NFA.cons_preimage_acceptsFrom E1 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S : Set σ} {a : α},
(fun x ↦ a :: x) ⁻¹' M.acceptsFrom S = M.acceptsFrom (M.stepSet S a)
theorem NFA.append_mem_acceptsFrom E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S : Set σ} {x y : List α},
x ++ y ∈ M.acceptsFrom S ↔ y ∈ M.acceptsFrom (M.evalFrom S x)
theorem NFA.append_preimage_acceptsFrom E1 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S : Set σ} {x : List α},
(fun x_1 ↦ x ++ x_1) ⁻¹' M.acceptsFrom S = M.acceptsFrom (M.evalFrom S x)
theorem NFA.acceptsFrom_union E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {S T : Set σ}, M.acceptsFrom (S ∪ T) = M.acceptsFrom S + M.acceptsFrom T
theorem NFA.acceptsFrom_iUnion E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {ι : Sort u_1} (s : ι → Set σ),
M.acceptsFrom (⋃ i, s i) = ⋃ i, M.acceptsFrom (s i)
theorem NFA.acceptsFrom_iUnion₂ E1 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) {ι : Sort u_1} {κ : ι → Sort u_2} (f : (i : ι) → κ i → Set σ),
M.acceptsFrom (⋃ i, ⋃ j, f i j) = ⋃ i, ⋃ j, M.acceptsFrom (f i j)
def NFA.eval E1 #
{α : Type u} → {σ : Type v} → NFA α σ → List α → Set σ
M.eval x computes all possible paths though M with input x starting at an element of
M.start.
theorem NFA.eval_nil E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ), M.eval [] = M.start
theorem NFA.eval_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (a : α), M.eval [a] = M.stepSet M.start a
theorem NFA.eval_append_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (x : List α) (a : α), M.eval (x ++ [a]) = M.stepSet (M.eval x) a
def NFA.accepts E1 #
{α : Type u} → {σ : Type v} → NFA α σ → Language α
M.accepts is the language of x such that there is an accept state in M.eval x.
theorem NFA.mem_accepts E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {x : List α}, x ∈ M.accepts ↔ ∃ S ∈ M.accept, S ∈ M.evalFrom M.start x
theorem NFA.accepts_eq_acceptsFrom_start E1 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ}, M.accepts = M.acceptsFrom M.start
inductive NFA.Path E1 #
{α : Type u} → {σ : Type v} → NFA α σ → σ → σ → List α → Type (max u v)
M.Path represents a concrete path through the NFA from a start state to an end state
for a particular word.
Note that due to the non-deterministic nature of the automata, there can be more than one Path
for a given word.
Also note that this is Type and not a Prop, so that we can speak about the properties
of a particular Path, such as the set of states visited along the way (defined as Path.supp).
ctor NFA.Path.nil E0 #
{α : Type u} → {σ : Type v} → {M : NFA α σ} → (s : σ) → M.Path s s []
ctor NFA.Path.cons E0 #
{α : Type u} →
{σ : Type v} →
{M : NFA α σ} → (t s u : σ) → (a : α) → (x : List α) → t ∈ M.step s a → M.Path t u x → M.Path s u (a :: x)
def NFA.Path.supp E1 #
{α : Type u} → {σ : Type v} → {M : NFA α σ} → [DecidableEq σ] → {s t : σ} → {x : List α} → M.Path s t x → Finset σ
Set of states visited by a path.
theorem NFA.mem_evalFrom_iff_nonempty_path E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {s t : σ} {x : List α}, t ∈ M.evalFrom {s} x ↔ Nonempty (M.Path s t x)
theorem NFA.accepts_iff_exists_path E1 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {x : List α},
x ∈ M.accepts ↔ ∃ s ∈ M.start, ∃ t ∈ M.accept, Nonempty (M.Path s t x)
def NFA.toDFA E1 #
{α : Type u} → {σ : Type v} → 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.
theorem NFA.toDFA_correct E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ}, M.toDFA.accepts = M.accepts
theorem NFA.pumping_lemma E3 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} [inst : Fintype σ] {x : List α},
x ∈ M.accepts →
Fintype.card (Set σ) ≤ x.length →
∃ a b c,
x = a ++ b ++ c ∧ a.length + b.length ≤ Fintype.card (Set σ) ∧ b ≠ [] ∧ {a} * KStar.kstar {b} * {c} ≤ M.accepts
def DFA.toNFA E1 #
{α : Type u} → {σ : Type v} → 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.
theorem DFA.toNFA_start E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), M.toNFA.start = {M.start}
theorem DFA.toNFA_step E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (s : σ) (a : α), M.toNFA.step s a = {M.step s a}
theorem DFA.toNFA_accept E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), M.toNFA.accept = M.accept
theorem DFA.toNFA_evalFrom_match E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (start : σ) (s : List α), M.toNFA.evalFrom {start} s = {M.evalFrom start s}
theorem DFA.toNFA_correct E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), M.toNFA.accepts = M.accepts
def NFA.reverse E1 #
{α : Type u} → {σ : Type v} → NFA α σ → NFA α σ
M.reverse constructs an NFA with the same states as M, but all the transitions reversed. The
resulting automaton accepts a word x if and only if M accepts List.reverse x.
theorem NFA.reverse_step E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ) (s : σ) (a : α), M.reverse.step s a = {s' | s ∈ M.step s' a}
theorem NFA.reverse_start E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ), M.reverse.start = M.accept
theorem NFA.reverse_accept E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ), M.reverse.accept = M.start
theorem NFA.reverse_reverse E0 #
∀ {α : Type u} {σ : Type v} (M : NFA α σ), M.reverse.reverse = M
theorem NFA.disjoint_stepSet_reverse E1 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {a : α} {S S' : Set σ},
Disjoint S (M.reverse.stepSet S' a) ↔ Disjoint S' (M.stepSet S a)
theorem NFA.disjoint_evalFrom_reverse E1 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {x : List α} {S S' : Set σ},
Disjoint S (M.reverse.evalFrom S' x) → Disjoint S' (M.evalFrom S x.reverse)
theorem NFA.disjoint_evalFrom_reverse_iff E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {x : List α} {S S' : Set σ},
Disjoint S (M.reverse.evalFrom S' x) ↔ Disjoint S' (M.evalFrom S x.reverse)
theorem NFA.mem_accepts_reverse E0 #
∀ {α : Type u} {σ : Type v} {M : NFA α σ} {x : List α}, x ∈ M.reverse.accepts ↔ x.reverse ∈ M.accepts
theorem Language.IsRegular.reverse E1 #
∀ {α : Type u} {L : Language α}, L.IsRegular → L.reverse.IsRegular
theorem Language.IsRegular.of_reverse E1 #
∀ {α : Type u} {L : Language α}, L.reverse.IsRegular → L.IsRegular
theorem Language.isRegular_reverse_iff E0 #
∀ {α : Type u} {L : Language α}, L.reverse.IsRegular ↔ L.IsRegular
Regular languages are closed under reversal.