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.
- newDefaultRef :: (DefaultStateRef sr m a, NewRef sr m a) => a -> m sr
- readDefaultRef :: (DefaultStateRef sr m a, ReadRef sr m a) => sr -> m a
- writeDefaultRef :: (DefaultStateRef sr m a, WriteRef sr m a) => sr -> a -> m ()
- atomicModifyDefaultRef :: (DefaultStateRef sr m a, ModifyRef sr m a) => sr -> (a -> (a, b)) -> m b
- modifyDefaultRef :: (DefaultStateRef sr m a, ModifyRef sr m a) => sr -> (a -> a) -> m ()
- readsRef :: (ReadRef sr m a, Monad m) => sr -> (a -> b) -> m b
- newCounter :: (DefaultStateRef sr m1 a, ModifyRef sr m1 a, NewRef sr m a, Enum a) => a -> m (m1 a)
- mkLapseReader :: (ReadRef sr m a, ReadRef sr m1 a, NewRef sr1 m a, DefaultStateRef sr1 m1 a1, ReadRef sr1 m1 a1, WriteRef sr1 m1 a) => sr -> (a -> a1 -> b) -> m (m1 b)
- module Data.StateRef.Classes
- module Data.StateRef.Instances
- module Data.Accessor
Documentation
newDefaultRef :: (DefaultStateRef sr m a, NewRef sr m a) => a -> m srSource
Create a reference and constrain its type to be the default reference type
for the monad in which it is being created. See newRef
.
readDefaultRef :: (DefaultStateRef sr m a, ReadRef sr m a) => sr -> m aSource
Read a reference and constrain its type to be the default reference type
for the monad in which it is being read. See readRef
.
writeDefaultRef :: (DefaultStateRef sr m a, WriteRef sr m a) => sr -> a -> m ()Source
Write a reference and constrain its type to be the default reference type
for the monad in which it is being written. See writeRef
atomicModifyDefaultRef :: (DefaultStateRef sr m a, ModifyRef sr m a) => sr -> (a -> (a, b)) -> m bSource
Modify a reference and constrain its type to be the default reference type
for the monad in which it is being modified. See modifyRef
.
modifyDefaultRef :: (DefaultStateRef sr m a, ModifyRef sr m a) => sr -> (a -> a) -> m ()Source
Modify a reference and constrain its type to be the default reference type
for the monad in which it is being modified. See modifyRef
.
readsRef :: (ReadRef sr m a, Monad m) => sr -> (a -> b) -> m bSource
Essentially the same concept as Control.Monad.State.gets
,
Control.Monad.State.asks
, et al. Typically useful to read a field of
a referenced ADT by passing a record selector as the second argument.
newCounter :: (DefaultStateRef sr m1 a, ModifyRef sr m1 a, NewRef sr m a, Enum a) => a -> m (m1 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, ReadRef sr m1 a, NewRef sr1 m a, DefaultStateRef sr1 m1 a1, ReadRef sr1 m1 a1, WriteRef sr1 m1 a) => sr -> (a -> a1 -> b) -> m (m1 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 (-)
note that there's a unification ghc missed here: the fundep sr -> a on NewRef and DefaultStateRef should cause a and a1 to be unified, because of the 2 constraints: NewRef sr1 m a DefaultStateRef sr1 m1 a1 this isn't a "bug" because the type is still valid, but it seems like something ghc "ought" to do, since a and a1 are doomed to unification anyway.
module Data.StateRef.Classes
module Data.StateRef.Instances
module Data.Accessor