MVar/Chan Ord instance

Jake McArthur jake.mcarthur at
Wed Jan 5 17:59:58 CET 2011

On 01/05/2011 06:16 AM, Mitar wrote:
> Is it possible to derive Ord for MVar (and based on that also for
> Chan)? Currently Eq is defined as reference equality (so each MVar is
> different from each other, independently from the content), at least
> as I understand it. But it would be useful to have also Ord defined in
> a such way (Ord of references). So without any semantic meaning of
> this ordering, but so that MVars (and Chans) can be used in set data
> types so that it is possible to define O(log n) isMember operation.
> I see that Eq is defined with internal function sameMVar#, so probably
> there would be also ordMVar# needed for this? Is there a way to get
> MVar internal references somehow (so that what sameMVar# is using)?

If MVar's Eq instance works the same way as IORef's, it uses pointer 
equality. The problem with making this also work for Ord is that the GC 
moves things around, so the pointers can change over time. The only way 
I know of to make it work is to add an identity field to IORefs/MVars, 
which might add some unnecessary overhead.

Probably the best thing you could do is wrap MVar/Chan in your own type 
definition that includes an identity field.

- Jake

More information about the Libraries mailing list