Understanding the Haskell Layout Rule & Indentation
Haskell uses the famous off-side rule (syntactic layout), meaning that whitespace and indentation define code blocks without needing curly braces or semicolons. Keywords like where, let, do, case...of, and class establish an indentation column for all subsequent statements. Misaligned indentation is one of the most common compilation blockers in GHC, making automated layout indentation essential.
The Standard 2-Space Rule
Modern formatters like Ormolu and Fourmolu standardize on 2 spaces per indentation level. Hard tabs (\t) are strictly forbidden in GHC by default because their visual width varies across editors.
Monadic do Notation
Statements inside a do block must align vertically to the same column. Any statement indented further is treated as a continuation of the previous expression.
Haskell Style Guidelines: Standard Conventions vs. Messy Layouts
| Construct | Unformatted / Misaligned | Beautified Haskell Standard | Formatting Rule |
|---|---|---|---|
| Monadic Do Block | main = do putStrLn "Hello" name <- getLine putStrLn name |
main = do putStrLn "Hello" name <- getLine putStrLn name |
2-space indent for all statements inside the do context. |
| Data Declaration | data Tree a = Leaf a | Node (Tree a) (Tree a) | data Tree a = Leaf a | Node (Tree a) (Tree a) |
Aligning constructors with leading pipes enhances readability. |
| Where Clause | calc x = x * factor where factor = 42 |
calc x = x * factor where factor = 42 |
Indenting where prevents lexical collisions with top-level decls. |
Frequently Asked Questions
What indentation style does this Haskell formatter follow?
By default, it follows modern Haskell community conventions (similar to Ormolu and Fourmolu), utilizing 2 spaces or 4 spaces per nesting level without hard tabs.
How does it handle Haskell's off-side layout rule?
The engine identifies layout-triggering keywords such as 'do', 'where', 'let', 'of', and 'mdo', calculating consistent column indentations so that lexical scope is clearly preserved.
Is my proprietary Haskell source code sent to a remote server?
No. All formatting logic runs entirely in your browser using JavaScript lexical parsing. Zero code or telemetry is transmitted across the network.