foreign-var-0.0.0.1: Encapsulating mutatable state in external libraries

Copyright(c) 2014-2015 Edward Kmett
LicenseBSD2
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Foreign.Var

Contents

Description

 

Synopsis

Variables

data Var a Source

This data type represents a piece of mutable, imperative state with possible side-effects. These tend to encapsulate all sorts tricky behavior in external libraries, and may well throw exceptions.

Inhabitants should satsify the following properties.

In the absence of concurrent mutation from other threads or a thrown exception:

do x <- getVar v; setVar v y; setVar v x

should restore the previous state.

Ideally, in the absence of thrown exceptions:

setVar v a >> getVar v

should return a, regardless of a. In practice some Vars only permit a very limited range of value assignments, and do not report failure.

The result of updateVar should also be compatible with the result of getting and setting separately, however, it may be more efficient or have better atomicity properties in a concurrent setting.

Constructors

Var 

Fields

getVar :: IO a

Used by get

updateVar :: (a -> a) -> IO ()

Used by ($~)

updateVar' :: (a -> a) -> IO ()

Used by ($~!)

setVar :: a -> IO ()

Used by ($=)

Instances

HasGetter (Var a) a 
HasSetter (Var a) a 
HasUpdate (Var a) a a 
Typeable (* -> *) Var 

newVar Source

Arguments

:: IO a

getter

-> (a -> IO ())

setter

-> Var a 

Build a Var form a getter and a setter.

mapVar :: (b -> a) -> (a -> b) -> Var a -> Var b Source

Change the type of a Var

newtype SettableVar a Source

Constructors

SettableVar (a -> IO ()) 

Instances

Classes

class HasSetter t a | t -> a where Source

Methods

($=) :: MonadIO m => t -> a -> m () infixr 2 Source

Instances

($=!) :: (HasSetter t a, MonadIO m) => t -> a -> m () infixr 2 Source

class HasSetter t a => HasUpdate t a b | t -> a b where Source

Minimal complete definition

Nothing

Methods

($~) :: MonadIO m => t -> (a -> b) -> m () infixr 2 Source

($~!) :: MonadIO m => t -> (a -> b) -> m () infixr 2 Source

Instances

Storable a => HasUpdate (Ptr a) a a 
HasUpdate (TVar a) a a 
HasUpdate (IORef a) a a 
HasUpdate (Var a) a a 

class HasGetter t a | t -> a where Source

Methods

get :: MonadIO m => t -> m a Source

Instances

HasGetter (IO a) a 
Storable a => HasGetter (Ptr a) a 
HasGetter (STM a) a 
HasGetter (TVar a) a 
HasGetter (IORef a) a 
HasGetter (Var a) a