简体   繁体   English

Haskell函数组成与fmap f

[英]Haskell function composition and fmap f

I have two simple examples: 我有两个简单的例子:

1) xt function (what is this?) 1) xt函数(这是什么?)

Prelude> :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b
Prelude> :{
Prelude| f::Int->Int
Prelude| f x = x
Prelude| :}
Prelude> xt = fmap f // ?
Prelude> :t xt
xt :: Functor f => f Int -> f Int
Prelude> xt (+2) 1
3

2) xq function (via composition) 2) xq函数(通过组合)

Prelude> :{
Prelude| return x = [x]
Prelude| :}
Prelude> xq = return . f
Prelude> :t xq
xq :: Int -> [Int]
Prelude> :t return
return :: a -> [a]

xq function I get through composition return(f(x)) . xq函数我通过组合return(f(x)) But what does that mean: fmap f and what is difference? 但这意味着什么: fmap f和有什么区别?

The Functor instance for (->) r defines fmap to be function composition: (->) rFunctor实例将fmap定义为函数组成:

fmap f g = f . g

Thus, xt (+2) == fmap f (+2) == f . (+2) == (+2) 因此, xt (+2) == fmap f (+2) == f . (+2) == (+2) xt (+2) == fmap f (+2) == f . (+2) == (+2) (since f is the identity function for Int ). xt (+2) == fmap f (+2) == f . (+2) == (+2) (因为fInt的恒等函数)。 Applied to 1, you get the observed answer 3. 应用于1,您将得到观察到的答案3。


fmap is the function defined by the Functor type class: fmap是由Functor类型类定义的函数:

class Functor f where
    fmap :: (a -> b) -> f a -> f b

It takes a function as its argument and returns a new function "lifted" into the functor in question. 它以一个函数为参数,并向该函子返回“提升”的新函数。 The exact definition is supplied by the Functor instance. 确切的定义由Functor实例提供。 Above is the definition for the function functor; 上面是函数函子的定义; here for reference are some simpler ones for lists and Maybe : 这里供参考的是一些简单的清单和Maybe

instance Functor [] where
    fmap = map

instance Functor Maybe where
    fmap f Nothing = Nothing
    fmap f (Just x) = Just (f x)

> fmap (+1) [1,2,3]
[2,3,4]
> fmap (+1) Nothing
Nothing
> fmap (+1) (Just 3)
Just 4

Since you can think of functors as boxes containing one or more values, the intuition for the function functor is that a function is a box containing the result of applying the function to its argument. 由于可以将函子视为包含一个或多个值的框,因此函数函子的直觉是,函数是包含将函数应用于其参数的结果的框。 That is, (+2) is a box that contains some value plus 2. (F)mapping a function on that box provides a box that contains the result of applying f to the result of the original function, ie, produces a function that is the composition of f with the original function. 也就是说, (+2)是一个包含一些值加2的框。(F)在该框上映射一个函数可提供一个框,其中包含将f应用于原始函数的结果的结果,即产生一个是具有原始功能的f的组成。

Both xq = return . f 两者xq = return . f xq = return . f and xt = fmap f can be eta-expanded : xq = return . fxt = fmap f可以被eta展开

xq x = (return . f) x = return (f x) = return x

Now it can be eta-contracted : 现在可以将其收缩

xq = return

The second is 第二个是

xt y = fmap f y = fmap (\x -> x) y = fmap id y = id y = y

fmap has type :: Functor f => (a -> b) -> fa -> fb so fmap f has type :: Functor f => f Int -> f Int , because f :: Int -> Int . fmap类型为fmap :: Functor f => (a -> b) -> fa -> fb fmap :: Functor f => (a -> b) -> fa -> fb fmap f :: Functor f => (a -> b) -> fa -> fb因此fmap f类型为fmap f :: Functor f => f Int -> f Int fmap f :: Functor f => f Int -> f Int ,因为f :: Int -> Int fmap f f :: Int -> Int From its type we see that fmap f is a function, expecting an Int , and producing an Int . 从类型上我们可以看到fmap f是一个函数,期望一个Int并产生一个Int

Since fx = x for Int s by definition, it means that f = id for Int s, where id is a predefined function defined just the same way as f is (but in general, for any type). 由于按定义fx = x表示Int ,因此这意味着f = id表示Int ,其中id是预定义的函数,定义的方式与f相同(但通常,对于任何类型)。

Then by Functor laws (and that's all we need to know about "Functors" here) , fmap id = id and so xt y = y , in other words it's also id - but only for Int s, 然后通过函子法 (这就是我们需要了解“函子”在这里), fmap id = idxt y = y ,换句话说,它也id -但仅限于Int S,

xt = id :: Int -> Int

Naturally, xt (+2) = id (+2) = (+2) . 自然地, xt (+2) = id (+2) = (+2)


Addendum: for something to be a "Functor" means that it can be substituted for f in 附录:如果要成为“ Functor”,则可以用f代替

fmap id (x :: f a) = x
(fmap g . fmap h)  = fmap (g . h)

so that the expressions involved make sense (ie are well formed, ie have a type), and the above equations hold (they are in fact the two "Functor laws"). 从而使所涉及的表达式有意义(即结构良好,即具有类型),并且上述等式成立(它们实际上是两个“函子定律”)。

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

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