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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:32:29+00:00 2026-05-28T03:32:29+00:00

Is it possible to expand data types with new values? E.g.: The following compiles:

  • 0

Is it possible to expand data types with new values?

E.g.:
The following compiles:

data Axes2D = X | Y
data Axes3D = Axes2D | Z

But, the following:

data Axes2D = X | Y deriving (Show, Eq)
data Axes3D = Axes2D | Z deriving (Show, Eq)

type Point2D = (Int, Int)
type Point3D = (Int, Int, Int)

move_along_axis_2D :: Point2D -> Axes2D -> Int -> Point2D
move_along_axis_2D (x, y) axis move | axis == X = (x + move, y)
                                    | otherwise = (x, y + move)

move_along_axis_3D :: Point3D -> Axes3D -> Int -> Point3D
move_along_axis_3D (x, y, z) axis move | axis == X = (x + move, y, z)
                                       | axis == y = (x, y + move, z)
                                       | otherwise = (x, y, z + move) 

gives the following compiling error (move_along_axis_3D commented out doesn’t give errors):

Prelude> :l expandTypes_test.hs 
[1 of 1] Compiling Main             ( expandTypes_test.hs, interpreted )

expandTypes_test.hs:12:50:
    Couldn't match expected type `Axes3D' with actual type `Axes2D'
    In the second argument of `(==)', namely `X'
    In the expression: axis == X
    In a stmt of a pattern guard for
                 an equation for `move_along_axis_3D':
          axis == X
Failed, modules loaded: none.

So is it possible to make X and Y of type Axes2D as well of type Axes3D?
If it is possible: what am I doing wrong? Else: why is it not possible?

  • 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-28T03:32:30+00:00Added an answer on May 28, 2026 at 3:32 am

    Along with what Daniel Fischer said, to expand on why this is not possible: the problems with the kind of subtyping you want run deeper than just naming ambiguity; they make type inference a lot more difficult in general. I think Scala’s type inference is a lot more restricted and local than Haskell’s for this reason.

    However, you can model this kind of thing with the type-class system:

    class (Eq t) => HasAxes2D t where
      axisX :: t
      axisY :: t
    
    class (HasAxes2D t) => HasAxes3D t where
      axisZ :: t
    
    data Axes2D = X | Y deriving (Eq, Show)
    data Axes3D = TwoD Axes2D | Z deriving (Eq, Show)
    
    instance HasAxes2D Axes2D where
      axisX = X
      axisY = Y
    
    instance HasAxes2D Axes3D where
      axisX = TwoD X
      axisY = TwoD Y
    
    instance HasAxes3D Axes3D where
      axisZ = Z
    

    You can then use guards to “pattern-match” on these values:

    displayAxis :: (HasAxes2D t) => t -> String
    displayAxis axis
      | axis == axisX = "X"
      | axis == axisY = "Y"
      | otherwise = "Unknown"
    

    This has many of the same drawbacks as subtyping would have: uses of axisX, axisY and axisZ will have a tendency to become ambiguous, requiring type annotations that defeat the point of the exercise. It’s also a fair bit uglier to write type signatures with these type-class constraints, compared to using concrete types.

    There’s another downside: with the concrete types, when you write a function taking an Axes2D, once you handle X and Y you know that you’ve covered all possible values. With the type-class solution, there’s nothing stopping you from passing Z to a function expecting an instance of HasAxes2D. What you really want is for the relation to go the other way around, so that you could pass X and Y to functions expecting a 3D axis, but couldn’t pass Z to functions expecting a 2D axis. I don’t think there’s any way to model that correctly with Haskell’s type-class system.

    This technique is occasionally useful — for instance, binding an OOP library like a GUI toolkit to Haskell — but generally, it’s more natural to use concrete types and explicitly favour what in OOP terms would be called composition over inheritance, i.e. explicitly wrapping “subtypes” in a constructor. It’s not generally much of a bother to handle the constructor wrapping/unwrapping, and it’s more flexible besides.

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

Sidebar

Related Questions

Is it possible with the standard asp:dropdownlist to make the list expand upwards instead
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
I'm a .net developer by trade but I'm looking to expand to PHP. I
Given the following structure: struct nmslist_elem_s { nmptr data; struct nmslist_elem_s *next; }; typedef
Assuming the following: The browser in question doesn't limit the size of data URIs.
I need a macro to expand to a c++ comment, is that possible? I've
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions

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.