Search code examples
haskelllambdalanguage-design

Anonymous function conceptual understanding


I am trying to learn and understand the design of Haskell. I am currently on Lambda / Anonymous functions and I was wondering.

Why aren't function types instances of the Eq class?

Prelude> (\z -> z + 5) == (+5)

On this question, I was wondering if it is because z can be anything and may be even be a free variable, in all lambda functions, so it would be a design flaw to make lambda functions of type Eq.

Why aren't function types instances of the type class Show?

Prelude> (\q -> q - 2)

I appreciate any clarification.

Many thanks in advance!


Solution

  • Are these functions the same or are they different:

    dbl1 :: Int -> Int
    dbl1 x = x + x
    
    dbl2 :: Int -> Int
    dlb2 x = 2 * x
    

    ?

    For some functions it's "easy" for the compiler to see that they contain the same logic. But most functions would be extremely difficult to compare. Then there are functions that are logically different, but behave the same - like dbl1 and dbl2 above. So, you would have to make a choice to either test them against every possible value, or decide they are not equal. The former is completely impractical in most cases. The latter is definitely not desirable or intuitive. Now, consider that the problem is already too difficult to solve, and then throw in IO...