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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:56:01+00:00 2026-06-17T15:56:01+00:00

I have some confusion with the function monad. The function monad is defined as

  • 0

I have some confusion with the function monad. The function monad is defined as follow:

instance Monad ((->) r) where
     return x = \_ -> x
     h >>= f = \w -> f (h w) w

I tried to play around with it by writing a binding operation:

( (*2) >>= (+10) ) 3 

(return 3) :: ((->) Int)

But it caused errors. And I also try to rewrite a function AddStuff into the binding operations.

addStuff = do
           a <- (*2)
           b <- (+10)
           return (a+b)

then convert this function into

addStuff' w = (*2)  w >>= (\a ->
              (+10) w >>= (\b ->
              return (a+b) ))

I check the type of the new function as see

addStuff :: (Monad m, Num (m b), Num b) => m b -> m b 

Why is that? How can I fix that?

  • 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-17T15:56:01+00:00Added an answer on June 17, 2026 at 3:56 pm

    In addStuff' you write (*2) w and (+10) w. Those are equivalent to w*2 and w+10 respectively. So addStuff' is equivalent to this:

    addStuff' w = w*2 >>= \a ->
                  w+10 >>= \b ->
                  return (a+b)
    

    Writing it this way should make it obvious that here the left operands to >>= are numbers, not functions. That’s why the inferred type is telling you that your function only works for numbers that are monads.

    When eliminating do notation the left operand to >>= should be exactly the same as the right operand of <-. Also eliminating do notation does not add any arguments to the function. So the correct rewriting would look like this:

    addStuff' = (*2) >>= \a ->
                (+10) >>= \b ->
                return (a+b)
    

    As to why your earlier pieces of code don’t work:

    ( (*2) >>= (+10) ) 3 
    

    The operator >>= has type m a -> (a -> m b) -> m b. For simplicity let’s assume that all the numbers in this code have type Int, then your left operand has type Int -> Int or m Int if m is (->) Int. So for some type b the right operand should have type Int -> ((->) Int) b or, more readably, Int -> Int -> b. The type it actually has though is Int -> Int. Therefore your expression is ill-typed.

    (return 3) :: ((->) Int)
    

    ((->) Int) has kind * -> * – the type of a value must have kind *.

    Or to approach this differently: return 3 has type m Int for some m (still assuming that all integer literals have type Int for simplicity). So if m is ((->) Int), the type of return 3 will be ((->) Int) Int or Int -> Int, not ((->) Int).

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

Sidebar

Related Questions

I have some confusion of the run time analysis of the merge function in
I have some confusion about when to use the self keyword during ivars assignment,
Hello everyone, I have some confusion regarding some Win32 API data types and macros
i have a coldfusion cfc and some methods in it. MethodA will return a
after some confusion in the comments to Is it safe to have 1 letter
I have some confusion over a jQuery extension not working as I'd expect. I
I am reading Real World Haskell (great book) and I have some confusion about
I am having some confusion regarding this Closure thing. I have two separate codes
I have some confusion when looking at how interrupt handler(ISR) is run. In Wiki
i am studying object orienting concepts in oracle and have some confusion in some

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.