Reputation: 1871
{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable #-}
import Data.Typeable;
data EnumBox = forall s. (Enum s, Show s) => EB s
deriving Typeable
instance Show EnumBox where
show (EB s) = "EB " ++ show s
This works. But if I want to add a instance of Class Enum for EnumBox likes:
instance Enum EnumBox where
succ (EB s) = succ s
It fails with the message:
Could not deduce (s ~ EnumBox)
from the context (Enum s, Show s)
bound by a pattern with constructor
EB :: forall s. (Enum s, Show s) => s -> EnumBox,
in an equation for `succ'
at typeclass.hs:11:9-12
`s' is a rigid type variable bound by
a pattern with constructor
EB :: forall s. (Enum s, Show s) => s -> EnumBox,
in an equation for `succ'
at typeclass.hs:11:9
In the first argument of `succ', namely `s'
In the expression: succ s
In an equation for `succ': succ (EB s) = succ s
Why the first show can be deduced but the second succ cannot?
Upvotes: 3
Views: 304
Reputation: 53911
You're only problem is that succ
has the type
succ :: Enum a => a -> a
So you need
succ (EB s) = EB . succ $ s
Just boxing it up again.
Also you'll probably want
instance Enum EnumBox where
toEnum = EB
fromEnum (EB i) = fromEnum i
As this is the minimum definition of completeness, since
succ = toEnum . succ . fromEnum
Upvotes: 2