Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Known/estabilished usecases for the monad instance of an homogeneous pair

Tags:

haskell

monads

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
like image 802
Enlico Avatar asked Mar 02 '23 16:03

Enlico


1 Answers

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.

like image 108
Joseph Sible-Reinstate Monica Avatar answered Apr 25 '23 11:04

Joseph Sible-Reinstate Monica