Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function composition type inference in haskell

In haskell, the type of (.) function is:

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

And the type of (.) (.) is:

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

I am not able to deduce the result, how is this done?

like image 942
richard.g Avatar asked Apr 20 '26 17:04

richard.g


1 Answers

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

Let's go through it. First thing this function takes is (b -> c) (so a function from b to c), cool. By adding a pair of (redundant) parentheses:

(.) :: (b -> c) -> ((a -> b) -> a -> c)
       ^-- I am b'  ^-- I am c'   -- (b' and c' not to have name clash)

That first part, we gave to the function (i.e. has been taken care of):

(.) (.) :: (a -> b') -> a -> c'
-- after substituting stuff (b' and c')
(.) (.) :: (a -> (b -> c)) -> a -> ((a1 -> b) -> a1 -> c)
                                    ^-- of course a1 /= a
-- you could eliminate redundant parentheses
(.) (.) :: (a -> b -> c) -> a -> (a1 -> b) -> a1 -> c
-- wee

I hope this settles it. Main point is: type inference is easy to 'get' and once you get it is only a matter of substitution to reach what ghci automagically infers. ot: we could call this quizzical operator boobs.

like image 188
Lolloman Avatar answered Apr 23 '26 11:04

Lolloman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!