什么时候需要使用 OneTuple?

发布于 2024-12-05 09:18:33 字数 221 浏览 0 评论 0原文

我刚刚在 hackage 上偶然发现了 OneTuple 包。我想知道它的目的,我确信作者创建它并不是为了好玩。那么这什么时候有用呢?它的作用很清楚,但何时使用却不清楚。

那么,有人知道您可以从中受益的任何很酷的例子吗?或者也许展示其背后的数学之美?

I just stumbled upon the package OneTuple on hackage. I want to know it's purpose, I'm certain the author didn't create it just for the fun of it. So when can this be useful? It's quite clear what it does, but not when one would use it.

So, anyone know any cool examples when you can benefit from this? Or perhaps show the mathematical beauty behind it?

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

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

发布评论

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

评论(4

一场信仰旅途 2024-12-12 09:18:34

这肯定不是一个笑话,就像 Identity monad 实现不是一个笑话一样。与普通类型 a 不同,您可以获得所有有用的实例,并且获得额外的底部,这使得单例元组更接近其他元组类型的语义。

一个用例是,就像身份单子一样,泛化。您有一个应用/一元函数,它具有多态函子类型。例如,许多有用的包(例如 enumerator)支持在用户选择的 monad 上进行操作。当您具有复杂的数据依赖性或想要利用额外的惰性时,OneTuple 的语义略有不同(与 Identity 相比),这可能非常有用,身份不会给你。

This is most certainly not a joke, just like the identity monad implementation is not a joke. Unlike the plain type a you get all the useful instances, and you get the additional bottom, which brings the singleton tuple much closer to the semantics of the other tuple types.

One use case for this is, just like the identity monad, generalization. You have an applicative/monadic function, which has a polymorphic functor type. For example, many useful packages like enumerator support operating over a user-chosen monad. The slightly different semantics of OneTuple (compared to Identity) can be very useful, when you have complicated data dependencies or want to make use of the additional laziness, which Identity doesn't give you.

如此安好 2024-12-12 09:18:34

我认为这是一个笑话,就像 不要打包。也就是说,OneTuple 确实向任何类型添加了一个新值(与 Maybe 相反,后者添加了两个:Nothing 和任一 只是未定义未定义取决于你的船如何摇摆)。因此,如果您需要它,您知道该去哪里......

I think it's something of a joke, like the don't package. That said, OneTuple does add a single new value to any type (as opposed to Maybe, which adds two: Nothing and either Just undefined or undefined depending on how your boat sways). So if it ever turns out you need that, you know where to go...

俯瞰星空 2024-12-12 09:18:33

它与通常用作 monad 转换器堆栈基础的 Identity monad 几乎相同,只不过 OneTuple 使用 data 代替newtype 它有一个额外的底部值。

它很有趣,因为从某种意义上说,它是它实现的大多数类型类中最简单的示例。不过,我认为它没有太多实际用途。

It's nearly the same as the Identity monad, which is commonly used as the base of a monad transformer stack, except that since OneTuple uses data instead of newtype it has an additional bottom value.

It's interesting because it's in a sense the most trivial example of most of the type classes it implements. I don't see much of a practical use for it, though.

嘿哥们儿 2024-12-12 09:18:33

假设您有一个(有点愚蠢的)对元组进行操作的类型类。您有一个 (a,a) 实例,一个 (a,a,a) 实例。您还需要一个单一值的实例。但是您不能只为 a 创建一个实例,因为这会与其他所有内容重叠!不过,您可以为 OneTuple a 创建一个实例。

现在,该类型类有点无用,但很容易想象一个与它几乎相似但更有用的类型类。事实上,这正是bos的mysql库中Only的使用:http://hackage.haskell.org/packages/archive/mysql-simple/0.2.0.2/doc/html/Database-MySQL-Simple-QueryResults.html

Suppose you have a (slightly silly) typeclass that operates on tuples. You have an instance for (a,a), an instance for (a,a,a). You want an instance for a single value as well. But you can't just make an instance for a because that would overlap with everything else! You can, however, make an instance for OneTuple a.

Now that typeclass is a bit useless, but its easy to imagine a typeclass almost like it but more useful. In fact, this is precisely the use of Only in bos' mysql library: http://hackage.haskell.org/packages/archive/mysql-simple/0.2.0.2/doc/html/Database-MySQL-Simple-QueryResults.html

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