Haskell type confusion

I do not understand why the following function works:

isLongerThanN :: Integral n => n -> [a] -> Bool
isLongerThanN n xs = length xs > fromIntegral n

but the following:

isLongerThanN' :: Integral n => n -> [a] -> Bool
isLongerThanN' n xs = length xs > n

which gives an error

Could not deduce (n ~ Int)
    from the context (Integral n)
      bound by the type signature for
                 isLongerThanN' :: Integral n => n -> [a] -> Bool
      at blah.hs:140:1-35
      `n' is a rigid type variable bound by
          the type signature for
            isLongerThanN' :: Integral n => n -> [a] -> Bool
          at blah.hs:140:1
    In the second argument of `(>)', namely `n'
    In the expression: length xs > n
    In an equation for `isLongerThanN'':
        isLongerThanN' n xs = length xs > n

(which I probably misunderstood)

Anyway, I expect it to be the other way around, since fromIntegral effectively extends the type of the variable n.

+5
source share
1 answer

Consider an expression that does not work

isLongerThanN' :: Integral n => n -> [a] -> Bool
isLongerThanN' n xs = length xs > n

n -y, Integer Word Int. (>) Ord a => a -> a -> Bool, . length xs Int, . n Integral, Int, - n Int. , fromIntegral ( , n Num, ).

, :

toInt :: Integral n => n -> Int
toInt = fromIntegral

isLongerThanN :: Integral n => n -> [a] -> Bool
isLongerThanN n xs = length xs > toInt n

, fromIntegral.

( , isLongerThanN n xs = fromIntegral (length xs) > n , length n.)

+12

All Articles