Question

I'm new to Haskell and Functional Programming and I'm not able to figure out why the type-signature of

t2 = (\x y z a -> ((x.y) z, (y.x) z))

looks like this:

t2 :: (a -> a) -> (a -> a) -> a -> b -> (a,a)

Could you please tell me how it works?

regards

Was it helpful?

Solution

(.) is the composition function and its type is

(.) :: (b -> c) -> (a -> b) -> (a -> c)

So, we know that because you are composing x and y, they must be functions and have types like this:

x :: a -> b
y :: a' -> b'

Note that when you compose two functions, the output type of the function on the right has to match the input type of the function on the left. Therefore, when you do x . y, we can infer that b' and a are the same type. Similarly, we can infer that a' and b are the same type. This gives us the following:

(y . x) :: a -> a
(x . y) :: b -> b

However, you then went ahead and applied both of these function to z, which means that a and b must be the same type, namely the type of z, so we can further infer that

(y . x) :: a -> a
(x . y) :: a -> a
z :: a

Lastly, the variable a is just a dummy variable that isn't actually used in the definition of the function, so it gets a different type variable.

OTHER TIPS

From the lambda we can see that t2 takes 4 arguments x, y, z, and a. And then it returns a tuple. . in is for composition. So from that we can see both x and y are functions, not just values. Then we see that we have both x . y and y . x That means that the return type of x must be the argument type of y and visa versa. So we know that x and y must have the same type thus a -> a is the type for both of them. Thus the x . y :: a -> a and y . x :: a -> a so we know z must have type a. Finally a, the variable is never used so it can be any type because we can't find out anything more from it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top