newbie: running a state transformer in context of a state reader

Marcin 'Qrczak' Kowalczyk
20 Feb 2001 18:17:07 GMT

Mon, 19 Feb 2001 18:07:17 -0800, Konst Sushenko <> pisze:

> now i am curious if it is possible to run the given parser (state
> transformer) in a context of a state reader somehow, so as the state
> gets preserved automatically. something that would let me omit the
> calls to fetch and set methods.

It should be possible to do something like this:

lookahead:: Parser a -> Parser a
lookahead p = do { s <- fetch
                 ; lift (evalState p s)

where evalState :: Monad m => State s m a -> s -> m a
      lift      :: Monad m => m a -> State s m a
are functions which should be available or implementable in a monad
transformer framework. I don't have the Hutton/Meijer's paper at hand
so I don't know if they provided them and under which names. Such
functions are provided e.g. in the framework provided with ghc (by
Andy Gill, inspired by Mark P Jones' paper "Functional Programming
with Overloading and Higher-Order Polymorphism").

This definition of lookahead uses a separate state transformer thread
instead of making changes in place and undoing them later. I don't
think that it could make sense to convert a state transformer to
a state reader by replacing its internals, because p does want to
transform the state locally; a value of type Parser a represents
a state transformation. The changes must be isolated from the main
parser, but they must happen in some context.

 __("<  Marcin Kowalczyk *
  ^^                      SYGNATURA ZASTĘPCZA