[Haskell-beginners] Enum and Bounded in generic type
mail at paulvisschers.net
Tue Dec 30 12:50:22 EST 2008
You seem to be having some trouble with the type system, not just in
this instance. I found when I was learning Haskell that when this
happens, it is useful to not add type annotations, then load it up in
GHCi and see what it comes up with (with the aforementions :t option).
Then you can see why that makes sense and possibly change the function.
Once you're happy with the function and are confident you understand its
type, go ahead and put the type annotation back into your source code.
I also found it useful to play around with various expressions and see
what their types where. Especially with things like partial application,
function composition and monadic functions.
Philippa Cowderoy wrote:
> [-cafe left out]
> On Tue, 2008-12-30 at 17:12 +0000, raeck at msn.com wrote:
>> Hi, how can I make the following code work? (show all the possible values of
>> a type 'a')
>>> showAll :: (Eq a, Bounded a, Enum a) => a -> [a]
>>> showAll a = [minBound..maxBound]::[a]
>> it keeps saying that I could fix the problem by adding (Enum a) and (Bounded
>> a) the the context, but I failed when I try.
> The problem is the inner annotation, ::[a]. It's not the same a as in
> (Eq a, Bounded a, Enum a) => a -> [a], and in Haskell98 it can't be. I'm
> not surprised that's confused you! If you remove the inner annotation it
> should work fine though.
> You can go a step further and make showAll a value rather than a
> showAll = [minBound..maxBound]
> (I've left out the type annotations, but you can put ":t
> [minBound..maxBound]" into ghci or hugs if you want one)
More information about the Beginners