我可以说 Monad 可以将某些类型视为同构吗?
Monad
可以将 Just [1,2]
(与原始 length
函数采用的类型不同)传递给 > >= 返回 .长度。
Just [1,2] >>= return . length
我可以说 Monad
可以使用 (> 来将
? (当然它们并不是真正同构的。)这种Maybe [a]
视为与 [a]
在长度上同构吗? >=,返回)
情况我可以选择“同构”这个词吗?
Monad
can pass Just [1,2]
, which is a different type from what the original length
function takes, to >>= return . length
.
Just [1,2] >>= return . length
Can I say that Monad
makes it possible to see Maybe [a]
as isomorphic with [a]
on length using (>>=, return)
? (Of course they are not really isomorphic.)
Can I choose the term "isomorphic" this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例最终说明的是
Maybe
是一个函子:如果您有一些f :: a ->; b
,可以使用fmap
将其变成fmap f :: Maybe a ->也许b
以保留身份和组合的方式。 Monad 是函子,带有\fm -> m>>=返回。 f
与fmap f m
相同。在您的例子中,我们有由Maybe
函子转换的length
函数。并不真地。
也许
的fmap
不是同构。同构需要有一个双向逆来消除它,在这种情况下,这将类似于:然而,不存在
(Maybe a -> Maybe b) ->; (a -> b)
函数,因为如果输入Maybe a ->; 则无法获得
函数给出了b
结果。也许 bNothing
。虽然某些特定函子的fmap
是同构的(Identity
就是一个例子),但一般情况并非如此。What your example ultimately illustrates is that
Maybe
is a functor: if you have somef :: a -> b
, you can usefmap
to turn it intofmap f :: Maybe a -> Maybe b
in a way that preserves identities and composition. Monads are functors, with\f m -> m >>= return . f
being the same asfmap f m
. In your case, we have thelength
function being transformed by theMaybe
functor.Not really.
fmap
forMaybe
is not an isomorphism. An isomorphism requires there being a two-sided inverse that undoes it, which in this case would be something like:However, there are no
(Maybe a -> Maybe b) -> (a -> b)
functions, as there is no way to obtain ab
result if the inputMaybe a -> Maybe b
function gives out aNothing
. While there are specific functors whosefmap
is an isomorphism (Identity
is one example), that is not the case in general.[a]
与Maybe 的 商类型 同构[a]
与Nothing
和Just []
被认为是等效的。或者它与Maybe 同构 (NonEmpty a)
,它简单地消除了Just []
情况。换句话说,
[a]
可以分解为Maybe
和NonEmpty
函子的组合。这样做的结果是,您可以将
NonEmpty a
上的任何函数提升为[a]
上的函数:但不确定这实际上与您的问题有多大关系。正如 duplode 回答的那样,除了简单的函子映射之外,您实际上什么也不做。我们最多可以详细说明 monad 法则确保
fmap
的行为确实就像length
直接作用于所包含的列表:[a]
is isomorphic to the quotient type ofMaybe [a]
withNothing
andJust []
considered equivalent. Alternatively it is isomorphic toMaybe (NonEmpty a)
, which simply eliminates theJust []
case.In other words,
[a]
can be factorized as a composition of theMaybe
andNonEmpty
functors.A result of this is that you can lift any function on
NonEmpty a
to a function on[a]
:Not sure that actually has much to do with your question though. As duplode answered, you don't really do anything but a simple functor mapping. We could at most elaborate that the monad laws ensure that the
fmap
really behaves as iflength
acted directly on the contained list: