generated at
Applicative型クラス
pure を実装した型クラス
Functor型クラスを継承する



定義
purs(hs)
class Apply f <= Applicative f where pure :: forall a. a -> f a
Apply型クラスを継承する


Haskellの場合
hs
class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b
f a はここでは「ファンクター値」ではなく「アプリカティブ値」と呼ぶ


instance例
Maybe
hs
instance Applicative Maybe where pure = Just Nothing <*> _ = Nothing (Just f) <*> something = fmap f something
List
hs
instance Applicative [] where pure x = [x] fs <*> xs = [f x | f <- fs, x <- xs]
Either
hs
instance Applicative Either a where pure = Right
IO
hs
instance Applicative IO where pure = return a <*> b = do f <- a x <- b return (f x)
<*> では↓をやってるんだねmrsekut
hs
-- このdo式全体の結果は`IO 値` (IO 関数) <*> (IO 値) = do 関数 <- IO 関数 値 <- IO 値 pure (関数 値) -- ←これの結果が`IO 値`になる
(->) r
関数Applicativeに書いた






関連






参考
Bryan O'Sullivanのブログ
>Brent さんから Applicative スタイルを習ったのが分かって、微笑ましいです。 ref