身份单片作为免费的单子
身份单元的函数可以定义为:
data Identity a = Identity a
因为这个单子是免费的,所以另一种定义是:
data Term f a = Pure a | Impure (f (Term f a))
data Zero a
type IdentityF a = Term Zero a
由于这是通过两种方式定义的单一单元,所以它们却不 互相转换。也就是说,一个应该能够定义两个 函数f ::身份a - > sidentityf a
和g :: identityf a - >身份a
使其构图f。 g
和g。 f
是 身份。函数f
易于定义:
f :: Identity a -> IdentityF a
f (Identity a) = Pure a
但是功能g
呢?
g :: IdentityF a -> Identity a
g (Pure a) = Identity a
g (Impure x) = ??????
g(不纯x)
的值应该是什么?我可以尝试作弊 是未定义的
,但是f。 g
不会是身份函数, Identity
和Identityf
不会是同构。
The functor of the identity monad can be defined as:
data Identity a = Identity a
Because this monad is free, an alternative definition is the following:
data Term f a = Pure a | Impure (f (Term f a))
data Zero a
type IdentityF a = Term Zero a
Since this is the same monad defined in two ways, they shoud be
convertible into each other. That is to say that one should be able to define two
functions f :: Identity a -> IdentityF a
and g :: IdentityF a -> Identity a
such that their compositions f . g
and g . f
are
identities. The function f
is easy to define:
f :: Identity a -> IdentityF a
f (Identity a) = Pure a
But what about the function g
?
g :: IdentityF a -> Identity a
g (Pure a) = Identity a
g (Impure x) = ??????
What should be the value of g (Impure x)
. I could try to cheat and say it
is undefined
but then f . g
would not be the identity function andIdentity
and IdentityF
would not be isomorphic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个合适的定义是:
案例
中没有分支。这不是错字。在此情况下,分支机构与Zero a
中的构造函数一样多。这是一个完整的模式匹配。(您必须打开
emptricage
GHC的扩展名才能接受此AS-I。)One suitable definition is:
There are no branches in the
case
. This was not a typo. There are exactly as many branches in the case as there are constructors inZero a
, as required; this is a complete pattern match.(You must turn on the
EmptyCase
extension for GHC to accept this as-is.)