【问题标题】:Kind mis-match in instance of MaybeMaybe 实例中的类型不匹配
【发布时间】:2016-03-11 05:52:52
【问题描述】:

我有一个类的定义

class Functor f  => Pointed f where
    pure :: a -> f a

现在我想为Maybe 创建实例。为什么这不起作用?

instance Pointed (Maybe s) where
    pure Nothing  = Nothing
    pure (Just a) =  a


class Functor f where
    fmap :: (a->b) -> f a -> f b 

错误是:

  Kind mis-match
    The first argument of `Pointed' should have kind `* -> *',
    but `Maybe s' has kind `*'
    In the instance declaration for `Pointed (Maybe s)'
Failed, modules loaded: none.

【问题讨论】:

    标签: haskell


    【解决方案1】:

    在Pointed的定义中,f有一种* -> *,所以你需要提供一个类型相同的构造函数。 Maybe 有那种,而(Maybe s) 有那种*。如果在pure 的定义中将Maybe 替换为f,则会得到:

    pure :: a -> Maybe a
    

    因此,您的定义应如下所示:

    instance Pointed Maybe where
        pure a =  Just a
    

    由于kind inference 的规则,类型* -> * 被推断为f。 a 被假定为具有种类 *,因为它是一个函数参数(具有种类 * -> * -> *)。 f a 应该有善意的* 所以f 有善意的* -> *。

    在Pointed的定义中,Functor f =>是一个类上下文,并声明Functor是Pointed的超类。这意味着Pointed 的所有实例也必须是Functor 的实例。

    Maybe 已经是Functor 的一个实例,所以你不需要自己定义它。

    【讨论】:

    • 我不明白为什么In the definition of Pointed, f has kind * -> * 对不起,我忘了给出函子定义。我编辑了帖子。特别是,它到底是什么意思?:class Functor f => Pointed f Functor (f) 和 Functor f 有区别吗?
    • @Gilgamesz 因为f a这个词出现在pure的类型中。
    • 好吧,你是对的。但也请回答:究竟是什么意思?:class Functor f => Pointed fFunctor (f)和Functor f有区别吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多