Re: [Haskell-cafe] Generating random enums

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:
[michael@localhost ~]$ ghci dow
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( dow.hs, interpreted )
Ok, modules loaded: Main.
*Main> fromEnum Wednesday
Loading package old-locale-1.0.0.1 ... linking ... done.
Loading package old-time-1.0.0.1 ... linking ... done.
Loading package random-1.0.0.1 ... linking ... done.
2
*Main> toEnum 2
*** Exception: Prelude.Enum.().toEnum: bad argument
*Main>
Michael
--- On Fri, 5/1/09, John Van Enk

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'

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.

Rahul Kapoor
*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'
Wild guess: The dread monomorphism restriction uses () as a reasonable type, and it only has one element. Does 'toEnum 0' give '()' as the result? (I couldn't reproduce it with my version of GHCi - I get the ambigous type error message) -k -- If I haven't seen further, it is by standing in the footprints of giants

Am Samstag 02 Mai 2009 21:22:26 schrieb Ketil Malde:
Rahul Kapoor
writes: *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'
Wild guess: The dread monomorphism restriction uses () as a reasonable type, and it only has one element. Does 'toEnum 0' give '()' as the result?
Actually, it seems to be the other way round here: Prelude> toEnum 2 <interactive>:1:0: Ambiguous type variable `a' in the constraint: `Enum a' arising from a use of `toEnum' at <interactive>:1:0-7 Probable fix: add a type signature that fixes these type variable(s) Prelude> :set -fno-monomorphism-restriction <no location info>: Warning: -fno-monomorphism-restriction is deprecated: use -XNoMonomorphismRestriction or pragma {-# LANGUAGE NoMonomorphismRestriction#-} instead Prelude> toEnum 2 *** Exception: Prelude.Enum.().toEnum: bad argument
(I couldn't reproduce it with my version of GHCi - I get the ambigous type error message)
-k
participants (4)
-
Daniel Fischer
-
Ketil Malde
-
michael rice
-
Rahul Kapoor