proposal: add 'unsafeCoerce'

Iavor Diatchki iavor.diatchki at
Tue Nov 21 13:44:51 EST 2006

I just wanted to point out that even the 'reasonable' uses of
'unsafeCoerce' (i.e., where a type does not appear directly in a
value) may not be compatible between different implementations.
Consider, for example, an implementation which turns a polymorphic
program into a monomorphic one by generating different versions of
functions for each of its instantiations (this is not a new idea).  In
particular, one also has to specialize constructor functions, which
amounts to specializing datatypes.   For example, 'Either at Int@Char'
and 'Either at Int@Bool' become completely separate types.  In addition,
we may choose to add only those constructors of a specialized type
that actually appear in the program. For example, 'Either at Int@Char'
may have both constructors 'Left at Int@Char' and 'Righ at Int@Char', while
'Either at Int@Bool' may only have a single constructor 'Right at Int@Bool'.
 Now, if we use an integer tag to distinguish constructors, then by
using 'unsafeCoerce' we may end up casting 'Left at Int@Char' into
'Right at Int@Bool'.

On 11/21/06, Malcolm Wallace <Malcolm.Wallace at> wrote:
> "David House" <dmhouse at> wrote:
> > >  * cast that changes a phantom type, or changes a type that is not
> > >    reflected by a part of the value,
> > >    eg. 'unsafeCoerce (Left 3) :: Either Int a' should be fine for
> > >    any 'a',
> >
> > Couldn't we have the following for this case?
> >
> > castEitherL :: Either a b -> Either a c
> > castEitherL (Left x) = Left x
> > castEitherL z = z
> >
> > castEitherR :: Either a b -> Either c b
> > castEitherR (Right x) = Right x
> > castEitherR z = z
> No actually.  Try it!  Any compiler will reject the catch-all cases
> ('z') because you are asking for the usage of z on the rhs to be at a
> different type than the pattern usage of z on the lhs.
> Regards,
>     Malcolm
> _______________________________________________
> Libraries mailing list
> Libraries at

More information about the Libraries mailing list