Haskell 编译器如何工作?

发布于 2024-10-06 17:54:43 字数 178 浏览 0 评论 0 原文

我在哪里可以获得一些描述 Haskell 编译器实际工作原理的论文/文档/其他内容?我读了相当多的 GHC 文档,但在头痛后就停止了。因此,不需要博士学位就能理解它并且不以“你应该已经熟悉它”的风格编写的东西会更好。如果它真的很长并且需要一些时间来理解它,那也不是问题。

PS:最有趣的是关于 GHC 的事情,但一切都可以。

Where can I get some paper/doc/whatever which describes how a Haskell compiler actually works? I read quite a few of the docs of GHC, but stopped after getting a headache. So, something which doesn't require a PhD to understand it and isn't written in the You're-supposed-to-be-already-familiar-with-it style would be preferable. It's not a problem if it's really long and takes some time to understand it though.

PS: Most interesting would be something about GHC, but anything is ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空名 2024-10-13 17:54:43

从马口中就能得到答案! Simon Peyton Jones(GHC 向导)写了一本书,解释如何实现函数式编程语言。该书现已绝版,可在线免费获取:http://research.microsoft.com/en-us/um/people/simonpj/papers/pj-lester-book/

当然,自从这本书写完以来,GHC 已经继续前进,但它仍然是非常相关。

You can get an answer from the horse's mouth! Simon Peyton Jones (GHC wizard) wrote a book explaining how to implement functional programming languages. It's available for free online since it's now out of print: http://research.microsoft.com/en-us/um/people/simonpj/papers/pj-lester-book/

Of course, GHC has moved on since the book was written, but it's still very relevant.

习惯那些不曾习惯的习惯 2024-10-13 17:54:43

您是否正在寻找有关编译延迟评估的详细信息? Max Bolingbroke 提到了 Simon Peyton-Jones 的书,详细介绍 Clean 实现的书也在网上:

http ://wiki.clean.cs.ru.nl/Functional_Programming_and_Parallel_Graph_Rewriting

如果您有大学背景并且想要较小的内容,您可以尝试购买这些书(Henderson & Diller 肯定已经绝版):

Antoni Diller“编译函数语言” ISBN 0 471 92027 4

Peter Henderson “函数式编程应用和实现” ISBN 0-13-331579-7

AJT Davie “使用 Haskell 的函数式编程系统简介” ISBN 0 521 27724 8

Diller 有一个完整的编译器,用于通过组合器归约的惰性语言(用 Pascal 实现)。这是 David Turner 为 SASL 发明的实现技术。 Henderson 拥有 LISPkit(Lisp 的小型惰性变体)编译器的许多部分。 Davie 详细介绍了编译惰性语言的机制,例如,STG 的描述比 Simon Peyton-Jones 的书短得多(STG 是 Haskell 所用的抽象机 SPJ)。

如果您查看 Clean 开发人员的出版物列表,他们会提供大量有关实施 SAPL(一种简单应用语言)的信息:

https://clean.cs.ru.nl/Publications

最后,有相当多的论文记录了 Utrecht Haskell 编译器 UHC(和 EHC)的各个方面。我认为大部分信息是编译器如何组织(使用属性语法和“Shuffle”)以及类型系统(EHC中有各种级别的类型系统)如何实现,而不是后端“编译”如何作品。

Are you looking for details especially about compiling lazy-evaluation? There is Simon Peyton-Jones's book mentioned by Max Bolingbroke, also the book detailing Clean's implementation is online:

http://wiki.clean.cs.ru.nl/Functional_Programming_and_Parallel_Graph_Rewriting

If you have a university affiliation and want something smaller you could try to get these books (Henderson & Diller are certainly out of print):

Antoni Diller "Compiling Function Languages" ISBN 0 471 92027 4

Peter Henderson "Functional Programming Application and Implementation" ISBN 0-13-331579-7

AJT Davie "An Introduction to Functional Programming Systems using Haskell" ISBN 0 521 27724 8

Diller has a full compiler for a lazy language (implemented in Pascal) via combinator reduction. This was the implementation technique invented by David Turner for SASL. Henderson has many parts of a compiler for LISPkit a miniature, lazy variant of Lisp. Davie details quite a bit of the machinery for compiling a lazy language, for instance there's a description of the STG thats much shorter than Simon Peyton-Jones's book (the STG is the abstract machine SPJ used for Haskell).

