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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:10+00:00 2026-05-26T12:35:10+00:00

I am trying to understand Monad and I have the following code f a

  • 0

I am trying to understand Monad and I have the following code

f a b c d =
   do one <- a + b
      two <- c * d
      three <- one + two
      return three

The above compiles

but get the an error when I

*Main> f 1 2 3 4

:1:1:
    No instances for (Num (a0 -> t0), Monad ((->) a0), Monad ((->) t0))
      arising from a use of `f'
    Possible fix:
      add instance declarations for
      (Num (a0 -> t0), Monad ((->) a0), Monad ((->) t0))
    In the expression: f 1 2 3 4
    In an equation for `it': it = f 1 2 3 4

:1:9:
    No instance for (Num (a0 -> a0 -> t0))
      arising from the literal `4'
    Possible fix:
      add an instance declaration for (Num (a0 -> a0 -> t0))
    In the fourth argument of `f', namely `4'
    In the expression: f 1 2 3 4
    In an equation for `it': it = f 1 2 3 4

I think I would be one step closer to understand Monad if I know why the above code dont work on
f 1 2 3 4

  • 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-26T12:35:10+00:00Added an answer on May 26, 2026 at 12:35 pm

    The problem is you are confusing wrapped monadic values with pure values.

    The first thing to know is that do notation is syntactic sugar for regular function calls (>>= and >>). So, it would help to see what your code desugars too.

    Lets try something simpler

     f a b =
       do one <- a + b
          return one
    

    This has the same problem as your code, but is simpler. To understand why it doesn’t work we ask: what does this actually mean? Well, we can rewrite the <- symbol using >>=

     f a b = (a + b) >>= \x -> return x
    

    (this is not the simplest representation, but makes the point clear)

    If you test the following in GHCi

     >> :t (>>=)
     Monad m => m a -> (a -> m b) -> m b
    

    that is, the function >>= takes: an argument of type m of a and a function from a to m of b and returns a m of b.

    What about in this code?

    (a + b)
    

    Is going to be a number.
    How about the other half

     \x -> return x
    

    Takes an object of type a and returns an object of type m a for any a

    So, you need to have a number, that is also some sort of monad of something. Can you think of anything like that? It is not clear what this would be, which is a reason to be suspicious that this should type check.

    One good way to come to terms with monads is to look at some specific examples.

    The Maybe monad expresses computations that might fail

     instance Monad Maybe where
          return = Just
          (>>=) (Just a) f = f a
          (>>=) Nothing _ = Nothing
    

    This lets you say things with a patter like

     f args = do x <- functionThatMightFail args
                 y <- anotherfunctionThatMightFail x
                 return y
    

    or the same code more simply

    f args = do x <- functionThatMightFail args
                anotherfunctionThatMightFail x
    

    or perhaps

    f args = functionThatMightFail args >>= anotherfunctionThatMightFail
    

    On the other hand the List monad captures the idea of performing the same function on every element of a list, and then concatenating the results together. Simple examples abound:

    f = do x <- [1,2,3,4]
           [1..x]
    

    If you understand these ones, play with the State monad. It helps you get the more general idea that “monads are models of computation.” I would then checkout Parsec, and of course, IO

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

Sidebar

Related Questions

Trying to understand what Sql Profiler means by emitting sp_reset_connection. I have the following,
Trying to understand the new async/await pattern, I have one question which I can't
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
Trying to understand why this doesn't work. I keep getting the following errors: left
I am trying understand how multi queries work in mysqli. But I confess that
After trying to understand why client code is not rendered in a page (injected
I am trying understand why one of my printf statements fail. #1 below is

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.