question about indentation conventions

I hope I'm not starting a holy war with this, but I'm curious about an aspect of coding style that's been bugging me for a while, and I'm not finding much discussion of this question on the web or in the mailing list archives. Two questions: 1) Are there wide-spread conventions in the Haskell community for how to indent an application expression that's split across multiple lines? For how to indent an expression that uses infix operators? Or does everyone pretty much do their own thing? 2) If there is such a convention, how do I make Emacs's haskell-mode do it? By default, in most cases Emacs's haskell-mode with turn-on-haskell-indentation does function firstArgument (second argument) thirdArgument Personally, I'd prefer some indentation on the 2nd and 3rd lines to indicate that they're continuing an expression begun on a previous line. I can use parens around the entire application to force haskell-mode to indent subsequent lines (and of course this is necessary in some contexts, like a 'do' expression), but haskell-mode only indents that by a single space: do (function firstArgument (second argument) thirdArgument) nextAction I'd find a larger indent---even just 2 spaces---to be more readable. My inclination to indent the second and following lines of a multi-line application expression is informed by my long experience in Scheme, Racket, and Lisp, whose S-expressions lend themselves to fairly straightforward (and automatable!) indentation conventions. If the Haskell community does things differently, though, I'm happy to adapt. This is the sort of thing that one picks up from the community, as in other languages. I don't, however, have a whole lot of contact with that community outside this list -- thus the post, despite the dangers inherent in discussing subjective stuff like this that people often feel strongly about. Thanks, Richard

Hi Richard,
This page helped me when starting out:
http://en.wikibooks.org/wiki/Haskell/Indentation
On 2013-06-30 4:55 PM, "Richard Cobbe"
I hope I'm not starting a holy war with this, but I'm curious about an aspect of coding style that's been bugging me for a while, and I'm not finding much discussion of this question on the web or in the mailing list archives.
Two questions:
1) Are there wide-spread conventions in the Haskell community for how to indent an application expression that's split across multiple lines? For how to indent an expression that uses infix operators? Or does everyone pretty much do their own thing?
2) If there is such a convention, how do I make Emacs's haskell-mode do it?
By default, in most cases Emacs's haskell-mode with turn-on-haskell-indentation does
function firstArgument (second argument) thirdArgument
Personally, I'd prefer some indentation on the 2nd and 3rd lines to indicate that they're continuing an expression begun on a previous line.
I can use parens around the entire application to force haskell-mode to indent subsequent lines (and of course this is necessary in some contexts, like a 'do' expression), but haskell-mode only indents that by a single space:
do (function firstArgument (second argument) thirdArgument) nextAction
I'd find a larger indent---even just 2 spaces---to be more readable.
My inclination to indent the second and following lines of a multi-line application expression is informed by my long experience in Scheme, Racket, and Lisp, whose S-expressions lend themselves to fairly straightforward (and automatable!) indentation conventions. If the Haskell community does things differently, though, I'm happy to adapt.
This is the sort of thing that one picks up from the community, as in other languages. I don't, however, have a whole lot of contact with that community outside this list -- thus the post, despite the dangers inherent in discussing subjective stuff like this that people often feel strongly about.
Thanks,
Richard
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sun, Jun 30, 2013 at 05:41:46PM -0700, Darren Grant wrote:
Hi Richard,
This page helped me when starting out: http://en.wikibooks.org/wiki/Haskell/Indentation On 2013-06-30 4:55 PM, "Richard Cobbe"
wrote:
<snip>
1) Are there wide-spread conventions in the Haskell community for how to indent an application expression that's split across multiple lines? For how to indent an expression that uses infix operators? Or does everyone pretty much do their own thing?
<snip> Thanks for the pointer, Darren, and I did come across that page earlier. I should have been clearer in my original question: I'm curious about what to do when a multi-argument function application gets split across lines. That wiki page dicsusses how the layout rule interacts with various special forms (let, where, if, do, case), but it doesn't seem to address function applications, beyond implying that it's ok to indent the continuing lines of a function application. Richard

