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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:32:23+00:00 2026-05-17T21:32:23+00:00

(Sorry in advance if the question is stupid or obvious — I don’t have

  • 0

(Sorry in advance if the question is stupid or obvious — I don’t have a lot of experience with Haskell).

Is there a way to express that a type should be an instance of a typeclass in more than one way? This is best illustrated with an example (which is probably somewhat silly): In mathematics, we can say that a semiring is a set that is a commutative monoid under one operation (which we’ll call addition, identity 0) and a monoid under another (which we’ll call multiplication) along with the requirements that multiplication distributes over addition and that 0 annihilates all elements under multiplication. The latter parts aren’t important here.

Suppose now that I have a typeclass Monoid (not to be confused with Data.Monoid),

class Monoid m where
    unit :: m 
    operation :: m -> m -> m

and would like to create a typeclass Semiring. From the definition given above, I’d like to say “if the type r is a monoid in two (distinct) ways, we’ll call it semiring”. So I’d like something like

class (Monoid r, Monoid r) => Semiring r where ...

which of course doesn’t work. Admittedly, the example becomes a bit strange since there are no more functions we’d like to require for semirings, so the typeclass would be empty, but I hope it illustrates what I’m asking about (or just pretend that we require some function f:r->r for Semiring r).

So, in the general setting, I’m asking: Given a typeclass A, is there a way to parametrize a typeclass B a with the requirement that a be an instance of A in two ways (meaning that a should implement the functions specified by A in two ways)?

  • 1 1 Answer
  • 1 View
  • 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-17T21:32:23+00:00Added an answer on May 17, 2026 at 9:32 pm

    One option is to define your own monoids for the two operations of a semiring:

    class AdditiveMonoid m where
        zero :: m
        (<+>) :: m -> m -> m
    
    class MultiplicativeMonoid m where
        one :: m
        (<*>) :: m -> m -> m
    

    and then combine them:

    class (MultiplicativeMonoid m, AdditiveMonoid m) => Semiring m
    

    The problem is that you cannot express the monoid laws or the fact that one operation is commutative. The best you can get is defining quickcheck properties for the laws.

    For some inspiration check the numeric prelude and this paper.

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

Sidebar

Related Questions

First of all Sorry if my question title sounds stupid.... I have the following
I am new to IOS, sorry in advance if I ask a stupid question.
I don't know how to phrase the question properly so sorry in advance. Using
Sorry in advance for this incredibly simple question, but what is the best way
I am sorry in advance for the stupid question but I am not a
Sorry in advance in case this is a stupid question! I was playing around
Sorry in advance if this is a really basic Matrix/OpenGl question. I have a
I'm a noob, and I'm sorry because the question is really stupid. I have
Sorry in advance everyone for this question as I know the cascading select boxes
Sorry in advance for this being such a beginner question. Here are the steps

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.