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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:38:53+00:00 2026-06-05T05:38:53+00:00

Or rather, why isn’t (==) usable on every data type? Why do we have

  • 0

Or rather, why isn’t (==) usable on every data type? Why do we have to derive Eq ourseleves? In other languages, such as Python, C++, and surely others, it has a default implementation for everything!
I can’t think of types that can’t be compared.

  • 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-05T05:38:54+00:00Added an answer on June 5, 2026 at 5:38 am

    In Python the default equality implementation compares identity, not value. This is useful for user-defined classes, which by default are mutable and do not have to have a well-defined notion of “value”. But even in that setting, it is more normal to use the is operator to directly compare identities for mutable objects.

    With Haskell’s immutability and sharing this notion of “identity” doesn’t make much sense. If you could compare two terms by identity you could find out whether or not they are shared, but it’s generally up to the implementation whether two terms that might be shared actually are shared, so such information shouldn’t be able to affect the behaviour of the program (unless you like programs that change their behaviour under different compiler optimisation strategies).

    So equality in Haskell is always value equality; it tells you whether two terms represent the same value (not necessarily whether they have equal structure; if you implement a set with an unordered list then two lists with different structure can represent the same set).

    Almost all of the built in types are members of Eq already; the big exception are function types. The only really sensible notion of value equality for functions is extensional equality (do they return the same output for every input). It’s tempting to say we’ll use that and let the compiler access a representation of the function definition to compute this, but unfortunately determining whether two arbitrary algorithms (here encoded in Haskell syntax) always produce the same output is a known uncomputable problem; if the compiler could actually do that it could solve the Halting Problem, and we wouldn’t have to put up with the bottom value being a member of every type.

    And unfortunately the fact that functions can’t be members of Eq means lots of other things can’t be either; lists of integers can be compared for equality, but lists of functions can’t, and the same goes for every other conatiner-ish type when it’s containing functions. This also goes for ADTs that you write, unless there is a sensible notion of equality you can define for that type that doesn’t depend on the equality of the contained functions (maybe the function is just a convenience in the implementation, and which function it is doesn’t affect the value you’re representing with ADT).

    So, there are (1) types that are already members of Eq, (2) types that can’t be members of Eq, (3) types that can be members of Eq in an obvious way, (4) types that can be a member of Eq but only in a non-obvious way, and (5) types that can be members of Eq in an obvious way, but the programmer would prefer an alternative way. I think the way Haskell handles these cases is actually the right way. (1) and (2) don’t require anything from you, and (4) and (5) are always going to require an explicit instance declaration. The only case where the compiler could help you out a little more is (3), where it could potentially save you 12 characters of typing (4 if you’re already deriving anything else).

    I think that would be a pretty small win for the cost. The compiler would have to try to construct an instance of everything and presume that anything for which that fails isn’t supposed to have an Eq instance. At the moment if you want to derive an Eq instance and accidentally write a type for which that doesn’t work, the compiler tells you then and there that there’s a problem. With the proposed “implicitly make everything Eq that you can” strategy, this error would show up as an unexplained “no Eq instance” error at the point that you go to use the assumed instance. It also means that if I’m thinking of the type as representing values for which the reasonable equality relation isn’t simple structural equality (case (5) above; remember the set represented by an unordered list?), and I forget to write my own Eq instance, then the compiler might automatically generate a wrong Eq instance for me. I’d much rather be told “you haven’t written an Eq instance yet” when I go to use it than have the program compile and run with a bug introduced by the compiler!

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

Sidebar

Related Questions

I have code that basically looks like this (it isn't actually object, but rather
I have a div element which isn't at the top, but rather in the
I have this rather complex query that grabs data from three tables, and now
Rather than have everything in one big dialog, I'm looking at being able to
Rather than have a NULL in a column, I want a 0 to be
This question isn't about popping up an iframe inside a Lightbox; rather, it's about
I'm rather new to ClearCase but have searched through a lot of solutions to
I have a rather annoying cosmetic issue with jQuery. I'm using the $(window).scroll event
I'm rather new to bash scripting, and Google isn't as useful as I'd like
I know this isn't exactly a programming question per se, but rather a settings

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.