proposal: add 'unsafeCoerce'

Simon Marlow simonmarhaskell at gmail.com
Tue Nov 21 08:01:46 EST 2006


Malcolm Wallace wrote:
> Taral <taralx at gmail.com> writes:
> 
> 
>>On 11/20/06, Malcolm Wallace <Malcolm.Wallace at cs.york.ac.uk> wrote:
>>
>>>    newtype Wrapper a = Wrap a
>>>    convert :: [a] -> [Wrapper a]
>>>    convert xs = map Wrap xs
>>
>>Interesting! Looks like the compiler lacks rules for optimizing "map
>>id" &c.\
> 
> 
> And not all compilers have optimisation phases.
> 
> 
>>Your coercion does assume that the underlying runtime doesn't have
>>some kind of type-tag implementation of type classes.
> 
> 
> I believe this is guaranteed by the definition of newtype in the
> Language Report.

The language doesn't say anything about the runtime representation of newtype. 
It so happens that the semantics lead to the obvious implementation of a newtype 
   as a type cast (that was the reason for introducing newtype, after all), but 
there's nothing to say you have to implement it this way.  It would be wrong to 
require that unsafeCoerce let you convert between a newtype and its underlying 
type across implementations.

Cheers,
	Simon


More information about the Libraries mailing list