Once I asked this question, which was correctly marked as duplicate of this other one.
Now I have a curiosity, is there any known usecase for monad instance of a pair of homogeneous types?
Here are its instances:
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
Your Pair a
is isomorphic to Reader Bool a
/Bool -> a
:
to (Pair f t) = \b -> if b then t else f
from f = Pair (f False) (f True)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With