Re: [Haskell-cafe] question about indentation conventions
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 wrote: > 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 > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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
Re: [Haskell-cafe] question about indentation conventions
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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 wrote: > 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 > allber...@gmail.com > ballb...@sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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 allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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 wrote: > 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-boun...@haskell.org [haskell-cafe-boun...@haskell.org] > on behalf of Richard Cobbe [co...@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 > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
On Mon, Jul 1, 2013 at 3:43 AM, Tom Ellis < tom-lists-haskell-cafe-2...@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 allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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-boun...@haskell.org [haskell-cafe-boun...@haskell.org] on behalf of Richard Cobbe [co...@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
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
Re: [Haskell-cafe] question about indentation conventions
> 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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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 wrote: > 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: > > > > > > 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? > > > > 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 > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] question about indentation conventions
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: > > 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? 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
Re: [Haskell-cafe] question about indentation conventions
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: > 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 > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe