7

I am using this implementation of maybeRead:

maybeRead :: (Read a) => String -> Maybe a
maybeRead = fmap fst . listToMaybe . filter (null . dropWhile isSpace . snd) . reads

and my own getNum function which prompts until it gets valid input:

getNum :: (Num a, Read a) => String -> IO a
getNum s = do
    putStr s
    input <- fmap maybeRead getLine
    if isNothing input
        then getNum s
        else return $ fromJust input

However if I enter 5.2 it treats it as bad input—Why? There is zero occurences of Int and Integer in my code. I am only using Num, since I want to accept any kind of number.

If I call it explicitly as getNum "Enter a number: " :: IO Double, then it works. Do I have to do this? Is Haskell's type system just tricking me into thinking I should be able to do this when really it is impossible without full dynamic typing? If so then why does my code even compile; why does it assume integers?

2
  • 2
    Small stylistic note: It's better to use pattern matching than isNothing and fromJust, so the if expression can be replaced with case input of Nothing -> getNum s; Just x -> return x. Commented Mar 4, 2012 at 12:29
  • Thanks, that does look a lot better. Commented Mar 4, 2012 at 19:43

1 Answer 1

14

Your function will indeed accept Integer, Float, or any other Num instance. However, which type it accepts, and by extension how it parses the String, is not determined by the input it receives, it's determined by what type the result should be based on what you do with it.

Say you use getNum and pass the resulting value to something that needs a Float; in that case, it will parse a Float value. If you pass it to something that needs an Integer instead, it will parse that.

As for why it assumes Integer, there is a "defaulting" system for ambiguous types specified in the Haskell Report, and the rules say that an ambiguous type with a Num constraint should default to Integer.

Sign up to request clarification or add additional context in comments.

7 Comments

Okay, so I guess in my example I would have to add :: (Fractional a, Read a) => IO a when I call getNum to allow for fractional numbers (and I don't want to explicitly specify float or double).
Yes, but note that adding a Fractional constraint will limit it to accepting only instances of Fractional, so you wouldn't be able to use it for other types that way. Most of the time in an actual program you'll know a specific type you need, so usually there's no reason to constrain things, except for stuff like testing in GHCi.
I didn't add it to the actual function, I added that as a type annotation when I use the function. I'm not quite sure I understand what you're saying in the last sentence—aren't typeclass constraints ubiquitous in Haskell code for generalizing functions?
Ah. Sorry, I misread. Anyway, all I was saying is that adding extra constraints or explicit type annotation is rarely necessary in practice, because eventually you'll pass the output of getNum to something that expects a particular type, and type inference will handle it from there.
I've changed it to that, and I'm also just using default (Double) instead of that long type annotation since this is very small module and I think it's more logical that way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.