The Clean developers have quite a bit of info on implementing SAPL (a Simple Applicative Language) if you look through their publications list:

https://clean.cs.ru.nl/Publications

Finally there are quite a number of papers documenting aspects of the Utrecht Haskell Compiler UHC (and EHC). I think most of the information is how the compiler is organized (with attribute grammars and "Shuffle") and how the type systems (there are various levels of type system in EHC) are implemented, rather than how the back-end 'compilation' works.

记忆消瘦 2024-10-13 17:54:43

编译器是一个很大的主题,在这里不可能完整地解释它们。但这里是通用编译器的概述。希望这能让您有所了解,从而使阅读有关 GHC 的具体内容更容易理解。

编译器通常通过前端和后端两部分进行一系列转换来工作。

第一个转换是将纯文本变成更容易遍历的内容。它本身通常分为两部分:

词法分析或标记化 - 将纯文本转换为小块(通常是运算符、标识符、文字等)。

句法分析或解析 - 将这些小块变成树结构。 (通常是AST,抽象语法树

下一阶段是语义分析。在此阶段,编译器通常会向 AST 添加信息(如类型信息)并构建符号表。前端部分就到此结束了。

下一个转换将 AST 转换为 IR,一种中间表示。如今,这通常是SSA 表单,单一静态分配

然后通过恒定传播、死代码分析、矢量化等进行优化。

最后的转换是代码生成。将 IR 转换为机器代码。这可能非常复杂。有时也称为降低。

如需了解更多信息,我推荐此维基百科页面

Compilers are a huge subject and it would be impossible to explain them in entirety here. But here's an overview for a general compiler. Hopefully this will give you some understanding that may make reading things specifically about GHC a little easier to understand.

Compilers generally work by a series of transformations in 2 parts, the front-end and back-end.

The first transformation is turning plain text into something a little easier to traverse. This itself is usually split into 2 parts:

Lexical Analysis or Tokenization - The act of transforming plain text into small chunks (typically operators, identifiers, literals etc).

Syntactic Analysis or Parsing - Turning these small chunks into a tree structure. (typically an AST, an Abstract Syntax Tree)

The next stage is semantic analysis. In this stage a compiler will usually add information to the AST (like type information) and build a symbol table. That concludes the front-end.

The next transformation transforms the AST into an IR, an Intermediate Representation. This is generally, nowadays an SSA form, a Single Static Assignment.

This is then optimized, via Constant Propagation, Dead code analysis, Vectorisation etc.

The last transformation is code generation. Transforming the IR into machine code. This can be very complicated. It is also sometimes referred to as lowering.

For more information I recommend this wikipedia page.

鹿童谣 2024-10-13 17:54:43

不幸的是,我怀疑您正在寻找的东西不存在。编译器理论和形式语言理论是计算机科学中相当复杂的主题,Haskell 绝不是一个起点。

首先,您可能应该在以下方面获得良好的基础:

C 语言更好地理解上述主题。

我怀疑任何解释 Haskell 内部结构的内容都需要比我使用 到目前为止,我还没有关于这个主题的课程,所以我没有正式的文献可以推荐,但我确信存在很多好的资源。

Unfortunately, I suspect that what you're looking for doesn't exist. Compiler theory and Formal Language theory are reasonably complex topics in Computer Science, and Haskell is by no means a starting point.

First, you should probably get a good grounding in:

I would suspect anything explaining anything about the internals of Haskell would require a substantially better understanding of the above topics than say, C would.

I've taken a single course on the subject so far, so I have no formal literature to recommend, but I'm sure there exist many good sources.

烟若柳尘 2024-10-13 17:54:43

维基百科对 GHC 的内部结构有一个很好的、可读的概述(类似于 @dan_waterworth 的解释,但特定于 Haskell 和 GHC):

http://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler#Architecture

Wikipedia has a good - readable - overview of the internals of the GHC (similar to @dan_waterworth's explanation, but specific to Haskell and the GHC):

http://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler#Architecture

心是晴朗的。 2024-10-13 17:54:43

我读过的关于该主题的最佳论文之一是:

One of the best papers on that subject that I have read is:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文