haskellghctypeclasscaballanguage-extension

Why does Cabal, unlike GHC, not automatically enable GeneralizedNewtypeDeriving if I explicitly enabled DerivingStrategies?


From the documentation I'm inclined to think that if I enable the DerivingStrategies extension, I don't need to enable GeneralizedNewtypeDeriving or DeriveAnyClass, nor any other extension that I currently listed right before §6.6.7.1, e.g. DerivingVia.

However, this toy example

{-# LANGUAGE DerivingStrategies #-}

newtype MyNum = MyNum Int
 deriving stock (Eq, Ord, Show, Read)
 deriving newtype (Num, Enum, Real, Integral)

main :: IO ()
main = print $ MyNum 0

compiles just fine via ghc this-file.hs (GHC 9.4.8), but it doesn't via cabal build (Cabal 3.10.2.1), because in the latter case, it is also required that I add

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

Any clue why is that?


The dummy foo.cabal file I'm using is

cabal-version:   3.8
name:            foo
version:         1.0

executable foo
    main-is:          main.hs
    build-depends:    base

Solution

  • You do need to enable the individual extensions. DerivingStrategies only gives you new syntax to specify which deriving strategy you want.

    The reason that GHC doesn't require you to enable GeneralizedNewtypeDeriving is because it is part of the GHC2021 language extension set and GHC uses the language extension set GHC2021 by default.

    Cabal determines the language extension set it uses via the default-language field of your components. In your example you have left it out (which is bad practice) and apparently Cabal still uses Haskell2010 by default in that case. So, to fix this you can simply set the default-language field:

    cabal-version:   3.8
    name:            foo
    version:         1.0
    
    executable foo
        main-is:          main.hs
        build-depends:    base
        default-language: GHC2021