Making newtype instance of Integral class in haskell

69 Views Asked by At

I want to make my newtype instance of Integral class. My newtype:

newtype NT = NT Integer deriving (Eq,Ord,Show)

I want to be able to do div with my newtype NT:

Prelude> (NT 5) `div` (NT 2)
2

Since:

Prelude> :t div
div :: Integral a => a -> a -> a 

I should make my NT instance of Integral class.

This is how is Integral class defined:

class  (Real a, Enum a) => Integral a  where
    quot, rem        :: a -> a -> a   
    div, mod         :: a -> a -> a
    quotRem, divMod  :: a -> a -> (a,a)
    toInteger        :: a -> Integer

and Since I need only div I tried:

instance Integral NT where
  (NT x) `div` (NT y) =  (NT q)  where (q,r) = divMod x y

or can I do this?

instance Integral NT where
  div (NT x)  (NT y) =  NT (div x y )

but either way I get the error:

• No instance for (Real NT)
  arising from the superclasses of an instance declaration
• In the instance declaration for ‘Integral NT’

Should I make NT instance of Real class First and how?

0

There are 0 best solutions below