[Haskell-cafe] Re: [Haskell] Parsec question: attempted 'notMatching' combinator

Andrew Pimlott andrew at pimlott.net
Wed Feb 18 10:18:16 EST 2004


On Wed, Feb 18, 2004 at 10:48:39AM +0000, Graham Klyne wrote:
> [To Haskell-cafe...]
> 
> At 16:57 17/02/04 -0500, Andrew Pimlott wrote:
> >On Tue, Feb 17, 2004 at 07:48:52PM +0000, Graham Klyne wrote:
> >> [[
> >> notMatching :: Show a => GenParser tok st a -> GenParser tok st ()
> >> notMatching p = do { a <- try p ; unexpected (show a) } <|> return ()
> >> ]]
> >
> >    notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()
> >    notFollowedBy' p    = do  res <-  do a <- try p; return $ Just a
> >                                      <|>
> >                                      return Nothing
> >                              case res of Just a  -> unexpected (show a)
> >                                          Nothing -> return ()
> 
> I don't see why that would work where the above case does not;  i.e. when p 
> consumes no input.

Because (in my version) when p succeeds, the left side of <|> succeeds;
in the original, the left side of <|> always failed.  Instead of
throwing the error immediately after p succeeds, I "package it up" in
Just a and throw it later.  So there is no possibility for the right
side of <|> to be evaluated "by accident", as there was in the original.

I think the key is to understand the paragraph in a previous message
about the original implementation being a "dirty trick".

Andrew


More information about the Haskell-Cafe mailing list