【发布时间】:2013-09-01 14:43:19
【问题描述】:
{-# 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
为什么第一个节目可以推断出第二个成功却不能?
【问题讨论】:
-
您好像忘记将
EB应用到succ s。 -
你是对的。我想念包装纸。
标签: haskell typeclass existential-type