The areas / V32. Logic, model theory and computability
Mathlib.Computability.RegularExpressions
66 declarations. Mathlib docs · Source
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 regexes used in computer science such as the POSIX standard.
TODO
Currently, we do not show that regular expressions and DFAs/NFAs are equivalent. Multiple competing PRs towards that goal are in review. See https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Regular.20languages.3A.20the.20review.20queue
theorem RegularExpression.matches'.match_1.congr_eq_3 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → Sort u_2) (x : RegularExpression α)
(h_1 : Unit → motive RegularExpression.zero) (h_2 : Unit → motive RegularExpression.epsilon)
(h_3 : (a : α) → motive (RegularExpression.char a)) (h_4 : (P Q : RegularExpression α) → motive (P.plus Q))
(h_5 : (P Q : RegularExpression α) → motive (P.comp Q)) (h_6 : (P : RegularExpression α) → motive P.star) (a : α),
x = RegularExpression.char a →
(match x with
| RegularExpression.zero => h_1 ()
| RegularExpression.epsilon => h_2 ()
| RegularExpression.char a => h_3 a
| P.plus Q => h_4 P Q
| P.comp Q => h_5 P Q
| P.star => h_6 P) ≍
h_3 a
theorem RegularExpression.deriv.match_1.congr_eq_6 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → α → Sort u_2) (x : RegularExpression α) (x_1 : α)
(h_1 : (x : α) → motive RegularExpression.zero x) (h_2 : (x : α) → motive RegularExpression.epsilon x)
(h_3 : (a₁ a₂ : α) → motive (RegularExpression.char a₁) a₂)
(h_4 : (P Q : RegularExpression α) → (a : α) → motive (P.plus Q) a)
(h_5 : (P Q : RegularExpression α) → (a : α) → motive (P.comp Q) a)
(h_6 : (P : RegularExpression α) → (a : α) → motive P.star a) (P : RegularExpression α) (a : α),
x = P.star →
x_1 = a →
(match x, x_1 with
| RegularExpression.zero, x => h_1 x
| RegularExpression.epsilon, x => h_2 x
| RegularExpression.char a₁, a₂ => h_3 a₁ a₂
| P.plus Q, a => h_4 P Q a
| P.comp Q, a => h_5 P Q a
| P.star, a => h_6 P a) ≍
h_6 P a
theorem RegularExpression.rmatch.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → List α → Sort u_2) (x : RegularExpression α) (x_1 : List α)
(h_1 : (P : RegularExpression α) → motive P [])
(h_2 : (P : RegularExpression α) → (a : α) → (as : List α) → motive P (a :: as)) (P : RegularExpression α),
x = P →
x_1 = [] →
(match x, x_1 with
| P, [] => h_1 P
| P, a :: as => h_2 P a as) ≍
h_1 P
theorem RegularExpression.deriv.match_1.congr_eq_3 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → α → Sort u_2) (x : RegularExpression α) (x_1 : α)
(h_1 : (x : α) → motive RegularExpression.zero x) (h_2 : (x : α) → motive RegularExpression.epsilon x)
(h_3 : (a₁ a₂ : α) → motive (RegularExpression.char a₁) a₂)
(h_4 : (P Q : RegularExpression α) → (a : α) → motive (P.plus Q) a)
(h_5 : (P Q : RegularExpression α) → (a : α) → motive (P.comp Q) a)
(h_6 : (P : RegularExpression α) → (a : α) → motive P.star a) (a₁ a₂ : α),
x = RegularExpression.char a₁ →
x_1 = a₂ →
(match x, x_1 with
| RegularExpression.zero, x => h_1 x
| RegularExpression.epsilon, x => h_2 x
| RegularExpression.char a₁, a₂ => h_3 a₁ a₂
| P.plus Q, a => h_4 P Q a
| P.comp Q, a => h_5 P Q a
| P.star, a => h_6 P a) ≍
h_3 a₁ a₂
theorem RegularExpression.brecOn.eq E1 #
∀ {α : Type u} {motive : RegularExpression α → Sort u_1} (t : RegularExpression α)
(F_1 : (t : RegularExpression α) → RegularExpression.below t → motive t),
RegularExpression.brecOn t F_1 = F_1 t (RegularExpression.brecOn.go t F_1).2
theorem RegularExpression.rmatch.congr_simp E1 #
∀ {α : Type u_1} {inst : DecidableEq α} [inst_1 : DecidableEq α] (a a_1 : RegularExpression α),
a = a_1 → ∀ (a_2 a_3 : List α), a_2 = a_3 → a.rmatch a_2 = a_1.rmatch a_3
abbrev RegularExpression.ctorElimType E1 #
{α : Type u} →
{motive : RegularExpression α → Sort u_1} → ℕ → Sort (max 1 u_1 (imax (u + 1) u_1) (imax (u + 1) (u + 1) u_1))
theorem RegularExpression.matches'.match_1.congr_eq_6 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → Sort u_2) (x : RegularExpression α)
(h_1 : Unit → motive RegularExpression.zero) (h_2 : Unit → motive RegularExpression.epsilon)
(h_3 : (a : α) → motive (RegularExpression.char a)) (h_4 : (P Q : RegularExpression α) → motive (P.plus Q))
(h_5 : (P Q : RegularExpression α) → motive (P.comp Q)) (h_6 : (P : RegularExpression α) → motive P.star)
(P : RegularExpression α),
x = P.star →
(match x with
| RegularExpression.zero => h_1 ()
| RegularExpression.epsilon => h_2 ()
| RegularExpression.char a => h_3 a
| P.plus Q => h_4 P Q
| P.comp Q => h_5 P Q
| P.star => h_6 P) ≍
h_6 P
theorem RegularExpression.matches'.match_1.congr_eq_5 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → Sort u_2) (x : RegularExpression α)
(h_1 : Unit → motive RegularExpression.zero) (h_2 : Unit → motive RegularExpression.epsilon)
(h_3 : (a : α) → motive (RegularExpression.char a)) (h_4 : (P Q : RegularExpression α) → motive (P.plus Q))
(h_5 : (P Q : RegularExpression α) → motive (P.comp Q)) (h_6 : (P : RegularExpression α) → motive P.star)
(P Q : RegularExpression α),
x = P.comp Q →
(match x with
| RegularExpression.zero => h_1 ()
| RegularExpression.epsilon => h_2 ()
| RegularExpression.char a => h_3 a
| P.plus Q => h_4 P Q
| P.comp Q => h_5 P Q
| P.star => h_6 P) ≍
h_5 P Q
theorem RegularExpression.deriv.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → α → Sort u_2) (x : RegularExpression α) (x_1 : α)
(h_1 : (x : α) → motive RegularExpression.zero x) (h_2 : (x : α) → motive RegularExpression.epsilon x)
(h_3 : (a₁ a₂ : α) → motive (RegularExpression.char a₁) a₂)
(h_4 : (P Q : RegularExpression α) → (a : α) → motive (P.plus Q) a)
(h_5 : (P Q : RegularExpression α) → (a : α) → motive (P.comp Q) a)
(h_6 : (P : RegularExpression α) → (a : α) → motive P.star a) (x_2 : α),
x = RegularExpression.zero →
x_1 = x_2 →
(match x, x_1 with
| RegularExpression.zero, x => h_1 x
| RegularExpression.epsilon, x => h_2 x
| RegularExpression.char a₁, a₂ => h_3 a₁ a₂
| P.plus Q, a => h_4 P Q a
| P.comp Q, a => h_5 P Q a
| P.star, a => h_6 P a) ≍
h_1 x_2
theorem RegularExpression.matches'.match_1.congr_eq_4 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → Sort u_2) (x : RegularExpression α)
(h_1 : Unit → motive RegularExpression.zero) (h_2 : Unit → motive RegularExpression.epsilon)
(h_3 : (a : α) → motive (RegularExpression.char a)) (h_4 : (P Q : RegularExpression α) → motive (P.plus Q))
(h_5 : (P Q : RegularExpression α) → motive (P.comp Q)) (h_6 : (P : RegularExpression α) → motive P.star)
(P Q : RegularExpression α),
x = P.plus Q →
(match x with
| RegularExpression.zero => h_1 ()
| RegularExpression.epsilon => h_2 ()
| RegularExpression.char a => h_3 a
| P.plus Q => h_4 P Q
| P.comp Q => h_5 P Q
| P.star => h_6 P) ≍
h_4 P Q
theorem RegularExpression.matches'.match_1.congr_eq_1 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → Sort u_2) (x : RegularExpression α)
(h_1 : Unit → motive RegularExpression.zero) (h_2 : Unit → motive RegularExpression.epsilon)
(h_3 : (a : α) → motive (RegularExpression.char a)) (h_4 : (P Q : RegularExpression α) → motive (P.plus Q))
(h_5 : (P Q : RegularExpression α) → motive (P.comp Q)) (h_6 : (P : RegularExpression α) → motive P.star),
x = RegularExpression.zero →
(match x with
| RegularExpression.zero => h_1 ()
| RegularExpression.epsilon => h_2 ()
| RegularExpression.char a => h_3 a
| P.plus Q => h_4 P Q
| P.comp Q => h_5 P Q
| P.star => h_6 P) ≍
h_1 ()
theorem RegularExpression.deriv.match_1.congr_eq_5 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → α → Sort u_2) (x : RegularExpression α) (x_1 : α)
(h_1 : (x : α) → motive RegularExpression.zero x) (h_2 : (x : α) → motive RegularExpression.epsilon x)
(h_3 : (a₁ a₂ : α) → motive (RegularExpression.char a₁) a₂)
(h_4 : (P Q : RegularExpression α) → (a : α) → motive (P.plus Q) a)
(h_5 : (P Q : RegularExpression α) → (a : α) → motive (P.comp Q) a)
(h_6 : (P : RegularExpression α) → (a : α) → motive P.star a) (P Q : RegularExpression α) (a : α),
x = P.comp Q →
x_1 = a →
(match x, x_1 with
| RegularExpression.zero, x => h_1 x
| RegularExpression.epsilon, x => h_2 x
| RegularExpression.char a₁, a₂ => h_3 a₁ a₂
| P.plus Q, a => h_4 P Q a
| P.comp Q, a => h_5 P Q a
| P.star, a => h_6 P a) ≍
h_5 P Q a
abbrev RegularExpression.brecOn.go E1 #
{α : Type u} →
{motive : RegularExpression α → Sort u_1} →
(t : RegularExpression α) →
((t : RegularExpression α) → RegularExpression.below t → motive t) → motive t ×' RegularExpression.below t
theorem RegularExpression.matches'.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → Sort u_2) (x : RegularExpression α)
(h_1 : Unit → motive RegularExpression.zero) (h_2 : Unit → motive RegularExpression.epsilon)
(h_3 : (a : α) → motive (RegularExpression.char a)) (h_4 : (P Q : RegularExpression α) → motive (P.plus Q))
(h_5 : (P Q : RegularExpression α) → motive (P.comp Q)) (h_6 : (P : RegularExpression α) → motive P.star),
x = RegularExpression.epsilon →
(match x with
| RegularExpression.zero => h_1 ()
| RegularExpression.epsilon => h_2 ()
| RegularExpression.char a => h_3 a
| P.plus Q => h_4 P Q
| P.comp Q => h_5 P Q
| P.star => h_6 P) ≍
h_2 ()
def RegularExpression.ctorIdx E1 #
{α : Type u} → RegularExpression α → ℕ
theorem RegularExpression.deriv.match_1.congr_eq_4 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → α → Sort u_2) (x : RegularExpression α) (x_1 : α)
(h_1 : (x : α) → motive RegularExpression.zero x) (h_2 : (x : α) → motive RegularExpression.epsilon x)
(h_3 : (a₁ a₂ : α) → motive (RegularExpression.char a₁) a₂)
(h_4 : (P Q : RegularExpression α) → (a : α) → motive (P.plus Q) a)
(h_5 : (P Q : RegularExpression α) → (a : α) → motive (P.comp Q) a)
(h_6 : (P : RegularExpression α) → (a : α) → motive P.star a) (P Q : RegularExpression α) (a : α),
x = P.plus Q →
x_1 = a →
(match x, x_1 with
| RegularExpression.zero, x => h_1 x
| RegularExpression.epsilon, x => h_2 x
| RegularExpression.char a₁, a₂ => h_3 a₁ a₂
| P.plus Q, a => h_4 P Q a
| P.comp Q, a => h_5 P Q a
| P.star, a => h_6 P a) ≍
h_4 P Q a
theorem RegularExpression.deriv.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → α → Sort u_2) (x : RegularExpression α) (x_1 : α)
(h_1 : (x : α) → motive RegularExpression.zero x) (h_2 : (x : α) → motive RegularExpression.epsilon x)
(h_3 : (a₁ a₂ : α) → motive (RegularExpression.char a₁) a₂)
(h_4 : (P Q : RegularExpression α) → (a : α) → motive (P.plus Q) a)
(h_5 : (P Q : RegularExpression α) → (a : α) → motive (P.comp Q) a)
(h_6 : (P : RegularExpression α) → (a : α) → motive P.star a) (x_2 : α),
x = RegularExpression.epsilon →
x_1 = x_2 →
(match x, x_1 with
| RegularExpression.zero, x => h_1 x
| RegularExpression.epsilon, x => h_2 x
| RegularExpression.char a₁, a₂ => h_3 a₁ a₂
| P.plus Q, a => h_4 P Q a
| P.comp Q, a => h_5 P Q a
| P.star, a => h_6 P a) ≍
h_2 x_2
theorem RegularExpression.rmatch.match_1.congr_eq_2 E1 #
∀ {α : Type u_1} (motive : RegularExpression α → List α → Sort u_2) (x : RegularExpression α) (x_1 : List α)
(h_1 : (P : RegularExpression α) → motive P [])
(h_2 : (P : RegularExpression α) → (a : α) → (as : List α) → motive P (a :: as)) (P : RegularExpression α) (a : α)
(as : List α),
x = P →
x_1 = a :: as →
(match x, x_1 with
| P, [] => h_1 P
| P, a :: as => h_2 P a as) ≍
h_2 P a as
inductive RegularExpression E3 #
Type u → Type u
This is the definition of regular expressions. The names used here are meant to mirror the definition of a Kleene algebra.
0(zero) matches nothing1(epsilon) matches only the empty stringchar amatches only the string 'a'star Pmatches any finite concatenation of strings that matchPP + Q(plus P Q) matches anything that matchesPorQP * Q(comp P Q) matchesx ++ yifxmatchesPandymatchesQ
ctor RegularExpression.zero E0 #
{α : Type u} → RegularExpression α
ctor RegularExpression.epsilon E0 #
{α : Type u} → RegularExpression α
ctor RegularExpression.char E0 #
{α : Type u} → α → RegularExpression α
ctor RegularExpression.plus E0 #
{α : Type u} → RegularExpression α → RegularExpression α → RegularExpression α
ctor RegularExpression.comp E0 #
{α : Type u} → RegularExpression α → RegularExpression α → RegularExpression α
ctor RegularExpression.star E0 #
{α : Type u} → RegularExpression α → RegularExpression α
instance RegularExpression.instInhabited E0 #
{α : Type u_1} → Inhabited (RegularExpression α)
instance RegularExpression.instAdd E0 #
{α : Type u_1} → Add (RegularExpression α)
instance RegularExpression.instMul E0 #
{α : Type u_1} → Mul (RegularExpression α)
instance RegularExpression.instOne E0 #
{α : Type u_1} → One (RegularExpression α)
instance RegularExpression.instZero E0 #
{α : Type u_1} → Zero (RegularExpression α)
instance RegularExpression.instPowNat E0 #
{α : Type u_1} → Pow (RegularExpression α) ℕ
theorem RegularExpression.zero_def E0 #
∀ {α : Type u_1}, RegularExpression.zero = 0
theorem RegularExpression.one_def E0 #
∀ {α : Type u_1}, RegularExpression.epsilon = 1
theorem RegularExpression.plus_def E0 #
∀ {α : Type u_1} (P Q : RegularExpression α), P.plus Q = P + Q
theorem RegularExpression.comp_def E0 #
∀ {α : Type u_1} (P Q : RegularExpression α), P.comp Q = P * Q
def RegularExpression.matches' E1 #
{α : Type u_1} → RegularExpression α → Language α
matches' P provides a language which contains all strings that P matches.
Not named matches since that is a reserved word.
theorem RegularExpression.matches'_zero E1 #
∀ {α : Type u_1}, RegularExpression.matches' 0 = 0
theorem RegularExpression.matches'_epsilon E1 #
∀ {α : Type u_1}, RegularExpression.matches' 1 = 1
theorem RegularExpression.matches'_char E1 #
∀ {α : Type u_1} (a : α), (RegularExpression.char a).matches' = {[a]}
theorem RegularExpression.matches'_add E1 #
∀ {α : Type u_1} (P Q : RegularExpression α), (P + Q).matches' = P.matches' + Q.matches'
theorem RegularExpression.matches'_mul E1 #
∀ {α : Type u_1} (P Q : RegularExpression α), (P * Q).matches' = P.matches' * Q.matches'
theorem RegularExpression.matches'_pow E0 #
∀ {α : Type u_1} (P : RegularExpression α) (n : ℕ), (P ^ n).matches' = P.matches' ^ n
theorem RegularExpression.matches'_star E1 #
∀ {α : Type u_1} (P : RegularExpression α), P.star.matches' = KStar.kstar P.matches'
def RegularExpression.matchEpsilon E1 #
{α : Type u_1} → RegularExpression α → Bool
matchEpsilon P is true if and only if P matches the empty string
def RegularExpression.deriv E1 #
{α : 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
theorem RegularExpression.deriv_zero E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (a : α), RegularExpression.deriv 0 a = 0
theorem RegularExpression.deriv_one E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (a : α), RegularExpression.deriv 1 a = 0
theorem RegularExpression.deriv_char_self E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (a : α), (RegularExpression.char a).deriv a = 1
theorem RegularExpression.deriv_char_of_ne E0 #
∀ {α : Type u_1} {a b : α} [inst : DecidableEq α], a ≠ b → (RegularExpression.char a).deriv b = 0
theorem RegularExpression.deriv_add E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (P Q : RegularExpression α) (a : α), (P + Q).deriv a = P.deriv a + Q.deriv a
theorem RegularExpression.deriv_star E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (P : RegularExpression α) (a : α), P.star.deriv a = P.deriv a * P.star
def RegularExpression.rmatch E1 #
{α : Type u_1} → [DecidableEq α] → RegularExpression α → List α → Bool
P.rmatch x is true if and only if P matches x. This is a computable definition equivalent
to matches'.
theorem RegularExpression.zero_rmatch E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (x : List α), RegularExpression.rmatch 0 x = false
theorem RegularExpression.one_rmatch_iff E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (x : List α), RegularExpression.rmatch 1 x = true ↔ x = []
theorem RegularExpression.char_rmatch_iff E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (a : α) (x : List α), (RegularExpression.char a).rmatch x = true ↔ x = [a]
theorem RegularExpression.add_rmatch_iff E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (P Q : RegularExpression α) (x : List α),
(P + Q).rmatch x = true ↔ P.rmatch x = true ∨ Q.rmatch x = true
theorem RegularExpression.mul_rmatch_iff E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (P Q : RegularExpression α) (x : List α),
(P * Q).rmatch x = true ↔ ∃ t u, x = t ++ u ∧ P.rmatch t = true ∧ Q.rmatch u = true
theorem RegularExpression.star_rmatch_iff E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (P : RegularExpression α) (x : List α),
P.star.rmatch x = true ↔ ∃ S, x = S.flatten ∧ ∀ t ∈ S, t ≠ [] ∧ P.rmatch t = true
theorem RegularExpression.rmatch_iff_matches' E0 #
∀ {α : Type u_1} [inst : DecidableEq α] (P : RegularExpression α) (x : List α), P.rmatch x = true ↔ x ∈ P.matches'
instance RegularExpression.instDecidablePredListMemLanguageMatches' E0 #
{α : Type u_1} → [DecidableEq α] → (P : RegularExpression α) → DecidablePred fun x ↦ x ∈ P.matches'
def RegularExpression.map E1 #
{α : Type u_1} → {β : Type u_2} → (α → β) → RegularExpression α → RegularExpression β
Map the alphabet of a regular expression.
theorem RegularExpression.map_pow E0 #
∀ {α : Type u_1} {β : Type u_2} (f : α → β) (P : RegularExpression α) (n : ℕ),
RegularExpression.map f (P ^ n) = RegularExpression.map f P ^ n
theorem RegularExpression.map_id E0 #
∀ {α : Type u_1} (P : RegularExpression α), RegularExpression.map id P = P
theorem RegularExpression.map_map E0 #
∀ {α : 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
theorem RegularExpression.matches'_map E0 #
∀ {α : 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.