[英]How are Haskell Monad laws derived from Monoid laws?
The laws for monoids in the category of endofunctors are:内函子范畴中幺半群的定律是:
And the Haskell monad laws are: Haskell monad 法则是:
Left identity: return a >>= k = ka
左身份:
return a >>= k = ka
Right identity: m >>= return = m
正确的身份:
m >>= return = m
Associativity: m >>= (\\x -> kx >>= h) = (m >>= k) >>= h
结合性:
m >>= (\\x -> kx >>= h) = (m >>= k) >>= h
I'm assuming the latter is derived from the former, but how so?我假设后者是从前者派生的,但怎么会呢? The diagrams basically say
图表基本上说
join (join x) = join (fmap join x)
join (return x) = x
join (fmap return x) = x
How are these equivalent to the Haskell monad laws?这些如何等同于 Haskell monad 定律?
To show the >>=
-monad laws from the join
-monad laws, one needs to define x >>= y
in terms of multiplication ( join
), unity ( return
), and functoriality ( fmap
), so we need to let, by definition,为了从
join
-monad 定律中显示>>=
-monad 定律,需要根据乘法 ( join
)、统一性 ( return
) 和函式 ( fmap
) 定义x >>= y
,所以我们需要让,根据定义,
(x >>= y) = join (fmap y x)
The left identity law then becomes左恒等式则变为
return a >>= k = k a
By definition of >>=
, it is equivalent to根据
>>=
定义,它等价于
join (fmap k (return a)) = k a
Now, return
is a natural transformation I -> T
(where I
is the identity functor), so fmap_T k . return = return . fmap_I k = return . k
现在,
return
是一个自然变换I -> T
(其中I
是恒等函子),所以fmap_T k . return = return . fmap_I k = return . k
fmap_T k . return = return . fmap_I k = return . k
fmap_T k . return = return . fmap_I k = return . k
. fmap_T k . return = return . fmap_I k = return . k
。 We reduce the law to:我们将法律简化为:
join (return (k a)) = k a
And this follows by the join
laws.这遵循
join
法。
The right identity law正确的身份法
m >>= return = m
reduces to, by definition of >>=
:根据
>>=
的定义, >>=
:
join (fmap return m) = m
which is exactly one of the join
laws.这正是
join
法之一。
I'll leave the associativity law for you to prove.我将把结合律留给你证明。 It should follow using the same tools (
join
laws, naturality, functoriality).它应该遵循使用相同的工具(
join
法则、自然性、功能性)。
Phrase the monad laws in terms of the Kleisli composition operator (>=>)
. 用 Kleisli 组合运算符
(>=>)
表述monad 定律。
Assuming k :: a -> mb
, k' :: b -> mc
, k'' :: c -> md
(ie k
, k'
, k''
are Kleisli arrows)假设
k :: a -> mb
, k' :: b -> mc
, k'' :: c -> md
(即k
, k'
, k''
是 Kleisli 箭头)
return >=> k = k
return >=> k = k
k >=> return = k
k >=> return = k
(k >=> k') >=> k'' = k >=> (k' >=> k'')
(k >=> k') >=> k'' = k >=> (k' >=> k'')
It's relatively straightforward from the definition of (>=>)
to show that these are equivalent to what you wrote.从
(>=>)
的定义中可以相对简单地表明这些与您编写的内容相同。 And you don't need any fancy diagrams or anything: these are literally the monoid laws, with return
as the identity and (>=>)
as your monoid operation.而且您不需要任何花哨的图表或任何东西:这些实际上就是幺半群定律,以
return
作为身份, (>=>)
作为您的幺半群运算。
The diagram you show in your picture is a different way of thinking about monads.您在图片中显示的图表是对 monad 的另一种思考方式。 You can equivalently define monads in terms of natural transformations (ie
join
and return
) or in terms of composition (ie return
and (>>=)
/ (>=>)
).您可以根据自然转换(即
join
和return
)或组合(即return
和(>>=)
/ (>=>)
)等价地定义 monad。 The latter approach lends itself to the monoid way of thinking that you're looking for.后一种方法适用于您正在寻找的幺半群思维方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.