[Haskell-cafe] pointer equality

David Barbour dmbarbour at gmail.com
Wed Jul 20 08:44:18 CEST 2011


On Tue, Jul 19, 2011 at 11:14 PM, yi huang <yi.codeplayer at gmail.com> wrote:

> 2011/7/20 Eugene Kirpichov <ekirpichov at gmail.com>
>
>> reallyUnsafePointerEq#, and it really is as unsafe as it sounds :)
>>
>> Why is it so unsafe? i can't find any documentation on it.
> I think always compare pointer first is a good optimization.
>

Any number of conditions could make it fail, but most especially GC.

It is almost always better to explicitly model a property when you want it.
Uniqueness based on construction, for example, can be modeled with a monad
and protected by an ADT.

> module Unique ( HasUnique(..), Unique, newUnique, openUnique) where
> class HasUnique m uid where
>   newUniqueID :: m uid
>
> data Unique uid a = U !uid a
>
> newUnique :: (HasUnique m uid, Monad m) => a -> m (Unique uid a)
> newUnique a = newUniqueID >>= \ uid -> return (U uid a)
>
> openUnique :: (Unique m a) -> a
> openUnique (U _ x) = x
>
> instance (Eq uid) => Eq (Unique uid a)
> instance (Ord uid) => Ord (Unique uid a)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110719/ddfcaa82/attachment.htm>


More information about the Haskell-Cafe mailing list