
Hi, Looking at the definition of return function for a Monad, it is described as - return :: a -> m a However when I do - return (Just 3) The result is Just 3 and not Just (Just 3) If I understand the definition correctly (which doesn't seem to be the case) the return function should wrap the parameter (which in my case is 'Just 3') into the Maybe Monad and therefore should return me 'Just (Just 3)' Please can someone help explain. Thanks, Shishir Srivastava +44 (0) 750 127 5019

Hi, On Tue, 21 Apr 2015, at 12:16 PM, Shishir Srivastava wrote:
Hi,
Looking at the definition of return function for a Monad, it is described as -
return :: a -> m a
However when I do -
return (Just 3)
The result is
Just 3
and not
Just (Just 3)
If I understand the definition correctly (which doesn't seem to be the case) the return function should wrap the parameter (which in my case is 'Just 3') into the Maybe Monad and therefore should return me 'Just (Just 3)'
You haven't been clear about what environment you are "doing" `return (Just 3)`. My guess is that you are running it in GHCi, which means that it gets coerced to the IO monad. If you add an explicit type declaration, you get the wrapping as expected: Prelude> :t return (Just 3) return (Just 3) :: (Num a, Monad m) => m (Maybe a) Prelude> return (Just 3) :: Maybe (Maybe Int) Just (Just 3) Thanks, Josh

My guess is that you are running it in GHCi, which means that it gets coerced to the IO monad.
"Coercion" doesn't quite capture it. Also it happens that the haskell research literature uses it in a technically specific context that doesn't apply here. What's going on in return (Just 3) being _executed_ in ghci to produce Just 3 is the REPL's defaulting to IO kicking in. p.s. In the above, I've made distinct execution from evaluation. This helps folks navigate the beginner-hazardous terrain of overloaded English. -- Kim-Ee
participants (3)
-
Josh Holland
-
Kim-Ee Yeoh
-
Shishir Srivastava