Haskell 2010 against GHC2021

Two default languages

A Haskell module is compiled with a language: a standard plus a set of extensions that are on without a pragma. Two of them matter in practice:

The two are a flag apart, and the file below is written so that the difference is visible.

GHC2021 is a fixed list, not "everything GHC has": it turns on about forty extensions such as TupleSections, ImportQualifiedPost, NumericUnderscores, TypeApplications and ScopedTypeVariables, and it deliberately leaves out others. The file below uses three of the ones it turns on.

A file written for GHC2021

module Main (main) where import Data.List qualified as L pairs :: [(Int, Int)] pairs = map (1,) [2, 3 :: Int] main :: IO () main = do print (L.sort [3, 1, 2 :: Int]) print pairs print (1_000_000 :: Int)

Three modern features in nine lines: the qualified import after the module name, the tuple section (1,) that fills in the second component, and the underscore in the numeric literal. Under the 2010 language none of them is enabled. With the default of this compiler the file compiles as it stands, which is the baseline the next section breaks.

The same file as Haskell 2010

ghc -Wall -XHaskell2010 -fno-code modern.hs

modern.hs:3:18: error: [GHC-87491]
    Found ‘qualified’ in postpositive position.
    Suggested fix: Perhaps you intended to use ImportQualifiedPost
  |
3 | import Data.List qualified as L
  |                 ^^^^^^^^^

The error names the extension that would fix it, which is the fastest way to learn the name of an extension: write the code, read the suggestion. The other two features fail the same way with TupleSections and NumericUnderscores in the suggestion, one error at a time because the parser stops at the first.

The GHC2021 build

ghc -Wall -XGHC2021 -O1 -o modern modern.hs
./modern

[1 of 2] Compiling Main ( modern.hs, modern.o )
[2 of 2] Linking modern
[1,2,3]
[(1,2),(1,3)]
1000000

The source compiles with no pragma because GHC2021 is the default here, and the explicit -XGHC2021 is what makes that a decision rather than an accident. The output is what the three features mean: the qualified sort gives the ascending list, the tuple section paired 1 with every element, and the underscore is only a separator in the literal. Note that -XGHC2021 is a choice on the command line: a file with a {-# LANGUAGE ... #-} pragma still gets that extension whatever the default.

What GHC2021 does not include

LambdaCase is a popular extension that is not in the set, and the difference matters when a project switches from 2010 to 2021 and expects everything to work:

module Main (main) where label :: Maybe Int -> String label = \case Nothing -> "none" Just n -> "some " ++ show n main :: IO () main = putStrLn (label (Just 3))

ghc -Wall -XGHC2021 -fno-code lam.hs

[1 of 1] Compiling Main ( lam.hs, nothing )
lam.hs:4:10: error: [GHC-51179]
    Illegal \case
    Suggested fix: Perhaps you intended to use LambdaCase
  |
4 | label = \case
  |         ^^^^^

The fix is one pragma or one flag, and it is not a bug of the compiler: the GHC2021 list was fixed in 2021 and does not grow. With the extension the file compiles and prints:

ghc -Wall -XLambdaCase -O1 -o lam lam.hs
./lam

[1 of 2] Compiling Main ( lam.hs, lam.o )
[2 of 2] Linking lam
some 3

default-language in a .cabal file

A Cabal component selects the language with one field, and the extensions a file needs beyond it with default-extensions:

executable modern     main-is: Main.hs     hs-source-dirs: app     default-language: GHC2021     default-extensions: LambdaCase     build-depends: base

The two fields do the work of a pragma per file, and they are visible in one place, which is what a project with many modules wants. The next lesson reads code written before any of this: a module of a tutorial from 2010, and the same module in the style of today.

Exercises

  1. Take the modern file and compile it with each of the three extensions as a pragma instead of the whole GHC2021 set.
  2. Add -XGHC2021 to a program with a \case and add the pragma, then explain the two results.
  3. Compile a file that uses RecordWildCards with -XGHC2021 and read the suggestion.
  4. Find the full GHC2021 list with ghc --supported-languages and ghc --show-options.
  5. Change a .cabal file to default-language: GHC2021 and rebuild the package of the previous lessons.
  6. Explain why a project pins the language instead of relying on the compiler default.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which language does plain ghc 9.10 use by default?

    Answer

    GHC2021

    GHC 9.10 defaults to GHC2021, and the modern file on this page compiles without a flag because of it.

  2. What is GHC2021?

    Answer

    A fixed set of extensions recommended as a default

    It is a named extension set, not a compiler version and not everything GHC supports.

  3. Which extension does import Data.List qualified as L need?

    Answer

    ImportQualifiedPost

    The postpositive position is the extension, and GHC2021 includes it.

  4. What does this program print? {-# LANGUAGE TupleSections #-} module Main (main) where main :: IO () main = print (map (1,) [2, 3 :: Int])

    Answer

    [(1,2),(1,3)]

    The section fills the second component, so each element becomes a pair with 1.

  5. Is LambdaCase part of GHC2021?

    Answer

    No, it needs a pragma or a flag

    The set is fixed and LambdaCase is not in it, as the real error shows.

  6. What does this program print? module Main (main) where main :: IO () main = print (1_000 + 1 :: Int)

    Answer

    1001

    The underscore is a separator in a numeric literal, so the value is 1000 and the sum is 1001.

  7. What does -XHaskell2010 do on the command line?

    Answer

    Selects the 2010 language for this build

    The flag selects the language and with it the enabled extensions for this build.

  8. What does this program print? module Main (main) where import Data.List qualified as L main :: IO () main = print (L.sort [3, 2, 1 :: Int])

    Answer

    [1,2,3]

    The code needs ImportQualifiedPost, which is on under GHC2021 and a flag or pragma otherwise.

  9. Which field of a Cabal component selects the language?

    Answer

    default-language

    default-language names the standard or set, and default-extensions adds to it.

  10. Why pin the language in a project instead of relying on the default?

    Answer

    A new compiler may change its default and break the build silently

    The default belongs to the compiler, so pinning it keeps the build reproducible.

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