2. GHC介绍¶
本文是格拉斯哥大学 Haskell 编译器(GHC)的使用指南,对应的 Haskell 语言 版本是 Haskell 2010 ,GHC 是一个交互式的 批编译系统。 This is a guide to using the Glasgow Haskell Compiler (GHC): an interactive and batch compilation system for the Haskell 2010 language.
GHC 由两个主要部分组成:一个是交互式的解译器(GHCi,参见 使用 GHCi ) 另一个是批编译器(参见 Using GHC )。事实上,GHC 是一个单一的的 程序,只是以两种不同的选项运行,以提供交互功能或批编译功能。 GHC has two main components: an interactive Haskell interpreter (also known as GHCi), described in 使用 GHCi, and a batch compiler, described throughout Using GHC. In fact, GHC consists of a single program which is just run with different options to provide either the interactive or the batch system.
批编译器可以与 GHCi 一起使用:编译过的模块能够被加载到交互环境中并能够像 解释的代码那样被使用,事实上当你使用 GHCi 时,大部分的库都将被预编译,这 意味着你可以同时享受预编译的代码带来的执行速度上的提升及开发过程中快速编 译所写代码而带来的便利性。 The batch compiler can be used alongside GHCi: compiled modules can be loaded into an interactive session and used in the same way as interpreted code, and in fact when using GHCi most of the library code will be pre-compiled. This means you get the best of both worlds: fast pre-compiled library code, and fast compile turnaround for the parts of your program being actively developed.
GHC 支持多种扩展特性,包括:并发、外部函数接口、异常、类型系统扩展(如: 多参数类型类、todo) GHC supports numerous language extensions, including concurrency, a foreign function interface, exceptions, type system extensions such as multi-parameter type classes, local universal and existential quantification, functional dependencies, scoped type variables and explicit unboxed types. These are all described in GHC Language Features.
GHC 提供了全面的优化措施可供选择,所以当你真的使用了这些优化措施时,GHC 能够生产出执行速度相当快的代码。GHC 默认选择以最快的速度编译代码,而不是 对生成的代码做太多的优化。(todo) GHC has a comprehensive optimiser, so when you want to Really Go For It (and you’ve got time to spare) GHC can produce pretty fast code. Alternatively, the default option is to compile as fast as possible while not making too much effort to optimise the generated code (although GHC probably isn’t what you’d describe as a fast compiler :-).
GHC 的性能分析系统提供“成本中心堆栈”的方式:以调用图的形式显示 Haskell 程序的运行情况。参见 Profiling 。 GHC’s profiling system supports “cost centre stacks”: a way of seeing the profile of a Haskell program in a call-graph like structure. See Profiling for more details.
GHC 同时提供了许多库,它们将在以后的章节中分别进行介绍。 GHC comes with a number of libraries. These are described in separate documentation.
2.1. 获得GHC¶
Go to the GHC home page and follow the “download” link to download GHC for your platform.
Alternatively, if you want to build GHC yourself, head on over to the GHC Building Guide to find out how to get the sources, and build it on your system. Note that GHC itself is written in Haskell, so you will still need to install GHC in order to build it.
2.2. Meta-information: Web sites, mailing lists, etc.¶
On the World-Wide Web, there are several URLs of likely interest:
- GHC home page
- GHC Developers Home (developer documentation, wiki, and bug tracker)
We run the following mailing lists about GHC. We encourage you to join, as you feel is appropriate.
glasgow-haskell-usersThis list is for GHC users to chat among themselves. If you have a specific question about GHC, please check the FAQ first.
Subscribers can post to the list by sending their message to glasgow-haskell-users@haskell.org. Further information can be found on the Mailman page.
ghc-devsThe GHC developers hang out here. If you are working with the GHC API or have a question about GHC’s implementation, feel free to chime in.
Subscribers can post to the list by sending their message to ghc-devs@haskell.org. Further information can be found on the Mailman page.
There are several other Haskell and GHC-related mailing lists served by
www.haskell.org. Go to http://www.haskell.org/mailman/listinfo/
for the full list.
2.3. Reporting bugs in GHC¶
Glasgow Haskell is a changing system so there are sure to be bugs in it. If you find one, please see this wiki page for information on how to report it.
2.4. GHC version numbering policy¶
As of GHC version 6.8, we have adopted the following policy for numbering GHC versions:
Stable branches are numbered
x.y, where ⟨y⟩ is even. Releases on the stable branchx.yare numberedx.y.z, where ⟨z⟩ (>= 1) is the patchlevel number. Patchlevels are bug-fix releases only, and never change the programmer interface to any system-supplied code. However, if you install a new patchlevel over an old one you will need to recompile any code that was compiled against the old libraries.The value of
__GLASGOW_HASKELL__(see Options affecting the C pre-processor) for a major releasex.y.zis the integer ⟨xyy⟩ (if ⟨y⟩ is a single digit, then a leading zero is added, so for example in version 6.8.2 of GHC we would have__GLASGOW_HASKELL__==608).We may make snapshot releases of the current stable branch available for download, and the latest sources are available from the git repositories.
Stable snapshot releases are named
x.y.z.YYYYMMDD. whereYYYYMMDDis the date of the sources from which the snapshot was built, andx.y.z+1is the next release to be made on that branch. For example,6.8.1.20040225would be a snapshot of the6.8branch during the development of6.8.2.The value of
__GLASGOW_HASKELL__for a snapshot release is the integer ⟨xyy⟩. You should never write any conditional code which tests for this value, however: since interfaces change on a day-to-day basis, and we don’t have finer granularity in the values of__GLASGOW_HASKELL__, you should only conditionally compile using predicates which test whether__GLASGOW_HASKELL__is equal to, later than, or earlier than a given major release.We may make snapshot releases of the HEAD available for download, and the latest sources are available from the git repositories.
Unstable snapshot releases are named
x.y.YYYYMMDD. whereYYYYMMDDis the date of the sources from which the snapshot was built. For example,6.7.20040225would be a snapshot of the HEAD before the creation of the6.8branch.The value of
__GLASGOW_HASKELL__for a snapshot release is the integer ⟨xyy⟩. You should never write any conditional code which tests for this value, however: since interfaces change on a day-to-day basis, and we don’t have finer granularity in the values of__GLASGOW_HASKELL__, you should only conditionally compile using predicates which test whether__GLASGOW_HASKELL__is equal to, later than, or earlier than a given major release.
The version number of your copy of GHC can be found by invoking ghc
with the --version flag (see Verbosity options).
The compiler version can be tested within compiled code with the
MIN_VERSION_GLASGOW_HASKELL CPP macro (defined only when
-XCPP is used). See Standard CPP macros for details.