
To add to this: There are other constants which are polymorphic, not only
numbers. Examples where you could add type signatures to make the type
explicit are the empty list '[]' and the 'Nothing' constructor of 'Maybe a'.
Adding type signatures to these will not be type casts, but telling the
compiler that you want to specialize the given polymorphic entity.
Abhay
On Wed, May 28, 2008 at 1:27 PM, Salvatore Insalaco
Hi (16 :: Float) is a perfectly legitimate statement although I'm surprised that it's allowed in a type strong language such as Haskell. It's a bit
2008/5/28 PR Stanley
: like casting in good old C. What's going on here?
Don't worry: it's not a cast. Numeric constants like "16" in Haskell have polymorphic types: Prelude> :t 16 16 :: (Num t) => t Prelude> :t 16.6 16.6 :: (Fractional t) => t
Writing "16 :: Float" you are simply making the type explicit, and you can do it only in the context of the typeclass.
Prelude> :t (16 :: Integer) (16 :: Integer) :: Integer
This works because Integer is a type of the typeclass Num, but: Prelude> :t (16.5 :: Integer) <interactive>:1:1: No instance for (Fractional Integer) arising from the literal `16.5' at <interactive>:1:1-4 Possible fix: add an instance declaration for (Fractional Integer)
This doesn't work. So everything is done at compile time, no casting (i.e. "believe me compiler, this it a Float") involved.
Notice that during binding the numeric constants' type is always made explicit (if you want to know more, look for section 4.3.4 in the Haskell Report): Prelude> let a = 3 Prelude> :t a a :: Integer
Prelude> let b = 3.3 Prelude> :t b b :: Double
Prelude> b :: Float <interactive>:1:0: Couldn't match expected type `Float' against inferred type `Double' In the expression: b :: Float In the definition of `it': it = b :: Float
Salvatore _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe