有一次我问 this question , 被正确标记为 this other one 的副本.
现在我有一个好奇心,对于一对同质类型的 monad 实例是否有任何已知的用例?
以下是它的实例:
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 -> a
同构:
to (Pair f t) = \b -> if b then t else f
from f = Pair (f False) (f True)
因此,the Reader monad 的任何用例也是您的 monad 的潜在用例。这些数据类型的总称是可表示的仿函数。
关于haskell - 同源对的单子(monad)实例的已知/已建立用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66587591/