简体   繁体   English

'f'在仿函数的fmap函数中表示什么?

[英]What does the 'f' represent in the fmap function of a functor?

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 ). 我想了解'f'是什么,如( fafb )。 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] . 具体来说,Functors必须具有类型(type-of-type) * -> * ,这意味着它们采用一种类型参数并生成具体类型* ,例如[Int]

Examples of such type constructors include IO, Maybe, [], Either e and many others, and these specific examples all have valid Functor instances. 此类型构造函数的示例包括IO, Maybe, [], Either e和许多其他构造函数,并且这些特定示例都具有有效的Functor实例。

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函数。例如,IO是一个fmap函数,因此您可以将fmap专门fmap

fmap :: (a -> b) -> IO a -> IO b

Similarly you could specialize it to lists: 同样,您可以将其专门化为列表:

fmap :: (a -> b) -> [a] -> [b]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM