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

  • Home
  • SEARCH
  • 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 6609679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:44:46+00:00 2026-05-25T19:44:46+00:00

I’ve written code with the following pattern several times recently, and was wondering if

  • 0

I’ve written code with the following pattern several times recently, and was wondering if there was a shorter way to write it.

foo :: IO String
foo = do
    x <- getLine
    putStrLn x >> return x

To make things a little cleaner, I wrote this function (though I’m not sure it’s an appropriate name):

constM :: (Monad m) => (a -> m b) -> a -> m a
constM f a = f a >> return a

I can then make foo like this:

foo = getLine >>= constM putStrLn

Does a function/idiom like this already exist? And if not, what’s a better name for my constM?

  • 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-25T19:44:47+00:00Added an answer on May 25, 2026 at 7:44 pm

    Well, let’s consider the ways that something like this could be simplified. A non-monadic version would I guess look something like const' f a = const a (f a), which is clearly equivalent to flip const with a more specific type. With the monadic version, however, the result of f a can do arbitrary things to the non-parametric structure of the functor (i.e., what are often called “side effects”), including things that depend on the value of a. What this tells us is that, despite pretending like we’re discarding the result of f a, we’re actually doing nothing of the sort. Returning a unchanged as the parametric part of the functor is far less essential, and we could replace return with something else and still have a conceptually similar function.

    So the first thing we can conclude is that it can be seen as a special case of a function like the following:

    doBoth :: (Monad m) => (a -> m b) -> (a -> m c) -> a -> m c
    doBoth f g a = f a >> g a
    

    From here, there are two different ways to look for an underlying structure of some sort.


    One perspective is to recognize the pattern of splitting a single argument among multiple functions, then recombining the results. This is the concept embodied by the Applicative/Monad instances for functions, like so:

    doBoth :: (Monad m) => (a -> m b) -> (a -> m c) -> a -> m c
    doBoth f g = (>>) <$> f <*> g
    

    …or, if you prefer:

    doBoth :: (Monad m) => (a -> m b) -> (a -> m c) -> a -> m c
    doBoth = liftA2 (>>)
    

    Of course, liftA2 is equivalent to liftM2 so you might wonder if lifting an operation on monads into another monad has something to do with monad transformers; in general the relationship there is awkward, but in this case it works easily, giving something like this:

    doBoth :: (Monad m) => ReaderT a m b -> ReaderT a m c -> ReaderT a m c
    doBoth = (>>)
    

    …modulo appropriate wrapping and such, of course. To specialize back to your original version, the original use of return now needs to be something with type ReaderT a m a, which shouldn’t be too hard to recognize as the ask function for reader monads.


    The other perspective is to recognize that functions with types like (Monad m) => a -> m b can be composed directly, much like pure functions. The function (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c) gives a direct equivalent to function composition (.) :: (b -> c) -> (a -> b) -> (a -> c), or you can instead make use of Control.Category and the newtype wrapper Kleisli to work with the same thing in a generic way.

    We still need to split the argument, however, so what we really need here is a “branching” composition, which Category alone doesn’t have; by using Control.Arrow as well we get (&&&), letting us rewrite the function as follows:

    doBoth :: (Monad m) => Kleisli m a b -> Kleisli m a c -> Kleisli m a (b, c)
    doBoth f g = f &&& g
    

    Since we don’t care about the result of the first Kleisli arrow, only its side effects, we can discard that half of the tuple in the obvious way:

    doBoth :: (Monad m) => Kleisli m a b -> Kleisli m a c -> Kleisli m a c
    doBoth f g = f &&& g >>> arr snd
    

    Which gets us back to the generic form. Specializing to your original, the return now becomes simply id:

    constKleisli :: (Monad m) => Kleisli m a b -> Kleisli m a a
    constKleisli f = f &&& id >>> arr snd
    

    Since regular functions are also Arrows, the definition above works there as well if you generalize the type signature. However, it may be enlightening to expand the definition that results for pure functions and simplify as follows:

    • \f x -> (f &&& id >>> arr snd) x
    • \f x -> (snd . (\y -> (f y, id y))) x
    • \f x -> (\y -> snd (f y, y)) x
    • \f x -> (\y -> y) x
    • \f x -> x.

    So we’re back to flip const, as expected!


    In short, your function is some variation on either (>>) or flip const, but in a way that relies on the differences–the former using both a ReaderT environment and the (>>) of the underlying monad, the latter using the implicit side-effects of the specific Arrow and the expectation that Arrow side effects happen in a particular order. Because of these details, there’s not likely to be any generalization or simplification available. In some sense, the definition you’re using is exactly as simple as it needs to be, which is why the alternate definitions I gave are longer and/or involve some amount of wrapping and unwrapping.

    A function like this would be a natural addition to a “monad utility library” of some sort. While Control.Monad provides some combinators along those lines, it’s far from exhaustive, and I could neither find nor recall any variation on this function in the standard libraries. I would not be at all surprised to find it in one or more utility libraries on hackage, however.

    Having mostly dispensed with the question of existence, I can’t really offer much guidance on naming beyond what you can take from the discussion above about related concepts.

    As a final aside, note also that your function has no control flow choices based on the result of a monadic expression, since executing the expressions no matter what is the main goal. Having a computational structure independent of the parametric content (i.e., the stuff of type a in Monad m => m a) is usually a sign that you don’t actually need a full Monad, and could get by with the more general notion of Applicative.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.