Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > by-pkgid > 3575c686237628b9bdedf5f294fbd58c > files > 29

hugs98-20011215-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.6">
 <TITLE>The Hugs-GHC Extension Libraries: Stable: Stable Names</TITLE>
 <LINK HREF="libs-24.html" REL=next>
 <LINK HREF="libs-22.html" REL=previous>
 <LINK HREF="libs.html#toc14" REL=contents>
</HEAD>
<BODY>
<A HREF="libs-24.html">Next</A>
<A HREF="libs-22.html">Previous</A>
<A HREF="libs.html#toc14">Contents</A>
<HR>
<H2><A NAME="sec:stable-pointers"></A> 14.2 Stable Names</H2>

<P>
<P>A haskell object can be given a <EM>stable name</EM> by calling
<CODE>makeStableName</CODE> on it.  Stable names solve the following problem:
suppose you want to build a hash table with Haskell objects as keys,
but you want to use pointer equality for comparison; maybe because the
keys are large and hashing would be slow, or perhaps because the keys
are infinite in size.  We can't build a hash table using the address
of the object as the key, because objects get moved around by the
garbage collector, meaning a re-hash would be necessary after every
garbage collection.
<P>Enter stable names.  A stable name is an abstract entity that supports
equality and hashing, with the following interface:
<P>
<BLOCKQUOTE><CODE>
<PRE>
data StableName a -- abstract, instance Eq.
makeStableName :: a -> IO (StableName a)
hashStableName :: StableName a -> Int
</PRE>
</CODE></BLOCKQUOTE>
<P>All these operations run in constant time.
<P>Stable names have the following properties:
<P>
<OL>
<LI> If <CODE>sn1 :: StablePtr</CODE> and <CODE>sn2 :: StablePtr</CODE> and <CODE>sn1
== sn2</CODE> then <CODE>sn1</CODE> and <CODE>sn2</CODE> are either the same stable name,
or they were created by calls to <CODE>makeStableName</CODE> on the same
object.</LI>
<LI> The reverse is not necessarily true: if two stable names are
not equal, it doesn't mean that they don't refer to the same Haskell
object (although they probably don't).</LI>
<LI> There is no <CODE>freeStableName</CODE> operation.  Stable names are
reclaimed by the runtime system when they are no longer needed.</LI>
<LI> There is no <CODE>deRefStableName</CODE> operation.  You can't get back
from a stable name to the original Haskell object.  The reason for
this is that the existence of a stable name for an object doesn't
guarantee the existence of the object itself; it can still be garbage
collected. </LI>
<LI> There is a <CODE>hashStableName</CODE> operation, which converts a
stable name to an <CODE>Int</CODE>.  The <CODE>Int</CODE> returned is not necessarily
unique (that is, it doesn't satisfy property (1) above), but it can be
used for building hash tables of stable names.</LI>
</OL>
<P>Properties (1) and (2) are similar to stable pointers, but the key
differences are that you can't get back to the original object from a
stable name, and you can convert one to an <CODE>Int</CODE> for hashing.
<P>
<HR>
<A HREF="libs-24.html">Next</A>
<A HREF="libs-22.html">Previous</A>
<A HREF="libs.html#toc14">Contents</A>
</BODY>
</HTML>