Skip to content

Remove `x-partial` from `-Wdefault`

GHC 9.8.1's new x-partial warning (currently warning about head and tail) is a hlint-style warning and should not be on by default.

While it is possible to switch it off in .cabal files, GHC is also used a lot in teaching where one works with small Haskell scripts without going through the effort of making a cabal file for each of these.

Here is an example for teaching material that uses head: https://github.com/teach-afp/afp-code/blob/f39715a6dfa81fde74d8043f0880e0b626da0f7d/L1/Lect1.hs#L102-L107

testLazy2 = head [3, undefined, 17]
testLazy3 = head (3:4:error "no tail")
testLazy4 = head [error "no first elem", 17, 13]
testLazy5 = head (error "no list at all")

Running this interactively (e.g. with haskell-mode in Emacs) my session is spammed with 4 instances of

Lect1.hs:114:13-16: warning: [GHC-63394] [-Wx-partial] …
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."

The warning seems resistant to efforts to disable, like :seti -Wno-x-partial in GHCi:

ghci> :seti -Wno-x-partial
ghci> :r
[1 of 1] Compiling Lect1            ( Lect1.hs, interpreted ) [Source file changed]

Lect1.hs:112:13: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
    |
112 | testLazy2 = head [3, undefined, 17]
    |             ^^^^

Lect1.hs:113:13: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
    |
113 | testLazy3 = head (3:4:error "no tail")
    |             ^^^^

Lect1.hs:114:13: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
    |
114 | testLazy4 = head [error "no first elem", 17, 13]
    |             ^^^^

Lect1.hs:115:13: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
    |
115 | testLazy5 = head (error "no list at all")
    |             ^^^^
Ok, one module loaded.

Update: :set -Wno-x-partial is required and works for disabling the warning for file content that is loaded interactively.

Edited by Andreas Abel
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information