Double type

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 different types? Are these two 0.15 instances really different types?

+6
source share
1 answer

This is due to the terrible restriction of monomorphism . Basically, GHCi likes to choose default types at runtime (the default Fractional type is Double ), but when you specify a type with :type , it selects the most common 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 

Although this extension has one of the more scary names, it is pretty simple when you break it:

 Mono -> One Morph -> shape (type) ism -> thingy Monomorphism -> one shape thingy -> one type thingy -> thing with a single type 

So basically this is a really long word that means "only type". Then with the β€œrestriction” you get that the restriction of monomorphism restricts things to one type. In this case, it restricts numbers (things) to type Double . Without this restriction, the type of numbers is limited only to the type class, which theoretically can be an infinite number of types.

+11
source

Source: https://habr.com/ru/post/970054/


All Articles