Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble understanding Haskell function

Tags:

haskell

monads

I'm having trouble understanding why the following code correctly compiles:

f :: a -> Maybe a
f = return Just 3

return Just has a type of Monad m => m (a -> Maybe a) so I'm not sure why passing in an Int unwraps the monad.

You can even get a little crazy with it:

f :: a -> Maybe a
f = return Just (Just . Just . Just . Just)

Can someone explain what exactly is going on here?

like image 355
noatbfgtxa Avatar asked Aug 31 '26 23:08

noatbfgtxa


1 Answers

You're using return from the reader monad (->) Int, which is defined as const. In this case return Just has type Int -> a -> Maybe a.

f = return Just 3
  = const Just 3
  = Just
like image 115
Naïm Favier Avatar answered Sep 02 '26 16:09

Naïm Favier