#2532: Add Typeable instance to Data.Unique

Sittampalam, Ganesh ganesh.sittampalam at credit-suisse.com
Sun Sep 7 16:39:15 EDT 2008


Sorry, I didn't intend to scare you into submission. I'm not even necessarily opposing this proposal, just warning of some consequences.

I'll try to summarise the issue below. The linked thread isn't of all that much relevance except in that it led to me realising that this issue probably exists.

Data.Unique uses a "top-level global variable" (an IORef) as a counter for the Integer underlying Unique values. This is initialised to 0 via a {-# NOINLINE -#} unsafePerformIO the first time it is needed.

If you load something via hs-plugins, any modules it uses are loaded into the process, even if they are already loaded via the static part of the program. This only happens once, i.e. the most copies of the module you can end up with is two - although another dynamic loading architecture might well do something different.

So any Unique values created in the plugin will be created with the counter starting from 0 again, so will collide with any Unique values created in the static part of the process.

hs-plugins uses special knowledge of how Data.Typeable works (another global variable!) to ensure that the "same" types have the same type reps in both the static and dynamic sides of the process.

So, adding Data.Typeable to Unique will allow these colliding Unique values to be passed between the static and dynamic sides, causing two separately generated Unique values to potentially compare equal.

As I said in my original comment, it could well be reasonable to blame hs-plugins for hacking around with Typeable instances. On the other hand, it's hard to see what else it can do if people are to actually use plugins from the main process, and Data.Unique is clearly using an unsafe hack. (In the thread referred to below, I'm mainly opposing the formalisation of that hack into the top-level <- operation.)

It doesn't really strike me as the end of the world if a module that is already doing something dodgy turns out to be broken in the dynamic loading world, as long as people are aware of this. Presumably the Typeable instance will be useful for many things other than dynamic loading.

Any further questions, please do ask. Also, Brandon or someone else who is in favour of top-level <- might be able to cast what Data.Unique is doing in a more positive light :-)

I've been trying to build a recent GHC HEAD and then hs-plugins (hackage package "plugins") all weekend so I can try this out for real, but haven't got far enough yet.

Ganesh

-----Original Message-----
From: libraries-bounces at haskell.org [mailto:libraries-bounces at haskell.org] On Behalf Of Jim Apple
Sent: 07 September 2008 17:55
To: libraries at haskell.org
Subject: Re: #2532: Add Typeable instance to Data.Unique

I'm afraid I haven't time to read the whole thread and come up with a good answer about whether or not adding Typeable to Data.Unique is dangerous.

Given that, I'm no longer, er, proposing this proposal.

Jim

On Sun, Sep 7, 2008 at 9:26 AM, Brandon S. Allbery KF8NH <allbery at ece.cmu.edu> wrote:
> On 2008 Sep 7, at 11:13, Jim Apple wrote:
>>
>> On Fri, Sep 5, 2008 at 5:23 AM, Sittampalam, Ganesh 
>> <ganesh.sittampalam at credit-suisse.com> wrote:
>>>
>>> (There's a long discussion about this on haskell-café at the 
>>> moment.)
>>
>> I tried to find this thread (by searching for "unique" and "plug" on 
>> http://www.haskell.org/pipermail/haskell-cafe/ ), but couldn't. What 
>> is the subject line?
>
>
> "Top-level <-"
_______________________________________________
Libraries mailing list
Libraries at haskell.org
http://www.haskell.org/mailman/listinfo/libraries


==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================



More information about the Libraries mailing list