How does GHC implement layout?

Simon Marlow marlowsd at
Tue Apr 6 11:10:39 UTC 2021

The history here: several people independently noticed that it might be
better to implement the "parse error" part of layout by doing something
simpler than full parsing. For example, if we just count brackets in the
lexer, then we can handle things like

   f (case x of y -> z)

and if we treat let/in as a pair of brackets too, then the common case of

  let x = y in z

also works. The AlternativeLayoutRule was Ian's implementation of this
idea, and (if I remember correctly) contains a number of fixes that arose
from discovering interesting cases of code in the wild that weren't handled
by the obvious bracket-matching techniques.

My conclusion from this experiment - which is a bit subjective and might
differ from others - is that
* you need a lot of special cases to handle code that Haskell users expect
to parse
* and then the specification becomes pretty complex, and hard to explain to

So ultimately it didn't solve the problem in a nice way, unfortunately. The
"parse error" rule is hard for implementers, but it's not actually that
hard for users.

If other people agree with this conclusion, we should kill off the ALR code

Someone is going to ask me for examples of those "special cases", I'm
afraid I don't remember - I only cached the answer to the question, not the
working :) You'd probably have to go digging through the ALR code.


On Tue, 6 Apr 2021 at 01:36, Alexis King <lexi.lambda at> wrote:

> On 4/5/21 2:36 PM, Ian Lynagh wrote:
> It was originally designed by John Meacham:
> Thanks, Ian—I had stumbled across a link to the old Haskell Prime trac
> wiki while I was searching for information, but I didn’t realize where it
> had been migrated to.
> It isn't exactly equivalent to the Haskell layout rule, but it's fairly
> close and much simpler (due to not having the "on a parse error" case).
> Yes, I gathered as much from the implementation. The idea makes sense, but
> of course it doesn’t provide much benefit to have a simpler implementation
> unless it actually *replaces* the “on parse error” approach.
> Given this appears to be a long-defunct proposal, a natural followup
> question is to ask whether there’s any reason this code is still in GHC. Is
> it used for any purpose, or could it be removed?
> Alexis
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list