stateref-0.3: Abstraction for things that work like IORef.

Safe Haskell | Safe-Inferred
Language | Haskell98

Data.StateRef

Description

This module provides classes and instances for mutable state references. Various implementation exist in common usage, but no way (until now ;-) to define functions using state references which don't depend on the specific monad or reference type in use.

These modules use several language extensions, including multi-parameter type classes and functional dependencies.

Synopsis

- readRef :: Ref m a -> m a
- writeRef :: Ref m a -> a -> m ()
- atomicModifyRef :: Ref m a -> (a -> (a, b)) -> m b
- modifyRef :: Ref m a -> (a -> a) -> m ()
- readsRef :: (ReadRef sr m a, Monad m) => sr -> (a -> b) -> m b
- newCounter :: (HasRef m, Monad m, Enum a) => a -> m (m a)
- mkLapseReader :: (ReadRef sr m a, HasRef m, Monad m) => sr -> (a -> a -> b) -> m (m b)
- module Data.StateRef.Types
- module Data.StateRef.Instances
- module Data.Accessor

# Documentation

readRef :: Ref m a -> m a Source #

Read a `Ref`. See `readReference`.

writeRef :: Ref m a -> a -> m () Source #

Write a `Ref`. See `writeReference`

atomicModifyRef :: Ref m a -> (a -> (a, b)) -> m b Source #

Modify a `Ref`. See `modifyReference`.

modifyRef :: Ref m a -> (a -> a) -> m () Source #

Modify a `Ref`. See `modifyReference`.

readsRef :: (ReadRef sr m a, Monad m) => sr -> (a -> b) -> m b Source #

Essentially the same concept as `gets`, `asks`, et al. Typically useful to read a field of a referenced ADT by passing a record selector as the second argument.

newCounter :: (HasRef m, Monad m, Enum a) => a -> m (m a) Source #

Construct a counter - a monadic value which, each time it is evaluated, returns the `succ` of the previous value returned.

mkLapseReader :: (ReadRef sr m a, HasRef m, Monad m) => sr -> (a -> a -> b) -> m (m b) Source #

Create a "lapse reader" (suggestions for better terminology are more than welcome), a sort of a time-lapse of the variable. The first motivating instance for this operation was a clock in a simple simulation application. Given a `TVar` `Double` called "clock", a useful value "dT" is yielded by the expression: `mkLapseReader` clock (-)

module Data.StateRef.Types

module Data.StateRef.Instances

module Data.Accessor