你在 Haskell 中发现了更高级别的类型有什么用途? [英] What uses have you found for higher-rank types in Haskell?

查看:20
本文介绍了你在 Haskell 中发现了更高级别的类型有什么用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更高等级的类型看起来很有趣.这个例子来自 Haskell wikibook:

Higher rank types look like great fun. From the Haskell wikibook comes this example:

foo :: (forall a. a -> a) -> (Char,Bool)
foo f = (f 'c', f True)

现在我们可以评估 foo id 而不会导致编译器爆炸.这个例子在本书后面很快出现了我在其他几个地方看到的真实例子:ST monad 和 runST.这很酷.

Now we can evaluate foo id without the compiler exploding. This example is quickly followed in the book by the real-world example I have seen in a few other places: the ST monad and runST. That's pretty cool.

但是我还没有遇到过这样的情况,即通过编写自己的函数并使用更高级别的参数来解决问题.你?你有哪些野外 rank-2 或 rank-n 多态性的例子?

But I have yet to come across a situation where I solve a problem by writing my own function with an argument of higher-rank. Have you? What examples do you have of rank-2 or rank-n polymorphism in the wild?

推荐答案

看看 Darcs源.

Take a look at functions like withRepoLock in the Darcs source.

Darcs 支持多种存储库格式,这种支持通过类型类表示.因此,您可以编写比存储库格式通用的代码.在实际读取磁盘存储库时,您希望通过一些通用代码分派到该代码,这些代码确定存储库的格式并选择正确的类型类实例化.

Darcs has support for multiple repository formats, and that support is expressed via a typeclass. So you can write code that is generic over repository formats. When actually reading an on-disk repository you want to dispatch to that code through some common code that figures out what format the repository is in and chooses the right typeclass instantiation.

这篇关于你在 Haskell 中发现了更高级别的类型有什么用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