[Haskell-cafe] Type Coercion
abhay.parvate at gmail.com
Wed May 28 08:19:43 EDT 2008
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.
On Wed, May 28, 2008 at 1:27 PM, Salvatore Insalaco <kirby81 at gmail.com>
> 2008/5/28 PR Stanley <prstanley at ntlworld.com>:
> > 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
> > 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)
> 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
> Couldn't match expected type `Float' against inferred type `Double'
> In the expression: b :: Float
> In the definition of `it': it = b :: Float
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe