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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:16:51+00:00 2026-06-05T21:16:51+00:00

I am learning about Algebraic DTs in haskell. What I would like to do

  • 0

I am learning about Algebraic DTs in haskell. What I would like to do is create a new ADT that kind of “extends” an existing one. I cannot find how to express what I would like, can someone sugest an alternative pattern or sugest a solution. I want them to be distinct types, but copying and pasting just seams like a silly solution. The code below best describes what I am seeking.

data Power =
  Abkhazia |
  -- A whole bunch of World powers and semi-powers
  Transnistria
    deriving (Eq, Show)

data Country = 
  --Everything in Power | 
  Netural |
  Water
    deriving (Eq, Show)

Edit: I think It need a little clarification… I want to be able to do this (in ghci)

let a = Abkhazia :: Country

and not

let a = Power Abkhazia :: Country
  • 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-05T21:16:53+00:00Added an answer on June 5, 2026 at 9:16 pm

    You need to represent them as a tree:

      data Power
          = Abkhazia
          | Transnistria
        deriving (Eq, Show)
    
      data Country 
          = Powers Power -- holds values of type `Power`
          | Netural      -- extended with other values.
          | Water
        deriving (Eq, Show)
    

    Edit: your extension to the question makes this a bit simpler: both the Country and Power types share some common behavior as “countries”. This suggests you use the open, extensible type class feature of Haskell to given common behaviors to the data type. E.g.

      data Power = Abkhazia | Transistria 
    
      data Countries = Neutral | Water
    

    then, a type class for things both Power and Countries share:

      class Countrylike a where
          landarea :: a -> Int -- and other things country-like entities share
    
      instance Countrylike Power where
          landarea Abkhazia    = 10
          landarea Transistria = 20
    
      instance Countrylike Countries where
          landarea Neutral     = 50
          landarea Water       = 0
    

    then you can use landarea cleanly on either powers or countries. And you can extend it to new types in the future by adding more instances.

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

Sidebar

Related Questions

Learning about Explicit Cursors and trying to create my frst one.: SET SERVEROUTPUT ON
I'm learning about namespaces in PHP 5.3 and I would like to use Namespaces
I am learning about raw sockets. I heard that ping utility is using raw
While learning about NSOperation, I wondered why a completion block would have any advantage
I am learning about tools like Chef/Puppet/etc for the first time and was wondering
I'm learning about hash tables, trying to understand how they work. I'd like to
am new to learning about MVC. I am wondering if there is a heuristic
In learning about Web Deploy I came across some netsh.exe commands that talk about
Just learning about mercurial's --style and --template options that can be used on hg
Often when learning about .NET I run into things that hail from the COM

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.