reference (empty) → 0.1
raw patch · 4 files changed
+102/−0 lines, 4 filesdep +basedep +stmsetup-changed
Dependencies added: base, stm
Files
- Data/Reference.hs +62/−0
- LICENSE +19/−0
- Setup.hs +2/−0
- reference.cabal +19/−0
+ Data/Reference.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{- |+Module : $Header$+Description : Class connecting mutable variables and monads they exists in.+Copyright : (c) Maciej Piechotka+License : MIT++Stability : none+Portability : portable++Reference is class which generalizes references and monads they exists in. It means that IORef, STRef and others can be accessed by common interface.+-}+module Data.Reference+ (+ Reference(..),+ )+where+import Control.Concurrent.MVar+import Control.Concurrent.STM+import Control.Monad.ST+import Data.IORef+import Data.STRef++-- | Class connecting mutable variable and monad it exists in.+class Monad m => Reference r m where+ -- | Create new reference.+ newRef :: a -- ^ An initial value+ -> m (r a)+ -- | Reads a reference.+ readRef :: r a -- ^ Reference+ -> m a+ -- | Write to reference.+ writeRef :: r a -- ^ Reference+ -> a -- ^ New value+ -> m ()+ -- | Modify the reference. Default implementation is provided but it MUST+ -- be overloaded if the reference is atomic to provide an atomic write.+ modifyRef :: r a -- ^ Reference+ -> (a -> m (a, b)) -- ^ Computation+ -> m b -- ^ Result of computation+ modifyRef r f = readRef r >>= f >>= \(a, b) -> writeRef r a >> return b++instance Reference IORef IO where+ newRef = newIORef+ readRef = readIORef+ writeRef = writeIORef++instance Reference (STRef s) (ST s) where+ newRef = newSTRef+ readRef = readSTRef+ writeRef = writeSTRef++instance Reference MVar IO where+ newRef = newMVar+ readRef = readMVar+ writeRef = putMVar+ modifyRef = modifyMVar++instance Reference TVar STM where+ newRef = newTVar+ readRef = readTVar+ writeRef = writeTVar
+ LICENSE view
@@ -0,0 +1,19 @@+Copyright (c) 2010 Maciej Piechotka++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ reference.cabal view
@@ -0,0 +1,19 @@+Name: reference+Version: 0.1+Synopsis: A class for references in Haskell+Description: Reference is class which generalizes references and monads they exists in. It means that IORef, STRef and others can+ be accessed by common interface.+License: MIT+License-file: LICENSE+Author: Maciej Piechotka+Maintainer: uzytkownik2@gmail.com+Copyright: 2010 (c) Maciej Piechotka+Category: Control, Data+Build-type: Simple+Cabal-version: >=1.4++Library+ Exposed-modules: Data.Reference+ Build-depends: base >= 4 && < 5,+ stm+