Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s simply the function 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading