Conceptual Understanding Anonymous Function

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

Why are function types not instances of the Eq class?

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

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

Why are function type instances of type classes not displayed?

Prelude> (\q -> q - 2)

I appreciate any clarification.

Thanks in advance!

+5
source share
4 answers

Are these functions the same or are they different:

dbl1 :: Int -> Int
dbl1 x = x + x

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

?

"" , . . , , - , dbl1 dbl2 . , , , , . . . , , IO...

+9

, , , : , () , () , , .

, , Eq - , . , Eq , . , , ; , , , , .

Haskell, , , , , . , :

(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \x -> f (g x)

, h :: c -> d, g :: b -> c h :: a -> b, f . (g . h) == (f . g) . h. , (.), :

f . (g . h) = f . (\y -> g (h y))          -- expand `g . h` by definition
            = \x -> f ((\y -> g (h y)) x)  -- expand `f . (\y -> g (h y))`
            = \x -> f (g (h x))            -- apply the inner lambda

(f . g) . h = (\y -> f (g y)) . h
            = \x -> (\y -> f (g y)) (h x)
            = \x -> f (g (h x))

, . , , , , , .

+6

Gödel , Eq , ⊥. , Eq ( ).

show Haskell, . Haskell, , , show .

show , , . "{-function-}" ( ), . Haskell. , .

+4
source

I like all the answers ... they seem to make sense. At this stage, I would not have thought why they are not set by default as instances of Eq and Show. These are just a few experiments that can give you ideas for testing:

Prelude> :set -XFlexibleInstances
Prelude> instance Eq (Int -> Int) where x == y = map x [0..10] == map y [0..10]
Prelude> ((\z -> z+5) :: Int -> Int) == ((+5) :: Int -> Int)
True
Prelude> instance Show (Int -> Int) where show x = show (zip [0..10] (map x [0..10]))
Prelude> (\q -> q-2) :: (Int -> Int)
[(0,-2),(1,-1),(2,0),(3,1),(4,2),(5,3),(6,4),(7,5),(8,6),(9,7),(10,8)]
+1
source

All Articles