Im trying to define the polymorphic type of the following function:
flip f x y = f y x
My thought was the following:
1st parameter of
flip,ftakes two arguments so(t1 -> t2 -> t3)2nd parameter of
flip,xis of typet1because of the parametert1insideffunction.3rd parameter of
flip,ywhich is of typet3because of the parametert3insideffunction.I don't know the polymorphic type of the overall return.
But when I checked the type in the ghci, I get:
flip :: (t2 -> t1 -> t) -> t1 -> t2 -> t
Can someone please help go through this example was to whats happening here?
Thanks
Your second assumption is wrong:
Let us first analyze the function:
We see that
fliphas three arguments in the head. So we first make the type:We will of course now aim to fill in the types. With:
We see on the right hand side:
So that means that
fis a function that takes as inputy. So that means thatais the same type asc -> e(or shortera ~ c -> e).So now:
Furthermore we see that:
So the result of
(f x)is another function, with as inputy. So that means thate ~ b -> f. Thus:Finally we see that
(f y) xis the result offlip f x y. So that means that the type of the result of(f y) xis the same type asd. So that means thatf ~ d. Which thus means that:Or if we drop some redundant brackets: