1

If I type the following into a Haskell script:

expressionTypeSigValue = 0 :: Integral a => a

typeSigValue :: Integral a => a
typeSigValue = 0

then load it in GHCi (v. 8.0.2), it informs me that the two constants have different types:

*Main> :t expressionTypeSigValue 
expressionTypeSigValue :: Integer
*Main> :t typeSigValue 
typeSigValue :: Integral a => a

I presume that the Integer type of expressionTypeSigValue is a result of type defaulting (correct me if I'm wrong). But I do not understand why the interpreter is not required to treat the two kinds of type signatures in the same way. Can someone explain this please?

Ose
  • 726
  • 7
  • 13

1 Answers1

3

It's the monomorphism restriction in action. In your first example, you are specifying the type of 0, not expressionTypeSigValue. Whether 0 has the type Integral a => a or its natural type Num a => a, the monomorphism restriction causes it to default to Integer. Consider the following in GHCi, where the monomorphism restriction is off by default:

-- Without the monomorphism restriction, the polymorphic type of the 
-- value is kept.
Prelude> expressionTypeSigValue = 0 :: Integral a => a
Prelude> :t expressionTypeSigValue
expressionTypeSigValue :: Integral a => a

-- With the monomorphism restriction, the polymorphic value
-- is replaced with the default monomorphic value.
Prelude> :set -XMonomorphismRestriction
Prelude> expressionTypeSigValue = 0 :: Integral a => a
Prelude> :t expressionTypeSigValue
expressionTypeSigValue :: Integer
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks! I wasn't familiar with this restriction, so I've been looking for an explanation of what it is and what the motivations for it are. For anyone else who is interested in this, the clearest explanation that I've found so far is: https://stackoverflow.com/a/32496865/1292784 . – Ose Jan 04 '20 at 22:40