Haskell: join 是如何自然转变的?
我可以在 Haskell 中将自然变换定义为:
h :: [a] -> Maybe a
h [] = Nothing
h (x:_) = Just x
并使用函数 k:
k :: Char -> Int
k = ord
满足自然性条件,因为:
h 。 fmap k == fmap k 。 h
List monad的join
函数的自然性条件是否可以用类似的方式证明?我无法理解join
(特别是concat
)是如何自然转换的。
I can define a natural transformation in Haskell as:
h :: [a] -> Maybe a
h [] = Nothing
h (x:_) = Just x
and with a function k:
k :: Char -> Int
k = ord
the naturality condition is met due to the fact that:
h . fmap k
== fmap k . h
Can the naturality condition of the List monad's join
function be demonstrated in a similar way? I'm having some trouble understanding how join
, say concat
in particular, is a natural transformation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,让我们看看
concat
。首先,这是实现:
这与
h
的结构类似,其中Maybe
被[]
替换,更重要的是,[ ]
被替换为——暂时滥用语法——[[]]
。当然,
[[]]
也是一个函子,但它不是自然条件使用它的方式的Functor
实例。直接翻译您的示例是行不通的:concat 。 fmap k
=/=fmap k 。 concat
...因为两个
fmap
都只作用于最外面的[]
。尽管
[[]]
假设是Functor
的一个有效实例,但由于可能显而易见的实际原因,您不能直接将其设为一个实例。但是,您可以像这样重建正确的提升:
concat 。 (fmap . fmap) k
==fmap k 。 concat
...其中
fmap 。 fmap
相当于为[[]]
假设的Functor
实例实现fmap
。作为相关附录,
return
由于相反的原因而显得尴尬:a ->; f a
是来自省略恒等函子的自然变换。使用: []
身份将被写成:(:[]) 。 ($) k
==fmap k 。 (:[])
...其中完全多余的
($)
代替了省略的恒等函子上的fmap
。Okay, let's look at
concat
.First, here's the implementation:
This parallels the structure of your
h
whereMaybe
is replaced by[]
and, more significantly,[]
is replaced by--to abuse syntax for a moment--[[]]
.[[]]
is a functor as well, of course, but it's not aFunctor
instance in the way that the naturality condition uses it. Translating your example directly won't work:concat . fmap k
=/=fmap k . concat
...because both
fmap
s are working on only the outermost[]
.And although
[[]]
is hypothetically a valid instance ofFunctor
you can't make it one directly, for practical reasons that are probably obvious.However, you can reconstruct the correct lifting as so:
concat . (fmap . fmap) k
==fmap k . concat
...where
fmap . fmap
is equivalent to the implementation offmap
for a hypotheticalFunctor
instance for[[]]
.As a related addendum,
return
is awkward for the opposite reason:a -> f a
is a natural transformation from an elided identity functor. Using: []
the identity would be written as so:(:[]) . ($) k
==fmap k . (:[])
...where the completely superfluous
($)
is standing in for what would befmap
over the elided identity functor.