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

Konst Sushenko konsu@microsoft.com
Mon, 19 Feb 2001 18:07:17 -0800


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C09AE1.D954882E
Content-Type: text/plain;
	charset="iso-8859-1"

hello,
 
i have a parser which is a state transformer monad, and i need to implement
a lookahead function, which applies a given parser but does not change the
parser state. so i wrote a function which reads the state, applies the
parser and restores the state (the State monad is derived from the paper
"Monadic parser combinators" by Hutton/Meijer):
 
 
type Parser a = State String Maybe a

lookahead  :: Parser a -> Parser a
lookahead p = do { s <- fetch
                 ; x <- p
                 ; set s
                 ; return x
                 }

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.
 
i would appreciate any advise
 
thanks
konst

------_=_NextPart_001_01C09AE1.D954882E
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.50.4208.1700" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2>hello,</FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" size=2>i have a 
parser which is a state transformer monad, and i need to implement a lookahead 
function, which applies a given parser but does not change the parser state. so 
i wrote a function which reads the state, applies the parser and restores the 
state (the State monad is derived from the paper "Monadic parser combinators" by 
Hutton/Meijer):</FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" size=2>type Parser 
a = State String Maybe a<BR></FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2>lookahead&nbsp; :: Parser a -&gt; Parser a<BR>lookahead p = do { s &lt;- 
fetch<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
; x &lt;- 
p<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
; set 
s<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
; return 
x<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR></FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" size=2>now i am 
curious if it is possible to run the given parser (state transformer) in a 
context of a state reader somehow, so&nbsp;as the state gets preserved 
automatically. something that would let me omit the calls&nbsp;to fetch and set 
methods.</FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" size=2>i would 
appreciate any advise</FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001></SPAN>&nbsp;</DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2>thanks</FONT></SPAN></DIV>
<DIV><SPAN class=942085801-20022001><FONT face="Courier New" 
size=2>konst</DIV></FONT></SPAN></BODY></HTML>

------_=_NextPart_001_01C09AE1.D954882E--