unsafeCoerce and type aliases under type applications

Ben Millwood haskell at benmachine.co.uk
Sat Dec 3 22:47:34 CET 2011


The semantics of unsafeCoerce are a bit informal, since it doesn't
exist as far as the standard is concerned. The docs are here:

http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/Unsafe-Coerce.html
for GHC: http://hackage.haskell.org/packages/archive/ghc-prim/0.2.0.0/doc/html/GHC-Prim.html#v:unsafeCoerce#

With regard to your question about tools and compilers, well,
unsafeCoerce is kind of for the situtations where your automated tools
fail to prove safety, so I don't think there's a good general
solution.

On Sat, Dec 3, 2011 at 7:39 PM, Joachim Breitner
<mail at joachim-breitner.de> wrote:
> Hi,
>
> Am Mittwoch, den 30.11.2011, 10:51 -0200 schrieb Felipe Almeida Lessa:
>> > How about using the Down/Dual/Desc/Converse/Opposite/Reverse newtype discussed
>> > in another recent thread, and providing for Data.Map:
>> >
>> >  reverse :: Map k a -> Map (Reverse k) a
>> >  reverse Tip = Tip
>> >  reverse (Bin n k a l r) = Bin n (Reverse k) a (reverse r) (reverse l)
>> >
>> > (Arguably we also need reverse' :: Map (Reverse k) a -> Map k a. Hmm...)
>>
>>   reverse' :: Map (Reverse k) a -> Map k a
>>   reverse' = unsafeCoerce . reverse
>>
>> Sorry, couldn't resist =).
>
> have used unsafeCoerce to change the type inside a container to a "type"
> alias in real code, but your post makes me wonder: Under what
> circumstances is that safe? Is that documented somehow? Can a tool or
> the compiler decide for us whether it is safe?
>
> Thanks,
> Joachim
>
> --
> Joachim "nomeata" Breitner
>  mail at joachim-breitner.de  |  nomeata at debian.org  |  GPG: 0x4743206C
>  xmpp: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/
>
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>



More information about the Libraries mailing list