提升功能与高阶功能之间的区别 [英] Difference between lifting and higher order functions
问题描述
当人们谈论map
,fold
或bind
时,我通常会听到提升"一词,但基本上每个高阶函数都不都是某种提升吗?
I usually hear the term lifting, when people are talking about map
, fold
, or bind
, but isn't basically every higher order function some kind of lifting?
为什么filter
不能成为从a -> Bool
到[a] -> [a]
的提升,甚至连bool
函数(模拟if语句的函数)也可以被认为是从a -> a
到Bool -> a
的提升.如果不是,那么为什么Applicative类型类中的ap
被认为是提升?
Why can't filter
be a lift from a -> Bool
to [a] -> [a]
, heck even the bool
function (which models an if statement) can be considered a lift from a -> a
to Bool -> a
. And if they are not, then why is ap
from the Applicative type class considered a lift?
如果重要的事情从... a ...
变为... f a ...
,则ap
也不适合这种情况:f (a -> b) -> f a -> f b
If the important thing is going from ... a ...
to ... f a ...
, then ap
wouldn't fit the case either: f (a -> b) -> f a -> f b
推荐答案
我很惊讶没有人回答这个问题.
I'm surprised no one has answered this already.
提升功能的作用是将功能提升到上下文中(通常是Functor或Monad).因此,将类型a -> b
的函数放到List
上下文中将导致类型为List[a] -> List[b]
的函数.如果您考虑一下,这正是map
(或Haskell中的fmap
)所做的事情.实际上,它是Functor定义的一部分.
A lifting function's role is to lift a function into a context (typically a Functor or Monad). So lifting a function of type a -> b
into a List
context would result in a function of type List[a] -> List[b]
. If you think about it this is exactly what map
(or fmap
in Haskell) does. In fact, it is part of the definition of a Functor.
但是,函子只能提升一个参数的功能.我们还希望能够处理其他Arities的功能.例如,如果我们具有类型a -> b -> c
的函数,则不能使用map
.这是图片中更一般的提升操作的地方.在Haskell中,对于这种情况,我们有一个lift2
:
However, a Functor can only lift functions of one argument. We also want to be able to deal with functions of other arities as well. For example if we have a function of type a -> b -> c
we cannot use map
. This is where a more general lifting operation comes into the picture. In Haskell we have a lift2
for this case:
lift2:: (a -> b -> c) -> (M[a] -> M[b] -> M[c])
其中M[a]
是使用给定类型a
参数化的某些特定Monad(如List
).
where M[a]
is some particular Monad (like List
) parameterized with a given type a
.
还有lift
的其他变体,也为其他工具定义了.
There are additional variants of lift
defined as well for other arities.
这也是为什么filter
不是提升函数的原因,因为它不符合所需的类型签名;您没有将a -> bool
类型的功能提升为M[a] -> M[bool]
.但是,它是一个高阶函数.
This is also why filter
is not a lifting function as it doesn't fit the type signature required; you are not lifting a function of type a -> bool
to M[a] -> M[bool]
. It is, however, a higher-ordered function.
如果您想了解有关解除Haskell Wiki的更多信息,请参见上面的好文章
If you want to read more about lifting the Haskell Wiki has a good article on it
这篇关于提升功能与高阶功能之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!