on develop help sign in
Use Control.Monad.Ref from the ref-tf package

The ref-tf package already contains classes for this purpose and also makes instances for transformer types. It also introduces an extra type classes for atomic updates. This removes some of the extra functions that the Control.Monad.Ref module provided (memoM, cacheM, cacheMWithTry, replaceRef).

commit94ba8c8a575aed16856688d5856be82089e3d33c authorBenno Fünfstück <benno.fuenfstueck@gmail.com> dateThu 9 Apr 2015, 18:36 (12 years ago) parent9ed658b
4 files changed+13−92
default.nix+2−2 reflex.cabal+4−4 src/Control/Monad/Ref.hs−85 src/Reflex/Spider/Internal.hs+7−1
default.nix+2−2
@@ -1,12 +1,12 @@
11{ mkDerivation, dependent-map, dependent-sum
2−, mtl, semigroups, these
2+, mtl, ref-tf, semigroups, these
33}:
44mkDerivation {
55 pname = "reflex";
66 version = "0.1";
77 src = builtins.filterSource (path: type: baseNameOf path != ".git") ./.;
88 buildDepends = [
9− dependent-map dependent-sum mtl semigroups these
9+ dependent-map dependent-sum mtl ref-tf semigroups these
1010 ];
1111 license = null;
1212}
reflex.cabal+4−4
@@ -20,11 +20,12 @@ library
2020 dependent-sum == 0.2.*,
2121 dependent-map == 0.1.*,
2222 semigroups == 0.16.*,
23− mtl == 2.2.*,
23+ mtl >= 2.1 && < 2.3,
2424 containers == 0.5.*,
2525 these == 0.4.*,
2626 primitive == 0.5.*,
27− template-haskell >= 2.9 && < 2.11
27+ template-haskell >= 2.9 && < 2.11,
28+ ref-tf == 0.4.*
2829
2930 exposed-modules:
3031 Reflex,
@@ -34,8 +35,7 @@ library
3435 Reflex.Dynamic,
3536 Reflex.Dynamic.TH,
3637 Reflex.Host.Class,
37− Data.Functor.Misc,
38− Control.Monad.Ref
38+ Data.Functor.Misc
3939
4040 other-extensions: TemplateHaskell
4141 ghc-prof-options: -fprof-auto
src/Control/Monad/Ref.hsdeleted−85
@@ -1,85 +0,0 @@
1−{-# LANGUAGE RecursiveDo, TypeFamilies, LambdaCase #-}
2−module Control.Monad.Ref where
3−
4−import Control.Monad
5−import Control.Monad.Trans
6−import Control.Monad.Reader
7−import Control.Monad.Writer
8−import Data.IORef
9−
10−class Monad m => MonadRef m where
11− type Ref m :: * -> *
12− newRef :: a -> m (Ref m a)
13− readRef :: Ref m a -> m a
14− writeRef :: Ref m a -> a -> m ()
15− atomicModifyRef :: Ref m a -> (a -> (a, b)) -> m b
16−
17−instance MonadRef IO where
18− type Ref IO = IORef
19− {-# INLINE newRef #-}
20− newRef = newIORef
21− {-# INLINE readRef #-}
22− readRef = readIORef
23− {-# INLINE writeRef #-}
24− writeRef = writeIORef
25− {-# INLINE atomicModifyRef #-}
26− atomicModifyRef r f = do
27− result <- atomicModifyIORef' r f
28−-- evaluate =<< readIORef r --TODO: Verify that ghcjs now strictly evaluates the values in atomicModifyIORef', then remove this line
29− return result
30−
31−{-# INLINE cacheM #-}
32−cacheM :: (MonadRef m, MonadRef m', Ref m ~ Ref m') => m' a -> m (m' a, m ())
33−cacheM a = do
34− r <- newRef undefined
35− let invalidate = writeRef r $ do
36− result <- a
37− writeRef r $ return result
38− return result
39− invalidate
40− return (join $ readRef r, invalidate)
41−
42−{-# INLINE cacheMWithTry #-}
43−cacheMWithTry :: (MonadRef m, MonadRef m', Ref m ~ Ref m') => m' a -> m (m' a, m (Maybe a), m ())
44−cacheMWithTry a = do
45− r <- newRef $ Left a
46− let invalidate = writeRef r $ Left a
47− get = readRef r >>= \case
48− Left a' -> do
49− result <- a'
50− writeRef r $ Right result
51− return result
52− Right result -> return result
53− tryGet = readRef r >>= \case
54− Left _ -> return Nothing
55− Right result -> return $ Just result
56− return (get, tryGet, invalidate)
57−
58−-- | Not thread-safe or reentrant
59−{-# INLINE memoM #-}
60−memoM :: (MonadRef m, MonadRef m', Ref m ~ Ref m') => m' a -> m (m' a)
61−memoM = liftM fst . cacheM
62−
63−{-# INLINE replaceRef #-}
64−replaceRef :: MonadRef m => Ref m a -> a -> m a
65−replaceRef r new = atomicModifyRef r $ \old -> (new, old)
66−
67−{-# INLINE modifyRef #-}
68−modifyRef :: MonadRef m => Ref m a -> (a -> a) -> m ()
69−modifyRef r f = atomicModifyRef r $ \a -> (f a, ())
70−
71−instance (Monoid w, MonadRef m) => MonadRef (WriterT w m) where
72− {-# SPECIALIZE instance Monoid w => MonadRef (WriterT w IO) #-}
73− type Ref (WriterT w m) = Ref m
74− newRef = lift . newRef
75− readRef = lift . readRef
76− writeRef r = lift . writeRef r
77− atomicModifyRef r f = lift $ atomicModifyRef r f
78−
79−instance MonadRef m => MonadRef (ReaderT r m) where
80− {-# SPECIALIZE instance MonadRef (ReaderT r IO) #-}
81− type Ref (ReaderT r m) = Ref m
82− newRef = lift . newRef
83− readRef = lift . readRef
84− writeRef r = lift . writeRef r
85− atomicModifyRef r f = lift $ atomicModifyRef r f
src/Reflex/Spider/Internal.hs+7−1
@@ -1282,10 +1282,12 @@ instance MonadRef EventM where
12821282 {-# INLINE newRef #-}
12831283 {-# INLINE readRef #-}
12841284 {-# INLINE writeRef #-}
1285− {-# INLINE atomicModifyRef #-}
12861285 newRef = liftIO . newRef
12871286 readRef = liftIO . readRef
12881287 writeRef r a = liftIO $ writeRef r a
1288+
1289+instance MonadAtomicRef EventM where
1290+ {-# INLINE atomicModifyRef #-}
12891291 atomicModifyRef r f = liftIO $ atomicModifyRef r f
12901292
12911293newtype SpiderHost a = SpiderHost { runSpiderHost :: IO a } deriving (Functor, Applicative, Monad, MonadFix, MonadIO)
@@ -1329,6 +1331,8 @@ instance MonadRef SpiderHost where
13291331 newRef = SpiderHost . newRef
13301332 readRef = SpiderHost . readRef
13311333 writeRef r = SpiderHost . writeRef r
1334+
1335+instance MonadAtomicRef SpiderHost where
13321336 atomicModifyRef r = SpiderHost . atomicModifyRef r
13331337
13341338instance MonadRef SpiderHostFrame where
@@ -1336,4 +1340,6 @@ instance MonadRef SpiderHostFrame where
13361340 newRef = SpiderHostFrame . newRef
13371341 readRef = SpiderHostFrame . readRef
13381342 writeRef r = SpiderHostFrame . writeRef r
1343+
1344+instance MonadAtomicRef SpiderHostFrame where
13391345 atomicModifyRef r = SpiderHostFrame . atomicModifyRef r