haskellmonadsisomorphic

Can I say that Monad makes it possible to see some types as isomorphic?


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?


Solution

  • What your example ultimately illustrates is that Maybe is a functor: if you have some f :: a -> b, you can use fmap to turn it into fmap 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 as fmap f m. In your case, we have the length function being transformed by the Maybe functor.

    can I choose term "isomorphic" this situation?

    Not really. fmap for Maybe is not an isomorphism. An isomorphism requires there being a two-sided inverse that undoes it, which in this case would be something like:

    unFmapMaybe :: (Maybe a -> Maybe b) -> (a -> b)
    
    -- For it to be a two-sided inverse to `fmap`, we should have:
    unFmapMaybe . fmap = id
    fmap . unFmapMaybe = id
    

    However, there are no (Maybe a -> Maybe b) -> (a -> b) functions, as there is no way to obtain a b result if the input Maybe a -> Maybe b function gives out a Nothing. While there are specific functors whose fmap is an isomorphism (Identity is one example), that is not the case in general.