hashcons-0.1.0: Hash-consing and memoisation

Copyright© 2018 Andy Morris
LicenseBSD-3-Clause
Maintainerhello@andy-morris.xyz
Stabilityexperimental
PortabilityGHC internals
Safe HaskellNone
LanguageHaskell2010

Data.HashCons.MkWeak

Description

A class for types which can have weak pointers/finalizers usefully attached to them.

Synopsis

Documentation

class MkWeak k where Source #

A class for weak pointer keys.

Creating weak pointers is only really safe for certain types of key. Ordinary Haskell values do not have a well defined lifetime. an example is with UNPACK pragmas, as in https://mail.haskell.org/pipermail/haskell-cafe/2018-January/128513.html; in principle the optimiser may also share or duplicate identical values.

This means that a value with an attached finaliser might go out of scope at an unexpected time, at which point the finalizer will also be run. This might lead to, e.g., file handles being closed while they are still in use.

In short, this class should only be implemented by values with specific identity, like references, and the key should be the underlying primitive. The instance for IORef attaches the pointer to the MutVar# inside, for example.

Minimal complete definition

mkWeak

Methods

mkWeak :: k -> v -> Maybe Finalizer -> IO (Weak v) Source #

mkWeakPtr :: k -> Maybe Finalizer -> IO (Weak k) Source #

Make a weak pointer where the value is the key.

addFinalizer :: k -> Finalizer -> IO () Source #

Add a finalizer to a value.

data Weak v :: * -> * #

A weak pointer object with a key and a value. The value has type v.

A weak pointer expresses a relationship between two objects, the key and the value: if the key is considered to be alive by the garbage collector, then the value is also alive. A reference from the value to the key does not keep the key alive.

A weak pointer may also have a finalizer of type IO (); if it does, then the finalizer will be run at most once, at a time after the key has become unreachable by the program ("dead"). The storage manager attempts to run the finalizer(s) for an object soon after the object dies, but promptness is not guaranteed.

It is not guaranteed that a finalizer will eventually run, and no attempt is made to run outstanding finalizers when the program exits. Therefore finalizers should not be relied on to clean up resources - other methods (eg. exception handlers) should be employed, possibly in addition to finalizers.

References from the finalizer to the key are treated in the same way as references from the value to the key: they do not keep the key alive. A finalizer may therefore ressurrect the key, perhaps by storing it in the same data structure.

The finalizer, and the relationship between the key and the value, exist regardless of whether the program keeps a reference to the Weak object or not.

There may be multiple weak pointers with the same key. In this case, the finalizers for each of these weak pointers will all be run in some arbitrary order, or perhaps concurrently, when the key dies. If the programmer specifies a finalizer that assumes it has the only reference to an object (for example, a file that it wishes to close), then the programmer must ensure that there is only one such finalizer.

If there are no other threads to run, the runtime system will check for runnable finalizers before declaring the system to be deadlocked.

WARNING: weak pointers to ordinary non-primitive Haskell types are particularly fragile, because the compiler is free to optimise away or duplicate the underlying data structure. Therefore attempting to place a finalizer on an ordinary Haskell type may well result in the finalizer running earlier than you expected. This is not a problem for caches and memo tables where early finalization is benign.

Finalizers can be used reliably for types that are created explicitly and have identity, such as IORef and MVar. However, to place a finalizer on one of these types, you should use the specific operation provided for that type, e.g. mkWeakIORef and addMVarFinalizer respectively (the non-uniformity is accidental). These operations attach the finalizer to the primitive object inside the box (e.g. MutVar# in the case of IORef), because attaching the finalizer to the box itself fails when the outer box is optimised away by the compiler.

type Finalizer = IO () Source #

A finalizer is a procedure attached to a weak pointer, which is run soon after the weak pointer's key is garbage collected.

deRefWeak :: Weak v -> IO (Maybe v) #

Dereferences a weak pointer. If the key is still alive, then Just v is returned (where v is the value in the weak pointer), otherwise Nothing is returned.

The return value of deRefWeak depends on when the garbage collector runs, hence it is in the IO monad.