[Haskell-cafe] Let's teach GHC idiom brackets!

Christopher Done chrisdone at gmail.com
Thu Feb 19 00:24:26 UTC 2015


Adding context from what brought me to start thinking about using idioms:

* I'm tired of writing (and reading) the awkward f <$> z <*> y <*> z pattern.
* It's a pain with editor support and pretty printing (which is a
solvable problem, but not the kind of problem I'd like to solve).
* It feels like using >> and >>= for monads instead of do-notation.
* The [i|…|] idiom quasi-quoter seems like it can solve these issues for me.

So I've started trying it out on future codebases. Naturally I added
support in SHM and hindent which reduces this pattern down to basic
function application, which is great for editing and reading. The more
function arguments, the more of a saving this is.

Here's a fair comparison of before/after on my ace package's parser:
https://github.com/chrisdone/ace/commit/36767780ca356db75468f803b0c91388186c0441

I say it's fair because this example involves mostly a low number of
arguments, but a couple larger ones with obvious savings. (But I'm the
kinda guy who writes do x;  y instead of x >> y.) The more
pathological examples come from e.g. writing JSON/Binary instances or
formlets or optparse-applicative things, which regularly have >5
arguments.

Pros for the template-haskell-based quasi-quoter:

* It doesn't seem to add any noticeable compiling overhead to this
large module (see commit message for timings).

Cons:

* Nesting is not supported by quasi-quoters.
* Currently it lacks the Alternative support as proposed by Conor.
That would look like this:

[i|TopicalizedSentenceExistential existentialTopic (optional (try
sentenceCoord))
  |TopicalizedSentenceUniversal universalTopic sentenceCoord
  |TopicalizedSentenceComposite compositeSentence|]

Given that nobody is maintaining the applicative-quoter, I might go
and add support for this extension too. That would also substantially
improve the Parser.hs example above. It's quite similar to the
parallel list comprehension notation.

One nice thing is how trivial it is to move to and from pure to
monadic/applicative code with this.

Comparatively, I've never used arrows ever and yet GHC has built-in
support for it. Meanwhile Applicatives have wide application (hehe)
and we're stuck writing this clunky stuff. But I figured I'd play with
the quasi-quoter and see how it feels, and see if other people get
onboard with it.

Ciao!


More information about the Haskell-Cafe mailing list