[Haskell-cafe] Upd: Parsec, updateState, and failure

Dimitry Golubovsky golubovsky at gmail.com
Sat May 31 10:39:47 EDT 2008


So far, I figured it out like this:

updateState ...
mbx <- (p3 >>= return . Just) <|> (return Nothing)
updateState ...
case mbx of
  Just x -> return x
  Nothing -> pzero

but this seems a bit clumsy - is there a more elegant solution?

---------- Forwarded message ----------
From: Dimitry Golubovsky <golubovsky at gmail.com>
Date: Sat, May 31, 2008 at 10:12 AM
Subject: Parsec, updateState, and failure
To: haskell <haskell-cafe at haskell.org>


If a parser which updated user state fails, will such update be reverted?

Suppose we have two parsers combined with <|>

p = p1 <|> p2

p1 has the following:

p1 = try $ do
 ... -- getting something from the input stream
 updateState (\st -> ...) -- updated state based on what gotten from the input
 x <- p3 -- p3 should see updated state and return something
 updateState (\st -> ...) -- updated state again (if p3 succeeded)
 return x

If p3 fails, p1 fails too (second updateState will not be reached).
But what will p2 (tried next) see in the user state? Will it be state
after the first updateState, or will failure of p1 roll the update

Is there any bracket- or try-catch-finally-like mechanism for Parsec?


Dimitry Golubovsky

Anywhere on the Web

Dimitry Golubovsky

Anywhere on the Web

More information about the Haskell-Cafe mailing list