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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:27:34+00:00 2026-06-08T21:27:34+00:00

When we create a type class, we usually assume that its functions must obey

  • 0

When we create a type class, we usually assume that its functions must obey some properties. Thus we have the Monoid and Monad laws for their respective type classes. But, what if there is some law, like associativity, that I want to specify that multiple classes either may or may not obey that law? Is there a way to do that in Haskell’s type system? Is this sort of type classes for type classes idea even feasible in practice?


Here’s a motivating example from algebra:

class Addition x where
    add :: x -> x -> x

class Multiplication x where
    mult :: x -> x -> x

instance Addition Int where
    add = (+)

instance Multiplication Int where
    add = (*)

Now, if I want to specify that addition over Int’s is associative and commutative, I can create the classes and instances:

class (Addition x) => AssociativeAddition x where
class (Addition x) => CommutativeAddition x where

instance AssociativeAddition Int where
instance CommutativeAddition Int where

But this is cumbersome because I have to create all possible combinations for all classes. I can’t just create Associative and Commutative classes, because what if addition is commutative, but multiplication is not (like in matrices)?

What I would like to be able to do is say something like:

class Associative x where

instance (Associative Addition, Commutative Addition) => Addition Int where
    add = (+)

instance (Commutative Multiplication) => Multiplication Int where
    mult = (*)

Can this be done?

(Haskell’s abstract algebra packages, like algebra and constructive-algebra, do not currently do this, so I’m guessing not. But why not?)

  • 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-06-08T21:27:35+00:00Added an answer on June 8, 2026 at 9:27 pm

    You actually can do this with some recent GHC extensions:

    {-# LANGUAGE ConstraintKinds, KindSignatures, MultiParamTypeClasses #-}
    import GHC.Exts (Constraint)
    
    class Addition (a :: *) where
        plus :: a -> a -> a
    
    instance Addition Integer where
        plus = (+)
    
    class (c a) => Commutative (a :: *) (c :: * -> Constraint) where
        op :: a -> a -> a
    
    instance Commutative Integer Addition where
        op = plus
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really like the following template to create properties for class in VS2005 Type
Sometimes, I usually create a class with the type of data I want like
I want to create a C++ class with the following type: It can be
I'm trying to create an instance of a generic class using a Type object.
I've created class that takes Exception type in constructor private readonly Exception _exception; public
I have my own type: CREATE TYPE MyType AS TABLE ( foo INT )
I'm trying to create am immutable type (class) in C++, I made it so
What I want to do is take any class type and create a list
I have the following structure: class FeatureType(models.Model): type = models.CharField(max_length=20) def __unicode__(self): return self.type
So in my postgres DB I have the following custom type: create type my_pg_type

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.