Learning Haskell, in ghci
:
Prelude Data.Ratio> :type 0.15
0.15 :: Fractional a => a
Prelude Data.Ratio> 0.15
0.15
it :: Double
Why are types different? Are those two instances of 0.15 actually different types?
This due to the dreaded monomorphism restriction. Basically, GHCi likes to choose default types when executed (the default Fractional
type is Double
), but when you ask the type using :type
it chooses the most general version. You can disable this behavior with the NoMonomorphismRestriction
extension:
> :set -XNoMonomorphismRestriction
> :set +t
> 0.15
0.15
it :: Fractional a => a
> :t 0.15
0.15 :: Fractional a => a
While this this extension has one of the scarier names, it's rather simple when you break it down:
Mono -> One
Morph -> shape (type)
ism -> thingy
Monomorphism -> one shape thingy -> one type thingy -> thing with a single type
So basically it's a really long word that means "single type". Then with "restriction", you get that the monomorphism restriction is restricting things to a single type. In this case, it's restricting numbers (the things) to the type Double
. Without this restriction, the type of the numbers is only constrained by a type class, which can in theory be an infinite number of types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With