[Haskell-beginners] type of zipWithPlus

Daniel Fischer daniel.is.fischer at web.de
Thu Sep 30 15:48:30 EDT 2010

On Thursday 30 September 2010 20:59:28, Russ Abbott wrote:
> When I define zipWithPlus at the top level of GHCi, the type is as
> shown.
> > let zipWithPlus = zipWith (+)
> zipWithPlus :: [Integer] -> [Integer] -> [Integer]
> Why isn't it:  (Num a)  => a -> a-> a

It's the monomorphism restriction. If you bind a name without function 
arguments at the prompt (or at the top-level of a module if the name is 
exported) and without a type signature, it is given a monomorphic type.

Type inference yields the polymorphic type, then it is monomorphised 
according to the defaulting rules. The default for Num constraints is 
Integer, hence you get zipWithPlus :: [Integer] -> [Integer] -> [Integer]

>   I was unable to find a way to get the type to be more general.

Three ways.

1. bind it with a type signature
ghci> let zipWithPlus :: Num a => [a] -> [a] -> [a]; zipWithPlus = zipWith 
ought to work (if it doesn't submit a bug report)

2. bind it with arguments
ghci> let zipWithPlus xs ys = zipWith (+) xs ys
ghci> let zipWithPlus xs = zipWith (+) xs
both ought to work (if not, bug report)

3. disable the monomorphism restriction

$ ghci -XNoMonomorphismRestriction


ghci> :set -XNoMonomorphismRestriction

disable the MR and give you the polymorphic type without type signatures or 

It's probably a good idea to put

:set -XNoMonomorphismRestriction

into your ~/.ghci

>   I tried various declarations within the let using
>   (Num a)  => a -> a-> a
> but none of them were accepted.
> Thanks.
> -- Russ Abbott

More information about the Beginners mailing list