不确定如何使用组合器设计有用的库

发布于 2024-11-29 14:46:12 字数 640 浏览 1 评论 0原文

我一直在阅读有关组合器的内容,并看到它们有多么有用(例如,在 Haskell 的秒差距中)。我的问题是我不太确定如何实际使用它们。

以下是问题的概述:可以生成、过滤和修改分布。可以组合分发来创建新的分发。

基本接口是(用伪 Haskell 类型术语):

generator::      parameters -> distribution

selector::       parameters -> (distribution -> distribution)

modifier::       parameters -> (distribution -> distribution)

现在,我认为我看到了三个组合器:

combine::     generator -> generator -> generator

filter::      generator -> selector -> generator

modify::      generator -> modifier -> generator

这些实际上是组合器吗?组合器有意义/我还缺少其他任何明显的组合器吗?

感谢您的任何建议。

I've been reading about combinators and seen how useful they are (for example, in Haskell's Parsec). My problem is that I'm not quite sure how to use them practically.

Here's an outline of the problem: distributions can be generated, filtered, and modified. Distributions may be combined to create new distributions.

The basic interfaces are (in pseudo-Haskell type terminology):

generator::      parameters -> distribution

selector::       parameters -> (distribution -> distribution)

modifier::       parameters -> (distribution -> distribution)

Now, I think that I see three combinators:

combine::     generator -> generator -> generator

filter::      generator -> selector -> generator

modify::      generator -> modifier -> generator

Are these actually combinators? Do the combinators make sense/are there any other obvious combinators that I'm missing?

Thanks for any advice.

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

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

发布评论

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

评论(1

柠檬心 2024-12-06 14:46:12

selectormodifier 函数已经是完美的组合器了!除了 generatorcombine 之外,你还可以做类似的事情(为了具体起见,我将假设统计分布,然后将事情编造出来!)

modifier (Scale 3.0) $ generator StandardGaussian `combine` selector (LargerThan 10) . modifier (Shift 7) $ generator (Binomial 30 0.2)

:位与组合运算符的优先级,以便顺利工作:)

一般来说,当我尝试为 A 类型的值设计组合器库时,我喜欢保留我的 A 的“最后”,所以部分应用的组合器(您的选择器修饰符)可以与.链接在一起,而不必翻转篮球。

这是一篇不错的博客文章,可以帮助您设计组合器,它影响了我很多想法: 语义编辑器组合器

编辑:鉴于 combine 的类型签名,我可能误读了您的问题。也许我错过了一些东西,但是分布不是你的组合器应该处理的更自然的对象吗?

The selector and modifier functions are already perfectly good combinators! Along with generator and combine you can do stuff like (I'm going to assume statistical distributions for concreteness and just make things up!):

modifier (Scale 3.0) $ generator StandardGaussian `combine` selector (LargerThan 10) . modifier (Shift 7) $ generator (Binomial 30 0.2)

You may have to mess around a bit with the priority of the combine operator for this to work smoothly :)

In general, when I'm trying to design a combinator library for values of type A, I like to keep my A's "at the end", so that the partially applied combinators (your selector and modifier) can be chained together with . instead of having to flip through hoops.

Here's a nice blog article which can help you design combinators, it influenced a lot of my thinking: Semantic Editor Combinators.

EDIT: I may have misread your question, given the type signature of combine. Maybe I'm missing something, but wouldn't the distributions be the more natural objects your combinator should work on?

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