when I type
:t map length . sum
into GHCi, it says that the type would be:
map length . sum :: Num [[a]] => [[[a]]] -> [Int]
However, if I create a file type-test.hs containing
x :: Num [[a]] => [[[a]]] -> [Int]
x = map length . sum
both ghc and ghci complain:
type-test.hs:1:1:
Non type-variable argument in the constraint: Num [[a]]
(Use -XFlexibleContexts to permit this)
In the type signature for `x': x :: Num [[a]] => [[[a]]] -> [Int]
Why does ghci allow me to infer the type for this (using :t), when FlexibleContexts are not enabled?
(This doesn’t answer your original question, but solves the problem with the code instead)
These errors hint at that your code as written probably isn’t what you meant. This code:
means “Take my list of numbers, and sum it, then compute the length of each element(??) of the resulting number.” That makes no sense.
You probably meant:
which means “Take my list of lists, compute the length of every element, and sum the lengths.”
What the error message itself means is that since
sumreturns a number, aka the type ofsumisNum n => [n] -> n, and you then try to usemap lengthon that, which has typeNum m => [[a]] -> [m], the compiler tries to say thatn == [[a]]to make the types match, and everything goes downhill from there.