Scary type inference for monadic function definitions

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.

Ahn, Ki Yung wrote:
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
How about liftM (fmap (v==)) readLine ? -- Mit freundlichen Gruessen Henning Thielemann Viele Gruesse Henning Martin-Luther-Universitaet Halle-Wittenberg, Institut fuer Informatik Tel. +49 - 345 - 55 24773 Fax +49 - 345 - 55 27333
participants (2)
-
Ahn, Ki Yung
-
Henning Thielemann