# 格拉斯哥哈斯克尔编译器

格拉斯哥哈斯克尔编译器（Glasgow Haskell Compiler，GHC）是函数式编程语言哈斯克尔（Haskell）的一个开源编译器与交互式环境，以BSD式许可证分发，支持完整的Haskell 2010语言以及大量扩展。<sup>[1](https://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf)</sup><sup> • </sup><sup>[2](https://www.haskell.org/ghc/index)</sup>它于1989年1月在[格拉斯哥大学](https://www.edgepedia.org/zh/university-of-glasgow)（University of Glasgow）启动，此后三十余年持续开发，几乎是所有实际使用Haskell的程序员所采用的编译器。<sup>[1](https://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf)</sup><sup> • </sup><sup>[3](https://discourse.haskell.org/t/state-of-haskell-2025-results/13755)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 1989年1月始于格拉斯哥大学；第一版由凯文·哈蒙德（Kevin Hammond）用LML编写，本质上是查尔姆斯LML编译器的新前端，1989年6月即可运行<sup>[1](https://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf)</sup> |
| 许可证 | 以宽松的BSD式许可证开源<sup>[1](https://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf)</sup> |
| 编译流水线 | Haskell源码被翻译为Core，经优化后转换为STG，再编译为供解释器使用的字节码，或编译为机器码<sup>[4](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)</sup> |
| 语言版本 | Haskell98、Haskell2010、GHC2021和GHC2024<sup>[5](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/control.html)</sup> |
| 当前版本 | GHC 9.14.1，2025年12月19日发布；要求cabal-install 3.16.0.0或更高版本<sup>[6](https://ghc.gitlab.haskell.org/homepage/download_ghc_9_14_1.html)</sup> |
| 长期支持 | 首个LTS版本为GHC 9.14，计划至少提供两年更新，目标为三年<sup>[7](https://ghc.gitlab.haskell.org/homepage/blog/20250702-ghc-release-schedules.html)</sup> |
| 采用情况 | 《2025年Haskell状况》调查受访者中99.40%使用GHC；63.45%通过ghcup安装<sup>[3](https://discourse.haskell.org/t/state-of-haskell-2025-results/13755)</sup> |

## 历史

GHC在Haskell语言初始设计确定后不久便启动了。凯文·哈蒙德用LML（Lazy ML）写出了第一版，本质上就是查尔姆斯LML编译器的新前端，原型于1989年6月开始工作，恰逢西蒙·佩顿·琼斯（Simon Peyton Jones）来到格拉斯哥加入当地的函数式编程小组。<sup>[1](https://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf)</sup>开发者科迪莉亚·V·霍尔（Cordelia V. Hall）、凯文·哈蒙德、威尔·帕坦（Will Partain）和西蒙·佩顿·琼斯撰写的一篇回顾文章描述了项目的创立目标：免费提供一个健壮且可移植、能生成高性能代码的编译器；提供一个模块化的基础，供其他研究者扩展；并了解真实程序的行为，以便构建更好的编译器。<sup>[4](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)</sup>

该项目最初由英国政府作为学术研究资助。在其生命周期中，通常同时只有两三名活跃开发者，却有数百名代码贡献者，而各版本后来有数十万人下载。<sup>[4](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)</sup>开发者描述了在研究用途与生产用途之间的刻意平衡：GHC既吸纳研究理念，又投入大量工程努力，使其能够可靠地用于生产软件。<sup>[4](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)</sup>

## 编译流水线

GHC的内部设计以两种中间语言为核心。前端将Haskell源码翻译为Core——一种带类型的中间表示，类似于Haskell的一个子集，采用多态lambda演算风格显式标注类型；优化器随后反复变换Core程序，同时保持其语义不变，而Core Lint环节可以在各遍变换之间对结果进行类型检查，以验证变换保持了类型正确性。<sup>[8](https://downloads.haskell.org/~ghc/7.6.3/docs/html/users_guide/an-external-representation-for-the-ghc-core-language-for-ghc-6.10.html)</sup>随着广义代数数据类型（GADTs）的出现，Core被扩展以支持类型相等约束和安全强制转换，如今基于一个称为F<sub>C</sub>的系统。<sup>[8](https://downloads.haskell.org/~ghc/7.6.3/docs/html/users_guide/an-external-representation-for-the-ghc-core-language-for-ghc-6.10.html)</sup>

优化完成后，代码生成器首先将Core转换为一种名为STG的语言，本质上是在Core上附加了代码生成器所需的额外信息。此后代码走两条路径之一：被转换为字节码供交互式解释器GHCi执行，或被传递下去最终翻译为机器码。<sup>[4](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)</sup>GHC可以直接编译为本机代码，也可以使用LLVM作为后端，还能生成C代码作为中间目标以便移植到新平台。<sup>[2](https://www.haskell.org/ghc/index)</sup>运行时系统链接进每个编译后的Haskell程序，负责Haskell对并发与并行的支持，包括软件事务内存。<sup>[4](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)</sup><sup> • </sup><sup>[2](https://www.haskell.org/ghc/index)</sup>

优化器足够激进，使GHC能够证明Haskell程序之间非平凡的相等性，尤其是通过简化器中的项重写引擎。<sup>[9](https://arxiv.org/abs/1805.08106)</sup>

## 包与工具

包是编译器所知的Haskell模块库。GHC自带若干包，更多包可从Hackage——在线Haskell包仓库——获取。ghc-pkg工具所知的包数据库栈可以通过GHC_PACKAGE_PATH环境变量或-package-db选项修改。<sup>[10](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/packages.html)</sup>

包的构建与分发由Cabal处理，即“构建应用与库的通用架构”（Common Architecture for Building Applications and Libraries），它为包作者定义了通用接口，并通过一个简单的配置文件自动完成包的配置、构建、安装与分发。<sup>[10](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/packages.html)</sup><sup> • </sup><sup>[11](https://www.haskell.org/cabal/)</sup>Cabal一词涵盖三样东西：.cabal文件规范、理解这些文件的库，以及cabal-install工具。<sup>[11](https://www.haskell.org/cabal/)</sup>另一种构建工具Stack自行管理GHC安装，并默认使用Stackage包集——定期进行兼容性测试的Hackage包精选集合——以避免依赖不兼容的问题。<sup>[12](https://docs.haskellstack.org/en/v2.11.1/)</sup>

调查数据展示了这些工具在实际中的使用情况。在《2025年Haskell状况》调查中，83.96%的受访者使用Cabal作为构建工具，39.59%使用Stack，39.48%使用Nix；在安装编译器方面，ghcup以63.45%领先，其次是Nix的41.57%和Stack的20.88%。<sup>[3](https://discourse.haskell.org/t/state-of-haskell-2025-results/13755)</sup>同一调查发现，27.08%的受访者在上一年因GHC升级导致代码无法正常工作，最常见的原因是依赖不兼容，调查讨论指出这一数字较往年有所改善。<sup>[3](https://discourse.haskell.org/t/state-of-haskell-2025-results/13755)</sup>

## 发布与支持

GHC遵循规律的发布节奏，带有编号的小版本与大版本。2025年12月19日发布的GHC 9.14.1带来了类型类特化的改进（SPECIALISE编译指示现在接受类型应用语法和表达式参数）、GHCi调试器与字节码解释器的改进、x86本机代码生成器中的SSE/AVX2支持、对LoongArch的本机代码生成器初步支持，以及[WebAssembly](https://www.edgepedia.org/zh/webassembly)后端中基于解释器的求值，使GHCi和Template Haskell能在浏览器中运行。<sup>[13](https://www.haskell.org/ghc/blog/20251219-ghc-9.14.1-released.html)</sup>由于担心性能回退，该版本中多态特化仍默认禁用，但可以通过-fpolymorphic-specialization标志启用。<sup>[13](https://www.haskell.org/ghc/blog/20251219-ghc-9.14.1-released.html)</sup>

2025年，GHC团队宣布了长期支持政策。首个LTS版本为GHC 9.14，计划至少提供两年更新，目标为三年支持窗口；LTS版本接收缺陷修复的点版本发布，但不回移新特性，相继推出的LTS版本的支持窗口计划重叠六个月。<sup>[7](https://ghc.gitlab.haskell.org/homepage/blog/20250702-ghc-release-schedules.html)</sup>到2026年9月，GHC 10.0的工作已进入alpha预发布阶段，GHC2024成为默认语言版本，提供可供解释器用来满足包依赖的字节码库，AArch64本机代码生成器支持SIMD，引导编译器的最低版本要求提升至GHC 9.10。<sup>[14](https://www.haskell.org/ghc/blog/20260918-ghc-10.0.1-alpha1-released.html)</sup>

## 平台目标

GHC运行于Windows、macOS、Linux以及多种处理器架构，二进制发行版面向x86-64、i386和AArch64发布，覆盖许多Linux发行版以及Windows和macOS。<sup>[2](https://www.haskell.org/ghc/index)</sup><sup> • </sup><sup>[6](https://ghc.gitlab.haskell.org/homepage/download_ghc_9_14_1.html)</sup>GHC是单目标编译器：每次构建只为一种架构和操作系统编译代码，因此要面向不同平台就需要单独构建的编译器。<sup>[15](https://downloads.haskell.org/ghc/9.12-latest/docs/users_guide/wasm.html)</sup>

WebAssembly后端面向wasm32-wasi，使用WASI作为系统调用层，因此其输出可在任何实现了WASI的wasm引擎中运行，包括浏览器。它仍是技术预览，未包含在官方二进制发行版中；可通过ghc-wasm-meta安装器设置或手动构建。<sup>[15](https://downloads.haskell.org/ghc/9.12-latest/docs/users_guide/wasm.html)</sup>该后端支持JavaScript外部函数接口，允许将[JavaScript](https://www.edgepedia.org/zh/javascript)值表示为Haskell堆上可由垃圾回收器管理的Haskell值，不过线性内存中的Haskell堆与宿主JavaScript堆之间的分离带来了限制，例如两个堆之间跨堆的循环引用无法回收。<sup>[15](https://downloads.haskell.org/ghc/9.12-latest/docs/users_guide/wasm.html)</sup>GHC本体中也有JavaScript后端；GHC 9.14.2修复了该后端的一个限制，该限制曾导致接受超过128个参数的函数在运行时出现ReferenceError失败。<sup>[16](https://ghc.gitlab.haskell.org/homepage/blog/20260917-ghc-9.14.2-rc2-released.html)</sup>

## 治理与资助

GHC是一个由其贡献者开发和指导的开源项目，更大的战略决策由一小群核心贡献者做出，详见ghc-hq治理项目。<sup>[2](https://www.haskell.org/ghc/index)</sup>持续的维护与发布管理由赞助方资助，包括Juspay、QBayLogic、Channable、Haskell基金会、Serokell、Well-Typed、Tweag、渣打银行、Mercury等，此外还有数百名开源贡献者。<sup>[13](https://www.haskell.org/ghc/blog/20251219-ghc-9.14.1-released.html)</sup>

## References

1. [A History of Haskell: Being Lazy with Class (HOPL-III)](https://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf)
2. [Glasgow Haskell Compiler, haskell.org/ghc](https://www.haskell.org/ghc/index)
3. [State of Haskell 2025 results, Haskell Foundation](https://discourse.haskell.org/t/state-of-haskell-2025-results/13755)
4. [The Glasgow Haskell Compiler: A Retrospective (Hall, Hammond, Partain, Peyton Jones)](https://www.researchgate.net/publication/220940047_The_Glasgow_Haskell_Compiler_A_Retrospective)
5. [Controlling editions and extensions, GHC User's Guide](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/control.html)
6. [GHC 9.14.1 download page](https://ghc.gitlab.haskell.org/homepage/download_ghc_9_14_1.html)
7. [GHC LTS Releases, GHC Developer Blog](https://ghc.gitlab.haskell.org/homepage/blog/20250702-ghc-release-schedules.html)
8. [An External Representation for the GHC Core Language, GHC User's Guide](https://downloads.haskell.org/~ghc/7.6.3/docs/html/users_guide/an-external-representation-for-the-ghc-core-language-for-ghc-6.10.html)
9. [The sufficiently smart compiler is a theorem prover, arXiv](https://arxiv.org/abs/1805.08106)
10. [Packages, GHC User's Guide](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/packages.html)
11. [The Haskell Cabal, Overview](https://www.haskell.org/cabal/)
12. [The Haskell Tool Stack documentation](https://docs.haskellstack.org/en/v2.11.1/)
13. [GHC 9.14.1 is now available, GHC Developer Blog](https://www.haskell.org/ghc/blog/20251219-ghc-9.14.1-released.html)
14. [GHC 10.0.1-alpha1 is now available, GHC Developer Blog](https://www.haskell.org/ghc/blog/20260918-ghc-10.0.1-alpha1-released.html)
15. [Using the GHC WebAssembly backend, GHC User's Guide](https://downloads.haskell.org/ghc/9.12-latest/docs/users_guide/wasm.html)
16. [GHC 9.14.2-rc2 is now available, GHC Developer Blog](https://ghc.gitlab.haskell.org/homepage/blog/20260917-ghc-9.14.2-rc2-released.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Compiler implementations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/glasgow-haskell-compiler

English original: https://www.edgepedia.org/glasgow-haskell-compiler
