[Haskell-cafe] advice: instantiating/duplicating modules

Arie Peterson ariep at xs4all.nl
Fri Jun 29 13:49:26 EDT 2007

Nicolas Frisby wrote:

| I wrote a combination reader/writer monad (a la the RWS monad in the
| mtl) and I find myself wanting to use multiple instances of it in the
| same stack of transformers. The functional dependencies prevent this
| from working out. The class is called MonadRW and the transformer is
| called RWT.
| I find myself wishing I could import the same module twice, but
| instead of having two names for the same entity, I want the
| definitions proper of the module to be duplicated: I want two separate
| MonadRW classes and two separate RWT transformers. Since the module
| integrates the MonadRW and RWT transformer with the mtl, I would then
| only need to lift the instances of the instantiated MonadRW classes
| through the other RWTs.
| I'm rather unfamiliar with Template Haskell, but it sounds like it
| might fit the bill. Although, if I recall correctly, instances and
| type declarations splicing are yet to be implemented in GHC?
| Does anyone have any advice how to do this? I'm envisioning some
| preprocessing. I'd like to know if someone has developed a tool for
| this already or if this path has been attempted.

Sounds familiar :-).

I wrote some TH code to get "named" versions of the StateT monad
transformer. Example usage:

> {-# OPTIONS_GHC -fth #-}
> module Test where
> import NamedStateT
> data Settings = ...
> $(namedStateT "Settings" $ Just $ ConT ''Settings)
> q :: (MonadSettings m,MonadIO m) => m ()
> q = do
>   settings <- getSettings
>   liftIO $ print settings


- The second argument to namedStateT is the type of the state. |Nothing|
keeps it parameterised, like |StateT| itself.

- The big comment shows the code that would be generated by a typical
splicing, although the code may have been updated at some point without
changing the example accordingly.

- Not all instances and helper functions from Control.Monad.State are in
there; |modify| seems to be missing, for instance. If you need any, I can
add them (or you can do it yourself).

- I just saw I also have a NamedReaderT module, but it has never been
used, so it may not work. Yell if you think you can use it.

Kind regards,



Familiarity with the Haskell language is assumed, but no prior experience
with warm, fuzzy things is required.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NamedStateT.hs
Type: application/octet-stream
Size: 7544 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070629/19b06fac/NamedStateT-0001.obj

More information about the Haskell-Cafe mailing list