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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:22:00+00:00 2026-05-23T20:22:00+00:00

I would like to define a monad instance with the container M as monad

  • 0

I would like to define a monad instance with the container M as monad and with the contained type a which should be a member of class Show. This constraint (that a is member of Show) should be ensured by the type system.

I gave it a try like this, but M is unfortunately not of the right Kind:

data M = forall a. Show a => M a 

instance Monad M where
 return x = M x

All other attempts to achieve that, run into the following problem: Since Monad is a constructor class, I don’t have explicit access to the type a of the contained element(s), so I can’t restrict it.

Does anyone know a solution to this without defining a new Monad class?

  • 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-23T20:22:00+00:00Added an answer on May 23, 2026 at 8:22 pm

    Well, it is actually possible to restrict the parameters of a type constructor in some sense, using GADTs:

    data M a where
        M :: (Show a) => a -> M a
    

    Unfortunately this doesn’t actually help you here. In a way it actually makes things worse, because rather than having a Monad instance without the constraint, it becomes impossible to write the instance at all.

    If you look at the above constructor type signature, it clearly resembles return–which demonstrates why what you’re doing is fundamentally impossible. The type for return is: (Monad m) => a -> m a, and as always unbound type variables are implicitly universally quantified at the outermost level, so you can read that as “for all possible types a, and all possible types m which are instances of Monad, given a value of type a you can construct a value of type m a“. The “for all” phrasing is quite literal there–the type of return isn’t just using a type variable, it’s actively asserting that any type a whatsoever must be allowed.

    So, in short, no. There’s no way to do what you want because the standard Monad class explicitly specifies the opposite.

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

Sidebar

Related Questions

I would like to define Sorted to be of type ErrorProviderMessageCollection , which is
I would like to define a class named List like this: class List {
I would like to define some template member methods inside a template class like
So , I've been reading this article: http://msdn.microsoft.com/en-us/library/aa290051%28VS.71%29.aspx And I would like to define
I would like to be able to define and use a custom type in
I would like to define a ControlTemplate at runtime. Is this possible? I have
I would like to define a new command like this \newcommand{\bob}[1]{\excerpt \begin{lstlisting} {#1} \end{lstlisting}}
I would like to define the following control: public partial class ObjectSelectorControl<T> : UserControl
Having defined type MyInt int I would like to define a method .ShowMe() that
I have an enumerated type that I would like to define the > ,

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.