同类对的monad实例的已知/已确定用例 [英] Known/estabilished usecases for the monad instance of an homogeneous pair
本文介绍了同类对的monad实例的已知/已确定用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一旦我问了这个问题,已正确标记为
Once I asked this question, which was correctly marked as duplicate of this other one.
现在我有好奇心,一对同类类型的monad实例是否有任何已知用例?
Now I have a curiosity, is there any known usecase for monad instance of a pair of homogeneous types?
这是它的实例:
data Pair a = Pair a a deriving Show
instance Functor Pair where
fmap f (Pair a b) = Pair (f a) (f b)
instance Applicative Pair where
pure a = Pair a a
Pair f g <*> Pair x y = Pair (f x) (g y)
instance Monad Pair where
m >>= f = joinPair (f <$> m)
joinPair :: Pair (Pair a) -> Pair a
joinPair (Pair (Pair x _) (Pair _ y)) = Pair x y
推荐答案
您的 Pair a
与 Reader Bool a
/ Bool->同构;一个
:
to (Pair f t) = \b -> if b then t else f
from f = Pair (f False) (f True)
因此,读者monad 也是您monad的潜在用例.这些类型的数据类型的通用术语是可表示的函子.
As such, any use-case for the Reader monad is also a potential use-case for your monad. The general term for these sorts of data types is representable functors.
这篇关于同类对的monad实例的已知/已确定用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文