1

I am trying to understand the Eq class and while I was reading a chapter about the types in Haskell I faced the following question:

"Explain why is not feasible in general for function types to be instances of the Eq class."

I tried to understand by checking the type of the (==) function.

:t (==)
(==) :: Eq a => a -> a -> Bool

So a is instance of Eq class, but this doesn't show that the (==) function is instance of the Eq class, does it? For me, seems that the two arguments of the function == are instances of the type Eq.

equal x1 x2 = x1 == x2

Is actually equivalent to (==).

My understanding is that, is not feasible always (my above example is feasible) for function types to be instances of the Eq class because they have to have the same number of arguments (two) and return the same type (Bool).

Although I am not sure if that is the case. Is not crystal clear what is meant for a function type to be an instance of a class.

Phrixus
  • 1,209
  • 2
  • 19
  • 36
  • The type of `==` indicates that if `a` is an instance of `Eq`, then `==` is defined for that type. It says nothing about whether `==` itself is an instance of `Eq` (which it cannot be, being a function and not a type). – chepner Sep 30 '16 at 22:58
  • 2
    You misunderstood the question. It is asking why we can't define `function1 == function2` as in e.g. `(+) == (-)` or `(\x -> x+x) == (\x ->2*x)`. This latter example should be a hint. – chi Sep 30 '16 at 23:29
  • 1
    Also possibly relevant: [How to compare two functions for equivalence](http://stackoverflow.com/q/17045941/791604). – Daniel Wagner Sep 30 '16 at 23:53
  • 1
    I don't understand what is being asked. The question does not contain a clear problem statement. I think we are misunderstanding a question that is itself based on a misunderstanding of a question. Before we mark it as a duplicate or attempt to answer it, we should try to figure out what the actual question is. – Rein Henrichs Oct 01 '16 at 04:12
  • 1
    it isn't feasible, because there can be a set A of elements on which functions f and g are defined, such that you can feed 99% of values from A to both f and g and get the same return results. But that 1% of values, call them x, gives you f(x) != g(x), so, you would literally have to apply those 2 functions to a lot of values in order to find out that they are not equal, this is why it isn't feasible. – pavel_orekhov Oct 27 '22 at 22:35

0 Answers0