[Haskell-cafe] Scary type inference for monadic function definitions

Ahn, Ki Yung kyagrd at gmail.com
Thu Jun 4 01:39:03 EDT 2009


Scary type inference for monadic function definitions
(or, why you'd want to annotate types for monadic function definitions)

This is a real example that I've experienced.

I defined the following function.

> checkOneVerseByLineWith readLine v =
>   do mg <- readLine
>      case mg of
>        Just g  -> return Just (v==g)
>        Nothing -> return Nothing

My intention was to use it something like this:

checkOneVerseByLineWith (readline "% ")

where readline is the library function from System.Console.Readline.

As you can see, there is an obvious mistake which I forgot to
group (Just (v==g)) in parenthesis.  However, GHC or any other
Haskell 98 compliant implementation will infer a type for you
and this will type check!  Try it yourself if in doubt.

Of course, checkOneVerseByLineWith (readline "% ") won't type check
because checkOneVerseByLineWith has strange type.  The reason why
the above definition type checks is because ((->) r) is an instance
of Monad.



More information about the Haskell-Cafe mailing list