Note also that typeclasses are open, so ghc is not allowed to say that there is 
no instance of Num for lists there; it will happily infer a type which requires 
such an instance, and only when it needs to firm down to concrete types at some 
point will it notice that there's no such instance in scope.  (I think some 
such instances do exist, in fact, in various programs.)  Just to make things 
more interesting, numeric literals are not sufficient to make it think 
otherwise because of the implicit fromIntegral / fromRational, which a Num 
instance for lists would need to supply at the appropriate type.

FlexibleContexts is because the Haskell standard is extremely pedantic about 
the form that typeclass instances and contexts may take; I think if you had 
written it as (Num ([] a)) it would have passed without an extension.  On the 
one hand, it's something of an irritation; on the other, it *does* help to 
catch thinkos like the above. 

-- 
brandon s allbery kf8nh
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


On Friday, February 15, 2013 at 2:33 AM, sheng chen wrote:

> Hi,
> 
> I was puzzled by the following little program.
> 
> sum' [] = []
> sum' (x:xs) = x + sum' xs
> 
> I thought the GHC type checker will report a type error. However, the type 
> checker accepts this program and gives the type 
> 
> Num [a] => [[a]] -> [a]
> 
> When I add type annotation to the program
> 
> sum' :: Num [a] => [[a]] -> [a]
> sum' [] = []
> sum' (x:xs) = x + sum' xs
> 
> The GHC asks me to add FlexibleContexts language extension.
> 
> I would appreciate explanation on this issue.
> 
> Thanks,
> Sheng
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org (mailto:Haskell-Cafe@haskell.org)
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to