[Haskell-beginners] type puzzlement

prad prad at towardsfreedom.com
Wed Jul 21 02:26:03 EDT 2010


i'm looking at a factoring function which i've defined as:

factorList = map factors
    where   factors p = [ f | f <- [1..fsq p], p `mod` f == 0 ]
            fsq :: (RealFrac a, Integral b, Floating a) => a -> b
            fsq = floor . sqrt

it goes into ghci just fine, but then when i give it something to go i
get errors:

*Main> :r 
Ok, modules loaded: Main.
*Main> factorList [4,8,16,32]

<interactive>:1:0:
    Ambiguous type variable `t' in the constraints:
      `RealFrac t' arising from a use of `it' at <interactive>:1:0-21
      `Integral t' arising from a use of `it' at <interactive>:1:0-21
      `Floating t' arising from a use of `it' at <interactive>:1:0-21
    Probable fix: add a type signature that fixes these type variable(s)


so i'm not sure where the unhappiness lies because there is no line
number.

i examine fsq more closely, because that looks like the most
complicated part of the whole thing and try to define it separately:

*Main> let fsq = floor . sqrt
*Main> :t fsq
fsq :: (RealFrac a, Integral b, Floating a) => a -> b

ok this seems weird to me because i would have thought things are far
simpler with 

fsq :: Int -> Int

however, i guess because we have a composition here, the compiler wants
the fact that 

sqrt :: (Floating a) => a -> a
floor :: (RealFrac a, Integral b) => a -> b

incorporated in the type?

but even if i put the 

fsq :: (RealFrac a, Integral b, Floating a) => a -> b

in to the factorList, i get errors, though the fsq works fine on its
own.

hence, i have a situation where the parts seem to work, but the whole
doesn't because there seems to be some problem with type?

-- 
In friendship,
prad

                                      ... with you on your journey
Towards Freedom
http://www.towardsfreedom.com (website)
Information, Inspiration, Imagination - truly a site for soaring I's


More information about the Beginners mailing list