[Haskell-cafe] Are casts required?

Ryan Ingram ryani.spam at gmail.com
Mon Jun 6 11:08:48 CEST 2011


I always forget to reply all.  Silly gmail.

On Mon, Jun 6, 2011 at 2:07 AM, Ryan Ingram <ryani.spam at gmail.com> wrote:

> Hi Pat.  There aren't any casts in that code.  There are type annotations,
> but this is different than the idea of a cast like in C.
>
> For example
>     ((3 :: Integer) :: Int)
> is a compile error.
>
> What you are seeing is that 3 has the type (forall a. Num a => a); that is,
> the literal '3' gets converted by the compiler into
>
> fromInteger (I# 3#)
>
> where 3# represents the machine word '3' and I# is the internal constructor
> Word# -> Integer.
>
> class Num a where
>     ...
>     fromInteger :: Integer -> a
>
> So by 'casting', or rather, providing a type annotation, you are specifying
> what instance of Num gets the call to 'fromInteger'.
>
> As to whether you *need* a type annotation: it depends.  For example:
>     foo () = sameId newId 3
> the compiler will infer the type of 'foo' to be
>     foo :: forall a. IDs a => () -> a
>
> If you declare foo as a value, though, you run into the dreaded
> monomorphism restriction, and you might get a complaint from the compiler
> about ambiguity.
>     foo2 = sameId newId 3
>
>
> The monomorphism restriction forces values to be values; otherwise consider
> this
>
>
> -- the usual 'expensive' computation
> fib :: Num a => a -> a
> fib 0 = 1
> fib n = fib (n-1) + fib (n-2)
>
> x = fib 100000
>
> What's the type of x?  Most generally, it's
>     x :: Num a => a
>
> But this means that x will be recalculated every time it's used; the value
> can't be saved since x doesn't represent a single value but rather a
> separate value for each instance of Num.  You are allowed to manually
> specify this type, but without it, the compiler says 'You meant this to be a
> value!' and forces it to a particular type if it can, or complains about
> ambiguity if it can't.  As to how it does so, look up the rules for
> defaulting and monomorphism in the Haskell report.
>
>   -- ryan
>
>
>
> On Mon, Jun 6, 2011 at 12:45 AM, Patrick Browne <patrick.browne at dit.ie>wrote:
>
>> Are casts required to run the code below?
>> If so why?
>> Thanks,
>> Pat
>>
>>
>> -- Idetifiers for objects
>> class (Integral i) => IDs i where
>>  startId :: i
>>  newId :: i -> i
>>  newId i = succ i
>>  sameId, notSameId :: i -> i -> Bool
>> -- Assertion is not easily expressible in Haskell
>> -- notSameId i newId i  = True
>>  sameId i j = i == j
>>  notSameId i j = not (sameId i j)
>>  startId = 1
>>
>>
>> instance IDs Integer where
>>
>>
>>
>> -- are casts need here?
>> sameId (newId startId::Integer) 3
>> sameId (3::Integer) (4::Integer)
>> notSameId (3::Integer) (newId (3::Integer))
>>
>> This message has been scanned for content and viruses by the DIT
>> Information Services E-Mail Scanning Service, and is believed to be clean.
>> http://www.dit.ie
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110606/638d3f2a/attachment.htm>


More information about the Haskell-Cafe mailing list