Re: Scary type inference for monadic function definitions

Ahn, Ki Yung 쓴 글:
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.
Oh, I happened to be importing Control.Monad.Trans somehow, it just doesn't work with Prelude import itself. And, there were already some discussions on this last year: [Haskell-cafe] The danger of Monad ((->) r) http://www.mail-archive.com/haskell-cafe@haskell.org/msg23680.html Tomasz Zielonka Tue, 15 May 2007 03:05:30 -0700
participants (1)
-
Ahn, Ki Yung