How does GHC implement layout?

Alexis King lexi.lambda at gmail.com
Sun Apr 4 00:04:45 UTC 2021


Hi all,

I’m wondering if there are any resources that discuss the design of 
GHC’s implementation of layout. (I haven’t been able to find any.) From 
looking at the code, here’s what I’ve gathered so far:

  * Layout is implemented in the lexer (compiler/GHC/Parser/Lexer.x).

  * The implementation is similar in some respects to the approach
    described in the Haskell Report, but still fairly different. Virtual
    braces and semicolons are inserted during the lexing process itself
    with the assistance of Alex lexer states (aka “start codes”).

  * In order to handle particularly tricky cases like

         if e then do x; y else z


    where the virtual close brace must be inserted in the middle of a
    line, tokens such as in and else are given special context-sensitive
    treatment. This appears to be quite subtle.

Overall, I can mostly follow the code, but I still have a few unanswered 
questions:

  * The layout-related code consistently uses the phrase “alternative
    layout rule”—what does “alternative” mean here? Does it refer to
    GHC’s implementation of layout? Or maybe it refers to
    NondecreasingIndentation? It isn’t clear.

  * The implementation of layout seems quite complex, in large part
    because it has to worry about parsing concerns in the lexer in order
    to handle tricky cases like the one I provided above. Is there are
    reason all this is done in the lexer, rather than deferring some
    more of the work to the parser?

I’ve found remarkably little information about implementing layout in 
general, so perhaps I’m missing some resources or keywords to search 
for, but any information or perspectives would be appreciated!

Thanks,
Alexis

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20210403/2204a436/attachment.html>


More information about the ghc-devs mailing list