Why does Haskell point free version of function result in ambiguous type error? -


it turns out in ghc 7.10, compiles fine:

mysum xs = foldr (+) 0 xs 

but this:

mysum    = foldr (+) 0 

results in following error:

no instance (foldable t0) arising use of ‘foldr’ type variable ‘t0’ ambiguous relevant bindings include   mysum :: t0 integer -> integer (bound @ src/main.hs:37:1) note: there several potential instances:   instance foldable (either a) -- defined in ‘data.foldable’   instance foldable data.functor.identity.identity     -- defined in ‘data.functor.identity’   instance foldable data.proxy.proxy -- defined in ‘data.foldable’   ...plus 5 others in expression: foldr (+) 0 in equation ‘mysum’: mysum = foldr (+) 0 

why happen, , insight that's achieved understanding difference? also, can give function type (that's still generic) make error go away?

as usual cases making well-typed function point-free results in type errors unfulfilled typeclass constraints, ultimate cause of monomorphism restriction, enabled default.

you can solve either adding type signature mysum:

mysum :: (foldable f, num a) => f -> 

or turning off monomorphism restriction:

{-# language nomonomorphismrestriction #-} 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -