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.instInhabitedRegularExpression = { default := RegularExpression.zero }
Equations
- RegularExpression.instAddRegularExpression = { add := RegularExpression.plus }
Equations
- RegularExpression.instMulRegularExpression = { mul := RegularExpression.comp }
Equations
- RegularExpression.instOneRegularExpression = { one := RegularExpression.epsilon }
Equations
- RegularExpression.instZeroRegularExpression = { zero := RegularExpression.zero }
Equations
- RegularExpression.instPowRegularExpressionNat = { pow := fun (n : RegularExpression α) (r : ℕ) => npowRec r n }
matches' P
provides a language which contains all strings that P
matches
Equations
- RegularExpression.matches' RegularExpression.zero = 0
- RegularExpression.matches' RegularExpression.epsilon = 1
- RegularExpression.matches' (RegularExpression.char a) = {[a]}
- RegularExpression.matches' (RegularExpression.plus P Q) = RegularExpression.matches' P + RegularExpression.matches' Q
- RegularExpression.matches' (RegularExpression.comp P Q) = RegularExpression.matches' P * RegularExpression.matches' Q
- RegularExpression.matches' (RegularExpression.star P) = KStar.kstar (RegularExpression.matches' P)
Instances For
matchEpsilon P
is true if and only if P
matches the empty string
Equations
- RegularExpression.matchEpsilon RegularExpression.zero = false
- RegularExpression.matchEpsilon RegularExpression.epsilon = true
- RegularExpression.matchEpsilon (RegularExpression.char a) = false
- RegularExpression.matchEpsilon (RegularExpression.plus P Q) = (RegularExpression.matchEpsilon P || RegularExpression.matchEpsilon Q)
- RegularExpression.matchEpsilon (RegularExpression.comp P Q) = (RegularExpression.matchEpsilon P && RegularExpression.matchEpsilon Q)
- RegularExpression.matchEpsilon (RegularExpression.star P) = true
Instances For
P.deriv a
matches x
if P
matches a :: x
, the Brzozowski derivative of P
with respect
to a
Equations
- One or more equations did not get rendered due to their size.
- RegularExpression.deriv RegularExpression.zero x = 0
- RegularExpression.deriv RegularExpression.epsilon x = 0
- RegularExpression.deriv (RegularExpression.char a₁) x = if a₁ = x then 1 else 0
- RegularExpression.deriv (RegularExpression.plus P Q) x = RegularExpression.deriv P x + RegularExpression.deriv Q x
- RegularExpression.deriv (RegularExpression.star P) x = RegularExpression.deriv P x * RegularExpression.star P
Instances For
P.rmatch x
is true if and only if P
matches x
. This is a computable definition equivalent
to matches'
.
Equations
Instances For
Equations
- One or more equations did not get rendered due to their size.
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 (RegularExpression.plus P Q) = RegularExpression.map f P + RegularExpression.map f Q
- RegularExpression.map f (RegularExpression.comp P Q) = RegularExpression.map f P * RegularExpression.map f Q
- RegularExpression.map f (RegularExpression.star P) = RegularExpression.star (RegularExpression.map f P)
Instances For
The language of the map is the map of the language.