I’m happy to announce the release of Tyre, a library for Typed Regular Expressions.

Tyre is a set of combinators to build type-safe regular expressions, allowing automatic extraction and modification of matched groups.

Tyre is bi-directional: a typed regular expressions can be used for parsing and unparsing. It also allows routing, by providing a list of regexs/routes and their handlers.

Here is a small bit of code to give you a taste. Documentation is available here. See also the examples/ directory.

# let dim = Tyre.( "dim:" *> int <*> "x" *> int ) ;; (* A new tyregex *)
val dim : (int * int) Tyre.t
# let dim_re = Tyre.compile dim ;; (* We compile it *)
val dim_re : (int * int) Tyre.re
# Tyre.exec dim_re "dim:3x4" ;; (* Using it for matching *)
- : (int * int, (int * int) Tyre.error) result = Result.Ok (3, 4)
# Tyre.eval dim (2, 5) ;; (* We can also use it for unparsing *)
- : string = "dim:2x5"

Tyre can be seen as a modern replacement of mikmatch. Contrary to mikmatch, tyre doesn’t use any syntax extension and instead rely on combinators. Tyre also uses re, which is an efficient pure OCaml regex library (which makes it also usable in JavaScript, Mirage, and other platforms).

For the most curious among you, the implementation of Tyre is a good example of “real world” GADT usage, while still being short (500 lines) and understandable (I hope). The details might be the subject of another blog post! :)

Good OCaml hacking!


Please discuss this on Reddit or Hacker News.