The Haskell Style Guide is quite popular:
https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.md
(accompying
elisp module:
https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.el)
I am not sure what the verdict is on functions spanning multiple lines,
other than 80 columns being the max line length. Personally, I do what
haskell-mode + style-guide.el default to, which is to indent to the same
level like you said, e.g.:
fun a b c
d e f
Looking at some of the style guide author's code seems to suggest this is
what he does too:
https://github.com/tibbe/ekg/blob/master/System/Remote/Snap.hs#L155
Of course, you're free to do it whichever way you prefer--it only matters
if you are collaborating with others who don't share your preferences
(always a fun activity.)
It would be cool if there was a The One Formatting Tool for Haskell, a la
"go fmt" for Go, or an 'indent' without so many options that the tool is
moot.
On Sun, Jun 30, 2013 at 9:04 PM, Richard Cobbe
On Sun, Jun 30, 2013 at 05:41:46PM -0700, Darren Grant wrote:
Hi Richard,
This page helped me when starting out: http://en.wikibooks.org/wiki/Haskell/Indentation On 2013-06-30 4:55 PM, "Richard Cobbe"
wrote: <snip>
1) Are there wide-spread conventions in the Haskell community for how to indent an application expression that's split across multiple lines? For how to indent an expression that uses infix operators? Or does everyone pretty much do their own thing?
<snip>
Thanks for the pointer, Darren, and I did come across that page earlier.
I should have been clearer in my original question: I'm curious about what to do when a multi-argument function application gets split across lines. That wiki page dicsusses how the layout rule interacts with various special forms (let, where, if, do, case), but it doesn't seem to address function applications, beyond implying that it's ok to indent the continuing lines of a function application.
Richard
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 1/07/2013, at 1:04 PM, Richard Cobbe wrote:
I should have been clearer in my original question: I'm curious about what to do when a multi-argument function application gets split across lines. That wiki page dicsusses how the layout rule interacts with various special forms (let, where, if, do, case), but it doesn't seem to address function applications, beyond implying that it's ok to indent the continuing lines of a function application.
It looked pretty explicit to me: The golden rule of indentation ... you will do fairly well if you just remember a single rule: Code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line). This means for example that f (g x y z) is OK but f (g x y z) is not.

On Mon, Jul 01, 2013 at 05:18:39PM +1200, Richard A. O'Keefe wrote:
On 1/07/2013, at 1:04 PM, Richard Cobbe wrote:
I should have been clearer in my original question: I'm curious about what to do when a multi-argument function application gets split across lines. That wiki page dicsusses how the layout rule interacts with various special forms (let, where, if, do, case), but it doesn't seem to address function applications, beyond implying that it's ok to indent the continuing lines of a function application.
It looked pretty explicit to me:
The golden rule of indentation ... you will do fairly well if you just remember a single rule: Code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line).
This means for example that f (g x y z) is OK but f (g x y z) is not.
It seems to me that this means f x1 x2 x3 x4 is not. The OP was initially asking about this situation. Tom

On Mon, Jul 1, 2013 at 3:43 AM, Tom Ellis < tom-lists-haskell-cafe-2013@jaguarpaw.co.uk> wrote:
is OK but f (g x y z) is not.
It seems to me that this means
f x1 x2 x3 x4
is not. The OP was initially asking about this situation.
If you wrote that in a do, the compiler would insert a (>>) between the two lines. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Code which is part of some expression should be indented further in than the beginning of that expression [...]
Yes. Then the next question is "how much further in". My answer is: it does not matter, but make it consistent (like 4 spaces), with the implication that indentation should *not* depend on length of identifiers in the previous line(s) Example (bad): foo x = do bar baz Example (good): foo x = do bar baz Reason: when refactoring later changes the name "foo", or the number or names of its arguments, you'll have to re-indent code (in the "bad" version): (a) this needs tool support and (b) it creates noise in the diffs. - J.W.

On Mon, Jul 01, 2013 at 05:18:39PM +1200, Richard A. O'Keefe wrote:
It looked pretty explicit to me:
The golden rule of indentation ... you will do fairly well if you just remember a single rule: Code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line).
This means for example that f (g x y z) is OK but f (g x y z) is not.
Sure. So my first question boils down to which of the two alternatives below does the community prefer? (To be clear about the intended semantics: this is the application of the function f to the arguments x, y, and z.) f x y z or f x y z Both are correct, in most contexts. And then there's the second question: if the second alternative is preferable, is there a way to get haskell-mode to do it automatically? As it is, it refuses to indent y any farther to the right than in the first alternative. I can space it in by hand, and then haskell-mode puts z under y, but that's annoying, and it gets in the way of reindenting large regions of code automatically. Richard

