haskell - 同源对的单子(monad)实例的已知/已建立用例

标签 haskell monads

有一次我问 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 aReader 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/

相关文章:

haskell - 为什么我的 Haskell 中的 gcd 函数仅在返回类型 Writer [String] Int 而不是 Writer String Int 时才起作用?

exception - 如何使用 unsafeInterleaveIO 处理异常?

haskell - 我如何在 Haskell 中显示派生树?

haskell - 可以在模式匹配中交换构造函数的通用和存在量词吗?

scala - Kiselyov zipper 的惯用 Scala 翻译?

haskell - IO monad 处理,简化绑定(bind)语法

haskell - cabal-install 不保留 happy 的版本

haskell - HTF 不测试 TH 生成的 Prop

haskell - Backwards 是否承认 Monad 实例?

haskell - 是否有一个 Monad 收集结果和 `mappend` s 它们?