import Data.List (genericLength)
len = genericLength
:t genericLength
genericLength :: (Num i) => [b] -> i
:t len
len :: [b] -> Integer
Why is the type of len different from type of genericLength? The intent here is to use a shorter alias for genericLength.
Aren’t functions first-class in haskell? Shouldn’t giving another name for a function result in an identical function?
What you’re seeing here is because of a requirement that top-level declarations with no arguments be monomorphic. You can find some discussion of the reasons for this on the Haskell wiki, and some information about controlling this behavior in the GHC user’s guide.
As illustration, note that giving
lenan argument fixes the problem:So does giving it a type signature:
And so does turning off the monomorphism restriction:
In this specific case, I think you’re also getting a different type (rather than a compiler error) because of defaulting rules that specify that certain type classes should default to specific types (in this case,
Numdefaults toInteger. If you try doing the same thing withfmapyou get this:You can find some information about defaulting in the Haskell 98 Report. I’ll also mention that GHC supports an extended form of defaulting that’s mostly used for GHCi (and is enabled there by default), which occasionally confuses people.