hlint and ormolu
Two tools keep a Haskell codebase readable, and they answer different questions. hlint reads the code and suggests a simpler way to write it, so it is a reviewer with opinions and it is sometimes wrong. ormolu reformats the code and has no opinions, so a project can stop arguing about layout and let the tool decide.
Both are in this image, so both run here for real. The file below has three hints and one formatting diff, which is a realistic first encounter: the hints are about meaning, the diff is about spaces, and the two sets of changes do not overlap.
Two tools, two questions
hlint takes a file or a folder, never rewrites it, and exits 1 when it has a hint. That status is what a build script reads, and it is why hlint sits in the same place as a compiler: a new hint fails the build and somebody decides whether the hint or the code is right. Its suggestions come from a database of rewrite rules written by people, so a rule can be wrong about your code, and the page has one of those.
ormolu is a formatter in the style of gofmt: one canonical layout, no configuration worth the name, and a --mode check that reports instead of writing. Its exit status is the other thing a build script reads. A file that passes both tools has one shape and no known simplification, which is as far as a tool can go.
The file before
Four small functions and a main that prints their results, so the rewrites can be checked against the output. The file builds clean under -Wall and runs correctly: both tools complain about style, neither about the program.
module Main (main) where
doubleAll :: [Int] -> [Int]
doubleAll xs = map (*2) xs
shift :: [Int] -> [Int]
shift = map (+1)
both :: [Int] -> [Int]
both xs = map (+1) (map (*2) xs)
isBig :: Int -> Bool
isBig n = if n > 100 then True else False
main :: IO ()
main = do
print (doubleAll [1, 2, 3])
print (shift [1, 2, 3])
print (both [1, 2, 3])
print (isBig 50, isBig 500)
ghc -Wall -O1 -o style-messy Style.hs
./style-messy; echo "status $?"
[1 of 2] Compiling Main ( Style.hs, Style.o )
[2 of 2] Linking style-messy
[2,4,6]
[2,3,4]
[3,5,7]
(False,True)
status 0
hlint
Three hints, one per shape. Eta reduce removes the argument that is only passed along, Use map once fuses two maps into one and Redundant if drops the then True else False that returns the condition itself. Each one prints the code it found and the code it suggests, which is what makes a hint reviewable.
hlint Style.hs; echo "status $?"
Style.hs:4:1-26: Warning: Eta reduce
Found:
doubleAll xs = map (* 2) xs
Perhaps:
doubleAll = map (* 2)
Style.hs:10:11-32: Suggestion: Use map once
Found:
map (+ 1) (map (* 2) xs)
Perhaps:
map ((+ 1) . (* 2)) xs
Style.hs:13:11-41: Warning: Redundant if
Found:
if n > 100 then True else False
Perhaps:
n > 100
3 hints
status 1
The three hints are also ranked: two are warnings and one is a suggestion. The rank is a hint about how safe the rewrite is, and the suggestion is the one to read twice, because fusing two maps changes when the work happens, not just how the code looks.
ormolu --mode check
ormolu --mode check prints the name of the file, then a unified diff of what it would write, and exits 100. A file it would reformat and a file it cannot read are two different answers, and the tool gives them two statuses: 100 for the reformat, and a parse error with the parser code for a file that is not Haskell at all.
ormolu --mode check Style.hs; echo "status $?"
Style.hs
@@ -1,20 +1,20 @@
module Main (main) where
doubleAll :: [Int] -> [Int]
- doubleAll xs = map (*2) xs
+ doubleAll xs = map (* 2) xs
shift :: [Int] -> [Int]
- shift = map (+1)
+ shift = map (+ 1)
both :: [Int] -> [Int]
- both xs = map (+1) (map (*2) xs)
+ both xs = map (+ 1) (map (* 2) xs)
isBig :: Int -> Bool
isBig n = if n > 100 then True else False
main :: IO ()
main = do
- print (doubleAll [1, 2, 3])
- print (shift [1, 2, 3])
- print (both [1, 2, 3])
- print (isBig 50, isBig 500)
+ print (doubleAll [1, 2, 3])
+ print (shift [1, 2, 3])
+ print (both [1, 2, 3])
+ print (isBig 50, isBig 500)
status 100
Every change is a space: (*2) becomes (* 2), the operator sections get their space, and the do block loses two columns of indentation. Nothing about the program changed, which is the point of a formatter and the reason a diff of it is easy to review. A broken file takes the other branch, with the line and the parser error instead of a diff; the file is one line of nonsense, let = 1, on line 4:
ormolu --mode check Broken.hs; echo "status $?"
Broken.hs:4:7
The GHC parser (in Haddock mode) failed:
[GHC-58481] parse error on input `='
status 3
The status is 3 and not 100, so a script that treats every non-zero status as "needs formatting" would try to reformat a file that does not parse. The two cases are worth telling apart.
The file after
The three hints applied by hand and the formatting applied by ormolu --mode inplace give one file. The two rewrites that were not formatting are worth reading: doubleAll = map (* 2) is the same function with the argument left implicit, and both composes the two operations instead of building an intermediate list.
module Main (main) where
doubleAll :: [Int] -> [Int]
doubleAll = map (* 2)
shift :: [Int] -> [Int]
shift = map (+ 1)
both :: [Int] -> [Int]
both = map ((+ 1) . (* 2))
isBig :: Int -> Bool
isBig n = n > 100
main :: IO ()
main = do
print (doubleAll [1, 2, 3])
print (shift [1, 2, 3])
print (both [1, 2, 3])
print (isBig 50, isBig 500)
The output is the same as before, which is the check that a rewrite was a rewrite:
ghc -Wall -O1 -o style Style.hs
./style; echo "status $?"
[1 of 2] Compiling Main ( Style.hs, Style.o )
[2 of 2] Linking style
[2,4,6]
[2,3,4]
[3,5,7]
(False,True)
status 0
Both tools are now silent, and both exit 0:
hlint Style.hs
ormolu --mode check Style.hs
echo "status $?"
No hints
status 0
What each one is for
hlint belongs in the review, as a check that fails and as a source of suggestions a person accepts or rejects. ormolu belongs in the editor, on save, and in a check that fails when somebody did not run it. The combination is the one most Haskell projects use: the formatter ends every discussion about layout, and the linter keeps a memory of the simplifications nobody wants to explain twice.
A hint can be wrong, and the counterexample is on the previous page: there the double reverse of a property is the claim under test, and hlint is right that it is redundant and wrong that it should go. A project silences that with a comment on the line, {-# ANN ... #-} or a rule in .hlint.yaml; the wrong answer is to stop running the linter.
The course lesson on warnings and errors covers the compiler side of the same idea, and the lesson on reading old code shows what a file looks like when neither tool has ever run on it. The next page here is the honest one about the test libraries that are not in the image.
The commands were run with GHC 9.10.3, hlint and ormolu as installed by the distribution packages of Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev