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 }
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
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
P.rmatch x
is true if and only if P
matches x
. This is a computable definition equivalent
to matches'
.
Instances For
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
The language of the map is the map of the language.