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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:03:56+00:00 2026-05-18T05:03:56+00:00

Let’s say I have three value constructors: A { a :: Int } B

  • 0

Let’s say I have three value constructors:

A { a :: Int }
B { b :: Char }
C { c :: Bool }

I would like to create two types X and Y such that a value of type X can be an A, B or C, something like this:

data X = A {...} | B {...} | C {...}

and a value of type Y can be only an A or B, something like this:

data Y = A {...} | B {...}

so that I can code something like this:

foo :: X -> Int -- can pattern match
foo (A _) = 1
foo (B _) = 2
foo (C _) = 3
bar :: Y -> Bool -- also can pattern match with the same constructors
bar (A _) = true
bar (B _) = false
baz = A 1 -- baz is inferred to be a type that can fit in both X and Y

I know that I can wrap the constructors in the definitions of X and Y like this:

data X = XA A | XB B | XC C
data Y = YA A | YB B

but this seems untidy (having to type XA A etc. all the time). I could expand the contents of A, B, and C into the definitions of X and Y, but A etc. are quite complicated and I would prefer not to duplicate the definition.

Is this possible with Haskell, including any GHC extensions?

Edit

It seems that GADTs can answer my question as asked (so I marked heatsink’s answer as correct), but still aren’t flexible enough for what I need. For example, as far as I know, you can’t do something like:

func1 :: [XY Y_] -- returns a list of items that can only be A or B
func1 = ...
func2 = func1 ++ [C True] -- adding a C item to the list

func2 should be typed as [XY X_], but that isn’t possible in Haskell (unless my experiment was wrong).

After more web searching, what I really want is OCaml’s polymorphic variants which (as far as I know) exist only in OCaml (looking at the more “practical” as opposed to “academic” languages).

Edit 2

See comonad’s answer. It seems that it really can be done, but I think I’d better not rewrite this question too many times. 🙂

  • 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-18T05:03:57+00:00Added an answer on May 18, 2026 at 5:03 am

    Type classes, as jetxee described, are probably the appropriate approach.

    If you also want the ability to pattern match and use constructors, then you can define all the constructors within one data type using GADTs and empty data declarations. If you take this approach, all the constructors will be members of the same data type, while letting you restrict the domain to only a subset of the constructors.

    data X_
    data Y_
    data XY a where
      A :: Int -> XY a
      B :: Char -> XY a
      C :: Bool -> XY X_
    type X = XY X_ -- Contains values built with constructors A, B, and C 
    type Y = XY Y_ -- Contains only values built with constructors A and B
    

    Now a function that uses only A and B works with both types X and Y. A function that uses C will work only with type X.

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

Sidebar

Related Questions

Let say I have the following desire, to simplify the IConvertible's to allow me

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.