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:

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 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:

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

  1. Rewrite lookupAge to return Maybe Int and handle the missing name in main.
  2. Find a use of head in a program of your own and make it total.
  3. Change the modern module to Data.Text and compare the two versions.
  4. Write a Monad instance without an Applicative and read the error the compiler gives.
  5. Explain why fromJust passes -Wall and hlint.
  6. 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.

  1. 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.

  2. What happens when fromJust gets Nothing?

    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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Which function is the safe way to give a default for a missing key?

    Answer

    fromMaybe

    fromMaybe takes the default as an argument and is total.

  8. 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.

  9. Where did fail move in 2019?

    Answer

    From Monad to its own class MonadFail

    MonadFail owns fail now, and only the monads that can fail have an instance.

  10. 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