Along the same line, there's also invertible syntax descriptions https://gist.github.com/tfausak/a8d7f135bf76e64ea6f35d3be692... , which allow one definition to create both a parser and pretty-printer
I like using Elm's JSON library as an example because it's very small. Since there are only a handful of functions you can understand the entire thing pretty easily. Plus, encoding JSON is a great jump-off into the decoding half of the library which is basically a simple parser combinator system.
Re:Parser Combinators in Haskell
by marginalia_nu on Wednesday December 22, 2021 @11:07AM
(#29651149)
I'm normally a recursive descent-man, but I implemented parsec-style parsing in Java once, just because I was curious to see what the world looked like when it was on fire. I don't have the code as this was just me wasting company time during a hackathon they insisted we all attended. I did get it working, despite the type system I was working in.
But yeah. Mistakes were made.
What are the benefits of these monadic builder interfaces instead of simply pure functions? One can imagine a simple Html type with html tag functions e.g.
`div :: [Attributes] -> [Html] -> Html`
Instead of using do notation to build up multiple children one would simply supply a list of children. Added benefit of using simpler map/filter/filterMap/mapMaybe vs their monadic counterparts
The builder interface has important performance benefits over working with lists: similar to e.g. concatenating many small Strings vs using StringBuilder in Java.
The builder interface indeed does not need to be monadic, you can simply append things using <> and get the same benefits.
The monadic interface is purely a syntactic enhancement on top of that, to reduce the number of operators you need.
One aspect that I see discussed less often is the idea of 'builder combinators', which are kind of the inverse of parser combinators. There are multiple libraries in Haskell which make it very easy to write HTML/SVG/... this way:
https://hackage.haskell.org/package/blaze-html
https://hackage.haskell.org/package/blaze-svg
I haven't needed to do the same with SQL but I imagine that this already exists as well