proposal: add 'unsafeCoerce'
wnoise at ofb.net
Tue Nov 21 15:05:26 EST 2006
On 2006-11-21, Malcolm Wallace <Malcolm.Wallace at cs.york.ac.uk> wrote:
> "David House" <dmhouse at gmail.com> 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.
> castEitherL :: Either a b -> Either a c
> castEitherL (Left x) = Left x
> castEitherL _ = error "castEitherL on Right"
> castEitherR :: Either a b -> Either c b
> castEitherR (Right x) = Right x
> castEitherR _ = error "castEitherR on Left"
More information about the Libraries