[Haskell-cafe] State separation/combination pattern question

Udo Stenzel u.stenzel at web.de
Fri Dec 22 08:31:29 EST 2006


Reto Kramer wrote:
> What I'm really looking for is not so much the chaining of StateT  
> compositions, but rather the isolation of StateA from StateB while  
> they both flow from the search loop into the respective library calls  
> (foo, bar) transparently to the application programmer.

How about this?

-- these two should be defined in two separate library modules, of course
trueFoo :: MonadState StateA m => m ()
trueBar :: MonadState StateB m => m ()

data AppStateRec = AppStateRec { a :: StateA, b :: StateB }

type Eval a = StateT AppStateRec Identity a

exec :: Eval ()
exec = do foo
  	  bar
  	  foo
  	  foo
  	  bar
  where
    -- you might want to define combinators for the following pattern,
    -- but for just two functions this is good enough
    foo = do AppStateRec a b <- get
             a' <- runStateT trueFoo a
	     put $ AppStateRec a' b
    bar = do AppStateRec a b <- get
             b' <- runStateT trueBar b
	     put $ AppStateRec a b'


-Udo
-- 
"In the software business there are many enterprises for which it is not
clear that science can help them; that science should try is not clear
either."
	-- E. W. Dijkstra
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20061222/10bda74e/attachment.bin


More information about the Haskell-Cafe mailing list