简体   繁体   English

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

[英]Can I say that Monad makes it possible to see some types as isomorphic?

Monad can pass Just [1,2] , which is a different type from what the original length function takes, to >>= return. length Monad可以将Just [1,2]传递给>>= return. length ,它与原始length function 所采用的类型不同。 >>= return. length . >>= 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) ?我可以说Monad可以使用(>>=, return)Maybe [a]视为与[a]在长度上同构吗? (Of course they are not really isomorphic.) (当然它们并不是真正的同构。)

Can I choose the term "isomorphic" this situation?我可以选择术语“同构”这种情况吗?

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 .你的例子最终说明的是Maybe是一个函子:如果你有一些f:: a -> b ,你可以使用fmap保留身份和组合的方式将它变成fmap f:: Maybe a -> Maybe b . Monads are functors, with \fm -> m >>= return. f Monad 是函子,具有\fm -> m >>= return. f \fm -> m >>= return. f being the same as fmap fm . \fm -> m >>= return. ffmap fm相同。 In your case, we have the length function being transformed by the Maybe functor.在你的例子中,我们有length function 被Maybe仿函数转换。

can I choose term "isomorphic" this situation?我可以选择术语“同构”这种情况吗?

Not really.并不真地。 fmap for Maybe is not an isomorphism. Maybefmap不是同构。 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 .但是,没有(Maybe a -> Maybe b) -> (a -> b)函数,因为如果输入Maybe a -> Maybe b function 给出Nothing ,则无法获得b结果。 While there are specific functors whose fmap is an isomorphism ( Identity is one example), that is not the case in general.虽然有一些特定的函子,其fmap是同构的( Identity是一个例子),但通常情况并非如此。

[a] is isomorphic to the quotient type of Maybe [a] with Nothing and Just [] considered equivalent. [a]Maybe [a]商类型同构, NothingJust []被认为是等价的。 Alternatively it is isomorphic to Maybe ( NonEmpty a) , which simply eliminates the Just [] case.或者它与Maybe ( NonEmpty a)同构,这简单地消除了Just []的情况。
In other words, [a] can be factorized as a composition of the Maybe and NonEmpty functors.换句话说, [a]可以分解为MaybeNonEmpty函子的组合。

A result of this is that you can lift any function on NonEmpty a to a function on [a] :这样做的结果是您可以将NonEmpty a上的任何 function 提升为[a]上的 function:

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.正如 duplode 回答的那样,除了简单的仿函数映射之外,您实际上什么都不做。 We could at most elaborate that the monad laws ensure that the fmap really behaves as if length acted directly on the contained list:我们最多可以详细说明 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

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

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