However, I don't think this solution will help too much with regexes. To me it's overly complex and less readable than regex strings.
However, I don't think this solution will help too much with regexes. To me it's overly complex and less readable than regex strings.
I didn't invented the combinators for regex, that was already in ocaml-re (which is used as backend for tyre). The combinator approach has many advantages:
- You don't need to remember which regex syntax the library is using. Is it using the emacs one ? The perl one ? The javascript one ? The bash one ?! (shudder) ...
- It's "self documenting". Your combinators are just functions, so you just expose them and give them type signatures, and the usual documentation/autocompletion/whatevertooling works.
- It composes better. You don't have to mash string together to compose your regex, you can name intermediary regexs with normal variables, etc.
- Related to the point above: No string quoting hell.
Now, tyre doesn't really improve any of that. If anything, combinators are simpler in ocaml-re[1]. What tyre gives you is the automatic extraction and casting of matching groups into the desired datatype (and the reverse direction, but that's almost free bonus). No need to select groups manually and transform into an integer, no need to reconstruct tuples/lists/records, tyre will do that for you (and handle conversion failures cleanly).
[1]: https://github.com/ocaml/ocaml-re/blob/master/lib/re.mli#L20...
Also, how flexible is the unparsing? Does it have to be the same regular expression? [Sorry, I haven't read in detail and I'm only slightly familiar with ocaml - I'm interested from an academic perspective.]
If you are interested by unparsing, I would advise you to read the (famous) paper functional unparsing by Danvy[1]. The module Printf and Format of the OCaml standard library are basically this paper on steroid. I recently wrote a blog on part of the implementation technique[2].
Hope that helps. :)
[1]: http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf
[2]: https://drup.github.io/2016/08/02/difflists/
[Printf]: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.htm...
[Format]: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Format.htm...
Uh... did you see my bug report on your webpage (first sentence above)? That link still doesn't work properly.