[Haskell-cafe] haskell-src-exts Question

Neil Mitchell ndmitchell at gmail.com
Fri Nov 13 15:08:42 EST 2009


Hi Niklas,

>> Do I have to write my own prettyprinter? Do I have to put in explicit
>> parentheses? The latter seems unsatisfactory as my generated AST is unambiguous
>> and bracketing ought to be part of the prettyprinter. The former would be quite
>> a lot of code as there are many cases to consider.
>
> Looking at your example, what you want is brackets to be inserted
> whenever the right subexpression in an application is non-atomic. That
> certainly seems reasonable to me. Would you file a ticket for it
> please? http://trac.haskell.org/haskell-src-exts :-)

I wanted that once, then I realised I was wrong :-)

Should you insert brackets everywhere it's ambiguous? What about
operators - you don't know the fixities, so can't tell if you should
insert a bracket or not. I can't see any way for HSE to implement this
feature correctly, and as such, it's really not a good feature to push
down in to the pretty printer.

In HLint I have a bracketing module, which has served me well. Please
take any ideas you need from it -
http://community.haskell.org/~ndm/darcs/hlint/src/HSE/Bracket.hs . In
particular, given a fully bracketed expression, I can call
transformBracket to transform the expression, not caring about
brackets, in a way that guarantees the right brackets are put back.
There is also needBracket and isAtom which are very useful. If you
call descendBi (transformBracket Just) it will automatically bracket
your term as much as is necessary.

If you don't understand any of the ideas in Bracket then look at
Uniplate (http://community.haskell.org/~ndm/uniplate) - it's where a
lot of the ideas came from. If you're working with a HSE source tree
without using Uniplate (or a competitor) then you're doing it wrong.

Thanks, Neil


More information about the Haskell-Cafe mailing list