在Haskell中如何派生工作? [英] How does deriving work in Haskell?

查看:133
本文介绍了在Haskell中如何派生工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell中的代数数据类型(ADT)可以自动成为某些类型的实例(例如 Show / code>, Eq )通过从它们派生

  data也许a = Nothing |只是
派生(Eq,Ord)

我的问题是,这是如何派生工作,即Haskell如何知道如何实现派生类型类型的派生ADT函数?

另外,为什么派生仅限于某些类型类?为什么我不能编写自己可以派生出来的类型类?

解决方案

简短的回答是,magic :-)。这就是说,自动派生被烘焙到Haskell规范中,并且每个编译器都可以选择以自己的方式实现它。然而,如何使其可扩展性还有很多工作要做。



Derive 是Haskell的工具,可让您编写自己的派生机制。



GHC用于提供可导出的类型扩展名,称为泛型类,但它很少使用,因为它有点软弱。
现在已经被取消,并且正在进行工作来整合本文中描述的新的通用推导机制: http://www.dreixel.net/research/pdf/gdmh.pdf



更多相关信息,请参阅:


Algebraic Data Types (ADTs) in Haskell can automatically become instances of some typeclasses (like Show, Eq) by deriving from them.

data  Maybe a  =  Nothing | Just a
  deriving (Eq, Ord)

My question is, how does this deriving work, i.e. how does Haskell know how to implement the functions of the derived typeclass for the deriving ADT?

Also, why is deriving restricted to certain typeclasses only? Why can't I write my own typeclass which can be derived?

解决方案

The short answer is, magic :-). This is to say that automatic deriving is baked into the Haskell spec, and every compiler can choose to implement it in its own way. There's lots of work on how to make it extensible however.

Derive is a tool for Haskell to let you write your own deriving mechanisms.

GHC used to provide a derivable type class extension called Generic Classes, but it was rarely used, as it was somewhat weak. That has now been taken out, and work is ongoing to integrate a new generic deriving mechanism as described in this paper: http://www.dreixel.net/research/pdf/gdmh.pdf

For more on this, see:

这篇关于在Haskell中如何派生工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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