The areas / V32. Logic, model theory and computability
Mathlib.Computability.DFA
67 declarations. Mathlib docs · Source
Deterministic Finite Automata
A Deterministic Finite Automaton (DFA) is a state machine which
decides membership in a particular Language, by following a path
uniquely determined by an input string.
We define regular languages to be ones for which a DFA exists, other formulations are later proved equivalent.
Note that this definition allows for automata with infinite states,
a Fintype instance must be supplied for true DFAs.
Main definitions
DFA α σ: automaton over alphabetαand set of statesσM.accepts: the language accepted by the DFAMLanguage.IsRegular L: a predicate stating thatLis a regular language, i.e. there exists a DFA that recognizes the language
Main theorems
DFA.pumping_lemma: every sufficiently long string accepted by the DFA has a substring that can be repeated arbitrarily many times (and have the overall string still be accepted)
Implementation notes
Currently, there are two disjoint sets of simp lemmas: one for DFA.eval, and another for
DFA.evalFrom. You can switch from the former to the latter using simp [eval].
TODO
- Should we unify these simp sets, such that
evalis rewritten toevalFromautomatically? - Should
mem_acceptsandmem_acceptsFrombe marked@[simp]?
theorem DFA.mk.inj E1 #
∀ {α : Type u} {σ : Type v} {step : σ → α → σ} {start : σ} {accept : Set σ} {step_1 : σ → α → σ} {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 DFA.mk.injEq E0 #
∀ {α : Type u} {σ : Type v} (step : σ → α → σ) (start : σ) (accept : Set σ) (step_1 : σ → α → σ) (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)
def DFA.ctorIdx E1 #
{α : Type u} → {σ : Type v} → DFA α σ → ℕ
structure DFA E3 #
Type u → Type v → Type (max u v)
A DFA is a set of states (σ), a transition function from state to state labelled by the
alphabet (step), a starting state (start) and a set of acceptance states (accept).
ctor DFA.mk E0 #
{α : Type u} → {σ : Type v} → (σ → α → σ) → σ → Set σ → DFA α σ
abbrev DFA.step E1 #
{α : Type u} → {σ : Type v} → DFA α σ → σ → α → σ
A transition function from state to state labelled by the alphabet.
abbrev DFA.start E1 #
{α : Type u} → {σ : Type v} → DFA α σ → σ
Starting state.
abbrev DFA.accept E1 #
{α : Type u} → {σ : Type v} → DFA α σ → Set σ
Set of acceptance states.
instance DFA.instInhabited E0 #
{α : Type u} → {σ : Type v} → [Inhabited σ] → Inhabited (DFA α σ)
def DFA.evalFrom E1 #
{α : Type u} → {σ : Type v} → DFA α σ → σ → List α → σ
M.evalFrom s x evaluates M with input x starting from the state s.
theorem DFA.evalFrom_nil E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (s : σ), M.evalFrom s [] = s
theorem DFA.evalFrom_cons E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (s : σ) (a : α) (x : List α),
M.evalFrom s (a :: x) = M.evalFrom (M.step s a) x
theorem DFA.evalFrom_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (s : σ) (a : α), M.evalFrom s [a] = M.step s a
theorem DFA.evalFrom_append_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (s : σ) (x : List α) (a : α),
M.evalFrom s (x ++ [a]) = M.step (M.evalFrom s x) a
def DFA.eval E1 #
{α : Type u} → {σ : Type v} → DFA α σ → List α → σ
M.eval x evaluates M with input x starting from the state M.start.
theorem DFA.eval_nil E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), M.eval [] = M.start
theorem DFA.eval_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (a : α), M.eval [a] = M.step M.start a
theorem DFA.eval_append_singleton E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (x : List α) (a : α), M.eval (x ++ [a]) = M.step (M.eval x) a
theorem DFA.evalFrom_of_append E1 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (start : σ) (x y : List α),
M.evalFrom start (x ++ y) = M.evalFrom (M.evalFrom start x) y
def DFA.acceptsFrom E1 #
{α : Type u} → {σ : Type v} → DFA α σ → σ → Language α
M.acceptsFrom s is the language of x such that M.evalFrom s x is an accept state.
theorem DFA.mem_acceptsFrom E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {s : σ} {x : List α}, x ∈ M.acceptsFrom s ↔ M.evalFrom s x ∈ M.accept
def DFA.accepts E1 #
{α : Type u} → {σ : Type v} → DFA α σ → Language α
M.accepts is the language of x such that M.eval x is an accept state.
theorem DFA.mem_accepts E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {x : List α}, x ∈ M.accepts ↔ M.eval x ∈ M.accept
theorem DFA.evalFrom_split E1 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) [inst : Fintype σ] {x : List α} {s t : σ},
Fintype.card σ ≤ x.length →
M.evalFrom s x = t →
∃ q a b c,
x = a ++ b ++ c ∧
a.length + b.length ≤ Fintype.card σ ∧ b ≠ [] ∧ M.evalFrom s a = q ∧ M.evalFrom q b = q ∧ M.evalFrom q c = t
theorem DFA.evalFrom_of_pow E1 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {x y : List α} {s : σ},
M.evalFrom s x = s → y ∈ KStar.kstar {x} → M.evalFrom s y = s
theorem DFA.pumping_lemma E3 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) [inst : Fintype σ] {x : List α},
x ∈ M.accepts →
Fintype.card σ ≤ x.length →
∃ a b c, x = a ++ b ++ c ∧ a.length + b.length ≤ Fintype.card σ ∧ b ≠ [] ∧ {a} * KStar.kstar {b} * {c} ≤ M.accepts
def DFA.comap E1 #
{α : Type u} → {σ : Type v} → {α' : Type u_1} → (α' → α) → DFA α σ → DFA α' σ
M.comap f pulls back the alphabet of M along f. In other words, it applies f to the input
before passing it to M.
theorem DFA.comap_start E0 #
∀ {α : Type u} {σ : Type v} {α' : Type u_1} (f : α' → α) (M : DFA α σ), (DFA.comap f M).start = M.start
theorem DFA.comap_accept E0 #
∀ {α : Type u} {σ : Type v} {α' : Type u_1} (f : α' → α) (M : DFA α σ), (DFA.comap f M).accept = M.accept
theorem DFA.comap_step E0 #
∀ {α : Type u} {σ : Type v} {α' : Type u_1} (f : α' → α) (M : DFA α σ) (s : σ) (a : α'),
(DFA.comap f M).step s a = M.step s (f a)
theorem DFA.comap_id E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), DFA.comap id M = M
theorem DFA.evalFrom_comap E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {α' : Type u_1} (f : α' → α) (s : σ) (x : List α'),
(DFA.comap f M).evalFrom s x = M.evalFrom s (List.map f x)
theorem DFA.eval_comap E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {α' : Type u_1} (f : α' → α) (x : List α'),
(DFA.comap f M).eval x = M.eval (List.map f x)
theorem DFA.accepts_comap E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {α' : Type u_1} (f : α' → α),
(DFA.comap f M).accepts = List.map f ⁻¹' M.accepts
def DFA.reindex E1 #
{α : Type u} → {σ : Type v} → {σ' : Type u_2} → σ ≃ σ' → DFA α σ ≃ DFA α σ'
Lifts an equivalence on states to an equivalence on DFAs.
theorem DFA.reindex_apply_accept E0 #
∀ {α : Type u} {σ : Type v} {σ' : Type u_2} (g : σ ≃ σ') (M : DFA α σ),
((DFA.reindex g) M).accept = ⇑g.symm ⁻¹' M.accept
theorem DFA.reindex_apply_start E0 #
∀ {α : Type u} {σ : Type v} {σ' : Type u_2} (g : σ ≃ σ') (M : DFA α σ), ((DFA.reindex g) M).start = g M.start
theorem DFA.reindex_apply_step E0 #
∀ {α : Type u} {σ : Type v} {σ' : Type u_2} (g : σ ≃ σ') (M : DFA α σ) (s : σ') (a : α),
((DFA.reindex g) M).step s a = g (M.step (g.symm s) a)
theorem DFA.reindex_refl E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), (DFA.reindex (Equiv.refl σ)) M = M
theorem DFA.symm_reindex E0 #
∀ {α : Type u} {σ : Type v} {σ' : Type u_2} (g : σ ≃ σ'), (DFA.reindex g).symm = DFA.reindex g.symm
theorem DFA.evalFrom_reindex E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {σ' : Type u_2} (g : σ ≃ σ') (s : σ') (x : List α),
((DFA.reindex g) M).evalFrom s x = g (M.evalFrom (g.symm s) x)
theorem DFA.eval_reindex E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {σ' : Type u_2} (g : σ ≃ σ') (x : List α),
((DFA.reindex g) M).eval x = g (M.eval x)
theorem DFA.accepts_reindex E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {σ' : Type u_2} (g : σ ≃ σ'), ((DFA.reindex g) M).accepts = M.accepts
theorem DFA.comap_reindex E1 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) {α' : Type u_1} {σ' : Type u_2} (f : α' → α) (g : σ ≃ σ'),
DFA.comap f ((DFA.reindex g) M) = (DFA.reindex g) (DFA.comap f M)
instance DFA.instCompl E0 #
{α : Type u} → {σ : Type v} → Compl (DFA α σ)
DFAs are closed under complement:
Given a DFA M, Mᶜ is also a DFA such that L(Mᶜ) = {x ∣ x ∉ L(M)}.
theorem DFA.compl_def E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), Mᶜ = { step := M.step, start := M.start, accept := M.acceptᶜ }
theorem DFA.acceptsFrom_compl E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ) (s : σ), Mᶜ.acceptsFrom s = (M.acceptsFrom s)ᶜ
theorem DFA.accepts_compl E0 #
∀ {α : Type u} {σ : Type v} (M : DFA α σ), Mᶜ.accepts = M.acceptsᶜ
def DFA.union E1 #
{α : Type u} → {σ1 σ2 : Type v} → DFA α σ1 → DFA α σ2 → DFA α (σ1 × σ2)
DFAs are closed under union.
theorem DFA.union_step E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2) (s : σ1 × σ2) (a : α),
(M1.union M2).step s a = (M1.step s.1 a, M2.step s.2 a)
theorem DFA.union_accept E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2),
(M1.union M2).accept = {s | s.1 ∈ M1.accept ∨ s.2 ∈ M2.accept}
theorem DFA.union_start E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2), (M1.union M2).start = (M1.start, M2.start)
theorem DFA.acceptsFrom_union E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2) (s1 : σ1) (s2 : σ2),
(M1.union M2).acceptsFrom (s1, s2) = M1.acceptsFrom s1 + M2.acceptsFrom s2
theorem DFA.accepts_union E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2), (M1.union M2).accepts = M1.accepts + M2.accepts
def DFA.inter E1 #
{α : Type u} → {σ1 σ2 : Type v} → DFA α σ1 → DFA α σ2 → DFA α (σ1 × σ2)
DFAs are closed under intersection.
theorem DFA.inter_start E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2), (M1.inter M2).start = (M1.start, M2.start)
theorem DFA.inter_accept E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2),
(M1.inter M2).accept = {s | s.1 ∈ M1.accept ∧ s.2 ∈ M2.accept}
theorem DFA.inter_step E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2) (s : σ1 × σ2) (a : α),
(M1.inter M2).step s a = (M1.step s.1 a, M2.step s.2 a)
theorem DFA.acceptsFrom_inter E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2) (s1 : σ1) (s2 : σ2),
(M1.inter M2).acceptsFrom (s1, s2) = M1.acceptsFrom s1 ⊓ M2.acceptsFrom s2
theorem DFA.accepts_inter E0 #
∀ {α : Type u} {σ1 σ2 : Type v} (M1 : DFA α σ1) (M2 : DFA α σ2), (M1.inter M2).accepts = M1.accepts ⊓ M2.accepts
def Language.IsRegular E3 #
{T : Type u} → Language T → Prop
A regular language is a language that is defined by a DFA with finite states.
theorem Language.isRegular_iff E0 #
∀ {T : Type u} {L : Language T}, L.IsRegular ↔ ∃ σ x M, M.accepts = L
A language is regular if and only if it is defined by a DFA with finite states.
This is more general than using the definition of Language.IsRegular directly, as the state type
σ is universe-polymorphic.
theorem Language.IsRegular.compl E1 #
∀ {T : Type u} {L : Language T}, L.IsRegular → Lᶜ.IsRegular
theorem Language.IsRegular.of_compl E1 #
∀ {T : Type u} {L : Language T}, Lᶜ.IsRegular → L.IsRegular
theorem Language.IsRegular_compl E0 #
∀ {T : Type u} {L : Language T}, Lᶜ.IsRegular ↔ L.IsRegular
Regular languages are closed under complement.
theorem Language.IsRegular.add E1 #
∀ {T : Type u} {L1 L2 : Language T}, L1.IsRegular → L2.IsRegular → (L1 + L2).IsRegular
Regular languages are closed under union.
theorem Language.IsRegular.inf E1 #
∀ {T : Type u} {L1 L2 : Language T}, L1.IsRegular → L2.IsRegular → (L1 ⊓ L2).IsRegular
Regular languages are closed under intersection.