Search code examples
functionhaskellconstructorfunctor

What is (->) in Haskell?


If you type :i (->) in GHCi and hit Enter it returns the following:

data (->) t1 t2     -- Defined in ‘GHC.Prim’
infixr 0 `(->)`
instance Monad ((->) r) – Defined in ‘GHC.Base’
instance Functor ((->) r) – Defined in ‘GHC.Base’
instance Applicative ((->) a) – Defined in ‘GHC.Base’
instance Monoid b => Monoid (a -> b) – Defined in ‘GHC.Base’

Judging by the data keyword, it's a type constructor of some kind, but what exactly does it construct and what are the value constructors for this type, if they exist?

The question arose when I learned that functions are part of the Functor type class and are listed as ((->) r) in the type class description returned by the :i Functor command. I tried to get information about ((->) r), but to no avail. Then, in the description of the Functor type class, I spotted (Either a) (whose description can be obtained with :i Either, i.e. without the parameter) and realized that I should try :i (->) instead, which I did and got the information shown above.


Solution

  • It's simply the function type constructor.

    The type (->) a b, which is more often written in infix form as a -> b, is the type of function which takes the type a as argument and returns the type b. You'll see this in the type signature of any function.

    Like most other things in GHC.Prim, it's built in and a little bit "magical", in the sense that it doesn't have value constructors - but you can define a value of type a -> b, that is a function, in all the ways I expect you already know about.