Applicative型クラス
pureを実装した型クラス
Functor型クラスを継承する
定義
code:purs(hs)
class Apply f <= Applicative f where
pure :: forall a. a -> f a
Apply型クラスを継承する
Haskellの場合
code:hs
class (Functor f) => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
fはApplicative Functor
f aはここでは「ファンクター値」ではなく「アプリカティブ値」と呼ぶ
<*>
instance例
Maybe
code:hs
instance Applicative Maybe where
pure = Just
Nothing <*> _ = Nothing
(Just f) <*> something = fmap f something
List
code:hs
instance Applicative [] where
pure x = x
fs <*> xs = f x | f <- fs, x <- xs
Either
code:hs
instance Applicative Either a where
pure = Right
IO
code:hs
instance Applicative IO where
pure = return
a <*> b = do
f <- a
x <- b
return (f x)
<*>では↓をやってるんだねmrsekut.icon
code:hs
-- このdo式全体の結果はIO 値
(IO 関数) <*> (IO 値) = do
関数 <- IO 関数
値 <- IO 値
pure (関数 値) -- ←これの結果がIO 値になる
(->) r
関数Applicativeに書いた
関連
Applicative Style
アプリカティブ則
アプリカティブ
Applicativeは全ての引数が独立に計算される
参考
/haskell-shoen/Applicative
The Typeclassopedia
http://www.serpentine.com/blog/2008/02/06/the-basics-of-applicative-functors-put-to-practical-work/
Bryan O'Sullivanのブログ
Brent さんから Applicative スタイルを習ったのが分かって、微笑ましいです。 ref
https://kazu-yamamoto.hatenablog.jp/entry/20101227/1293419146
https://kazu-yamamoto.hatenablog.jp/entry/2019/04/11/111238
https://hirokif.hatenablog.com/entry/20161224/1482609665
https://medium.com/axiomzenteam/functor-applicative-and-why-8a08f1048d3d
https://doitaka.hatenadiary.org/entry/20130613/1371146740
https://xtech.nikkei.com/it/article/COLUMN/20120110/378061/
https://aratama.github.io/purescript/chapter07.html
https://viercc.github.io/blog/posts/2020-05-30-applicatives.html