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 <toa...@gmail.com> 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 <kkchensh...@gmail.com> 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