I always preferred (I think going back to my lisp days) foo x y z indenting subsequent arguments to the same level as the first, but I have not convinced haskell-mode to do that for me. (The general rule here being that similar things should be at the same indent, which will always result in subexpressions being further indented, but is somewhat more specific.) The case I never quite know what to do with is a series of expressions connected with operators, e.g. foo <|> bar <|> baz Leaving operators at the beginning of lines (rather than trailing the previous line) seems preferable, but does one (where the layout rules apply) align the operator or the function? (the alternative being, if your mail client does not make a mess of it with a variable-width font) foo <|> bar <|> baz ~IRS ________________________________________ From: haskell-cafe-bounces@haskell.org [haskell-cafe-bounces@haskell.org] on behalf of Richard Cobbe [cobbe@ccs.neu.edu] Sent: Monday, July 01, 2013 8:00 AM To: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] question about indentation conventions On Mon, Jul 01, 2013 at 05:18:39PM +1200, Richard A. O'Keefe wrote:
It looked pretty explicit to me:
The golden rule of indentation ... you will do fairly well if you just remember a single rule: Code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line).
This means for example that f (g x y z) is OK but f (g x y z) is not.
Sure. So my first question boils down to which of the two alternatives below does the community prefer? (To be clear about the intended semantics: this is the application of the function f to the arguments x, y, and z.) f x y z or f x y z Both are correct, in most contexts. And then there's the second question: if the second alternative is preferable, is there a way to get haskell-mode to do it automatically? As it is, it refuses to indent y any farther to the right than in the first alternative. I can space it in by hand, and then haskell-mode puts z under y, but that's annoying, and it gets in the way of reindenting large regions of code automatically. Richard _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

My current approach is not to have one rule for every case but rather to
indent however seems best for the particular code. For example, for
Parsec's <|>, I try to make the code look like a BNF grammar rather than
adhering to normal indentation conventions. Perhaps as a carry-over from my
C-style programming days, I usually have the operators at the end of the
line, but I put <|> at the beginning, several steps *left* of where it
would normally be, to get everything aligned nicely.
Similarly, I try to align things to show the structure of the code. For
example, if I have two similar function calls on top of each other, I try
to highlight the parallelism:
foo (Just $ a + b) Nothing
foo Nothing (Just $ a + b)
The main idea is that I try to format my code not based on what the *code*
is but based on what it *means*. I don't know if this is entirely
reasonable, but I like it.
I've thought about writing an automatic indenting tool for Haskell (or,
more accurately, a pretty-printer) for another project I have, and this is
the main thing that threw me off. While automatic indentation might make
sense for less expressive languages (Google Go being an extreme example), I
think it would be too constraining for Haskell. After all, in reasonable
code, chances are that similar constructs end up meaning wildly different
things (especially with the advent of pervasive embedded DSLs), so the code
itself is a poor indicator of its own structure.
So this is really something of an aside, but I do not think that a
gofmt-style tool would be quite as useful for Haskell, and it certainly
does not replace good taste.
**
On Mon, Jul 1, 2013 at 9:34 AM, Sturdy, Ian
I always preferred (I think going back to my lisp days)
foo x y z
indenting subsequent arguments to the same level as the first, but I have not convinced haskell-mode to do that for me. (The general rule here being that similar things should be at the same indent, which will always result in subexpressions being further indented, but is somewhat more specific.)
The case I never quite know what to do with is a series of expressions connected with operators, e.g.
foo <|> bar <|> baz
Leaving operators at the beginning of lines (rather than trailing the previous line) seems preferable, but does one (where the layout rules apply) align the operator or the function? (the alternative being, if your mail client does not make a mess of it with a variable-width font)
foo <|> bar <|> baz
~IRS ________________________________________ From: haskell-cafe-bounces@haskell.org [haskell-cafe-bounces@haskell.org] on behalf of Richard Cobbe [cobbe@ccs.neu.edu] Sent: Monday, July 01, 2013 8:00 AM To: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] question about indentation conventions
On Mon, Jul 01, 2013 at 05:18:39PM +1200, Richard A. O'Keefe wrote:
It looked pretty explicit to me:
The golden rule of indentation ... you will do fairly well if you just remember a single rule: Code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line).
This means for example that f (g x y z) is OK but f (g x y z) is not.
Sure. So my first question boils down to which of the two alternatives below does the community prefer? (To be clear about the intended semantics: this is the application of the function f to the arguments x, y, and z.)
f x y z
or
f x y z
Both are correct, in most contexts.
And then there's the second question: if the second alternative is preferable, is there a way to get haskell-mode to do it automatically? As it is, it refuses to indent y any farther to the right than in the first alternative. I can space it in by hand, and then haskell-mode puts z under y, but that's annoying, and it gets in the way of reindenting large regions of code automatically.
Richard
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Jul 1, 2013 at 9:56 AM, Tikhon Jelvis
I've thought about writing an automatic indenting tool for Haskell (or, more accurately, a pretty-printer) for another project I have, and this is the main thing that threw me off. While automatic indentation might make sense for less expressive languages (Google Go being an extreme example), I think it would be too constraining for Haskell. After all, in reasonable code, chances are that similar constructs end up meaning wildly different things (especially with the advent of pervasive embedded DSLs), so the code itself is a poor indicator of its own structure.
One might look at the history of the indentation modules for Emacs haskell-mode. Short version: they gave up, tab iterates through the possibilities because it's quite impossible to know which one is correct without a *semantic*, not just syntactic, understanding of the code. (Which, when you think about it, is pretty much par for the Haskell language definition. See also the literally impossible brace insertion rule of Haskell98.) -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

