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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:59:15+00:00 2026-05-11T00:59:15+00:00

In Haskell, is there a way to restrict a monad M a so that

  • 0

In Haskell, is there a way to restrict a monad M a so that a satisfy a type class constraint?

I am translating the probabilistic modeling example from F# to Haskell. However, in Haskell, I omitted support because it would change data Distribution a to data (Ord a) => Distribution a. With this change, I get the following error:

...probabilisticModeling.hs:42:13:     Could not deduce (Ord a) from the context ()       arising from a use of `always'                    at ...probabilisticModeling.hs:42:13-18     Possible fix:       add (Ord a) to the context of the type signature for `return'     In the expression: always     In the definition of `return': return = always     In the instance declaration for `Monad Distribution' 

Indeed, the type of always/return is: (Ord a) => a -> Distribution a. Is there a way I can have a monad Distribution, but force the constraint (Ord a) on this monad? I tried:

instance Monad Distribution where     (>>=) = bind     return :: (Ord a) => a -> Distribution a = always 

But I get the error:

...probabilisticModeling2.hs:48:4:     Pattern bindings (except simple variables) not allowed in instance declarations       return :: (Ord a) => a -> Distribution a = always Failed, modules loaded: none. 

So it there a way to have a monad M a, but restrict the a with a constraint such as Ord a?

Thanks.

  • 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. 2026-05-11T00:59:16+00:00Added an answer on May 11, 2026 at 12:59 am

    My understanding of this is that you simply cannot, because a monad is meant to be generalized over all types, not some restricted subset of types such as (Ord a).

    Instead of restricting the monadic type M a, you can simply restrict functions which use that monadic type, e.g.,

    foo :: Ord a => Int -> M a

    In fact, it is preferable to keep types as general as possible and use type classes only to restrict functions.

    etc.

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

Sidebar

Related Questions

From the haskell report: The quot, rem, div, and mod class methods satisfy these
Is there any way in Haskell to get the constant that is the largest
Is there a way to lift a class instance in Haskell easily? I've been
In Haskell, is there a way to compare that all wildcards are of the
Is there any way to compare two functions in Haskell? My thought is that
Is there some Haskell extension that enables the creation of more complex data constructors
Is there a way to use Haskell's map or something similar with multiple arguments?
Is there any way to view the reduction steps in haskell, i.e trace the
Is there a way to get line-number/traceback information in Haskell? (like C's __LINE__ macro
In Haskell there are two functions that allow one to perform an operation on

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.