Issue
In the Haskell documentation on indentation, it indicates that all "all grouped expressions must be exactly aligned." What does the term "grouped expressions" mean? Why do they have to be exactly aligned?
Solution
All that article means by a "grouped expression" is a member of a group of things that have to be aligned!
I don't think it's a great term, because few of the things that need to be aligned are technically expressions (they are mostly entire declarations).
I also think the "golden rule" listed at the start of that article is not a great way of explaining Haskell's indentation requirements. It says:
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).
However this is not in fact a general rule that applies to all Haskell expressions.1
The only times indentation matters in Haskell is always in the context of "blocks" containing a variable number of entries (the wikibooks article would call these "groups"). For example:
- a
let <decls> in <expr>
expression contains 1 or more declarations in the<decls>
part - a
where
clause introduces 1 or more declarations - an
instance
definition'swhere
part has zero or more method definitions - a
do
block has 1 or more statements - a
case
expression has 1 or more cases (zero or more with theEmptyCase
extension) - etc, etc
The purpose of the layout rules is to have a way to indicate (both to human readers and to the compiler) exactly the extend of each entry in the block, and where the block as a whole ends, using spatial layout instead rather than explicit marker symbols.
The "real" golden rule is this: All the entries in these blocks/groups must always start at the same column2. If an entry itself spans more than one line, then the continuation lines of these entries must be strictly more indented than column that would start an entry of the block.
If, as is commonly the case, you are writing any syntactic structure that isn't an entry in a block, then the wikibook's version of the "golden rule" is false; you can spread it across multiple lines and indent those lines however you want, with no need to indent continuation lines further than the start of the expression.3
1 And as I mentioned it rarely applies to expressions at all; off the top of my head I think statements in a do block that are a single expression (rather than a <-
binding) are the only expressions to which this rule could formally apply. All other blocks than do
blocks that I can think of right now have entries which are syntactic structures that are not simple expressions.
2 Or you can use explicit braces and semicolons to delimit the start/end of the block, and the extent of each entry in the block. In that case indentation/alignment doesn't matter at all.
3 The only gotcha is that you might be writing code nested inside an outer block of entries, and you need to not accidentally fall afoul of the alignment requirements of any outer block. This generally means the parts of an expression all have to be more indented that the innermost enclosing entry in a block, not that the parts have to be more indented than the start of the expression itself.
Answered By - Ben Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.