{-# 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
这行得通。但是,如果我想为EnumBox添加类Enum的实例,例如:
instance Enum EnumBox where
succ (EB s) = succ s
它失败,并显示以下消息:
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
为什么可以演绎第一场演出而不能演绎第二场成功?
您唯一的问题是succ
类型
succ :: Enum a => a -> a
所以你需要
succ (EB s) = EB . succ $ s
只是再次装箱。
你可能还会想要
instance Enum EnumBox where
toEnum = EB
fromEnum (EB i) = fromEnum i
由于这是完整性的最低定义,因为
succ = toEnum . succ . fromEnum
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句