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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:53:24+00:00 2026-06-02T01:53:24+00:00

In my project I have created a data type, that can hold one of

  • 0

In my project I have created a data type, that can hold one of a few types of values:

data PhpValue = VoidValue | IntValue Integer | BoolValue Bool

What I wanted to do now, is to have a simple way of checking if two values of the PhpValue type are of the same constructor (correct me if I’m confused with the terminology here, but basically what I want to check if both are, for example, are IntValue, without caring about the particular value).

Here is a function I wrote for that:

sameConstructor :: PhpValue -> PhpValue -> Bool
sameConstructor VoidValue VoidValue = True
sameConstructor (IntValue _) (IntValue _) = True
sameConstructor (BoolValue _) (BoolValue _) = True
sameConstructor _ _ = False

This works as it should, but I don’t really like it: if I add more constructors (like FloatValue Float) I am going to have to rewrite the function, and it will get bigger as my data definition gets bigger.

The Question: Is there a way of writing such a function, so that its implementation doesn’t change when I add more constructors?

For the record: I don’t want to change the data definition, I have enough Monads in the rest of my code as it is 😉

  • 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-02T01:53:26+00:00Added an answer on June 2, 2026 at 1:53 am

    Take a look at Data.Data and its toConstr function. This returns a representation of the constructor which can be compared for equality.

    With an extension (you can put {-# LANGUAGE DeriveDataTypeable #-} at the top of your module), you can have a Data instance derived for you automatically:

    data PhpValue = VoidValue | IntValue Integer | BoolValue Bool 
                  deriving (Typeable, Data)
    

    You should then be able to use the toConstr function to compare by constructor.

    Now the following will be true:

    toConstr (BoolValue True) == toConstr (BoolValue False)
    

    Using on from Data.Function you can now rewrite sameConstructor to:

    sameConstructor = (==) `on` toConstr
    

    This is the same as

    sameConstructor l r = toConstr l == toConstr r
    

    I think the version using on is easier to read at a glance.

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

Sidebar

Related Questions

I have created data sources from my objects in my project, some of which
I have created a project that uses scala that has some abstract classes definitions
I have a Access Data Project that connects to an SQL server database. Recently
For a new project I have created a local folder tree with empty Visual
I have created a project in Dashcode, inserted a List control, enabled a dynamic
I have created SSRS project in BIDS and I want to use the reports
I have created the following project structure for my new asp.net mvc project any
I have created (generated) a MDI project with tabbed documents with VS2008 Pro. Visual
I have created a MDI project with CView's using VS2008Pro. I want to have
I have created a multi module maven project. Now I have shared the project

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.