Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chessai
Created November 8, 2019 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chessai/8a6b17f05d1d970925fa40988e220e0e to your computer and use it in GitHub Desktop.
Save chessai/8a6b17f05d1d970925fa40988e220e0e to your computer and use it in GitHub Desktop.
regex
data Regex t
= RegexLiteral t t
--^ low and high, inclusive
| RegexEmpty
| RegexRejection
| RegexUnion (Regex t) (Regex t)
| RegexAppend (Regex t) (Regex t)
| RegexStar (Regex t)
instance Semiring (Regex t) where
zero = RegexRejection
RegexRejection `plus` y = y
x `plus` RegexRejection = x
RegexEmpty `plus` RegexEmpty = RegexEmpty
RegexEmpty `plus` RegexStar y = RegexStar y
RegexStar x `plus` RegexEmpty = RegexStar x
x `plus` y = RegexUnion x y
one = RegexEmpty
RegexEmpty `times` y = y
x `times` RegexEmpty = x
RegexRejection `times` _ = RegexRejection
_ `times` RegexRejection = RegexRejection
x `times` y = RegexAppend x y
instance Star (Regex t) where
star RegexRejection = RegexEmpty
star RegexEmpty = RegexEmpty
star (RegexStar x) = star x
star x = star (RegexStar x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment