generating parens for pretty-printing code in haskell-src-exts

I'm using haskell-src-exts together with SYB for a code-rewriting project, and I'm having difficulty with parenthesization. I naïvely expected that parentheses would be absent from the abstract syntax, being removed during parsing and inserted during pretty-printing. It's easy for me to remove them myself, but harder to add them (minimally) after transformation. Rather than re-inventing the wheel, I thought I'd ask for help. Has anyone written automatic minimal parens insertion for haskell-src-exts? -- Conal

Hi Conal I don't know if any Haskell src-exts code exists. Norman Ramsey has published an algorithm for it, plus ML code: http://www.cs.tufts.edu/~nr/pubs/unparse-abstract.html I've transcribed the code to Haskell a couple of times for small expression languages. As far as I remember you need to know what constructors you are working with so it can't be put in a "generic" pretty print library, but the constructor specific code is "stereotypical" so it should be easy (if boring) to write for Haskell src-exts. Best wishes Stephen

Hi Conal,
On Sun, Jan 15, 2012 at 6:45 AM, Conal Elliott
I'm using haskell-src-exts together with SYB for a code-rewriting project, and I'm having difficulty with parenthesization. I naïvely expected that parentheses would be absent from the abstract syntax, being removed during parsing and inserted during pretty-printing. It's easy for me to remove them myself, but harder to add them (minimally) after transformation. Rather than re-inventing the wheel, I thought I'd ask for help.
It's not at all straight-forward to properly insert minimal parentheses during pretty-printing, since "minimal" is a semantic notion. For that to work, we would need the pretty-printer to take a set of known fixities as an argument, just like the parser currently does. I'm not opposed to the idea in principle, but it's not how it currently works.
Has anyone written automatic minimal parens insertion for haskell-src-exts?
... or we could do it this way, in a two-stage modular process which gives even more flexibility. I'm not aware of any such functionality, currently, but if it exists (or happens to come into existence) then I would be interested in including it in haskell-src-exts. Best regards, /Niklas

Neil Mitchell's HLint (http://community.haskell.org/~ndm/hlint/) warns
about "extra parens" in Haskell code. So, it must have code for
detecting those. I wonder if you can just insert parens liberally in a
first run, and then use his algorithm to remove those that are
unnecessary. The two passes can probably be fused, if you're willing
to use haskell-stc-exts and do a bit of bookkeeping..
-Levent.
On Sat, Jan 21, 2012 at 10:39 AM, Niklas Broberg
Hi Conal,
On Sun, Jan 15, 2012 at 6:45 AM, Conal Elliott
wrote: I'm using haskell-src-exts together with SYB for a code-rewriting project, and I'm having difficulty with parenthesization. I naïvely expected that parentheses would be absent from the abstract syntax, being removed during parsing and inserted during pretty-printing. It's easy for me to remove them myself, but harder to add them (minimally) after transformation. Rather than re-inventing the wheel, I thought I'd ask for help.
It's not at all straight-forward to properly insert minimal parentheses during pretty-printing, since "minimal" is a semantic notion. For that to work, we would need the pretty-printer to take a set of known fixities as an argument, just like the parser currently does. I'm not opposed to the idea in principle, but it's not how it currently works.
Has anyone written automatic minimal parens insertion for haskell-src-exts?
... or we could do it this way, in a two-stage modular process which gives even more flexibility. I'm not aware of any such functionality, currently, but if it exists (or happens to come into existence) then I would be interested in including it in haskell-src-exts.
Best regards,
/Niklas
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Conal Elliott
-
Levent Erkok
-
Niklas Broberg
-
Stephen Tetley