I was so sleepy that I wrote the following code (modified to just show the confusion):
fac s = take 10 [s, s `mod` 1 ..]
maxFactor x = if (s == [])
then x
else head <-- this should be 'head x' instead of just 'head'
where s = fac x
However, this load into ghci (and compiles) just fine. When I executed maxFactor 1, it complains (of course):
<interactive>:0:1:
No instance for (Integral ([a0] -> a0))
arising from a use of `maxFactor'
Possible fix:
add an instance declaration for (Integral ([a0] -> a0))
In the expression: maxFactor 1
In an equation for `it': it = maxFactor 1
<interactive>:0:11:
No instance for (Num ([a0] -> a0))
arising from the literal `1'
Possible fix: add an instance declaration for (Num ([a0] -> a0))
In the first argument of `maxFactor', namely `1'
In the expression: maxFactor 1
In an equation for `it': it = maxFactor 1
However, I don’t understand this behavior:
fac‘s type is:
fac :: Integral a => a -> [a]
while maxFactor‘s type is:
maxFactor :: Integral ([a] -> a) => ([a] -> a) -> [a] -> a
Doesn’t this mean the following:
- the first input to
facmust be of typeclassIntegral(e.g.,fac 10); - since in the definition of
maxFactor, there isfac x, x must also be of typeclassIntegral, thus,maxFactor‘s type would be begin with something likemaxFactor :: (Integral a) => a ->… then something else? However, if that is the case, then why this code compiles since the return ofmaxFactorcan bexorhead, which when following this line of reasoning, does not have the same type?
What am I missing here?
Thanks for any inputs in advance!
in
maxFactorthe compiler infers that the function argumentxnecessarily has the same type ashead(in yourifclause). Since you also callfaconx(which in turn callsmod) it infers thatxis also someIntegralclass type. Consequently, the compiler infers the typethat takes some
head-like and integer-like argument… which is unlikely to be a real thing.I think the fact that you can load that code into GHCi is more a quirk of the interpreter. If you were just compiling the code above, it would fail.Edit: I guess the issue is that the type checker can make sense of your code, however there probably isn’t any sensible way to use it.