Monad
can pass Just [1,2]
, which is a different type from what the original length
function takes, to >>= return . length
.
Just [1,2] >>= return . length
Can I say that Monad
makes it possible to see Maybe [a]
as isomorphic with [a]
on length using (>>=, return)
? (Of course they are not really isomorphic.)
Can I choose the term "isomorphic" this situation?
What your example ultimately illustrates is that
Maybe
is a functor: if you have somef :: a -> b
, you can usefmap
to turn it intofmap f :: Maybe a -> Maybe b
in a way that preserves identities and composition. Monads are functors, with\f m -> m >>= return . f
being the same asfmap f m
. In your case, we have thelength
function being transformed by theMaybe
functor.Not really.
fmap
forMaybe
is not an isomorphism. An isomorphism requires there being a two-sided inverse that undoes it, which in this case would be something like:However, there are no
(Maybe a -> Maybe b) -> (a -> b)
functions, as there is no way to obtain ab
result if the inputMaybe a -> Maybe b
function gives out aNothing
. While there are specific functors whosefmap
is an isomorphism (Identity
is one example), that is not the case in general.