That's certainly true. As I mentioned, I was actually considering a
*pretty-printer* rather than an automatic indentation tool per se. The end
results are similar, but the pretty-printer is really only the latter part
of the problem: it's predicated on already having a valid AST.
My particular use case involved diffing and merging ASTs directly; this
means that I would have to somehow output the result in a human-readable
format. Moreover, the actual AST I was outputting could have been the
combination of two others, without any prior concrete syntax! I still
haven't worked out a good way to do this for Haskell (or, to be fair, any
other language).
But yeah, Haskell is pretty intractable as far as language grammars go. I
think this is a great compromise--I value language expressiveness over
tooling--but it certainly is a compromise.
On Mon, Jul 1, 2013 at 10:10 AM, Brandon Allbery
On Mon, Jul 1, 2013 at 9:56 AM, Tikhon Jelvis
wrote: I've thought about writing an automatic indenting tool for Haskell (or, more accurately, a pretty-printer) for another project I have, and this is the main thing that threw me off. While automatic indentation might make sense for less expressive languages (Google Go being an extreme example), I think it would be too constraining for Haskell. After all, in reasonable code, chances are that similar constructs end up meaning wildly different things (especially with the advent of pervasive embedded DSLs), so the code itself is a poor indicator of its own structure.
One might look at the history of the indentation modules for Emacs haskell-mode. Short version: they gave up, tab iterates through the possibilities because it's quite impossible to know which one is correct without a *semantic*, not just syntactic, understanding of the code. (Which, when you think about it, is pretty much par for the Haskell language definition. See also the literally impossible brace insertion rule of Haskell98.)
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 2/07/2013, at 12:00 AM, Richard Cobbe wrote:
Sure. So my first question boils down to which of the two alternatives below does the community prefer? (To be clear about the intended semantics: this is the application of the function f to the arguments x, y, and z.)
f x y z
This (a) clearly violates the Golden Rule of Indentation and (b) Goes out of its way to confuse human readers. I do not know any indenting program that would tolerate such a layout for C or Lisp.
or
f x y z
Both are correct, in most contexts.
What part of "y and z are PARTS of f x y z and so SHOULD BE INDENTED relative to the whole expression" is hard to understand? If by "correct" you mean "will not confuse a Haskell parser", you're right. If you mean "will not dangerously mislead human readers", only the second form is acceptable. I do not trust any program to do my indentation. And let's face it, if your Haskell functions are big enough that manual indentation is a big issue, they are too big.

Hi, on a related note: At HaL8 I was wondering how well Haskell would be suited for a workflow similar to what the go programmers do: Do not worry about code layout, but let the computer handle it! For more rationale and examples see http://golang.org/doc/effective_go.html#formatting While everyone has his own style and preferences, as soon as multiple programmers work on the same code, a consistently enforced formatting would probably help here a lot. Also, I would be happy to give up some bikeshedding^Wformatting freedom for not having to worry about this aspect. I found https://github.com/jaspervdj/stylish-haskell/ (found via http://stackoverflow.com/q/6870148/946226) which formats just some very few aspects of Haskell. Does anyone have a more complete solution? Or is interested in creating one? Greetings, Joachim -- Joachim Breitner e-Mail: mail@joachim-breitner.de Homepage: http://www.joachim-breitner.de ICQ#: 74513189 Jabber-ID: nomeata@joachim-breitner.de

