tags:

views:

110

answers:

2

Specifically, I want to do something like this :

myfunc:: [(Integer, Integer)]
myfunc = [(x^2 - y^2, 2 * x * y) | x <- [1..], y <- [1.. (x-1)]]

When I try to load this through ghci I get

    Warning: Defaulting the following constraint(s) to type `Integer'
         `Integral t' arising from a use of `^' at myfunc.hs:76:20-22

So I know that x should obviously be an Integer, how do I go ahead and let Haskell know thats what I want ?

+10  A: 

The problem here is that ^ has type (Num a, Integral b) => a -> b -> a, so the second argument to ^ can be any integral type. Because the result type is Integer Haskell knows that a is Integer, but it knows nothing about b. Not knowing what to do, it defaults to Integer and produces the warning. Since this is perfectly ok in this case, you can just ignore or disable the warning.

However if you'd rather add type annotations to make explicit that you want the right operand to have type Integer, you can do so like this:

(x^(2::Integer) - y^(2::Integer), 2 * x * y)

As a sidenote: [] creates a list, not a set.

sepp2k
Yes, a list. It is typically referred to as "list comprehension".
TomMD
thanks, re-writing it as x*x made the warning go away.
agam
+1  A: 

To be more specific, the warning is about the 2, not about x (or y).

KennyTM's suggestion or writing it as (x*x - y*y, ...) will make the warning go away.

Also, note that it is a warning - your system seems set up to give as many warnings as possible ("-Wall") in your programs, which is a great way to get a grip on what the compiler thinks you're doing. Since it is just a warning, you can feel free to ignore it; it'll do what you intended. It is good to get a human being's input on why the warning came up, to better learn the system.

BMeph