Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 605717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:10:34+00:00 2026-05-13T17:10:34+00:00

I’m trying to use MonadError together with Parsec. I’ve come up with the following

  • 0

I’m trying to use MonadError together with Parsec. I’ve come up with the following code snippet:

f5 = do
    char 'a'
    throwError "SomeError"

f6 = f5 `catchError` (\e -> unexpected $ "Got the error: " ++ e)

ret = runErrorT (runParserT f6 () "stdin" "a")

However, ret is Left "SomeError", it seems the catchError doesn’t have any effect. What’s the right way to use MonadError here?

I’d prefer to use MonadError over Parsec’s own error handling, as for example when I have:

try (many1 parser1) <|> parser2

If parser1 fails here, parser2 will continue, but I’d like to have an exception which aborts the parsing entirely.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:10:34+00:00Added an answer on May 13, 2026 at 5:10 pm

    I’m under the impression that you’re trying to involve MonadError for the wrong reason.

    In the try (many1 parser1) <|> parser2, the behaviour you’re trying to avoid stems from the use of try and <|> — if you don’t like it, use different combinators. Perhaps an expression like (many1 parser1) >> parser2 would work better for you? (This discards the results from (many1 parser1); you could of course use >>= and combine the results from (many1 parser1) with those from parser2.)


    (Note: Below this point, there is no really good solution to the problem at hand, just some musings as to why some things probably won’t work… Hopefully this may be (somewhat) enlightening, but don’t expect too much.)

    A closer examination of ParsecT / MonadError interaction. I’m afraid it’s a bit messy and I’m still not really sure how best to go about doing what the OP wants to do, but I’m hoping the following will at least provide insight into the reasons for the lack of success of the original approach.

    Firstly, note that it is not correct to say that Parsec is an instance of MonadError. Parsec is the monad produced by ParsecT when the inner monad is Identity; ParsecT produces instances of MonadError if and only if it is given an inner monad which is itself an instance of MonadError to work with. Relevant fragments of GHCi interactions:

    > :i Parsec
    type Parsec s u = ParsecT s u Identity
        -- Defined in Text.Parsec.Prim
    -- no MonadError instance
    
    instance (MonadError e m) => MonadError e (ParsecT s u m)
      -- Defined in Text.Parsec.Prim
    -- this explains why the above is the case
    -- (a ParsecT-created monad will only become an instance of MonadError through
    -- this instance, unless of course the user provides a custom declaration)
    

    Next, let’s have ourselves a working example with catchError and ParsecT. Consider this GHCi interaction:

    > (runParserT (char 'a' >> throwError "some error") () "asdf" "a" :: Either String (Either ParseError Char)) `catchError` (\e -> Right . Right $ 'z')
    Right (Right 'z')
    

    The type annotation appears necessary (this seems to make intuitive sense to me, but it isn’t pertinent to the original question, so I won’t try to elaborate). The type of the whole expression is determined by GHC to be as follows:

    Either String (Either ParseError Char)
    

    So, we’ve got a regular parse result — Either ParseError Char — wrapped in the Either String monad in place of the usual Identity monad. Since Either String is an instance of MonadError, we can use throwError / catchError, but the handler passed to catchError must of course produce a value of the correct type. That’s not very useful for breaking out of the parsing routine, I’m afraid.

    Back to the example code from the question. That does a slightly different thing. Let’s examine the type of ret as defined in the question:

    forall (m :: * -> *) a.
    (Monad m) =>
    m (Either [Char] (Either ParseError a))
    

    (According to GHCi… note that I had to lift the monomorphism restriction with {-# LANGUAGE NoMonomorphismRestriction #-} to have the code compile without type annotations.)

    That type is a hint as to the possibility of doing something amusing with ret. Here we go:

    > runParserT ret () "asdf" "a"
    Right (Left "some error")
    

    In hindsight, the handler given to catchError produces a value using unexpected, so of course it’s going to be (usable as) a parser… And I’m afraid I don’t see how to hammer this into something useful for breaking out of the parsing process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.