Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make a derived instance of Num

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.

like image 800
Ammlan Ghosh Avatar asked Sep 01 '14 10:09

Ammlan Ghosh


1 Answers

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.  

like image 183
MathematicalOrchid Avatar answered Oct 06 '22 01:10

MathematicalOrchid