I'm playing with Rank-N-type and trying to type x x
. But I find it counter intuitive that these two functions can be typed in the same way.
f :: (forall a b. a -> b) -> c
f x = x x
g :: (forall a b. a -> b) -> c
g x = x x x x x
I have also noticed that something like f x = x x ... x x
(many x
s) still has the same type.
Can anyone explain why it is the case?
Answer: The chain rule explains that the derivative of f(g(x)) is f'(g(x))⋅g'(x). In other words, the chain rule helps in differentiating *composite functions*.
The key is that x :: a -> b
is a function that can provide a value of any type, no matter what argument is given. That means x
can be applied to itself, and the result can be applied to x
again, and so on and so on.
At least, that's what it promises the type checker it can do. The type checker isn't concerned about whether or not any such value exists, only that the types align. Neither f
nor g
can actually be called, because no such value of type a -> b
exists (ignoring bottom and unsafeCoerce
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With