
2008/5/28 PR Stanley
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 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