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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:24:04+00:00 2026-06-09T05:24:04+00:00

I am looking for some refactoring / Best practice advice on the following code.

  • 0

I am looking for some refactoring / Best practice advice on the following code. I would like to try to avoid the extensions while maintaining separate modules for different “versions” that are mutually exclusive. My current solution is to use a class and use existential quantification to create a common type for each of the CountrySets.

This is an easy thing for me to accomplish if I was using OOP, but I can’t seam to think “functional” yet.

Thanks for your time.

Province.hs

{-# LANGUAGE ExistentialQuantification, RankNTypes  #-}
module Province where

  class Country a where
    --some stuff that is not important

  data Power = 
    forall a. (Show a, Eq a, Country a) => Power a |
    Netural |
    Water

  data Unit = Unit {
    power  :: forall a. (Show a, Eq a, Country a) => a, 
    piece :: Piece

  data Piece = Tank | Plane

  data Province = Province {
    power  :: Power,
    provName :: String
  } deriving (Eq)

  instance Show Power where
    show (Power b) = "Power " ++ show b
    show (Netural) = "Netural"
    show (Water) = "Water"

  instance Eq Power where
    (==) a b = Prelude.show a == Prelude.show b

Version1.hs

import Province

  data CountrySet1 =
    Country11 |
      Country12 
    deriving (Eq, Show)
  instance Country CountrySet1 where

  provs = 
    one1:one2:[]

  one1 = Province (Power Country11) "Place11"
  one2 = Province (Power Country12) "Place12" 

Version2.hs

import Province

  data CountrySet2 =
    Country21 |
    Country22 
      deriving (Eq, Show)
  instance Country CountrySet2 where

  provs = 
    two1:two2:[]

  two1 = Province (Power Country11) "Place21"
  two2 = Province (Power Country12) "Place22" 
  • 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-09T05:24:06+00:00Added an answer on June 9, 2026 at 5:24 am

    You don’t have to put class constraints in the data types. You can instead parametrize your data type on the a variable so that you can place the constraints on the type-class instances themselves, like so:

    -- Note that I added a type variable to "Power"
    data Power a = Power a | Neutral | Water
    
    instance (Show a) => Show (Power a) where ...
    
    instance (Eq a) => Eq (Power a) where ...
    

    … or you could do what most people do and use deriving:

    data Power a = Power a | Neutral | Water deriving (Eq, Show)
    

    This generates the exact same instances you wrote (except the Eq one will be much more efficient than what you wrote). No extensions required!

    Then if you want a to be a specific type, you just say so!

    -- Version1.hs
    myValue1 :: Power CountrySet1
    myValue1 = ...
    
    
    -- Version2.hs
    myValue2 :: Power CountrySet2
    myValue2 = ...
    

    These are then completely compatible and both implementations can coexist alongside each other.

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

Sidebar

Related Questions

I was looking at some code that I am refactoring and notices the try
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking for some advice on the best way to implement localization along with client
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
I am currently refactoring some old code. I am looking for directions on the
Recently, during a refactoring session, I was looking over some code I wrote and
I'm refactoring some code and I'm looking at a class called HFile. HFile has
I have been looking into refactoring some old code into a new WCF service,
Looking at some code I'm maintaining in System Verilog I see some signals that
I'm writing a microformats parser in C# and am looking for some refactoring advice.

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.