Regular Expressions #
This file contains the formal definition for regular expressions and basic lemmas. Note these are regular expressions in terms of formal language theory. Note this is different to regex's used in computer science such as the POSIX standard.
TODO #
- Show that this regular expressions and DFA/NFA's are equivalent.
This is the definition of regular expressions. The names used here is to mirror the definition of a Kleene algebra (https://en.wikipedia.org/wiki/Kleene_algebra).
0
(zero
) matches nothing1
(epsilon
) matches only the empty stringchar a
matches only the string 'a'star P
matches any finite concatenation of strings which matchP
P + Q
(plus P Q
) matches anything which matchP
orQ
P * Q
(comp P Q
) matchesx ++ y
ifx
matchesP
andy
matchesQ
- zero: {α : Type u} → RegularExpression α
- epsilon: {α : Type u} → RegularExpression α
- char: {α : Type u} → α → RegularExpression α
- plus: {α : Type u} → RegularExpression α → RegularExpression α → RegularExpression α
- comp: {α : Type u} → RegularExpression α → RegularExpression α → RegularExpression α
- star: {α : Type u} → RegularExpression α → RegularExpression α
Instances For
Equations
- RegularExpression.instInhabited = { default := RegularExpression.zero }
Equations
- RegularExpression.instAdd = { add := RegularExpression.plus }
Equations
- RegularExpression.instMul = { mul := RegularExpression.comp }
Equations
- RegularExpression.instOne = { one := RegularExpression.epsilon }
Equations
- RegularExpression.instZero = { zero := RegularExpression.zero }
Equations
- RegularExpression.instPowNat = { pow := fun (n : RegularExpression α) (r : ℕ) => npowRec r n }
@[simp]
theorem
RegularExpression.plus_def
{α : Type u_1}
(P : RegularExpression α)
(Q : RegularExpression α)
:
@[simp]
theorem
RegularExpression.comp_def
{α : Type u_1}
(P : RegularExpression α)
(Q : RegularExpression α)
:
matches' P
provides a language which contains all strings that P
matches
Equations
- RegularExpression.zero.matches' = 0
- RegularExpression.epsilon.matches' = 1
- (RegularExpression.char a).matches' = {[a]}
- (P.plus Q).matches' = P.matches' + Q.matches'
- (P.comp Q).matches' = P.matches' * Q.matches'
- P.star.matches' = KStar.kstar P.matches'
Instances For
@[simp]
@[simp]
@[simp]
theorem
RegularExpression.matches'_char
{α : Type u_1}
(a : α)
:
(RegularExpression.char a).matches' = {[a]}
@[simp]
theorem
RegularExpression.matches'_add
{α : Type u_1}
(P : RegularExpression α)
(Q : RegularExpression α)
:
@[simp]
theorem
RegularExpression.matches'_mul
{α : Type u_1}
(P : RegularExpression α)
(Q : RegularExpression α)
:
@[simp]
@[simp]
theorem
RegularExpression.matches'_star
{α : Type u_1}
(P : RegularExpression α)
:
P.star.matches' = KStar.kstar P.matches'
def
RegularExpression.deriv
{α : Type u_1}
[DecidableEq α]
:
RegularExpression α → α → RegularExpression α
P.deriv a
matches x
if P
matches a :: x
, the Brzozowski derivative of P
with respect
to a
Equations
- RegularExpression.zero.deriv x = 0
- RegularExpression.epsilon.deriv x = 0
- (RegularExpression.char a₁).deriv x = if a₁ = x then 1 else 0
- (P.plus Q).deriv x = P.deriv x + Q.deriv x
- (P.comp Q).deriv x = if P.matchEpsilon = true then P.deriv x * Q + Q.deriv x else P.deriv x * Q
- P.star.deriv x = P.deriv x * P.star
Instances For
@[simp]
theorem
RegularExpression.deriv_zero
{α : Type u_1}
[DecidableEq α]
(a : α)
:
RegularExpression.deriv 0 a = 0
@[simp]
theorem
RegularExpression.deriv_one
{α : Type u_1}
[DecidableEq α]
(a : α)
:
RegularExpression.deriv 1 a = 0
@[simp]
theorem
RegularExpression.deriv_char_self
{α : Type u_1}
[DecidableEq α]
(a : α)
:
(RegularExpression.char a).deriv a = 1
@[simp]
theorem
RegularExpression.deriv_char_of_ne
{α : Type u_1}
{a : α}
{b : α}
[DecidableEq α]
(h : a ≠ b)
:
(RegularExpression.char a).deriv b = 0
@[simp]
theorem
RegularExpression.deriv_add
{α : Type u_1}
[DecidableEq α]
(P : RegularExpression α)
(Q : RegularExpression α)
(a : α)
:
@[simp]
theorem
RegularExpression.deriv_star
{α : Type u_1}
[DecidableEq α]
(P : RegularExpression α)
(a : α)
:
@[simp]
theorem
RegularExpression.one_rmatch_iff
{α : Type u_1}
[DecidableEq α]
(x : List α)
:
RegularExpression.rmatch 1 x = true ↔ x = []
theorem
RegularExpression.char_rmatch_iff
{α : Type u_1}
[DecidableEq α]
(a : α)
(x : List α)
:
(RegularExpression.char a).rmatch x = true ↔ x = [a]
theorem
RegularExpression.add_rmatch_iff
{α : Type u_1}
[DecidableEq α]
(P : RegularExpression α)
(Q : RegularExpression α)
(x : List α)
:
@[simp]
theorem
RegularExpression.rmatch_iff_matches'
{α : Type u_1}
[DecidableEq α]
(P : RegularExpression α)
(x : List α)
:
instance
RegularExpression.instDecidablePredListMemLanguageMatches'
{α : Type u_1}
[DecidableEq α]
(P : RegularExpression α)
:
DecidablePred fun (x : List α) => x ∈ P.matches'
Equations
- P.instDecidablePredListMemLanguageMatches' x = decidable_of_iff (P.rmatch x = true) ⋯
Map the alphabet of a regular expression.
Equations
- RegularExpression.map f RegularExpression.zero = 0
- RegularExpression.map f RegularExpression.epsilon = 1
- RegularExpression.map f (RegularExpression.char a) = RegularExpression.char (f a)
- RegularExpression.map f (P.plus Q) = RegularExpression.map f P + RegularExpression.map f Q
- RegularExpression.map f (P.comp Q) = RegularExpression.map f P * RegularExpression.map f Q
- RegularExpression.map f P.star = (RegularExpression.map f P).star
Instances For
@[simp]
theorem
RegularExpression.map_pow
{α : Type u_1}
{β : Type u_2}
(f : α → β)
(P : RegularExpression α)
(n : ℕ)
:
RegularExpression.map f (P ^ n) = RegularExpression.map f P ^ n
@[simp]
theorem
RegularExpression.map_id
{α : Type u_1}
(P : RegularExpression α)
:
RegularExpression.map id P = P
@[simp]
theorem
RegularExpression.map_map
{α : Type u_1}
{β : Type u_2}
{γ : Type u_3}
(g : β → γ)
(f : α → β)
(P : RegularExpression α)
:
RegularExpression.map g (RegularExpression.map f P) = RegularExpression.map (g ∘ f) P
@[simp]
theorem
RegularExpression.matches'_map
{α : Type u_1}
{β : Type u_2}
(f : α → β)
(P : RegularExpression α)
:
(RegularExpression.map f P).matches' = (Language.map f) P.matches'
The language of the map is the map of the language.