Adding state to a library

I have a library of functions that all take cfg parameter (and usually others) and return results in the IO monad. It is sometimes useful to drop the config parameter by using a state-like monad.. I have found that I can wrap all my functions like so: withLibrary cfg f = f cfg stateF a b c d =

My apologies. My last message was sent prematurely.
I have a library of functions that all take a config parameter (and usually
others) and return results in the IO monad.
It is sometimes useful to drop the config parameter by using a state-like
monad..
I have found that I can wrap all my functions like so:
withLibrary cfg f = f cfg
stateF a b c d =
getConfig >>= \cfg -> liftIO $ withLibrary cfg
libraryF a b c d
notice that I need stateF and libraryF lines, each with n parameters.
Upgrading my library like this is rather tedious.
I would prefer to just write something like
stateF = upgrade libraryF
but I can find no way to define the function upgrade in Haskell.
This must be a fairly common problem. Is there a simple solution?
On Sun, Dec 18, 2011 at 10:20 PM, Kevin Jardine
I have a library of functions that all take cfg parameter (and usually others) and return results in the IO monad.
It is sometimes useful to drop the config parameter by using a state-like monad..
I have found that I can wrap all my functions like so:
withLibrary cfg f = f cfg
stateF a b c d =

On 18 December 2011 22:26, Kevin Jardine
I have a library of functions that all take a config parameter (and usually others) and return results in the IO monad.
It is sometimes useful to drop the config parameter by using a state-like monad..
If you're not modifying the configuration, a reader monad transformer is probably enough: http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/html/Co... You probably want to define your own monad transformer for your library: newtype MyMonad m a = M {unM :: ReaderT Config m a} deriving (Functor, Applicative, Monad, MonadTrans, MonadIO) getConfig :: MyMonad m Config getConfig = M ask
I have found that I can wrap all my functions like so:
withLibrary cfg f = f cfg
This can now be defined as: withLibrary :: Config -> MyMonad m a -> m a withLibrary cfg m = runReaderT (unM m) cfg
stateF a b c d = getConfig >>= \cfg -> liftIO $ withLibrary cfg libraryF a b c d
notice that I need stateF and libraryF lines, each with n parameters.
Upgrading my library like this is rather tedious.
I would prefer to just write something like
stateF = upgrade libraryF
but I can find no way to define the function upgrade in Haskell.
This must be a fairly common problem. Is there a simple solution?
What do you mean by "upgrading"? Cheers, Bas

By upgrading, I meant move a function from an IO monad with a config
parameter to a state monad without one.
On Dec 18, 10:52 pm, Bas van Dijk
On 18 December 2011 22:26, Kevin Jardine
wrote: I have a library of functions that all take a config parameter (and usually others) and return results in the IO monad.
It is sometimes useful to drop the config parameter by using a state-like monad..
If you're not modifying the configuration, a reader monad transformer is probably enough:
http://hackage.haskell.org/packages/archive/transformers/0.2.2.0/doc/...
You probably want to define your own monad transformer for your library:
newtype MyMonad m a = M {unM :: ReaderT Config m a} deriving (Functor, Applicative, Monad, MonadTrans, MonadIO)
getConfig :: MyMonad m Config getConfig = M ask
I have found that I can wrap all my functions like so:
withLibrary cfg f = f cfg
This can now be defined as:
withLibrary :: Config -> MyMonad m a -> m a withLibrary cfg m = runReaderT (unM m) cfg
stateF a b c d = getConfig >>= \cfg -> liftIO $ withLibrary cfg libraryF a b c d
notice that I need stateF and libraryF lines, each with n parameters.
Upgrading my library like this is rather tedious.
I would prefer to just write something like
stateF = upgrade libraryF
but I can find no way to define the function upgrade in Haskell.
This must be a fairly common problem. Is there a simple solution?
What do you mean by "upgrading"?
Cheers,
Bas
_______________________________________________ Haskell-Cafe mailing list Haskell-C...@haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Bas van Dijk
-
Kevin Jardine