r/haskellquestions • • Apr 29 '23

Monadic bind understanding problem

I am puzzled why the following works correctly.

ghc> Identity 4 >>= (*10) >>= (+3)
Identity 43

Neither (*10) nor (+3) return an Identity value.

ghc> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
18 Upvotes

4 comments sorted by

3

u/ZeroidOne Apr 30 '23

Many thanks, both of you, u/Iceland_jack & u/gabedamien.
Haven't encountered this before in years.
Overloading still presents some "mysteries" to me. Need to give it a closer look, certainly. For sure not the only topic of importance.
That's the "fun" of Haskell. Learning never stops. Challenges neither. :-)

3

u/Hjulle Apr 30 '23

i think using :set +t can help with demystifying some of these by automatically showing the type of each expression you write in ghci.

if you use haskell-language-server in your editor it can give even more hints since you can hover over a subexpression and get its concrete type in context

14

u/Iceland_jack Apr 29 '23

It's a sensible thing to be puzzled at. Numeric overloading can certainly be confusing.

Your initial monadic expression has type

as :: Identity (Identity (Identity Integer))
as = Identity 4

where 4 is overloaded

4 :: Identity (Identity Integer)

This causes (* 10) to work over Identity of Identity

timesTen :: Identity (Identity Integer) -> Identity (Identity Integer)
timesTen = (* 10)

and (+ 3) to work over Identity

plusThree :: Identity Integer -> Identity Integer
plusThree = (+3)

Resulting in join-like behaviour, remember that join = (>>= id), where each bind collapses one level of the monadic structure

>> as
Identity (Identity (Identity 4))
>> as >>= timesTen
Identity (Identity 40)
>> as >>= timesTen >>= plusThree
Identity 43

11

u/gabedamien Apr 29 '23 edited Apr 29 '23

Actually, (*10) can return an Identity, because Identity has a Num instance. Identity also has a Monad instance, so everything here just works out. EDIT: also, confusingly, since Identity has a Num instance, literal 4 can actually mean Identity 4! So Identity 4 can mean (and here, does mean) Identity (Identity 4)! How fun.

instance Monad Identity
instance Num n => Num (Identity n)

(*10) :: Num x   =>        x            -> x
(*10) :: Num n   =>        (Identity n) -> (Identity n)
(>>=) :: Monad m => m a -> (a           ->  m        b) -> m b

m   ~          Identity
m a ~ Num n => Identity (Identity n)
  a ~ Num n =>           Identity n
  4 =                    Identity 4
b   ~ Num n =>                    n