Reading old Haskell
What old Haskell looks like
A module from a tutorial of 2010 still compiles with GHC 9.10, which is one of the strengths of the language and the reason old code is worth reading. Four habits date it:
- String everywhere, because Data.Text was not in the platform yet.
- import qualified Data.Map as Map with the alias written by hand, which is still correct and no longer the only spelling.
- fromJust and head where a total function would do, because a crash in a demo did not matter.
- fail in a monad and a Monad class without Applicative above it, which the AMP change of 2016 made invalid for new code.
None of that is a reason to rewrite a working module. It is a reason to read it with the right expectations and to fix the parts that a compiler cannot check: the partial functions.
A module from a tutorial
module Main (main) where
import qualified Data.Map as Map
import Data.Maybe (fromJust)
ages :: Map.Map String Int
ages = Map.fromList [("ada", 36), ("grace", 45)]
lookupAge :: String -> Int
lookupAge name = fromJust (Map.lookup name ages)
main :: IO ()
main = print (lookupAge "ada")
ghc -Wall -O1 -o old old.hs
./old
36
It runs, it has no warning under -Wall, and hlint has no suggestion: nothing in the tooling says that lookupAge is partial. The type String -> Int promises an Int for every string, and the function can only keep that promise for the names in the map.
The missing case
A single unknown name turns the promise into an exception. The same program with lookupAge "zoe" prints one line and exits with status 1:
ghc -Wall -O1 -o oldcrash oldcrash.hs
./oldcrash
echo "status: $?"
oldcrash: Maybe.fromJust: Nothing
status: 1
The line after it is a call stack that names fromJust and the source position, which is useful once and not a design. In a program that reads a name from the command line, the crash is reachable from the outside, which is the bug this lesson fixes.
The same module today
module Main (main) where
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
ages :: Map.Map String Int
ages = Map.fromList [("ada", 36), ("grace", 45)]
lookupAge :: String -> Either String Int
lookupAge name =
case Map.lookup name ages of
Nothing -> Left ("unknown name: " ++ name)
Just age -> Right age
main :: IO ()
main = do
print (lookupAge "ada")
print (lookupAge "zoe")
print (fromMaybe 0 (Map.lookup "zoe" ages))
ghc -Wall -O1 -o modern modern.hs
./modern
Right 36
Left "unknown name: zoe"
0
Three changes, each one small:
- The result is Either String Int, so the missing case is in the type and the caller has to handle it.
- Map.lookup is matched with a case instead of being fed to fromJust, which also names the missing key.
- Where a default really is wanted, fromMaybe says so at the call site with the default in view, and the entry point still has to choose it.
The module still uses String and a strict Map, which is fine for a table of two names. A text-heavy program would switch to Data.Text as in the text lesson, and the change would be at the edges only.
The changes that matter
Four language and library changes are the ones that bite when old code meets a modern compiler:
- AMP (2016): Applicative became a superclass of Monad, so a hand-written Monad instance must come with an Applicative one. The lessons on the classes show the modern shape.
- MonadFail (2019): fail moved out of Monad, so a pattern match that can fail in a do block now needs a MonadFail instance, which Maybe and [] have and IO does not.
- The Foldable-Traversable proposal: length, sum and elem now work on any Foldable, so an old type error about [a] can become an ambiguity with two instances in scope.
- Type defaulting: a numeric literal without a type is still Integer or Double, which is why -Wall asks for an annotation in this course.
The way to read old code is to compile it with -Wall and to treat every partial function as a bug report. The compiler will not point at fromJust; the next lesson is about measuring the program instead of trusting it.
Exercises
- Rewrite lookupAge to return Maybe Int and handle the missing name in main.
- Find a use of head in a program of your own and make it total.
- Change the modern module to Data.Text and compare the two versions.
- Write a Monad instance without an Applicative and read the error the compiler gives.
- Explain why fromJust passes -Wall and hlint.
- Take a module from an old tutorial and list every partial function in it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is
lookupAge name = fromJust (Map.lookup name ages)partial?Answer
It can only return an Int for a name that is in the map
The type promises an Int for every string, and a missing key has no Int to give.
-
What happens when
fromJustgetsNothing?Answer
An exception ends the program with a non-zero status
fromJust is defined with error, so the runtime prints the message and exits with status 1.
-
Why does the old module pass
-Wall?Answer
A partial function is not a warning; the type checker cannot see the missing case
The compiler checks types, and fromJust has a type that promises a value for every Maybe.
-
What does this program print?
module Main (main) where import Data.Maybe (fromMaybe) main :: IO () main = print (fromMaybe 0 (Just 7) + fromMaybe 0 (Nothing :: Maybe Int))Answer
7
The first fromMaybe gives 7 and the second 0, so the sum is 7.
-
What did the AMP change require?
Answer
Applicative became a superclass of Monad
A Monad instance now needs an Applicative instance, which is why old instances do not compile.
-
What does this program print?
module Main (main) where import qualified Data.Map.Strict as Map main :: IO () main = print (Map.lookup "a" (Map.fromList [("a", 1 :: Int)]))Answer
Just 1
The key is present, so lookup gives the value wrapped in Just.
-
Which function is the safe way to give a default for a missing key?
Answer
fromMaybefromMaybe takes the default as an argument and is total.
-
What does this program print?
module Main (main) where import Data.Maybe (fromMaybe) main :: IO () main = putStrLn (fromMaybe "unknown" (Just "zoe"))Answer
zoe
The Just is used, so the default is not needed and the name is printed.
-
Where did
failmove in 2019?Answer
From Monad to its own class MonadFail
MonadFail owns fail now, and only the monads that can fail have an instance.
-
What is the first thing to do with an old module?
Answer
Compile it with -Wall and list the partial functions
The compiler catches the language changes and the partial functions are the bugs it cannot see.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev