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

The Archive Base Latest Questions

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

I am defining an instance of a monad as follows: data Something = Something

  • 0

I am defining an instance of a monad as follows:

data Something = Something a

instance Monad Something where
    return a = Something a        --Wraps a in 'Something', correct?
    m >>= f = do
        var <- m
        return $ f var            --I want this to pass var to f, then wrap the result
                                  --back up in the 'Something' monad with the return I
                                  --Just defined

The questions are ->

1: Are there any glaring errors/misconceptions with what I am doing?

2: Will Haskell know to call the return I have defined above from m >>= f

3: If I for some reason define another function

f :: Something a -> Something b
f x = do
    var <- x
    return $ doMagicTo x

Will return call the return I defined in the monad instance and wrap x in Something?

  • 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-26T10:27:36+00:00Added an answer on May 26, 2026 at 10:27 am

    There are a few big problems here.

    First, a Monad instance must have kind * -> *. That means they need at least one type variable, where your Something doesn’t have any. For comparison:

    -- kind * -> *
    Maybe
    IO
    Either String
    
    -- kind *
    Maybe Int
    IO ()
    Either String Double
    

    See how each of Maybe, IO, and Either String need a type parameter before you can use them? With Something, there’s no place for the type parameter to fill in. So you need to change your definition to:

    data Something a = Something a
    

    The second big problem is that the >>= in your Monad instance is wrong. You generally can’t use do-notation because that just calls the Monad functions return and >>=. So you have to write it out without any monad functions, either do-notation or calling >>= or return.

    instance Monad Something where
        return a = Something a        --Wraps a in 'Something'
        (Something m) >>= f = f m     --unwraps m and applies it to f
    

    The definition of >>= is simpler than you expected. Unwrapping m is easy because you just need to pattern-match on the Something constructor. Also f :: a -> m b, so you don’t need to worry about wrapping it up again, because f does that for you.

    While there’s no way to unwrap a monad in general, very many specific monads can be unwrapped.

    Be aware that there’s nothing syntactically wrong with using do-notation or >>= in the monad instance declaration. The problem is that >>= is defined recursively so the program goes into an endless loop when you try to use it.

    (N.B. Something as defined here is the Identity monad)

    For your third question, yes the return function defined in the Monad instance is the one that will be called. Type classes are dispatched by type, and as you’ve specified the type must be Something b the compiler will automatically use the Monad instance for Something. (I think you meant the last line to be doMagicTo var).

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

Sidebar

Related Questions

My problem is this: I want to create a grails domain instance, defining the
When defining a method on a class in Python, it looks something like this:
I'm defining my own AR class in Rails that will include dynamically created instance
Why does defining __getitem__ on a class make it iterable? For instance if I
When defining parameter type that is e.g. in System.Data you have no intellisense and
I have trouble defining a trigger for a MySQL database. I want to change
When defining a struct type and instance, I can print the value and get
When defining a RelayCommand in the ViewModel, this is normally done using once [lazy
Does defining an instance as dynamic in C# mean: The compiler does not perform
Suppose for instance I'm defining a Complex class for representing complex numbers. I would

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.