Haskell:需要了解Functor的签名 [英] Haskell: need to understand signature of Functor
问题描述
Prelude> :info Functor
class Functor(f :: * - > *)其中
fmap ::(a - > b) - > f a - > f b
(< $):: a - > f b - > fa
我不明白 *
的意思。
这意味着 f
是更高级的(在类型级别上考虑函数)
这里<$ c $ (第一个 *
),并产生另一个类型(第二个 *
你基本上可以忘掉所有这些,只需将其读为:
pre > class Functor f where
fmap ::(a - > b) - > f a - > f b
(< $):: a - > f b - > fa
但它是一个很好的IMO文档,有很多类更复杂, em> kind-signature 真的很有用 - 例如:
class MonadTrans(t ::(* - > ; *) - > * - > *)其中
lift :: Monad m => m a - > tma
- 在`Control.Monad.Trans.Class'中定义
这里 t
将一个类型构造函数本身(Monad)与另一个类型一起使用,并重新生成一个类型。
Can some body explain me the signature of Functor.
Prelude> :info Functor
class Functor (f :: * -> *) where
fmap :: (a -> b) -> f a -> f b
(<$) :: a -> f b -> f a
I don't understand what *
means.
*
is the syntax used by Haskell for kinds
In this case it means that f
is higher-kinded (think functions on the type level)
Here f
is taking one type (the first *
) and is producing another type (the second *
)
you can basically forget all this here and just read it as:
class Functor f where
fmap :: (a -> b) -> f a -> f b
(<$) :: a -> f b -> f a
but it's a nice documentation IMO and there are quite a few classes that are more complicated and the kind-signature is really helpful - for example:
class MonadTrans (t :: (* -> *) -> * -> *) where
lift :: Monad m => m a -> t m a
-- Defined in `Control.Monad.Trans.Class'
Here t
takes a type-constructor itself (the Monad) together with another type and produces a type again.
这篇关于Haskell:需要了解Functor的签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!