STMonadTrans 0.1 → 0.2
raw patch · 2 files changed
+78/−9 lines, 2 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Control.Monad.ST.Trans: instance (Functor m) => Functor (STT s m)
+ Control.Monad.ST.Trans: instance (Monad m, Functor m) => Applicative (STT s m)
+ Control.Monad.ST.Trans: instance (MonadFix m) => MonadFix (STT s m)
+ Control.Monad.ST.Trans: instance Functor (STTRet s)
+ Control.Monad.ST.Trans: unsafeIORefToSTRef :: IORef a -> STRef s a
+ Control.Monad.ST.Trans: unsafeIOToSTT :: (Monad m) => IO a -> STT s m a
+ Control.Monad.ST.Trans: unsafeSTRefToIORef :: STRef s a -> IORef a
+ Control.Monad.ST.Trans: unsafeSTToIO :: STT s IO a -> IO a
Files
- Control/Monad/ST/Trans.hs +60/−3
- STMonadTrans.cabal +18/−6
Control/Monad/ST/Trans.hs view
@@ -1,6 +1,5 @@ {-# LANGUAGE MagicHash, UnboxedTuples, Rank2Types, FlexibleInstances,- MultiParamTypeClasses #-}-{-# OPTIONS_GHC -fallow-undecidable-instances #-}+ MultiParamTypeClasses, UndecidableInstances, RecursiveDo #-} {- | Module : Control.Monad.ST.Trans Copyright : Josef Svenningsson 2008@@ -13,6 +12,13 @@ This library provides a monad transformer version of the ST monad. + Warning! This monad transformer should not be used with monads that+ can contain multiple answers, like the list monad. The reason is that + the will be duplicated across the different answers and this cause+ Bad Things to happen (such as loss of referential transparency). Safe + monads include the monads State, Reader, Writer, Maybe and + combinations of their corresponding monad transformers.+ -} module Control.Monad.ST.Trans( -- * The ST Monad Transformer@@ -32,19 +38,33 @@ numElementsSTArray, freezeSTArray, thawSTArray,- runSTArray+ runSTArray,+ -- * Unsafe Operations+ unsafeIOToSTT,+ unsafeSTToIO,+ unsafeSTRefToIORef,+ unsafeIORefToSTRef )where import GHC.Base import GHC.Arr (Ix(..), safeRangeSize, safeIndex, Array(..), arrEleBottom) +import Control.Monad.Fix import Control.Monad.Trans import Control.Monad.Error.Class import Control.Monad.Reader.Class import Control.Monad.State.Class import Control.Monad.Writer.Class +import Control.Applicative++import Data.IORef++import Unsafe.Coerce+import System.IO.Unsafe++ -- | 'STT' is the monad transformer providing polymorphic updateable references newtype STT s m a = STT (State# s -> m (STTRet s a)) unSTT (STT f) = f@@ -63,7 +83,28 @@ lift m = STT $ \st -> do a <- m return (STTRet st a)+ +liftSTT :: STT s m a -> State# s -> m (STTRet s a)+liftSTT (STT m) s = m s +instance (MonadFix m) => MonadFix (STT s m) where+ mfix k = STT $ \ s -> mdo+ ans@(STTRet _ r) <- liftSTT (k r) s+ return ans++instance Functor (STTRet s) where+ fmap f (STTRet s a) = STTRet s (f a)++instance Functor m => Functor (STT s m) where+ fmap f (STT g) = STT $ \s# -> (fmap . fmap) f (g s#)++instance (Monad m, Functor m) => Applicative (STT s m) where+ pure a = STT $ \s# -> return (STTRet s# a)+ (STT m) <*> (STT n) = STT $ \s1 ->+ do (STTRet s2 f) <- m s1+ (STTRet s3 x) <- n s2+ return (STTRet s3 (f x))+ -- | Mutable references data STRef s a = STRef (MutVar# s a) @@ -202,4 +243,20 @@ => (forall s . STT s m (STArray s i e)) -> m (Array i e) runSTArray st = runST (st >>= unsafeFreezeSTArray)+++{-# NOINLINE unsafeIOToSTT #-} +unsafeIOToSTT :: (Monad m) => IO a -> STT s m a+unsafeIOToSTT m = return $! unsafePerformIO m++unsafeSTToIO :: STT s IO a -> IO a+unsafeSTToIO m = runST $ unsafeCoerce m++-- This should work, as STRef and IORef should have identical internal representation+unsafeSTRefToIORef :: STRef s a -> IORef a+unsafeSTRefToIORef ref = unsafeCoerce ref++unsafeIORefToSTRef :: IORef a -> STRef s a+unsafeIORefToSTRef ref = unsafeCoerce ref+
STMonadTrans.cabal view
@@ -1,25 +1,37 @@ name: STMonadTrans-version: 0.1+version: 0.2+cabal-version: >= 1.6 license: BSD3 license-file: LICENSE author: Josef Svenningsson maintainer: josef.svenningsson@gmail.com category: Monads-synopsis: A monad transformer version of the ST monad-description: A monad transformer version of the ST monad-cabal-version: >=1.2 build-type: Simple+synopsis: A monad transformer version of the ST monad+description: + A monad transformer version of the ST monad + Warning! This monad transformer should not be used with monads that+ can contain multiple answers, like the list monad. The reason is that + the will be duplicated across the different answers and this cause+ Bad Things to happen (such as loss of referential transparency). Safe + monads include the monads State, Reader, Writer, Maybe and + combinations of their corresponding monad transformers.++source-repository head+ type: darcs+ location: http://patch-tag.com/r/josef/STMonadTrans+ flag splitBase description: Choose the new smaller, split-up base package. library if flag(splitBase)- build-depends: base >= 3, mtl, array+ build-depends: base >= 3, base < 4, mtl, array else build-depends: base < 3 exposed-modules: Control.Monad.ST.Trans extensions: MagicHash, UnboxedTuples, Rank2Types, FlexibleInstances, - MultiParamTypeClasses+ MultiParamTypeClasses, UndecidableInstances