Questions tagged [monomorphism-restriction]

Haskell's dreaded monomorphism restriction.

Haskell's monomorphism restriction causes some functions that should be polymorphic to be forced into monomorphism, causing errors. For instance:

sum = foldr (+) 0

in GHC 7.10, this will fail with this message:

No instance for (Foldable t0) arising from a use of ‘foldr’
The type variable ‘t0’ is ambiguous
Relevant bindings include
  mysum :: t0 Integer -> Integer (bound at src/Main.hs:37:1)
Note: there are 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 five others
In the expression: foldr (+) 0
In an equation for ‘mysum’: mysum = foldr (+) 0

This can usually be solved by explicitly defining the type signature:

sum :: (Foldable f, Num a) => f a -> a

Or by removing the restriction:

{-# LANGUAGE NoMonomorphismRestriction #-}
34 questions
86
votes
1 answer

What is the monomorphism restriction?

I'm puzzled by how the Haskell compiler sometimes infers types that are less polymorphic than what I'd expect, for example when using point-free definitions. It seems like the issue is the "monomorphism restriction", which is on by default on older…
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
29
votes
3 answers

Why do 3 and x (which was assigned 3) have different inferred types in Haskell?

Type inference in Haskell has a bit of a learning curve (to say the least!). A good way to start learning it is with simple examples. So, the following is a bit of a "hello world" for type inference. Consider the following example: Prelude> :t 3 3…
Peter Skirko
  • 695
  • 7
  • 14
22
votes
2 answers

Why are polymorphic values not inferred in Haskell?

Numeric literals have a polymorphic type: *Main> :t 3 3 :: (Num t) => t But if I bind a variable to such a literal, the polymorphism is lost: x = 3 ... *Main> :t x x :: Integer If I define a function, on the other hand, it is of course…
17
votes
2 answers

Effects of monomorphism restriction on type class constraints

This code breaks when a type declaration for baz is added: baz (x:y:_) = x == y baz [_] = baz [] baz [] = False A common explanation (see Why can't I declare the inferred type? for an example) is that it's because of polymorphic recursion. But…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
15
votes
3 answers

Transforming a function to point-free style changes its type

I'm beginning Haskell... I tried to write the following trivial function in two different ways, letting Haskell decide the types, and the type system does something different in each case. What is the explanation for that behavior? Prelude> let f x…
Frank
  • 4,341
  • 8
  • 41
  • 57
15
votes
1 answer

What is XNoMonomorphismRestriction?

This page usages $ ghci -XNoMonomorphismRestriction to start the haskell interpreter. What does XNoMonomorphismRestriction switch mean?
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
14
votes
1 answer

:sprint for polymorphic values?

I am wondering why :sprint reports xs = _ in this case: Prelude> xs = map (+1) [1..10] Prelude> length xs 10 Prelude> :sprint xs xs = _ but not in this case: Prelude> xs = map (+1) [1..10] :: [Int] Prelude> length xs 10 Prelude> :sprint xs xs =…
ErikR
  • 51,541
  • 9
  • 73
  • 124
12
votes
1 answer

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

It turns out that in GHC 7.10, this compiles fine: mysum xs = foldr (+) 0 xs But this: mysum = foldr (+) 0 results in the following error: No instance for (Foldable t0) arising from a use of ‘foldr’ The type variable ‘t0’ is ambiguous Relevant…
user1002430
10
votes
2 answers

NoMonomorphismRestriction helps preserve sharing?

I was trying to answer another question about polymorphism vs sharing when I stumbled upon this strange behaviour. In GHCi, when I explicitly define a polymorphic constant, it does not get any sharing, which is understandable: > let fib :: Num a =>…
Rotsor
  • 13,655
  • 6
  • 43
  • 57
10
votes
1 answer

Using the state monad to hide explicit state

I'm trying to write a little game in Haskell, and there's a fair amount of state necessary to pass around. I want to try hiding the state with the State monad Now I've run into a problem: functions which take the state and an argument were easy to…
user1432855
9
votes
1 answer

When can I bind a function to another name?

When working in the interpreter, it's often convenient to bind a function to a name, for example: ghci> let f = (+1) ghci> f 1 2 This aliases the name f to the function (+1). Simple. However, this doesn't always work. One example I've found which…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
9
votes
2 answers

"Overloaded signature conflicts with monomorphism restriction"

Why is this allowed: i :: Num a => a i = 1 While none of these is allowed: i' :: Num a => a Just i' = Just 1 x, y :: Num a => a (x, y) = (1, 2) x :: Num a => a y :: Num a => a (x, y) = (1, 2) y :: Num a => a x :: Num a => a (x, y) = (1, 2) m ::…
Futarimiti
  • 551
  • 2
  • 18
8
votes
1 answer

Haskell type inference for Functors

Lately I've been playing around with Haskell, and specifically the whole functors concept. The more I dive into it, the more a-ha moments I'm getting, and it certainly tickles my dopamine receptors quite a bit. The problem I'm stuck with is the…
SkyWriter
  • 1,454
  • 10
  • 17
8
votes
1 answer

Explain monomorphism restriction to me please?

I started doing 99 haskell problems and I was on problem 7 and my unittests were blowing up. Apparently, it's due to this: http://www.haskell.org/haskellwiki/Monomorphism_restriction I just wanted to make sure I understood this correctly because I'm…
user1561402
  • 141
  • 5
7
votes
4 answers

How to print type of polymorphic function (or value) in ghci with type defaulting rules applied?

When I enter :t command in GHCi I see polymorphic type: ghci> :t 42 42 :: Num t => t ghci> :t div div :: Integral a => a -> a -> a But after I actually evaluate such functions I see result of type defaulting rules. Is there some command or ability…
Shersh
  • 9,019
  • 3
  • 33
  • 61
1
2 3