我明白(->) a是一种* -> *高阶类型,当应用于类型参数b给a -> b 我可以写一个类型的一种* -> *,当应用于c会给a -> b -> c类型? 如果不是,为什么不呢?也许使用一些语言扩展和forall? 这将让我写函子与应用型(和其它类),其中函子结构是“a -> b ->”的实例如下所示: (<*>) :: Applicative t => t (c -> d) -> t c -
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
Const :: a -> Const a b
Monoid m => Applicative (Const * m)
Const "a" <*> Const "b" -- yields Const "ab"
Const a <*> Const a = Const a <> Const
如果我有一个功能(例如类型a -> b的)包裹在一个Applicative和可施加到其上的值(即在上述示例中a类型的值),I可以如下应用它: doSomething :: Applicative f => f (a -> b) -> a -> f b
doSomething wrappedFn arg = wrappedFn <*> (pure arg)
我发现自己做了很多。是否有标准的预定
我有以下代码: {-# LANGUAGE DeriveFunctor #-}
data Foo a = Foo { field1 :: a, field2 :: a} deriving (Functor)
instance Applicative Foo where
pure a = Foo a a
f <*> a = Foo (r field1) (r field2)
的确如此: λ :i Applicative
class Functor f => Applicative (f :: * -> *) where
同时: fmap f x = pure f <*> x
- 由Applicative规律,我们可以从pure & <*>定义fmap。 我不明白为什么我要我要一个Applicative如果,真的,fmap可以自动在pure和<*>规定设立每次