On Mon, Jul 01, 2013 at 09:48:42AM +0200, Joachim Breitner wrote: <SNIP>
I found https://github.com/jaspervdj/stylish-haskell/ (found via http://stackoverflow.com/q/6870148/946226) which formats just some very few aspects of Haskell. Does anyone have a more complete solution? Or is interested in creating one?
I'd love a fully-automatic indentation tool; it's one of the things about the Lisp world that I miss. But is this actually a solvable problem? I think that, to be fully correct, an indentation algorithm for Haskell would have to know not only the syntactic structure of the program but also the programmer's intent, in some cases. Heh -- forgive the pun. Nested CASE expressions are actually one of the trickiest bits. If the programmer doesn't put parens around the inner expression (and doesn't use braces and semis), then only indentation serves to indicate when the inner case ends and the next branch of the outer case begins: case x of A -> ... B -> case y of Just z -> ... Nothing -> ... C -> ... Similarly (tying this back to my original question), in a do block, applications that span multiple lines are tricky: do f x y z a b c How does the indentation tool know if (a b c) is supposed to be the next item in the do block, or merely a continuation of the previous application of f? Richard

Hi, Am Montag, den 01.07.2013, 07:59 -0400 schrieb Richard Cobbe:
How does the indentation tool know if (a b c) is supposed to be the next item in the do block, or merely a continuation of the previous application of f?
I would still expect the developer to write correctly intended Haskell, so the the tool would have to employ a Haskell parser to get an AST (with extra information on where comments were – probably the trickiest part) and then pretty-prints it. If it were not for comments, using haskell-src-exts would already provide a first approximation to the problem, as shown in this script: https://github.com/djv/small/blob/master/tidy.hs Greetings, Joachim -- Joachim “nomeata” Breitner mail@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0x4743206C Debian Developer: nomeata@debian.org

On Sun, Jun 30, 2013 at 07:53:08PM -0400, Richard Cobbe wrote:
Two questions:
And what I've concluded by reading this thread:
1) Are there wide-spread conventions in the Haskell community for how to indent an application expression that's split across multiple lines?
Well, there's general consensus in favor of indenting the continuing lines, but not a lot of consensus on how much, etc. Not surprising, and that's OK. Infix operators are more complicated.
2) If there is such a convention, how do I make Emacs's haskell-mode do it?
Doesn't seem to be possible, unless you're in a "do" block. To be clear: I wasn't asking how to make Emacs do all of my indentation automatically, as it can in Lisp and C modes. I realize that isn't possible. But since haskell-mode doesn't have very good support for the style I personally prefer (which is, in fact, consistent with Richard O'Keefe's favorite rule), I wondered if that might indicate that the community prefers a different style. Apparently not. Thanks to all who responded; it's been an interesting (if occasionally overly excited) discussion. Richard

I prefer the other style--as do others, evidently (see the example in my
first reply.) I agree that this was a good discussion, but let's not
conclude so easily that the entire community is in favor of one thing or
the other.
On Mon, Jul 1, 2013 at 8:24 PM, Richard Cobbe
On Sun, Jun 30, 2013 at 07:53:08PM -0400, Richard Cobbe wrote:
Two questions:
And what I've concluded by reading this thread:
1) Are there wide-spread conventions in the Haskell community for how to indent an application expression that's split across multiple lines?
Well, there's general consensus in favor of indenting the continuing lines, but not a lot of consensus on how much, etc. Not surprising, and that's OK. Infix operators are more complicated.
2) If there is such a convention, how do I make Emacs's haskell-mode do it?
Doesn't seem to be possible, unless you're in a "do" block.
To be clear: I wasn't asking how to make Emacs do all of my indentation automatically, as it can in Lisp and C modes. I realize that isn't possible.
But since haskell-mode doesn't have very good support for the style I personally prefer (which is, in fact, consistent with Richard O'Keefe's favorite rule), I wondered if that might indicate that the community prefers a different style. Apparently not.
Thanks to all who responded; it's been an interesting (if occasionally overly excited) discussion.
Richard
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (10)
-
Brandon Allbery
-
Darren Grant
-
Joachim Breitner
-
Johannes Waldmann
-
Patrick Mylund Nielsen
-
Richard A. O'Keefe
-
Richard Cobbe
-
Sturdy, Ian
-
Tikhon Jelvis
-
Tom Ellis