Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding the type signature of (.)

I'm just learning Haskell and functional programming using Richard Bird's book and came across the type signature of the (.) function. Namely

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

and the associated definition

(f . g) x = f (g x)

I understand what the operator is doing but am a bit confused about how to read the type signature. Is it saying that (.) takes as its first argument a function of type (b -> c), then returns a function of type (a -> b), and finally returns a function of type (a -> c)? Is this the right way to read the type signature?

Also, could this be an example of currying, where (.) is a curried function that takes two parameters? Or is that not the correct way to think about currying?

like image 349
user139014 Avatar asked Dec 05 '22 08:12

user139014


1 Answers

You've almost got it, it takes a b -> c and returns a function (a -> b) -> (a -> c) which, when given an (a -> b) returns a function a -> c. It may also be helpful to know that in Haskell, you can wrap an operator in parens and use it prefix so

f . g === (.) f g

Now it's easier to see the currying

((.) f) g === f . g

Finally, notice that this type signature is equivalent to

(b -> c) -> (a -> b) -> a -> c

Since -> is right associative.

like image 104
Daniel Gratzer Avatar answered Jan 03 '23 09:01

Daniel Gratzer