Addition of Reader interface#70
Conversation
|
This PR essentially allows us to forget about passing around the generator and ability to grab it from the reader environment. Here are some examples: internalMutability ::
(HasGen env g, MonadReader env m, MonadRandom g m, PrimMonad m) => m Word
internalMutability = do
w1 <- uniformEnvM
ref <- newMutVar w1
w2 <- uniformEnvM
modifyMutVar' ref (+w2)
readMutVar ref
data App = App
{ appGen :: MutGen RealWorld StdGen
, appDbConnection :: () -- hypothetical
}
instance HasGen App (MutGen RealWorld StdGen) where
getGen = appGen
runApp :: App -> ReaderT App m a -> m a
runApp app action = runReaderT action app
data AppPure = AppPure
{ appPureGen :: PureGen StdGen
, appPureDbConnection :: () -- hypothetical
}
instance HasGen AppPure (PureGen StdGen) where
getGen = appPureGen
runAppPure :: AppPure -> ReaderT AppPure m a -> m a
runAppPure app action = runReaderT action app
main :: IO ()
main = do
let g = mkStdGen 217
gen <- thawGen $ MutGen g
runApp (App gen ()) $ do
v <- internalMutability
liftIO $ print v
runReaderT (internalMutability >>= liftIO . print) gen
runGenStateT_ g $ \pg -> runAppPure (AppPure pg ()) $ do
v1 <- internalMutability
liftIO $ print v1
v2 <- internalMutability
liftIO $ print v2
let (v1, v2) =
runST $ runGenStateT_ g $ \pg -> runAppPure (AppPure pg ()) $ do
v1' <- internalMutability
v2' <- internalMutability
pure (v1', v2')
print v1 >> print v2Runnnig this produces: λ> main
13567046000262619
2533773515975174092
13567046000262619
2533773515975174092
13567046000262619
2533773515975174092 |
|
This looks like it allows users to avoid boilerplate if they are using the I've raised a similar point at other occasions. My feeling is that as a core library, |
|
@curiousleo I am certainly OK with not including this by default and instead make another library handle this case. Closing this PR, but just in case someone complains again about an extra argument to monadic functions being a problem you can point them to this PR ;) |
No description provided.