r/haskellquestions • u/ZeroidOne • Apr 29 '23
Monadic bind understanding problem
I am puzzled why the following works correctly.
ghc> Identity 4 >>= (*10) >>= (+3)
Identity 43Neither (*10) nor (+3) return an Identity value.
ghc> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b14
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 4where 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 4311
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
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. :-)