Hopefully this will be the first in a series of posts about monad transformers. I’m not saying anything new here and there are other resources online that go through this area starting from more basic principles. My goal is to give an overview of transformers and then to explore some of the transformer libraries in later posts.
I’m trying to keep things short and sweet so it’ll be mostly code with a few comments:
module TransformersPart1 where
import Text.Read (readMaybe)
-- plain old Maybe example (avoiding division by zero)
ex1 :: Int -> Int -> Maybe Int
ex1 x y | y > 0 = Just (x `div` y)
| otherwise = Nothing
-- now we have two Monad layers. Maybe and IO.
ex2 :: IO (Maybe Int)
ex2 = readMaybe <$> getLine
-- using the previous example, we now want to map the Int to a String
-- we fmap twice; once on the IO and then on the Maybe.
ex3 :: IO (Maybe String)
ex3 = fmap (fmap show) ex2
-- we can actually do this for any two functors though
-- because functors compose
ex4 :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
ex4 f a = fmap (fmap f) a
-- but when we try to bind we have issues
-- we can do it if we know about one of the Monads.
ex5 :: Monad m => m (Maybe a) -> (a -> m (Maybe b)) -> m (Maybe b)
ex5 a f = a >>= (maybe (return Nothing) f)
-- but you can't write bind for any two arbitrary monads
ex6 :: (Monad m, Monad n) => m (n a) -> (a -> m (n b)) -> m (n b)
ex6 = error "try implementing it"
-- With transformers, you have a monad instance for the transformer
-- where there's a concrete monad and any other monad on top. Hence
-- Monad transformers such as MaybeT, EitherT, etc. The monad instance has to
-- be defined with regard to some known, conrete monad + an arbitrary monad.
-- the type is `newtype MaybeT m a` so we can use `ex2` to construct one
ex7 :: MaybeT IO Int
ex7 = MaybeT $ ex2
-- the monad instance for MaybeT lets us to a single bind
ex8 :: MaybeT IO Int
ex8 = do
x <- ex7
return (x + 1)
The HBO show “Silicon Valley” is mostly parody, but there are some valuable lessons to glean from the jokes. In the first few minutes of season 1 episode 1, we learn exactly why you need a co-founder. Erlich pulls Richard aside to see how things are going with Pied Piper:
Now, Richard, when you pitched me Pied Piper you said it was gonna be, “the Google of music.” Which is a really rad way to pitch something. I mean, I liked it. I thought it had “applications”.
No, it has all that. Look, when it blows up, and it will once it reaches a critical mass of users, Pied Piper will be able to search the whole world of recorded music to find out if there’s a match to see if you’re infringing on any copyrighted material. So, if you’re a song writer or a band..
Ok, first of all, nobody gives a shit about stealing other people’s music, ok? Everybody involved in the music industry is either stealing it or sharing it. They’re all a bunch of assholes, especially Radiohead.
Richard is just merrily trekking along, applying his talents…to solve a problem that no one cares about. Getting out of your own headspace and talking to someone, anyone, is essential. That’s why you need a co-founder. You can go on for years working on something that no one cares about and solving problems that don’t need to be solved. It’s not atypical for a two minute conversation with someone else to keep you from going off the rails.