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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:10:58+00:00 2026-05-30T04:10:58+00:00

I have type classes, for all of which I would like to have some

  • 0

I have type classes, for all of which I would like to have some common behavior. My problem is explained in the following code:

class A a
class B b

class X x where
    method :: (A a, B b) =>  x -> a -> b

data T = L | M | N
data U = P | Q | R

instance A T
instance B U

data Y = ZZZ

instance X Y where
    method _ L = P
    method _ M = Q
    method _ N = R

When I load this module, I get the following error:

example.hs:19:14:
    Could not deduce (a ~ T)
    from the context (A a, B b)
      bound by the type signature for method :: (A a, B b) => Y -> a -> b
      at example.hs:(17,5)-(19,18)
      `a' is a rigid type variable bound by
          the type signature for method :: (A a, B b) => Y -> a -> b
          at example.hs:17:5
    In the pattern: N
    In an equation for `method': method _ N = R
    In the instance declaration for `X Y'

example.hs:19:18:
    Could not deduce (b ~ U)
    from the context (A a, B b)
      bound by the type signature for method :: (A a, B b) => Y -> a -> b
      at example.hs:(17,5)-(19,18)
      `b' is a rigid type variable bound by
          the type signature for method :: (A a, B b) => Y -> a -> b
          at example.hs:17:5
    In the expression: R
    In an equation for `method': method _ N = R
    In the instance declaration for `X Y'
Failed, modules loaded: none.

I am at loss what to do in this case. Even when T and U are instance of A and B, I get this error. If I cannot return a rigid type value from method, how do I code this part?

  • 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-30T04:10:59+00:00Added an answer on May 30, 2026 at 4:10 am

    The signature method :: (A a, B b) => x -> a -> b promises that method works for every pair of types (a, b) with a an instance of A and b an instance of B, but you define it to work only with two specific types.

    This is fundamentally different from interfaces in Java or the like, where the callee chooses which type is used, the only thing the caller knows is that interface X is implemented. In Haskell, given such a signature, the caller decides which types are used (here, what type is passed as second argument and what type shall be returned) and the callee has to be able to provide the demanded functionality (as long as the demanded types are instances of the required classes).

    Without any methods in classes A and B to analyse respectively construct values of an instance of that class, you cannot implement method other than with undefined (various degrees of undefinedness are possible due to seq), so you would have to tell the world that you are in fact using T and U.

    Another way is to make X a multiparameter type class,

    {-# LANGUAGE MultiParamTypeClasses #-}
    
    class (A a, B b) => X x a b where
        method :: x -> a -> b
    

    However, that might require functional dependencies to resolve instances. Another way would be to use associated types,

    {-# LANGUAGE TypeFamilies #-}
    
    class X x where
        type AType x
        type BType x
        method :: x -> AType x -> BType x
    
    instance X Y where
        type AType Y = T
        type BType Y = U
        method ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which I would like to pass instances or classes interchangeably.
I have a stateless, abstract base class from which various concrete classes inherit. Some
I have some *Type form classes and a forms.html.twig to customize form appearance. By
If I have classes of Type A and B: public class A { public
I have an std::list<TYPE> which may contain child classes of TYPE. I need to
The following code fails in 'Evaluate' with: This expression was expected to have type
Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are
I have a base class, Primitive , from which I derive several other classes--
I would like to have a C# method which counts the number of cases
I have a base abstract class with several classes which extend and override it,

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.