Re: [Haskell-cafe] (+) on two lists ?
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
Re: [Haskell-cafe] (+) on two lists ?
On 15 February 2013 19:22, Raphael Gaschignard wrote: > Out of curiosity, what is the rationale for allowing programs to infer > certain types, but not for the (inferred) type to be declared? That's the type that's needed; the fact that you need an extension for GHC to allow you to use it is irrelevant. > > > > On Fri, Feb 15, 2013 at 4:54 PM, David McBride wrote: >> >> sum' [] = [] -- returns a list of something the way you intended >> sum' (x:xs) = x + xum' xs -- you intended it not to return a list but it >> could if you think about it. >> >> The compiler says I think returns a list based on what I see so far, well >> if you can add these together then the only way you could get a list from >> that is if you were adding two lists together ie (+ l1 l2) :: [a] -> [a] -> >> [a]. That works if we assume that sum' must have accepted [[a]] and >> returned [a]. >> >> But in order for that to be the case [a] must be an instance of Num, >> otherwise they couldn't be added together like that, so tack on a Num [a] >> requirement on. >> >> But having a typeclass of the form [a] that requires an extension, >> FlexibleContexts, which you can read about here: >> http://www.haskell.org/ghc/docs/7.0.2/html/users_guide/other-type-extensions.html#flexible-contexts >> >> On Fri, Feb 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 >>> http://www.haskell.org/mailman/listinfo/haskell-cafe >>> >> >> >> ___ >> Haskell-Cafe mailing list >> 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 > -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com http://IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] (+) on two lists ?
Out of curiosity, what is the rationale for allowing programs to infer certain types, but not for the (inferred) type to be declared? On Fri, Feb 15, 2013 at 4:54 PM, David McBride wrote: > sum' [] = [] -- returns a list of something the way you intended > sum' (x:xs) = x + xum' xs -- you intended it not to return a list but it > could if you think about it. > > The compiler says I think returns a list based on what I see so far, well > if you can add these together then the only way you could get a list from > that is if you were adding two lists together ie (+ l1 l2) :: [a] -> [a] -> > [a]. That works if we assume that sum' must have accepted [[a]] and > returned [a]. > > But in order for that to be the case [a] must be an instance of Num, > otherwise they couldn't be added together like that, so tack on a Num [a] > requirement on. > > But having a typeclass of the form [a] that requires an extension, > FlexibleContexts, which you can read about here: > http://www.haskell.org/ghc/docs/7.0.2/html/users_guide/other-type-extensions.html#flexible-contexts > > On Fri, Feb 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 >> http://www.haskell.org/mailman/listinfo/haskell-cafe >> >> > > ___ > Haskell-Cafe mailing list > 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
Re: [Haskell-cafe] (+) on two lists ?
sum' [] = [] -- returns a list of something the way you intended sum' (x:xs) = x + xum' xs -- you intended it not to return a list but it could if you think about it. The compiler says I think returns a list based on what I see so far, well if you can add these together then the only way you could get a list from that is if you were adding two lists together ie (+ l1 l2) :: [a] -> [a] -> [a]. That works if we assume that sum' must have accepted [[a]] and returned [a]. But in order for that to be the case [a] must be an instance of Num, otherwise they couldn't be added together like that, so tack on a Num [a] requirement on. But having a typeclass of the form [a] that requires an extension, FlexibleContexts, which you can read about here: http://www.haskell.org/ghc/docs/7.0.2/html/users_guide/other-type-extensions.html#flexible-contexts On Fri, Feb 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 > http://www.haskell.org/mailman/listinfo/haskell-cafe > > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] (+) on two lists ?
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 http://www.haskell.org/mailman/listinfo/haskell-cafe