safe-globals-0.1.1: Safe top-level mutable variables which scope like ordinary values

Data.Global

Contents

Description

Declare safe top-level mutable variables which scope like ordinary values.

Synopsis

Using this module

Declare a top-level variable like so:

import Data.Global
import Control.Concurrent

declareChan "ch"  [t| Maybe Char |]

main = do
    writeChan ch (Just 'x')
    readChan  ch >>= print

This will create a module-level binding

ch :: Chan (Maybe Char)

The declareChan syntax is a Template Haskell declaration splice. The type of channel contents is given inside a Template Haskell type quotation. The TemplateHaskell extension must be enabled.

The scope of this variable can be controlled through the usual module import/export mechanism. If another module defines a Chan also named ch, there is no implicit relationship between the two.

Some declarations take an initalizer as an expression quotation. The variable will initially hold an unevaluated thunk for this expression.

declareIORef "ref"
    [t| Int |]
    [e| 3   |]

main = do
    readIORef  ref >>= print
    writeIORef ref 5
    readIORef  ref >>= print

For safety, it's important not to create polymorphic references. As a conservative restriction, this library statically forbids syntactically polymorphic types for reference contents. If you need to store polymorphic values in a reference, you can create a wrapper type with -XPolymorphicComponents.

IORef

declareIORef :: DeclareInitSource

Declare an IORef with an initial value.

declareIORef "foo" [t| Char |] [e| 'x' |]

Control.Concurrent

declareMVar :: DeclareInitSource

Declare an MVar with an initial value.

declareMVar "foo" [t| Char |] [e| 'x' |]

declareEmptyMVar :: DeclareSource

Declare an empty MVar.

declareEmptyMVar "foo" [t| Char |]

declareSampleVar :: DeclareInitSource

Declare a SampleVar with an initial value.

declareSampleVar "foo" [t| Char |] [e| 'x' |]

declareChan :: DeclareSource

Declare an empty Chan.

declareChan "foo" [t| Char |]

declareQSem :: DeclareSemSource

Declare a QSem with the specified quantity.

declareQSem "foo" [e| 3 |]

declareQSemN :: DeclareSemSource

Declare a QSemN with the specified quantity.

declareQSemN "foo" [e| 3 |]

STM

declareTVar :: DeclareInitSource

Declare a TVar with an initial value.

declareTVar "foo" [t| Char |] [e| 'x' |]

declareTMVar :: DeclareInitSource

Declare a TMVar with an initial value.

declareTMVar "foo" [t| Char |] [e| 'x' |]

declareEmptyTMVar :: DeclareSource

Declare an empty TMVar.

declareEmptyTMVar "foo" [t| Char |]

declareTChan :: DeclareSource

Declare an empty TChan.

declareTChan "foo" [t| Char |]

Type synonyms

type Declare = String -> Q Type -> Q [Dec]Source

The type of macros for declaring variables.

type DeclareInit = String -> Q Type -> Q Exp -> Q [Dec]Source

The type of macros for declaring variables with initializers.

type DeclareSem = String -> Q Exp -> Q [Dec]Source

The type of macros for declaring semaphores.