一旦我问了这个问题,这个问题就被正确地标记为与另一个问题重复。
现在我很好奇,一对同构类型的 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
Run Code Online (Sandbox Code Playgroud)
你的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)
Run Code Online (Sandbox Code Playgroud)
因此,Reader monad的任何用例也是您的 monad 的潜在用例。这些数据类型的通用术语是可表示函子。
| 归档时间: |
|
| 查看次数: |
166 次 |
| 最近记录: |