身份单片作为免费的单子

发布于 2025-01-25 05:19:09 字数 798 浏览 3 评论 0原文

身份单元的函数可以定义为:

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 ag :: identityf a - >身份a使其构图f。 gg。 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不会是身份函数, IdentityIdentityf不会是同构。

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 and
Identity and IdentityF would not be isomorphic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

反差帅 2025-02-01 05:19:09

一个合适的定义是:

g (Impure x) = case x of

案例中没有分支。这不是错字。在此情况下,分支机构与Zero a中的构造函数一样多。这是一个完整的模式匹配。

(您必须打开emptricage GHC的扩展名才能接受此AS-I。)

One suitable definition is:

g (Impure x) = case x of

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 in Zero a, as required; this is a complete pattern match.

(You must turn on the EmptyCase extension for GHC to accept this as-is.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文