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
(.)
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.