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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:09:35+00:00 2026-06-11T18:09:35+00:00

I have some types data Foo = Foo data Bar = Bar data Baz

  • 0

I have some types

data Foo = Foo
data Bar = Bar 
data Baz = Baz

I want to use them as keys for a Map. Is this possible, and if so, how?

Additonal context below:

I have an application that builds VMs. I have seperated the work into Phases. Currently I have this type

data CurrentPhase = PHASEONE
                  | PHASETWO
                  | PHASETHREE (deriving Eq,Ord)

So far so good right, no problems like what I mentioned above. However, I made a type class to describe operations that are phase specific

class PhaseOps phase where
  preValidate :: JobID -> phase -> Handler (Status)
  doPreProc :: JobID -> phase -> Handler (Status)
  updateConfig :: JobID -> phase -> Handler ()
  postValidate :: JobID -> phase -> Handler (Status)

in order for this to work, I had to create a new set of singleton data types to use for PhaseOps instances.

data PhaseOne = PhaseOne

.. and so on

now I have these singleton types, and CurrentPhase. I’d like to get rid of CurrentPhase (which I am using for a Map with CurrentPhase being the key), and use my singleton data types.

  • 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-11T18:09:37+00:00Added an answer on June 11, 2026 at 6:09 pm

    The straightforward solution is to use keys of type Either Foo (Either Bar Baz). This gets verbose quickly as you add possible types and is a bit ugly anyway, so it often makes more sense to use a special-purpose equivalent such as:

    data FooBarBaz = FooVal Foo | BarVal Bar | BazVal Baz
    

    This is similar to combining them into one type directly, but trades off a bit more verbosity in the combined type for being able to still use the individual types elsewhere. This is a relatively common pattern; for instance, I’ve seen it often in types representing syntax trees, where a “top level declaration” type might take this form, with each kind of declaration being its own separate type.

    Depending on the nature of your problem there might be other approaches that would be better, but the above is the only good general-purpose solution I can think of–if you don’t like doing it this way, you’ll need to more clearly specify why and elaborate somewhat on what you need these types to accomplish.


    Edit in response to clarification:

    As I mentioned in the comments on the question, PhaseOps looks a great deal like a class that wants to be a record of functions instead. Furthermore, if you have such a class, wanting a way to work with multiple instance types as if they were a single type is a very strong indication that it’s time to step back and rethink your design.

    Continuing with such a design almost always leads to either mucking about with Typeable, as Thomas M. DuBuisson mentions in the comments, or to mucking about with existential types (which is a well-known anti-pattern these days). It’s true that such approaches are occasionally required, but are best avoided unless you can very clearly explain (even if only to yourself) why you need them. Otherwise, they create far more problems than they solve.

    Incidentally, if you want to retain some of the benefits of separate types, I’d consider using your singleton types for phantom type tagging and/or hiding the constructor for the PhaseOps record and using a smart constructor that takes a CurrentPhase argument.

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

Sidebar

Related Questions

Suppose that I have two data types Foo and Bar. Foo has fields x
I've got two data types, Foo and Bar , that have a property to
I have some tabular data: Foo Bar ------------- fooes 42 bars 666 ... So,
I have some problems with OpenCV s cvCanny(...) and the Image data types it
Hello everyone, I have some confusion regarding some Win32 API data types and macros
I have grabbed some XML data using this piece of jQuery: $.ajax({ type: POST,
I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
I have some types which are generated by a web service reference. I want
I have some complex types here so I decided to use nifty trick to
In my application I have use some data ValueCell (something like 20) and I

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.