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 9024503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:01:43+00:00 2026-06-16T06:01:43+00:00

In learning Parsec, I’ve found that somewhat verbose rules like type PhonemeClassMap = Map

  • 0

In learning Parsec, I’ve found that somewhat verbose rules like

type PhonemeClassMap = Map Char String
type ContextElement = Parser String

phonemeContext :: Parsec String PhonemeClassMap ContextElement
phonemeContext = do
    c <- lower
    return $ char c

can be simplified by lifting functions like char into the Parsec / ParsecT monad.

phonemeContext :: Parsec String PhonemeClassMap ContextElement
phonemeContext = liftM char lower

Now I’m trying to simplify a rule which modifies the user state:

import Data.Map (insert)

phonemeClassDefinition :: Parsec String PhonemeClassMap ()
phonemeClassDefinition = do
    upperChar <- upper
    lowerChars <- char ':' >> spaces >> many1 lower
    modifyState (insert upperChar lowerChars)

I can easily lift insert :: Char -> String -> PhonemeClassMap -> PhonemeClassMap to make the following improvement:

phonemeClassDefinition = do
    f <- liftM2 insert upper (char ':' >> spaces >> many1 lower)
    modifyState f

Is there any way to state these two expressions as one? The same lifting technique does not work for modifyState :: Monad m -> (u -> u) -> ParsecT s u m ().

  • 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-06-16T06:01:45+00:00Added an answer on June 16, 2026 at 6:01 am

    In this case, you are looking for monadic bind >>= :: Monad m => (a -> m b) -> m a -> m b which allows you to apply a function that takes a pure a and returns a monadic action to a monadic a (i.e. apply the function “through” the monad). This function is actually an integral part of the monadic type class, and is what the <- in do notation desugars to under the hood.

    (Side note, unlike liftM2, liftM3…, there doesn’t appear to be predefined bindM2 :: Monad m => (a -> b -> m c) -> m a -> m b -> m c (or bindM3 etc) for convenience. (Hoogle draws a blank.))

    Also, Parsec parsers often benefit (stylistically and otherwise) from using its Applicative and Functor instances, not just its Monad one, specifically <$> (alias of fmap/liftM) and the various (semi-)equivalents of monadic ap & >>: <*>, <* & *>.

    phonemeContext = char <$> lower
    
    phonemeClassDefinition = (insert <$> upper <*> (char ':' *> spaces *> many1 lower)) >>= modifyState
    

    (Note that @melpomene’s =<< is just flip (>>=), i.e. with the arguments swapped.)

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

Sidebar

Related Questions

Learning a lot in my few years of programming that the best projects are
Learning Scala currently and needed to invert a Map to do some inverted value->key
I'm learning jQuery and I'd like to use it to solve an old problem
I'm learning to write a simple parser-combinator. I'm writing the rules from bottom up
i have just started learning linq because i like the sound of it. and
I am learning LINQ and this seems like a fairly simple problem. I have
I'm trying to implement simple parser in haskell using parsec library (for learning purposes).
I am learning C# and keep getting an error Cannot implicitly convert type 'int'
I'm just learning Haskell and I am trying to write some code that simply
I'm just learning the Scala parser combinator library. I've experimented a working parser that

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.