Documentation

Mathlib.Computability.RegularExpressions

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

inductive RegularExpression (α : Type u) :

This is the definition of regular expressions. The names used here are meant 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 that match P
  • P + Q (plus P Q) matches anything that matches P or Q
  • P * Q (comp P Q) matches x ++ y if x matches P and y matches Q
Instances For
    @[implicit_reducible]
    instance RegularExpression.instInhabited {α : Type u_1} :
    Inhabited (RegularExpression α)
    @[implicit_reducible]
    instance RegularExpression.instAdd {α : Type u_1} :
    @[implicit_reducible]
    instance RegularExpression.instMul {α : Type u_1} :
    @[implicit_reducible]
    instance RegularExpression.instOne {α : Type u_1} :
    @[implicit_reducible]
    instance RegularExpression.instZero {α : Type u_1} :
    @[implicit_reducible]
    instance RegularExpression.instPowNat {α : Type u_1} :
    Pow (RegularExpression α)
    @[simp]
    theorem RegularExpression.zero_def {α : Type u_1} :
    zero = 0
    @[simp]
    theorem RegularExpression.plus_def {α : Type u_1} (P Q : RegularExpression α) :
    P.plus Q = P + Q
    @[simp]
    theorem RegularExpression.comp_def {α : Type u_1} (P Q : RegularExpression α) :
    P.comp Q = P * Q

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

    Not named matches since that is a reserved word.

    Instances For
      theorem RegularExpression.matches'_char {α : Type u_1} (a : α) :
      (char a).matches' = {[a]}
      @[simp]
      theorem RegularExpression.matches'_pow {α : Type u_1} (P : RegularExpression α) (n : ) :
      (P ^ n).matches' = P.matches' ^ n

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

      Instances For
        def RegularExpression.deriv {α : Type u_1} [DecidableEq α] :

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

        Instances For
          @[simp]
          theorem RegularExpression.deriv_zero {α : Type u_1} [DecidableEq α] (a : α) :
          deriv 0 a = 0
          @[simp]
          theorem RegularExpression.deriv_one {α : Type u_1} [DecidableEq α] (a : α) :
          deriv 1 a = 0
          @[simp]
          theorem RegularExpression.deriv_char_self {α : Type u_1} [DecidableEq α] (a : α) :
          (char a).deriv a = 1
          @[simp]
          theorem RegularExpression.deriv_char_of_ne {α : Type u_1} {a b : α} [DecidableEq α] (h : a b) :
          (char a).deriv b = 0
          @[simp]
          theorem RegularExpression.deriv_add {α : Type u_1} [DecidableEq α] (P Q : RegularExpression α) (a : α) :
          (P + Q).deriv a = P.deriv a + Q.deriv a
          @[simp]
          theorem RegularExpression.deriv_star {α : Type u_1} [DecidableEq α] (P : RegularExpression α) (a : α) :
          P.star.deriv a = P.deriv a * P.star
          def RegularExpression.rmatch {α : 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'.

          Instances For
            @[simp]
            theorem RegularExpression.zero_rmatch {α : Type u_1} [DecidableEq α] (x : List α) :
            rmatch 0 x = false
            theorem RegularExpression.one_rmatch_iff {α : Type u_1} [DecidableEq α] (x : List α) :
            rmatch 1 x = true x = []
            theorem RegularExpression.char_rmatch_iff {α : Type u_1} [DecidableEq α] (a : α) (x : List α) :
            (char a).rmatch x = true x = [a]
            theorem RegularExpression.add_rmatch_iff {α : Type u_1} [DecidableEq α] (P Q : RegularExpression α) (x : List α) :
            (P + Q).rmatch x = true P.rmatch x = true Q.rmatch x = true
            theorem RegularExpression.mul_rmatch_iff {α : Type u_1} [DecidableEq α] (P Q : RegularExpression α) (x : List α) :
            (P * Q).rmatch x = true ∃ (t : List α) (u : List α), x = t ++ u P.rmatch t = true Q.rmatch u = true
            @[irreducible]
            theorem RegularExpression.star_rmatch_iff {α : Type u_1} [DecidableEq α] (P : RegularExpression α) (x : List α) :
            P.star.rmatch x = true ∃ (S : List (List α)), x = S.flatten tS, t [] P.rmatch t = true
            @[simp]
            theorem RegularExpression.rmatch_iff_matches' {α : Type u_1} [DecidableEq α] (P : RegularExpression α) (x : List α) :
            P.rmatch x = true x P.matches'
            @[implicit_reducible]
            instance RegularExpression.instDecidablePredListMemLanguageMatches' {α : Type u_1} [DecidableEq α] (P : RegularExpression α) :
            DecidablePred fun (x : List α) => x P.matches'
            def RegularExpression.map {α : Type u_1} {β : Type u_2} (f : αβ) :

            Map the alphabet of a regular expression.

            Instances For
              @[simp]
              theorem RegularExpression.map_pow {α : Type u_1} {β : Type u_2} (f : αβ) (P : RegularExpression α) (n : ) :
              map f (P ^ n) = map f P ^ n
              @[simp]
              theorem RegularExpression.map_id {α : Type u_1} (P : RegularExpression α) :
              map id P = P
              @[simp]
              theorem RegularExpression.map_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} (g : βγ) (f : αβ) (P : RegularExpression α) :
              map g (map f P) = map (g f) P
              @[simp]
              theorem RegularExpression.matches'_map {α : Type u_1} {β : Type u_2} (f : αβ) (P : RegularExpression α) :

              The language of the map is the map of the language.