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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:38:26+00:00 2026-05-15T19:38:26+00:00

In order to grasp better typeclasses (starting pretty much form scratch) I had a

  • 0

In order to grasp better typeclasses (starting pretty much form scratch) I had a go at modelling 2-D shapes with area calculations, like this:

module TwoDShapes where

class TwoDShape s where
    area :: s -> Float

data Circle = Circle Float deriving Show
aCircle radius | radius < 0 = error "circle radius must be non-negative"
               | otherwise  = Circle radius
instance TwoDShape Circle where
    area (Circle radius) = pi * radius * radius

data Ellipse = Ellipse Float Float deriving Show
anEllipse axis_a axis_b | axis_a < 0 || axis_b < 0 = error "ellipse axis length must be non-negative"
                        | otherwise                = Ellipse axis_a axis_b
instance TwoDShape Ellipse where         
    area (Ellipse axis_a axis_b) = pi * axis_a * axis_b

And so on for other kinds of shape.

This is fine but it occurred to me to try this:

module TwoDShapes  where

class TwoDShape s where
    area :: s -> Float

data TwoDShapeParams = TwoDShapeParams Float Float Float deriving Show

instance TwoDShape TwoDShapeParams where
    area (TwoDShapeParams length_a length_b constant) = foldl (*) 1 [length_a, length_b, constant]

aCircle radius | radius < 0 = error "circle radius must be non-negative"
               | otherwise  = TwoDShapeParams radius radius pi

anEllipse axis_a axis_b | axis_a < 0 || axis_b < 0 = error "ellipse axis length must be non-negative"
                        | otherwise                = TwoDShapeParams axis_a axis_b pi

etc. which is also fine. With the goal of information hiding I change the module declaration to look like this:

module TwoDShapes (TwoDShape, area, aCircle, anEllipse, aRectangle, aTriangle)

and slightly to my surprise this 1) works and 2) in ghci aCircle evaluates to TwoDShapeParams 1.0 1.0 3.1415927 which is true but I don’t understand how the type TwoDShapeParams is visible outside the module. I’m not sure what I was expecting, but not this.

What I’d really like is for the typeclass, it’s method and the “smart constructors” to be visible outside the module and nothing else. Can that be done?

  • 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-15T19:38:27+00:00Added an answer on May 15, 2026 at 7:38 pm

    Although the representation of TwoDShapes is hidden, you have derived a Show instance for it, which allows an arbitrary value of type TwoDShapes to be converted to a String, so this is the source of the information leakage. A truly abstract type should not define a Show instance, or indeed a Data instance which similarly exposes information about the representation. It’s fine to have a way to convert your type to a String, as long as the String is independent of the representation (see the Show instances of Data.Map.Map and Data.Array.Array for good examples of this).

    Note that the module system is doing its job: you still can’t refer to the TwoDShapes constructor outside of the module that defines it.

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

Sidebar

Related Questions

I'm trying to grasp higher-order-polymophism in scala by implementing a very basic interface that
I'm currently trying to grasp both function overloading and function pointers. In order to
order by in query is taking to much time in MySQL. SHOW PROFILES shows
In order to prevent DivideByZeroException in C#, people often write things like double f(double
I am pretty much a newbie to objective-c and as I started to program,
Which class hierarchy is most necessary in order to get an excellent grasp of
I think I have a pretty good grasp on the tenets of a RESTful
Sorry. Pretty new to this and trying to get a grasp on getting extended
Order by descending is not working on LINQ to Entity In the following Query
Order props: Object selectedItems: Array[1] 0: SelectedItem length: 1 __proto__: Array[0] __proto__: Order I

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.