packages feed

persistent-refs 0.1 → 0.2

raw patch · 4 files changed

+37/−46 lines, 4 filesdep −lensdep ~containersdep ~mtldep ~ref-fdPVP ok

version bump matches the API change (PVP)

Dependencies removed: lens

Dependency ranges changed: containers, mtl, ref-fd, transformers

API changes (from Hackage documentation)

- Control.Monad.ST.Persistent: data ST s a
- Data.STRef.Persistent: instance MonadRef (STRef s) (ST s)
+ Control.Monad.ST.Persistent: data STT s m a
+ Control.Monad.ST.Persistent: runSTT :: Monad m => (forall s. STT s m a) -> m a
+ Control.Monad.ST.Persistent: type ST s a = STT s Identity a
+ Data.STRef.Persistent: instance Monad m => MonadRef (STRef s) (STT s m)

Files

persistent-refs.cabal view
@@ -1,5 +1,5 @@ name:                persistent-refs-version:             0.1+version:             0.2 license:             BSD3 license-file:        LICENSE author:              Adam C. Foltzer@@ -9,7 +9,7 @@ category:            Data build-type:          Simple cabal-version:       >=1.8-synopsis:            +synopsis:     Haskell references backed by an IntMap for persistence and reversibility. description:     This library provides support for a persistent version of the@@ -24,11 +24,10 @@  library   build-depends:     base         >= 4.0 && < 5,-                     containers   >= 0.4 && < 0.6,-                     lens         >= 3.7 && < 3.9,-                     mtl          >= 2.1 && < 2.2,-                     ref-fd       >= 0.3 && < 0.4,-                     transformers >= 0.3 && < 0.4+                     containers   >= 0.4,+                     mtl          >= 2.1,+                     ref-fd       >= 0.3,+                     transformers >= 0.3   exposed-modules:   Control.Monad.ST.Persistent,                      Data.STRef.Persistent   other-modules:     Control.Monad.ST.Persistent.Internal
src/Control/Monad/ST/Persistent.hs view
@@ -20,6 +20,9 @@     -- * The Persistent 'ST' Monad     ST   , runST+    -- * The Persistent 'ST' Monad transformer+  , STT+  , runSTT   ) where  import Control.Monad.ST.Persistent.Internal
src/Control/Monad/ST/Persistent/Internal.hs view
@@ -1,49 +1,33 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE Rank2Types #-}-{-# LANGUAGE TemplateHaskell #-} -{-# OPTIONS_GHC -ddump-splices #-} module Control.Monad.ST.Persistent.Internal where  import Control.Applicative (Applicative)-import Control.Lens import Control.Monad.State.Strict-import Data.IntMap-import GHC.Base--data Heap = Heap { _heap :: IntMap Any, _next :: Int }--- makeLenses ''Heap+import Data.Functor.Identity+import Data.IntMap (IntMap, empty)+import GHC.Base (Any) --- manually using splices because TH before 7.6 can't handle Any-heap :: Lens' Heap (IntMap Any)-heap _f_a1T2 (Heap __heap'_a1T3 __next_a1T5)-  = ((\ __heap_a1T4 -> Heap __heap_a1T4 __next_a1T5)-     `fmap` (_f_a1T2 __heap'_a1T3))-{-# INLINE heap #-}-next :: Lens' Heap Int-next _f_a1T6 (Heap __heap_a1T7 __next'_a1T8)-  = ((\ __next_a1T9 -> Heap __heap_a1T7 __next_a1T9)-     `fmap` (_f_a1T6 __next'_a1T8))-{-# INLINE next #-}+data Heap = Heap { heap :: IntMap Any, next :: Int }  emptyHeap :: Heap-emptyHeap = Heap { _heap = empty, _next = minBound }+emptyHeap = Heap { heap = empty, next = minBound }  -- | A persistent version of the 'Control.Monad.ST.ST' monad.-newtype ST s a = ST (State Heap a)-    deriving (Functor, Applicative, Monad)+type ST s a = STT s Identity a  -- | Run a computation that uses persistent references, and return a -- pure value. The rank-2 type offers similar guarantees to -- 'Control.Monad.ST.runST'. runST :: (forall s. ST s a) -> a-runST (ST c) = evalState c emptyHeap---- I'm not sure whether the semantics of this transformer actually--- make any sense, so I'm not exporting this for now...+runST = runIdentity . runSTT  newtype STT s m a = STT (StateT Heap m a)-    deriving (Functor, Applicative, Monad, MonadIO)+    deriving (Functor, Applicative, Monad, MonadIO, MonadTrans) +-- | Run a computation that uses persistent references, and return a+-- pure value. The rank-2 type offers similar guarantees to+-- 'Control.Monad.ST.runST'. runSTT :: Monad m => (forall s. STT s m a) -> m a runSTT (STT c) = evalStateT c emptyHeap
src/Data/STRef/Persistent.hs view
@@ -25,7 +25,7 @@   , modifyRef'   ) where -import Control.Lens+import Control.Monad.State import Control.Monad.Ref import Control.Monad.ST.Persistent.Internal import Data.IntMap as IntMap@@ -34,21 +34,26 @@  newtype STRef s a = STRef Int -newSTRef :: a -> ST s (STRef s a)-newSTRef x = ST $ do -    i <- next <<%= (+1)-    heap %= insert i (unsafeCoerce x)+newSTRef :: Monad m => a -> STT s m (STRef s a)+newSTRef x = STT $ do+    s <- get+    let i = next s+        heap' = insert i (unsafeCoerce x) (heap s)+        next' = next s + 1+    put $ s { heap = heap', next = next' }     return (STRef i) -readSTRef :: STRef s a -> ST s a-readSTRef (STRef i) = -    ST $ uses heap (unsafeCoerce . fromJust . IntMap.lookup i)+readSTRef :: Monad m => STRef s a -> STT s m a+readSTRef (STRef i) = STT $+    return . unsafeCoerce . fromJust . IntMap.lookup i =<< gets heap -writeSTRef :: STRef s a -> a -> ST s ()-writeSTRef (STRef i) x = -    ST $ heap %= insert i (unsafeCoerce x)+writeSTRef :: Monad m => STRef s a -> a -> STT s m ()+writeSTRef (STRef i) x = STT $ do+    s <- get+    let heap' = insert i (unsafeCoerce x) (heap s)+    put $ s { heap = heap' } -instance MonadRef (STRef s) (ST s) where+instance Monad m => MonadRef (STRef s) (STT s m) where     newRef   = newSTRef     readRef  = readSTRef     writeRef = writeSTRef