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?
Your function will indeed accept
Integer,Float, or any otherNuminstance. However, which type it accepts, and by extension how it parses theString, 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
getNumand pass the resulting value to something that needs aFloat; in that case, it will parse aFloatvalue. If you pass it to something that needs anIntegerinstead, 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 aNumconstraint should default toInteger.