我可以说 Monad 可以将某些类型视为同构吗?

发布于 2025-01-14 05:58:28 字数 349 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2025-01-21 05:58:28

您的示例最终说明的是 Maybe 是一个函子:如果您有一些 f :: a ->; b,可以使用fmap将其变成fmap f :: Maybe a ->也许b 以保留身份和组合的方式。 Monad 是函子,带有 \fm -> m>>=返回。 ffmap f m 相同。在您的例子中,我们有由 Maybe 函子转换的 length 函数。

这种情况我可以选择术语“同构”吗?

并不真地。 也许fmap不是同构。同构需要有一个双向逆来消除它,在这种情况下,这将类似于:

unFmapMaybe :: (Maybe a -> Maybe b) -> (a -> b)

-- For it to be a two-sided inverse to `fmap`, we should have:
unFmapMaybe . fmap = id
fmap . unFmapMaybe = id

然而,不存在 (Maybe a -> Maybe b) ->; (a -> b) 函数,因为如果输入 Maybe a ->; 则无法获得 b 结果。也许 b 函数给出了 Nothing。虽然某些特定函子的 fmap 是同构的(Identity 就是一个例子),但一般情况并非如此。

What your example ultimately illustrates is that Maybe is a functor: if you have some f :: a -> b, you can use fmap to turn it into fmap 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 as fmap f m. In your case, we have the length function being transformed by the Maybe functor.

can I choose term "isomorphic" this situation?

Not really. fmap for Maybe is not an isomorphism. An isomorphism requires there being a two-sided inverse that undoes it, which in this case would be something like:

unFmapMaybe :: (Maybe a -> Maybe b) -> (a -> b)

-- For it to be a two-sided inverse to `fmap`, we should have:
unFmapMaybe . fmap = id
fmap . unFmapMaybe = id

However, there are no (Maybe a -> Maybe b) -> (a -> b) functions, as there is no way to obtain a b result if the input Maybe a -> Maybe b function gives out a Nothing. While there are specific functors whose fmap is an isomorphism (Identity is one example), that is not the case in general.

何处潇湘 2025-01-21 05:58:28

[a]Maybe 的 商类型 同构[a]NothingJust [] 被认为是等效的。或者它与 Maybe 同构 (NonEmpty a),它简单地消除了 Just [] 情况。

换句话说,[a] 可以分解为 MaybeNonEmpty 函子的组合。

这样做的结果是,您可以将 NonEmpty a 上的任何函数提升为 [a] 上的函数:

liftEmptyable :: (NonEmpty a -> r) -> [a] -> Maybe r
liftEmptyable _ [] = Nothing
liftEmptyable f (x:xs) = Just $ f (x:|xs)

但不确定这实际上与您的问题有多大关系。正如 duplode 回答的那样,除了简单的函子映射之外,您实际上什么也不做。我们最多可以详细说明 monad 法则确保 fmap 的行为确实就像 length 直接作用于所包含的列表:

Just [1,2] >>= return . length
  ≡ return [1,2] >>= return . length  -- def. of `Monad Maybe`
  ≡ return (length [1,2])             -- left-identity monad law

[a] is isomorphic to the quotient type of Maybe [a] with Nothing and Just [] considered equivalent. Alternatively it is isomorphic to Maybe (NonEmpty a), which simply eliminates the Just [] case.

In other words, [a] can be factorized as a composition of the Maybe and NonEmpty functors.

A result of this is that you can lift any function on NonEmpty a to a function on [a]:

liftEmptyable :: (NonEmpty a -> r) -> [a] -> Maybe r
liftEmptyable _ [] = Nothing
liftEmptyable f (x:xs) = Just $ f (x:|xs)

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 if length acted directly on the contained list:

Just [1,2] >>= return . length
  ≡ return [1,2] >>= return . length  -- def. of `Monad Maybe`
  ≡ return (length [1,2])             -- left-identity monad law
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文