# mathlibdocumentation

computability.regular_expressions

# 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.
• attribute [pattern] has_mul.mul has been added into this file, it could be moved.
inductive regular_expression (α : Type u) :
Type u
• zero : Π (α : Type u),
• epsilon : Π (α : Type u),
• char : Π (α : Type u), α →
• plus : Π (α : Type u),
• comp : Π (α : Type u),
• star : Π (α : Type u),

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 nothing
• 1 (epsilon) matches only the empty string
• char a matches only the string 'a'
• star P matches any finite concatenation of strings which match P
• P + Q (plus P Q) matches anything which match P or Q
• P * Q (comp P Q) matches x ++ y if x matches P and y matches Q
@[instance]
def regular_expression.inhabited {α : Type u} :
Equations
@[instance]
def regular_expression.has_add {α : Type u} :
Equations
@[instance]
def regular_expression.has_mul {α : Type u} :
Equations
@[instance]
def regular_expression.has_one {α : Type u} :
Equations
@[instance]
def regular_expression.has_zero {α : Type u} :
Equations
@[simp]
theorem regular_expression.zero_def {α : Type u} :
@[simp]
theorem regular_expression.one_def {α : Type u} :
@[simp]
theorem regular_expression.plus_def {α : Type u} (P Q : regular_expression α) :
P.plus Q = P + Q
@[simp]
theorem regular_expression.comp_def {α : Type u} (P Q : regular_expression α) :
P.comp Q = P * Q
def regular_expression.matches {α : Type u} :

matches P provides a language which contains all strings that P matches

Equations
@[simp]
theorem regular_expression.matches_zero_def {α : Type u} :
@[simp]
theorem regular_expression.matches_epsilon_def {α : Type u} :
@[simp]
theorem regular_expression.matches_add_def {α : Type u} (P Q : regular_expression α) :
@[simp]
theorem regular_expression.matches_mul_def {α : Type u} (P Q : regular_expression α) :
(P * Q).matches = (P.matches) * Q.matches
@[simp]
def regular_expression.match_epsilon {α : Type u} :

match_epsilon P is true if and only if P matches the empty string

Equations
def regular_expression.deriv {α : Type u} [dec : decidable_eq α] :
α →

P.deriv a matches x if P matches a :: x, the Brzozowski derivative of P with respect to a

Equations
def regular_expression.rmatch {α : Type u} [dec : decidable_eq α] :
list αbool

P.rmatch x is true if and only if P matches x. This is a computable definition equivalent to matches.

Equations
@[simp]
theorem regular_expression.zero_rmatch {α : Type u} [dec : decidable_eq α] (x : list α) :
0.rmatch x = ff
theorem regular_expression.one_rmatch_iff {α : Type u} [dec : decidable_eq α] (x : list α) :
(1.rmatch x)
theorem regular_expression.char_rmatch_iff {α : Type u} [dec : decidable_eq α] (a : α) (x : list α) :
x) x = [a]
theorem regular_expression.add_rmatch_iff {α : Type u} [dec : decidable_eq α] (P Q : regular_expression α) (x : list α) :
((P + Q).rmatch x) (P.rmatch x) (Q.rmatch x)
theorem regular_expression.mul_rmatch_iff {α : Type u} [dec : decidable_eq α] (P Q : regular_expression α) (x : list α) :
((P * Q).rmatch x) ∃ (t u : list α), x = t ++ u (P.rmatch t) (Q.rmatch u)
theorem regular_expression.star_rmatch_iff {α : Type u} [dec : decidable_eq α] (P : regular_expression α) (x : list α) :
(P.star.rmatch x) ∃ (S : list (list α)), x = S.join ∀ (t : list α), t S (P.rmatch t)
@[simp]
theorem regular_expression.rmatch_iff_matches {α : Type u} [dec : decidable_eq α] (P : regular_expression α) (x : list α) :
@[instance]
Equations