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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:30:43+00:00 2026-05-30T19:30:43+00:00

I have a class Shape which should have area defined on all instances. area

  • 0

I have a class “Shape” which should have “area” defined on all instances. area returns “Area b” (a data type) that contains a number (b belongs to Num typeclass) signifying the area of that Shape.

Haskell has problem binding that b to (x*y) where x and y are of type ‘a’ and ‘a’ is also of typeclass Num.
How do I solve this ?? [If i replace (x*y) by 0, it works but doesn’t work even with (0::Int)]

Code :

data Unit = Unit | Meter | CentiMeter               deriving Show

data Area a = Area a Unit                           deriving Show

class Shape a where
      area :: (Num b) => a -> Area b

data Rectangle side = Rectangle side side Unit  deriving Show

instance (Num a) => Shape (Rectangle a) where
     area (Rectangle x y unit) = Area (x*y) unit

Error :

[1 of 1] Compiling Main             ( y.hs, interpreted )

y.hs:11:46:
    Could not deduce (a ~ b)
    from the context (Num a)
      bound by the instance declaration at y.hs:10:10-39
    or from (Num b)
      bound by the type signature for
                 area :: Num b => Rectangle a -> Area b
      at y.hs:11:10-52
      `a' is a rigid type variable bound by
          the instance declaration at y.hs:10:15
      `b' is a rigid type variable bound by
          the type signature for area :: Num b => Rectangle a -> Area b
          at y.hs:11:10
    In the second argument of `(*)', namely `y'
    In the first argument of `Area', namely `(x * y)'
    In the expression: Area (x * y) unit
Failed, modules loaded: none.
  • 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-30T19:30:45+00:00Added an answer on May 30, 2026 at 7:30 pm

    The problem here is area‘s type signature:

    area :: (Num b) => a -> Area b
    

    What it says is “give me an a, and I’ll give you an Area b for any b you want; you can pick”. So, for instance, I could give area an Integer and expect back an Area Double. Clearly, this isn’t what you want!

    In this case, the error arises because you use x*y, which has type a, when b is expected — you have to give a value that works for any numeric type b, but you’re giving a value that only works for one (a).

    If you changed area‘s type to a -> Area Integer, or such, then it would work. However, I have a feeling you want the instance to be able to specify what the type of the area is. For this, you’ll need to use a language extension called type families:

    {-# LANGUAGE TypeFamilies #-}
    
    class (Num (AreaComponent a)) => Shape a where
        type AreaComponent a
        area :: a -> Area (AreaComponent a)
    
    instance (Num a) => Shape (Rectangle a) where
        type AreaComponent (Rectangle a) = a
        area (Rectangle x y unit) = Area (x*y) unit
    

    This says that for every type a that’s an instance of Shape, there is an associated type AreaComponent a, representing the type of each component of its area. That type is required to be an instance of Num by the definition of Shape.

    Another thing you could do, if all your shapes take a numeric type parameter, is to make the instances be for the type constructors of each shape, rather than the full shape types themselves:

    class Shape sh where
        area :: (Num a) => sh a -> Area a
    
    instance Shape Rectangle where
        area (Rectangle x y unit) = Area (x*y) unit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large Shape class, instances of which can (should) be able to
Suppose I have requirement where I have Shape which contains area as only operation.
I have a class which represents a shape. The Shape class has a property
I have an abstract base class called Shape from which both Circle and Rectangle
What I have: LINQ to SQL auto generated class, which corresponds to the shape
I have some ASP.NET web services which all share a common helper class they
I have class method that returns a list of employees that I can iterate
Let's say I have an abstract parent class called shape, and that there are
I have a Shape class containing potentially many vertices, and I was contemplating making
Let's say we already have a hierarchy of classes, e.g. class Shape { virtual

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.