Reputation: 2110
I'm reading now a book for parallel programming in Haskell. And there I see an example like that:
Prelude> let x = 2 + 3
Prelude> :sp x
x = _
Prelude> x
5
Prelude> :sp x
x = 5
But instead in my GHCi 7.8.3 from Platform Haskell 2014.02 I got this behaviour:
Prelude> let x = 2 + 3
Prelude> :sp x
x = _
Prelude> x
5
Prelude> :sp x
x = _
See the last line. Why x is not evaluated? I tryed using "seq x ()" and it is not evaluated, too.
Upvotes: 4
Views: 115
Reputation: 116174
Making the type of x
monomorphic solved the issue.
Prelude> let x::Int ; x = 2+3
Prelude> :sp x
x = _
Prelude> x
5
Prelude> :sp x
x = 5
The problem here lies in that 2+3
can be any numeric type, so x
is more like a function in disguise.
Some time ago, GHCi used to consider these definitions as monomorphic by default. This however lead to subtle type errors, and this feature often got called "the dreaded monomorphism restriction". Hence, GHCi now does not apply the monomorphism restriction any longer.
Upvotes: 4