<Sect1 id="memo-library">
<Title><Literal>Memo</Literal>: Fast memo functions
<IndexTerm><Primary>Memo</Primary></IndexTerm>
</Title>

<Para>
The <Literal>Memo</Literal> library provides fast polymorphic memo functions using hash
tables.  The interface is:
</Para>

<Para>

<ProgramListing>
memo :: (a -&#62; b) -&#62; a -&#62; b
</ProgramListing>

</Para>

<Para>
So, for example, <Function>memo f</Function> is a version of <Function>f</Function> that caches the results
of previous calls.
</Para>

<Para>
The searching is very fast, being based on pointer equality.  One
consequence of this is that the caching will only be effective if
<Emphasis>exactly the same argument is passed again to the memoised
function</Emphasis>.  This means not just a copy of a previous argument, but the
same instance.  It's not useful to memoise integer functions using
this interface, because integers are generally copied a lot and two
instances of '27' are unlikely to refer to the same object.
</Para>

<Para>
This memoisation library works well when the keys are large (or even
infinite).
</Para>

<Para>
The memo table implementation uses weak pointers and stable names (see
the GHC/Hugs library document) to avoid space leaks and allow hashing
for arbitrary Haskell objects.  NOTE: while individual memo table
entries will be garbage collected if the associated key becomes
garbage, the memo table itself will not be collected if the function
becomes garbage.  We plan to fix this in a future version.
</Para>

<Para>
There's another version of <Function>memo</Function> if you want to explicitly give a
size for the hash table (the default size is 1001 buckets):
</Para>

<Para>

<ProgramListing>
memoSized :: Int -&#62; (a -&#62; b) -&#62; a -&#62; b
</ProgramListing>

</Para>

</Sect1>
