15

I am using ghci, this code section

newtype Gold = Gold Int
    deriving (Eq, Ord, Show, Num)

is showing the error as

Can't make a derived instance of 'Num Gold':
  'Num' is not a derivable class
  Try GeneralizedNewTypeDeriving for GHC's newtype-deriving extension in the newtype declaration for 'Gold'

Please suggest the solution.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Ammlan Ghosh
  • 365
  • 3
  • 12

1 Answers1

25

You can only derive from Eq, Ord, Enum, Bounded, Show and Read automatically. In order to derive other instances, you need to enable the GeneralizedNewtypeDeriving extension as GHCi suggests:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

newtype Gold = Gold Int
  deriving (Eq, Ord, Show, Num)

Note that the {-# ... #-} isn't a comment, but a compiler pragma, in this case enabling the given language extension.  

freshtop
  • 706
  • 8
  • 17
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
  • Error Message shows: Can't make a derived instance of 'Num Gold': Num is not a derivable class Try GeneralizedNewtypeDeriving for GHC's newtype-deriving extension In the newtype declaration for 'Gold' – Ammlan Ghosh Sep 01 '14 at 10:45
  • Thank you @MathematicalOrchid. I am very new to Haskell. Actually I didn't include the line {-# LANGUAGE GeneralizedNewTypeDeriving #-}. – Ammlan Ghosh Sep 01 '14 at 10:54
  • 10
    @milleniumbug No need to be rude. The OP could be forgiven for thinking it was a comment, given the syntax. – AndrewC Sep 01 '14 at 11:04