[Haskell-cafe] coerce (safe!)

Bernie Pope bjpop at csse.unimelb.edu.au
Mon Mar 3 00:23:52 EST 2008


On 03/03/2008, at 8:30 AM, Luke Palmer wrote:

> 2008/3/2 Roman Cheplyaka <roma at ro-che.info>:
>> * Krzysztof Skrzętnicki <gtener at gmail.com> [2008-03-02 01:21:42 
>> +0100]
>>
>>> Well, it is simply
>>>
>>>> coerce :: a -> b
>>>> coerce _ = undefined
>>>
>>> so coerce is simply empty function. But still, it is possible to  
>>> write a
>>> function of type (a->b).
>>> Well, possibly I didn't write anything particularly new, but  
>>> please excuse
>>> me for I'm still in
>>> sort of a shock after I've discovered it.
>>
>>  Also there's nice possibility of defining Maybe a without ADT.
>>  type Maybe a = (a, Bool)
>>  just x = (x, True)
>>  nothing = (undefined, False)
>
> That's a hack.  This is my favorite:
>
>   type Maybe a = forall b. (a -> b) -> b -> b
>   just x  = \j n -> j x
>   nothing = \j n -> n

For something slightly different, I've always enjoyed lists (or  
integer indexed structures) as functions:

    type List a = Integer -> Maybe a

You've got to watch out for non-contiguous lists. It's a good  
challenge to write
head, tail, nil and cons for this type. Then write conversions to/ 
from normal
lists.

Bernie.


More information about the Haskell-Cafe mailing list