How I Learned to Stop Worrying And Love Haskell's Type Inference
In developing the HAppS example that I have been posting here, I
came upon a problem that gave me new insight to Haskell. I don't
think it was a particularly deep insight, but it is significant to me
as someone new to the language, and new to type inference.Consider a user authentication function that retrieves a map from the
reader monad, looks up a username, and compares the password retrieved
with a passed in parameter. I first implemented this function
something like this:
data User = User {
username :: String,
password :: String
}
authUser name pass = do
u <- liftM (M.lookup name) ask
liftM2 (==) (liftM password u) (return pass)
In the process of getting there, I stumbled around rearranging and
inserting various liftM calls until I finally got it to work.
Many of the reasons behind the type errors still seemed like voodoo to
me. And my haphazard approach to fixing them is evident looking at
the code. The problem with th…