I'm looking at the following function:
fmap :: (a -> b) -> f a -> f b
and I want to understand what the 'f' is, as in ( fa
or fb
). The article I am reading describes it as a 'box' but what's the actual correct name for it? Is it just a type variable? I think I'm confusing it and thinking it's a function application - which is correct?
Your intuition that it is a kind of function application is correct, but they are not regular functions. Instead, this is application of type constructors on the type level.
Specifically, Functors must have kind (type-of-type) * -> *
which means they take one type argument and produce a concrete type *
such as, for example, [Int]
.
Examples of such type constructors include IO, Maybe, [], Either e
and many others, and these specific examples all have valid Functor instances.
fmap (+1) [1,2,3] :: [] Int -- also known as [Int]
= [2,3,4]
fmap (+1) (Just 1) :: Maybe Int
= Just 2
fmap (+1) (Right 1) :: Either e Int
= Right 2
fmap (+1) (return 1) :: IO Int -- Uses Monad IO instance as well
"=" 2
It's a type variable, representing the particular functor you're working in. For example IO is a functor, so you can specialize fmap
to
fmap :: (a -> b) -> IO a -> IO b
Similarly you could specialize it to lists:
fmap :: (a -> b) -> [a] -> [b]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.