3 级(或更高)多态性的用例?
我见过一些 2 级多态性的用例(最突出的例子是 ST monad< /a>),但没有一个比这更高的等级。有谁知道这样的用例?
I've seen a few use cases for rank-2 polymorphism (the most prominent example being the ST monad), but none for a higher rank than that. Does anyone know of such a use case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也许能帮上忙,尽管这样的野兽不可避免地会受到牵连。这是我有时在开发带有绑定和 de Bruijn 索引(瓶装)的范围明确的语法时使用的模式。
通常情况下,我会使用类型类来隐藏最糟糕的部分,但如果你打开字典,你会发现这是什么。
要点是,mangle 是一个 2 级操作,它采用在其工作的变量集中配备适当多态操作的事物概念:将变量映射到事物的操作被转换为术语转换器。整个事情展示了如何使用
mangle
来生成重命名和替换。下面是该模式的一个具体实例:
我们仅以非常通用的方式实现术语遍历一次,然后通过部署 mkRenSub 模式(以两种不同的方式使用通用遍历)来进行替换。
另一个例子,考虑类型运算符之间的多态操作
IMonad(索引单子)是一些
m :: (* -> *) -> 。 *-> *
配备了多态运算符,因此这些操作的等级为 2。
现在,任何由任意索引单子参数化的操作都是等级 3。因此,例如,构建通常的单子组合,
依赖于等级 3 量化,一旦你解压
IMonad
的定义。故事的寓意:一旦您在多态/索引概念上进行高阶编程,您的有用工具包字典就会成为排名 2,而您的通用程序就会成为排名 3。当然,这是可能再次发生的升级。
I may be able to help, although such beast are inevitably a bit involved. Here's a pattern I sometimes use in developing well-scoped syntax with binding and de Bruijn indexing, bottled.
Normally, I'd use type classes to hide the worst of the gore, but if you unpack the dictionaries, this is what you'll find.
The point is that
mangle
is a rank-2 operation which takes a notion of thingy equipped with suitable operations polymorphic in the variable sets over which they work: operations which map variables to thingies get turned into term-transformers. The whole thing shows how to usemangle
to generate both renaming and substitution.Here's a concrete instance of that pattern:
We implement the term traversal just once, but in a very general way, then we get substitution by deploying the mkRenSub pattern (which uses the general traversal in two different ways).
For another example, consider polymorphic operations between type operators
An
IMonad
(indexed monad) is somem :: (* -> *) -> * -> *
equipped with polymorphic operatorsso those operations are rank 2.
Now any operation which is parametrized by an arbitrary indexed monad is rank 3. So, for example, constructing the usual monadic composition,
relies on rank 3 quantification, once you unpack the definition of
IMonad
.Moral of story: once you're doing higher order programming over polymorphic/indexed notions, your dictionaries of useful kit become rank 2, and your generic programs become rank 3. This is, of course, an escalation that can happen again.
也许是我读过的摘要的最好结局:“除了 Haskell 的正常类型类机制之外,Multiplate 仅需要 3 级多态性。” 。 (哦,只是三级多态性,没什么大不了的!)
Perhaps the best ending to an abstract I've read yet: "Multiplate only requires rank 3 polymorphism in addition to the normal type class mechanism of Haskell.". (Oh, only rank-3 polymorphism, no big deal!)