Stacking up state transformers

Iavor S. Diatchki
Tue, 04 Feb 2003 17:42:32 -0800


Guest, Simon wrote:
 > ...
 > Now what I want to do is have two elements of state, an element that 
gets backtracked,
 > and an element that doesn't.
 > My monad now looks like this:
 > type NondetState bs ns a = StateT bs (NondetT (StateT ns Maybe)) a
 > where bs is my backtracked state, and ns my non-backtracked state.
 > I can still access my backtracked state using 
Control.Monad.State.{get,put}, but
 > I can't access my non-backtracked state.
 > How do I burrow through the stack of monad transformers to get and put
 > the 'ns' state?

you can use the "lift" method.  that however can get pretty cumbersome. 
  an alternative way to deal with multiple transformers of the same type
(i.e. many states, environments, or output) is to parameterize the 
transformer operations by an inedx specifying *which* state 
(environemnt, etc) you want to access, or update. the indexes are 
basically just used for their types, and one hopes that can be optimized 
away, i wonder if GHC can do it... the parameterization can be done 
(reasonably) nicely with Haskell's class system. for an implementation 
you can take a look at:
i use the library quite regularly and it seems to work pretty well.

 > Supplementary question: What documentation should I be reading?
unfortunatelly there isn't much documentation.  there are some useful 
papers about monad transformers, one that comes to mind is
"Monad Transformers and Modular Interpreters"
by Sheng Liang, Paul Hudak, and Mark P. Jones

hope this helped

| Iavor S. Diatchki, Ph.D. student               |
| Department of Computer Science and Engineering |
| School of OGI at OHSU                          |
|               |