[Haskell-cafe] question about indentation conventions

Tikhon Jelvis tikhon at jelv.is
Mon Jul 1 15:56:52 CEST 2013


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 <sturdyi12 at mail.wlu.edu> 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-bounces at haskell.org [haskell-cafe-bounces at haskell.org]
> on behalf of Richard Cobbe [cobbe at ccs.neu.edu]
> Sent: Monday, July 01, 2013 8:00 AM
> To: haskell-cafe at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130701/e76d6914/attachment.htm>


More information about the Haskell-Cafe mailing list