data-quotientref-0.1: Reference cells that need two independent indices to be accessed.

Stabilityexperimental
MaintainerLuke Palmer <lrpalmer@gmail.com>

Data.QuotientRef

Description

A Quotient Reference is a reference cell that needs two values to dereference. In a way it is a two-dimensional table indexed by references. The trick is that if a cell is indexed by a and b, then if either a or b gets cleaned up by the garbage collector, then so does the cell, because it would not be able to be accessed anymore.

There are two different types of indices, LeftRef and RightRef. You need one of each, of the same type, to access a cell.

The name comes from the idea that the product of two indices is a reference, so each index is a quotient.

Example usage:

 do
   l_1 <- newLeft
   l_2 <- newLeft
   r_1 <- newRight
   r_2 <- newRight
   write l_1 r_1 "Foo"
   write l_2 r_1 "Bar"
   print =<< read l_1 r_1  -- Just "Foo"
   print =<< read l_1 r_2  -- Nothing
   print =<< read l_2 r_1  -- Just "Bar"
   print =<< read l_2 r_2  -- Nothing

Synopsis

Documentation

data LeftRef a Source

The left half of a reference cell. Combine this with a RightRef to access a cell.

data RightRef a Source

The right half of a reference cell. Combine this with a LeftRef to access a cell.

read :: LeftRef a -> RightRef a -> IO (Maybe a)Source

Combine the two halves of a reference and return the result if it exists.

write :: LeftRef a -> RightRef a -> a -> IO ()Source

Combine the two halves of a reference and write a value to the product.