你在Haskell中找到更高级的类型有什么用处? [英] What uses have you found for higher-rank types in Haskell?

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

问题描述

较高的排名类型看起来非常有趣。这个例子来自 Haskell wikibook

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

现在我们可以在没有编译器爆炸的情况下评估 foo id 。这个例子在本书中被我在其他一些地方看到的真实世界的例子迅速地追踪到:ST monad和 runST 。这很酷。



但是我还没有遇到这样一种情况,我通过编写自己的函数来解决一个问题,并且使用更高级的参数。你有吗?你有什么样的例子在野外有2级或n级多态性吗?

解决方案

看看像 withRepoLock

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)

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.

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?

解决方案

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

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天全站免登陆