
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 I was unable to find a way to get the type to be more general. I tried various declarations within the let using (Num a) => a -> a-> a but none of them were accepted. Thanks. -- Russ Abbott ______________________________________ Professor, Computer Science California State University, Los Angeles Google voice: 424-242-USA0 (last character is zero) blog: http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ ______________________________________

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 and ghci> let zipWithPlus xs = zipWith (+) xs both ought to work (if not, bug report) 3. disable the monomorphism restriction $ ghci -XNoMonomorphismRestriction or ghci> :set -XNoMonomorphismRestriction disable the MR and give you the polymorphic type without type signatures or arguments 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

Thanks. I tried the first two, which worked. Previously I had tried:
let (zipWithPlus :: Num a => [a] -> [a] -> [a]) = zipWith (+)
<interactive>:1:5:
Illegal signature in pattern: (Num a) => [a] -> [a] -> [a]
Use -XScopedTypeVariables to permit it
I tried setting -XScopedTypeVariables. When I ran it again I got a different
error message:
Prelude> let (zipWithPlus :: Num a => [a] -> [a] -> [a]) = zipWith (+)
<interactive>:1:5:
Illegal polymorphic or qualified type:
forall a. (Num a) => [a] -> [a] -> [a]
Perhaps you intended to use -XRankNTypes or -XRank2Types
In a pattern type signature: (Num a) => [a] -> [a] -> [a]
In the pattern: zipWithPlus :: (Num a) => [a] -> [a] -> [a]
In a pattern binding:
(zipWithPlus :: (Num a) => [a] -> [a] -> [a]) = zipWith (+)
-- Russ Abbott
______________________________________
Professor, Computer Science
California State University, Los Angeles
Google voice: 424-242-USA0 (last character is zero)
blog: http://russabbott.blogspot.com/
vita: http://sites.google.com/site/russabbott/
______________________________________
On Thu, Sep 30, 2010 at 12:48 PM, Daniel Fischer
let zipWithPlus xs ys = zipWith (+) xs ys

On 01.10.2010 05:43, Russ Abbott wrote:
let (zipWithPlus :: Num a => [a] -> [a] -> [a]) = zipWith (+)
<interactive>:1:5: Illegal signature in pattern: (Num a) => [a] -> [a] -> [a] Use -XScopedTypeVariables to permit it
It works if you write it as: let zipWithPlus :: Num a => [a] -> [a] -> [a]; zipWithPlus = zipWith (+) I.e. writing it exactly as you would in a file (inside a do-block), but with a semicolon where the linebreak would be.
participants (3)
-
Daniel Fischer
-
Russ Abbott
-
Sebastian Hungerecker