Haskell algebraic data types: "pseudo-extension"

I am learning Algebraic DT in haskell. What I would like to do is create a new ADT that "extends" the existing one. I can not find how to express what I would like, can someone come up with an alternative pattern or a sax solution. I want them to be of different types, but only copy and paste seams, like a stupid solution. The code below best describes what I'm looking for.

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 this needs a little clarification ... I want to be able to do this (in ghci)

let a = Abkhazia :: Country

but not

let a = Power Abkhazia :: Country
+5
source share
2 answers

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)

: : , "". , , Haskell . .

  data Power = Abkhazia | Transistria 

  data Countries = Neutral | Water

, Power, :

  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

landarea . , .

+8
{-# LANGUAGE GADTs, StandaloneDeriving #-}
data POWER
data COUNTRY

data CountryLike a where
    Abkhazia :: CountryLike a 
    Transnistria :: CountryLike a
    Netural :: CountryLike COUNTRY
    Water :: CountryLike COUNTRY

deriving instance Show (CountryLike a)
deriving instance Eq (CountryLike a)

type Power      = CountryLike POWER
type Country    = CountryLike COUNTRY

foo :: Power
foo = Abkhazia

bar :: Country
bar = Abkhazia

baz :: Country
baz = Netural

: type Power = forall a. CountryLike a (: Power Country. : , , , Power -> Int , , , ( ...))

+2

All Articles