Haskell 代码格式化程序(Fourmolu / Ormolu 风格)
指导
Haskell 代码格式化程序(Fourmolu / Ormolu 风格)
Paste raw Haskell source and get back code formatted to a deterministic, Fourmolu / Ormolu-inspired style — with consistent operator spacing, sorted LANGUAGE pragmas, grouped imports, and tabs normalized to spaces. Everything runs locally in your browser, so your source code never leaves your machine.
如何使用
- Upload a
.hsfile or paste your Haskell source into the input box (use the “Try an example” link for a quick sample). - Pick your indent size — 2 spaces (Fourmolu default) or 4 spaces (Ormolu default).
- Set the max line length used to flag overly long lines in the validation summary (defaults to 100).
- 切换 排序导入, Sort & deduplicate LANGUAGE pragmas,并且 合并空行 to match your project’s house style.
- The formatted output appears automatically with syntax highlighting; use Copy or Download to grab the result.
特征
- Deterministic spacing — adds canonical spaces around
=,::,->,<-,=>,$,++, comparison operators, and Applicative / Monad combinators like<$>,<*>,>>=. - Pragma normalization — pulls every
{-# LANGUAGE ... #-}pragma to the top of the file, splits combined pragmas into one extension per line, sorts them alphabetically, and removes duplicates. - 导入排序 — sorts consecutive
importstatements alphabetically by module name while preserving the unqualified / qualified grouping convention. - Layout-safe — preserves the user’s leading indentation (Haskell’s layout rule is significant) while converting tabs to spaces using the chosen indent width.
- Comment-safe tokenizer — correctly handles line comments (
--), nested block comments ({- ... -}), pragmas, strings with escapes, and character literals so operator characters inside them are never mangled. - Bracket + block validation — flags unbalanced parentheses, brackets, braces, and unterminated block comments with line numbers.
- Long-line report — highlights any line that exceeds your configured maximum width so you can refactor before review.
- 100% 客户端 — your code is processed entirely in the browser; nothing is uploaded, logged, or stored.
常问问题
-
What is the difference between Ormolu and Fourmolu?
Ormolu is a strict, opinionated Haskell formatter with effectively no configuration — every project formatted by Ormolu looks the same. Fourmolu is a fork of Ormolu that exposes a handful of style knobs (indentation size, comma placement, leading-vs-trailing commas, record-brace spacing) for teams that want a consistent style that is not identical to upstream Ormolu. Both share the same parser and AST-driven approach, so they produce reliably valid Haskell.
-
Why is Haskell formatting hard for general-purpose AI?
Haskell uses the off-side rule — indentation is semantically significant and determines where blocks like let, where, do, and case ... of begin and end. A formatter must understand the parse tree, not just the token stream, to re-indent safely without changing program meaning. General-purpose language models reason about code as text and routinely produce indentation that compiles differently from the input, especially for nested where-clauses and multi-line lambdas.
-
What is a LANGUAGE pragma?
A LANGUAGE pragma is a directive of the form {-# LANGUAGE ExtensionName #-} placed at the top of a Haskell source file to enable a GHC language extension for that module — for example OverloadedStrings, ScopedTypeVariables, or TypeApplications. Pragmas can be combined ({-# LANGUAGE A, B, C #-}) but most style guides recommend one extension per line and alphabetical sorting for easy diffs.
-
Why are qualified imports usually grouped separately?
Convention groups qualified imports (import qualified Data.Map as M) after plain imports (import Data.List) so a reader can scan unqualified names first and see at a glance which modules are accessed through aliases. This is the layout produced by stylish-haskell, Ormolu, and Fourmolu by default, and it keeps merge conflicts localized when a project adds or removes qualified aliases.
