【问题标题】:Type kind error when creating instance of Functor typeclass创建 Functor 类型类的实例时出现类型错误
【发布时间】:2018-11-22 12:14:10
【问题描述】:

我正在尝试为一个非常简单的类型Foo 实现Functor 类型类实例:

data Foo a = Foo a

instance functorFoo :: Functor (Foo a) where
  map fn (Foo a) = Foo (fn a)

Purescript 给了我不太有用的错误信息:

Could not match kind

    Type -> Type

with kind

    Type

这是什么意思?我对 Kind-system 还不是很熟悉。

【问题讨论】:

    标签: typeclass functor purescript


    【解决方案1】:

    但我仍然想知道为什么第一个版本不起作用。就像 Functor 类型类与例如如何不同一样第一个版本运行良好的 Semigroup。

    我们来看Functor类型类的定义:

    class Functor f where
      map :: forall a b. (a -> b) -> f a -> f b
    

    它在map 的类型签名中包含f af b 类型,这清楚地表明f 是一种Type -> Type(它“携带”了另一种类型)。另一方面,Semigroup 显然与 Type 的普通类型有关:

    class Semigroup a where
      append :: a -> a -> a
    

    所以Semigroup 是一个可以为普通类型定义的类,例如StringIntList aMap k v,甚至是函数a -> b(也可以写成(->) a b ) 但不是需要其他类型的类型构造函数,例如 List Map k (->) a (我必须在这里使用这个符号)。

    另一方面,Functor 类需要类型构造函数,因此您可以拥有 Functor ListFunctor (Map k)Functor ((->) a) 等实例。

    【讨论】:

    • 感谢您的详细解释。非常感谢!
    【解决方案2】:

    我找到了解决方案。而不是将其定义为:

    instance functorFoo :: Functor (Foo a) where
    

    我需要将其定义为:

    instance functorFoo :: Functor Foo where
    

    但我仍然想知道为什么第一个版本不起作用。就像 Functor 类型类与例如如何不同一样第一个版本运行良好的 Semigroup。

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 2015-10-27
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多