Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `f x = x x` and `g x = x x x x x` have the same type

Tags:

haskell

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 xs) still has the same type. Can anyone explain why it is the case?

like image 586
Meowcolm Law Avatar asked Nov 08 '21 13:11

Meowcolm Law


People also ask

What differentiation rule we can use in solving the derivative of composite function f/g x ))?

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*.


1 Answers

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

like image 143
chepner Avatar answered Oct 05 '22 03:10

chepner