简体   繁体   English

Haskell 的 Data.Typeable 是什么?

[英]What is Haskell's Data.Typeable?

I've come across references to Haskell's Data.Typeable , but it's not clear to me why I would want to use it in my code.我遇到过对 Haskell 的Data.Typeable的引用,但我不清楚为什么要在我的代码中使用它。

What problem does it solve, and how?它解决了什么问题,如何解决?

Data.Typeable is an encoding of an well known approach (see eg Harper) to implementing delayed (dynamic) type checking in a statically typed language -- using a universal type. Data.Typeable是一种众所周知的方法(参见 Harper)的编码,用于在静态类型语言中实现延迟(动态)类型检查——使用通用类型。

Such a type wraps code for which type checking would not succeed until a later phase.这种类型包装了类型检查直到稍后阶段才会成功的代码。 Rather than reject the program as ill-typed, the compiler passes it on for runtime checking.编译器不会将程序作为错误类型拒绝,而是将其传递给运行时检查。

The style originated in Abadi et al., and developed for Haskell by Cheney and Hinze as a wrapper to represent all dynamic types, with the Typeable class appearing as part of the SYB work of SPJ and Lammel.这种风格起源于 Abadi 等人,由 Cheney 和 Hinze 为 Haskell 开发,作为表示所有动态类型的包装器, Typeable class 出现在 SPJ 和 Lammel 的 SYB 工作中。


Reference参考


Even in the text books: dynamic types (with typeable representations) are statically typed languages with only one type, Harper ch 20:即使在教科书中:动态类型(具有可类型表示)是只有一种类型的静态类型语言,Harper ch 20:

20.4 Untyped Means Uni-Typed 20.4 无类型意味着单一类型

The untyped λ-calculus may be faithfully embedded in a typed language with recursive types.无类型的 λ 演算可以忠实地嵌入到具有递归类型的类型语言中。 This means that every untyped λ-term has a representation as a typed expression in such a way that execution of the representation of a λ-term corresponds to execution of the term itself.这意味着每个无类型的 λ-term 都有一个作为类型化表达式的表示,这样一个 λ-term 的表示的执行对应于该术语本身的执行。 This embedding is not a matter of writing an interpreter for the λ-calculus in ℒ{+×⇀µ} (which we could surely do), but rather a direct representation of untyped λ-terms as typed expressions in a language with recursive types.这种嵌入不是为 ℒ{+×⇀µ} 中的 λ-演算编写解释器(我们当然可以这样做),而是将非类型化 λ-项直接表示为具有递归类型的语言中的类型化表达式.

The key observation is that the untyped λ-calculus is really the uni-typed λ-calculus!关键的观察是无类型的 λ-演算实际上是单类型的λ-演算! It is not the absence of types that gives it its power, but rather that it has only one type, namely the recursive type赋予它强大的不是类型的缺失,而是它只有一种类型,即递归类型

D = µt.t → t. D = µt.t → t。

It's a library that allows, among other things, naming types.它是一个允许命名类型的库。 If a type a is declared Typeable , then you can get its name using show $ typeOf x where x is any value of type a .如果类型a被声明为Typeable ,那么您可以使用show $ typeOf x获取它的名称,其中x是类型a的任何值。 It also features limited type-casting .它还具有有限的类型转换功能。

(This is somewhat similar to C++'s RTTI or dynamic languages' reflection.) (这有点类似于 C++ 的 RTTI 或动态语言的反射。)

One of the earliest descriptions I could find of a Data.Typeable -like library for Haskell is by John Peterson from 1992: http://www.cs.yale.edu/publications/techreports/tr1022.pdf我能找到的关于 Haskell 的类似Data.Typeable库的最早描述之一是 John Peterson 从 1992 年开始的: http://www.cs.yale.edu/publications/techreports/tr1022.Z414EE0975BA419724

The earliest "official" paper I know of introducing the actual Data.Typeable library is the first Scrap Your Boilerplate paper from 2003: http://research.microsoft.com/en-us/um/people/simonpj/Papers/hmap/index.htm我知道的最早介绍实际Data.Typeable库的“官方”论文是 2003 年的第一篇 Scrap Your Boilerplate 论文: http://research.microsoft.com/en-us/um/people/simonpj/Papers/hmap/索引.htm

I'm sure there's lots of intervening history that someone here can chime in with!我敢肯定这里有很多人可以参与其中的历史!

The Data.Typeable class is used primarily for generic programming in the Scrap Your Boilerplate (SYB) style. Data.Typeable class 主要用于Scrap Your Boilerplate (SYB) 风格的通用编程。 See also Data.Data另请参阅Data.Data

The idea is that SYB defines a collection combinators for performing operations such as printing, counting, searching, substiting, etc in a uniform manner over a variety of user-created types.这个想法是 SYB 定义了一个集合组合器,用于在各种用户创建的类型上以统一的方式执行诸如打印、计数、搜索、替换等操作。 The Typeable typeclass provides the necessary plumbing. Typeable类型类提供了必要的管道。

In modern GHC, you can just say deriving Data.Typeable when defining your own type in order to provide it with the necessary instances.在现代 GHC 中,您可以在定义自己的类型时只说deriving Data.Typeable以便为其提供必要的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM