safe-freeze 0.0 → 0.2
raw patch · 5 files changed
+119/−251 lines, 5 filesdep +indexeddep +mtldep +vectordep −category-extrasdep −transformersdep −uvectordep ~base
Dependencies added: indexed, mtl, vector
Dependencies removed: category-extras, transformers, uvector
Dependency ranges changed: base
Files
- Control/Monad/ST/Freeze.hs +3/−2
- Data/Array/Vector/STFreeze.hs +0/−236
- Data/Vector/Generic/Mutable/STFreeze.hs +58/−0
- LICENSE +30/−0
- safe-freeze.cabal +28/−13
Control/Monad/ST/Freeze.hs view
@@ -44,7 +44,8 @@ import Control.Applicative import Control.Monad import Control.Monad.Identity(Identity(..))-import qualified Control.Monad.ST as S+import qualified Control.Monad.ST as S (ST)+import qualified Control.Monad.ST.Unsafe as S import Control.Monad.Indexed(IxMonad(..),IxFunctor(..),IxPointed(..),IxApplicative(..)) import System.IO.Unsafe @@ -80,7 +81,7 @@ -- Defining 'STT' as a monad transformer rather than just a monad -- allows ST arrays to be used in the 'IO' monad, bringing safe -- freezing also to the 'IO' monad.-newtype STT m s t a = STT { unSTT :: IO a }+newtype STT (m :: * -> *) s t a = STT { unSTT :: IO a } class STTBase m where -- | Runs the monad
− Data/Array/Vector/STFreeze.hs
@@ -1,236 +0,0 @@--------------------------------------------------------------------------------- |--- Module: Data.Array.Vector.Freeze--- License: BSD3--- Maintainer: Reiner Pope <reiner.pope@gmail.com>--- Stability: experimental--- Portability: portable------ Support for using uvector's 'MUArr's in the new ST monad. Reexports--- all of "Data.Array.Vector"'s API, but all the ST operations are--- done with the new ST monad.------ Notable differences in API:------ * the former @unsafeFreezeMU@ is in fact safe, and is now named 'freezeMU'.------ * similarly for @unsafeFreezeAllMU@------ * @newU@ is no longer required as a primitive; it can be implemented using @runSTT@ and @freezeAllMU@, mentioning no unsafe operations.--------- Example demonstration of using the new ST monad:------ @foo :: (UArr Int, UArr Int)---foo = 'runST' go where--- go :: forall s. 'ST' ('Normal' s) ('Freeze' s) (UArr Int, UArr Int)--- go = newMU 5 >>>= \a1 ->--- newMU 6 >>>= \a2 ->--- writeMU a1 0 3 >>>= \() ->--- writeMU a2 1 2 >>>= \() ->--- freezeAllMU a1 >>>= \v1 ->--- freezeAllMU a2 >>>= \v2 ->--- return (v1,v2)@--------------------------------------------------------------------------------module Data.Array.Vector.STFreeze (-- -- * Array classes- UA,-- -- (*) The pure and mutable array types- UArr, MUArr,-- -- * Streaming pure arrays- V.streamU, V.unstreamU,-- -- * Conversions to\/from lists- V.toU, V.fromU,-- -- * Basic operations on pure arrays- -- ** Introducing and eliminating UArrs- V.emptyU,- V.singletonU,-- -- ** Basic interface- V.consU,- V.snocU,- -- uncons- V.appendU,- V.headU,- V.lastU,- V.tailU,- V.initU,- V.nullU,- V.unitsU,- V.lengthU,-- -- * Transforming UArrs- V.mapU,-- -- * Reducing UArrs (folds)- V.foldU,- V.fold1U,- V.fold1MaybeU,-- V.foldlU,- V.foldl1U,- V.foldl1MaybeU,-- -- ** Logical operations- V.andU,- V.orU,- V.anyU,- V.allU,-- -- * Arithmetic operations- V.sumU, V.productU,- V.maximumU, V.minimumU,- V.maximumByU, V.minimumByU,--- maximumIndexU, minimumIndexU,--- maximumIndexByU, minimumIndexByU,-- -- * Building UArrs- -- ** Scans- V.scanlU,- V.scanl1U,- {-scanrU, scanr1U,-}- V.scanU,- V.scan1U,- V.scanResU,-- -- ** Accumulating UArrs- V.mapAccumLU,-- -- ** Generating UArrs- V.iterateU,- V.replicateU,- V.replicateEachU,-- -- ** Unfolding UArrs-- V.unfoldU, -- -- * Subarrays-- -- ** Breaking arrays- V.sliceU,--- extractU,- V.takeU,- V.dropU,- V.splitAtU,- V.takeWhileU,- V.dropWhileU,- {- spanU, breakU,-}-- -- * Searching Arrays-- -- ** Searching by equality- V.elemU,- V.notElemU,-- -- ** Searching with a predicate- V.filterU,- V.findU,- V.findIndexU,-- -- * Indexing UArrs- V.indexU,- V.lookupU,-- -- * Zipping and unzipping- V.zipU, V.zip3U,- V.unzipU, V.unzip3U,- V.zipWithU,- V.zipWith3U,- V.fstU,- V.sndU,-- -- * Enumerations- V.enumFromToU,- V.enumFromToFracU,- V.enumFromThenToU,- V.enumFromStepLenU,- V.enumFromToEachU,--{-- -- * Low level conversions- -- ** Copying arrays- -- ** Packing CStrings and pointers- -- ** Using UArrs as CStrings- -- * I\/O with UArrs-- -- creating them, generating new arrays from old ones.--}---------------------------------------------------------------------------- V.combineU,- V.packU,- V.indexedU,- V.repeatU,--{-- -- * Permutations- -- permuteU, bpermuteU, bpermuteDftU, reverseU, updateU,-- -- * Searching- {- indexOfU,-}-- -- * Arrays of pairs- {-crossU,-}-- -- * Random arrays- -- randomU, randomRU,--}-- -- * I\/O- V.UIO(..),-- -- * Operations on mutable arrays- V.lengthMU, newMU, readMU, writeMU, freezeMU, freezeAllMU,- copyMU, permuteMU, atomicUpdateMU, unstreamMU,- memcpyMU, memcpyOffMU, memmoveOffMU,- V.unsafeZipMU, V.unsafeUnzipMU,-- - ) where--import Control.Monad.ST.Freeze-import qualified Data.Array.Vector as V-import Data.Array.Vector(MUArr, UArr, UA, (:*:))--newMU :: (UA e, MonadST st) => Int -> STNormal st s (MUArr e s)-newMU i = liftST (V.newMU i)--readMU :: (UA e, MonadST st) => MUArr e s -> Int -> STRead st s e-readMU a i = liftRead (V.readMU a i)--writeMU :: (UA e, MonadST st) => MUArr e s -> Int -> e -> STNormal st s ()-writeMU a i e = liftST (V.writeMU a i e)--copyMU :: (UA e, MonadST st) => MUArr e s -> Int -> UArr e -> STNormal st s ()-copyMU m i a = liftST (V.copyMU m i a)--freezeMU :: (UA e, MonadST st) => MUArr e s -> Int -> STFreeze st s (UArr e)-freezeMU m i = liftUnsafeFreeze (V.unsafeFreezeMU m i)--memcpyMU :: (UA e, MonadST st) => MUArr e s -> MUArr e s -> Int -> STNormal st s ()-memcpyMU m1 m2 i = liftST (V.memcpyMU m1 m2 i)--memcpyOffMU :: (UA e, MonadST st) => MUArr e s -> MUArr e s -> Int -> Int -> Int -> STNormal st s ()-memcpyOffMU m1 m2 i j k = liftST (V.memcpyOffMU m1 m2 i j k)--memmoveOffMU :: (UA e, MonadST st) => MUArr e s -> MUArr e s -> Int -> Int -> Int -> STNormal st s ()-memmoveOffMU m1 m2 i j k = liftST (V.memmoveOffMU m1 m2 i j k)--freezeAllMU :: (UA e, MonadST st) => MUArr e s -> STFreeze st s (UArr e)-freezeAllMU m = liftUnsafeFreeze (V.unsafeFreezeAllMU m)--permuteMU :: (UA e, MonadST st) => MUArr e s -> UArr e -> UArr Int -> STNormal st s ()-permuteMU m a b = liftST (V.permuteMU m a b)--atomicUpdateMU :: (UA e, MonadST st) => MUArr e s -> UArr (Int :*: e) -> STNormal st s ()-atomicUpdateMU m a = liftST (V.atomicUpdateMU m a)--unstreamMU m s = liftST (V.unstreamMU m s)
+ Data/Vector/Generic/Mutable/STFreeze.hs view
@@ -0,0 +1,58 @@+-----------------------------------------------------------------------------+-- |+-- Module: Data.Array.Vector.Freeze+-- License: BSD3+-- Maintainer: Reiner Pope <reiner.pope@gmail.com>+-- Stability: experimental+-- Portability: portable+--+-- Proof-of-concept support for using vector's 'MVector's in the new ST monad. Reexports+-- only a minimal API from "Data.Vector.Generic.Mutable", with all ST operations+-- done in the new ST (indexed) monad.+--+-- Notable differences in API:+--+-- * the former @unsafeFreeze@ is in fact safe, and is now named 'freeze'.+--+-- Example demonstration of using the new ST monad:+--+-- @foo :: forall v. Vector v Int => (v Int, v Int)+--foo = 'runST' go where+-- go :: forall s. 'ST' ('Normal' s) ('Freeze' s) (v Int, v Int)+-- go = new 5 >>>= \a1 ->+-- new 6 >>>= \a2 ->+-- write a1 0 3 >>>= \() ->+-- write a2 1 2 >>>= \() ->+-- freeze a1 >>>= \v1 ->+-- freeze a2 >>>= \v2 ->+-- return (v1,v2)@+-----------------------------------------------------------------------------++module Data.Vector.Generic.Mutable.STFreeze (+ new,+ read,+ write,+ copy,+ freeze,+ ) where++import Prelude hiding(read)+import Control.Monad.ST.Freeze+import qualified Data.Vector.Generic.Mutable as V+import qualified Data.Vector.Generic as V(Vector, Mutable, unsafeFreeze)+++new :: (V.MVector v a, MonadST st) => Int -> STNormal st s (v s a)+new i = liftST (V.new i)++read :: (V.MVector v a, MonadST st) => v s a -> Int -> STRead st s a+read a i = liftRead (V.read a i)++write :: (V.MVector v a, MonadST st) => v s a -> Int -> a -> STNormal st s ()+write a i e = liftST (V.write a i e)++copy :: (V.MVector v a, MonadST st) => v s a -> v s a -> STNormal st s ()+copy a b = liftST (V.copy a b)++freeze :: (V.Vector v a, MonadST st) => (V.Mutable v) s a -> STFreeze st s (v a)+freeze m = liftUnsafeFreeze (V.unsafeFreeze m)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Reiner Pope++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Reiner Pope nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
safe-freeze.cabal view
@@ -1,7 +1,8 @@ Name: safe-freeze-Version: 0.0+Version: 0.2 Synopsis: Support for safely freezing multiple arrays in the ST monad. License: BSD3+License-file: LICENSE Author: Reiner Pope <reiner.pope@gmail.com> Maintainer: Reiner Pope <reiner.pope@gmail.com> Stability: experimental@@ -9,24 +10,38 @@ Support for safely freezing multiple arrays in the ST monad. Developed in- <http://reinerp.wordpress.com/2009/09/18/making-runstarray-more-flexible/>.+ <http://haskell.reinerpope.com/2009/09/making-runstarray-more-flexible-or.html>. . The new monad is defined in Control.Monad.ST.Freeze. An example- of its use is given in Data.Array.Vector.STFreeze. The modules- Data.Array.Vector.STFreeze and Data.STRef.Freeze are reexports of- Data.Array.Vector and Data.STRef but lifted to the new ST monad.+ of its use is given in Data.Vector.Generic.Mutable.STFreeze. The+ module Data.STRef.Freeze is a reexports of Data.STRef but lifted+ to the new ST monad. Category: Data Tested-with: Cabal-Version: >= 1.6 Build-type: Simple--Library- Exposed-Modules: Control.Monad.ST.Freeze- Data.STRef.Freeze- Data.Array.Vector.STFreeze- Other-Modules: - Extensions: EmptyDataDecls, Rank2Types, KindSignatures, FlexibleInstances, TypeOperators- Build-Depends: base<4.2, category-extras>=0.44.4 && <0.54, transformers<0.2, uvector>=0.1.0.4 && <0.2+homepage: https://github.com/reinerp/safe-freeze+bug-reports: https://github.com/reinerp/safe-freeze/issues +source-repository head+ type: git+ location: git://github.com/reinerp/safe-freeze.git +Library+ Exposed-Modules: + Control.Monad.ST.Freeze+ Data.STRef.Freeze+ Data.Vector.Generic.Mutable.STFreeze+ Other-Modules:+ Extensions:+ EmptyDataDecls,+ Rank2Types,+ KindSignatures,+ FlexibleInstances,+ TypeOperators+ Build-Depends: + base< 5, + indexed < 0.2,+ mtl == 2.0.*,+ vector>=0.4 && < 0.10