Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of a double

Tags:

haskell

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?

like image 264
LetMeSOThat4U Avatar asked May 29 '14 21:05

LetMeSOThat4U


1 Answers

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.

like image 80
bheklilr Avatar answered Sep 21 '22 12:09

bheklilr