[Haskell-cafe] Generating random enums
daniel.is.fischer at web.de
Sat May 2 14:59:42 EDT 2009
Am Samstag 02 Mai 2009 20:00:54 schrieb Rahul Kapoor:
> > OK, I think what you're saying is to work with (random) integers and use
> > fromEnum and toEnum to get corresponding DayOfWeek. But I get this when I
> > try to use toEnum:
> > *Main> toEnum 2
> ghci does not know what type of enum you want to create from the number 2.
> Try: toEnum 2 :: DayOfWeek
> That said, I would expect "toEnum 2" to give an error like: 'Ambiguous
> type variable `a'....'. So I am not sure why your error message says:
> '** Exception: Prelude.Enum.().toEnum: bad argument'
Defaulting. If ghci doesn't know which type (let's call it a) is demanded, it looks at the
constraints (here Enum a) and tries to choose a type from its default list that satisfies
the constraints. AFAIK, the default list is (), Integer, Double. () satisfies the
constraint, so ghci chooses that. fromEnum () is 0, so 0 is the only acceptable argument
for toEnum :: Int -> ().
It's a two-edged sword, if ghci didn't default, it would have to issue a *lot* of
ambiguous type messages and you would have to give a type signature to most expressions
you type at the prompt, with the defaulting, you get pretty unintuitive error messages
when it does the wrong thing.
More information about the Haskell-Cafe