Jump to content

Glasgow Haskell Compiler

From Wikipedia, the free encyclopedia

The Glasgow Haskell Compiler
Original author(s)Kevin Hammond
Developer(s)Simon Marlow,Simon Peyton Jones,The Glasgow Haskell Team[1]
Initial releaseDecember 1992;31 years ago(1992-12)[2]
Stable release
9.10.1Edit this on Wikidata / 10 May 2024;4 months ago(10 May 2024)[3]
Repository
Written inHaskell,C
Operating systemLinux,macOS Catalina[4]and later,Windows 2000and later,FreeBSD
Platformx86-64[4],AArch64
Available inEnglish
TypeCompiler
LicenseBSD New
Websitewww.haskell.org/ghc

TheGlasgow Haskell Compiler(GHC) is a native ormachine codecompilerfor thefunctional programminglanguageHaskell.[5]It provides across-platform softwareenvironment for writing and testing Haskell code and supports many extensions,libraries,and optimisations that streamline the process of generating and executing code. GHC is the most commonly used Haskell compiler.[6]It isfree and open-source softwarereleased under aBSD license.

History

[edit]

GHC originally begun in 1989 as a prototype, written inLazy ML(LML) by Kevin Hammond at theUniversity of Glasgow.Later that year, the prototype was completely rewritten in Haskell, except for itsparser,by Cordelia Hall, Will Partain, and Simon Peyton Jones. Its first beta release was on 1 April 1991. Later releases added astrictness analyzerand language extensions such asmonadic I/O,mutable arrays, unboxed data types, concurrent and parallel programming models (such assoftware transactional memoryanddata parallelism) and aprofiler.[2]

Peyton Jones, and Marlow, later moved toMicrosoft ResearchinCambridge,where they continued to be primarily responsible for developing GHC. GHC also contains code from more than three hundred other contributors.[1] From 2009 to about 2014, third-party contributions to GHC were funded by the Industrial Haskell Group.[7]

GHC names

[edit]

Since early releases the official website[8]has referred to GHC asThe Glasgow Haskell Compiler,whereas in the executable version command it is identified asThe Glorious Glasgow Haskell Compilation System.[9]This has been reflected in the documentation.[10]Initially, it had the internal name ofThe Glamorous Glasgow Haskell Compiler.[11]

Architecture

[edit]

GHC iswritten in Haskell,[12]but theruntime systemfor Haskell, essential to run programs, is written inCandC--.

GHC'sfront end,incorporating thelexer,parser andtypechecker,is designed to preserve as much information about the source language as possible until aftertype inferenceis complete, toward the goal of providing clear error messages to users.[2]After type checking, the Haskell code isdesugaredinto a typedintermediate languageknown as "Core" (based onSystem F,extended withletandcaseexpressions). Core has been extended to supportgeneralized algebraic datatypesin itstype system,and is now based on an extension to System F known as System FC.[13]

In the tradition of type-directed compiling, GHC's simplifier, or "middle end", where most of theoptimizationsimplemented in GHC are performed, is structured as a series ofsource-to-sourcetransformationson Core code. The analyses and transformations performed in this compiler stage include demand analysis (a generalization ofstrictness analysis), application of user-definedrewrite rules(including a set of rules included in GHC's standard libraries that performs foldr/buildfusion), unfolding (called "inlining"in more traditional compilers),let-floating,an analysis that determines which function arguments can be unboxed,constructed product result analysis,specializationofoverloadedfunctions, and a set of simpler local transformations such asconstant foldingandbeta reduction.[14]

The back end of the compiler transforms Core code into an internal representation of C--, via an intermediate language STG (short for "Spineless Tagless G-machine" ).[15]The C-- code can then take one of three routes: it is either printed as C code for compilation withGCC,converted directly into native machine code (the traditional "code generation"phase), or converted toLLVM IRfor compilation with LLVM. In all three cases, the resultant native code is finally linked against the GHC runtime system to produce an executable.

Language

[edit]

GHC complies with the language standards, bothHaskell 98[16]andHaskell 2010.[17] It also supports many optional extensions to the Haskell standard: for example, thesoftware transactional memory(STM) library, which allows forComposable Memory Transactions.

Extensions to Haskell

[edit]

Many extensions to Haskell have been proposed. These provide features not described in the language specification, or they redefine existing constructs. As such, each extension may not be supported by all Haskell implementations. There is an ongoing effort[18]to describe extensions and select those which will be included in future versions of the language specification.

The extensions[19]supported by the Glasgow Haskell Compiler include:

  • Unboxed types and operations. These represent the primitive datatypes of the underlying hardware, without the indirection of a pointer to theheapor the possibility of deferred evaluation. Numerically intensive code can be significantly faster when coded using these types.
  • The ability to specifystrict evaluationfor a value, pattern binding, or datatype field.
  • More convenient syntax for working with modules, patterns,list comprehensions,operators, records, and tuples.
  • Syntactic sugarfor computing witharrowsand recursively-definedmonadicvalues. Both of these concepts extend the monadicdo-notation provided in standard Haskell.
  • A significantly more powerful system of types and typeclasses, described below.
  • Template Haskell,a system for compile-timemetaprogramming.Expressions can be written to produce Haskell code in the form of anabstract syntax tree.These expressions are typechecked and evaluated at compile time; the generated code is then included as if it were part of the original code. Together with the ability toreflecton definitions, this provides a powerful tool for further extensions to the language.
  • Quasi-quotation, which allows the user to define new concrete syntax for expressions and patterns. Quasi-quotation is useful when a metaprogram written in Haskell manipulates code written in a language other than Haskell.
  • Generictypeclasses, which specify functions solely in terms of the algebraic structure of the types they operate on.
  • Parallel evaluation of expressions using multiple CPU cores. This doesnotrequire explicitly spawning threads. The distribution of work happens implicitly, based on annotations provided in the program.
  • Compilerpragmasfor directing optimizations such asinline expansionand specializing functions for particular types.
  • Customizable rewrite rules are rules describing how to replace one expression with an equivalent, but more efficiently evaluated expression. These are used within core data structure libraries to provide improved performance throughout application-level code.[20]
  • Record dot syntax. Providessyntactic sugarfor accessing the fields of a (potentially nested) record which is similar to the syntax of many other programming languages.[21]

Type system extensions

[edit]

An expressive static type system is one of the major defining features of Haskell. Accordingly, much of the work in extending the language has been directed towardsdata typesandtype classes.

The Glasgow Haskell Compiler supports an extended type system based on the theoretical System FC.[13]Major extensions to the type system include:

  • Arbitrary-rankandimpredicativepolymorphism.Essentially, a polymorphic function or datatype constructor may require that one of its arguments is also polymorphic.
  • Generalized algebraic data types.Each constructor of a polymorphic datatype can encode information into the resulting type. A function which pattern-matches on this type can use the per-constructor type information to perform more specific operations on data.
  • Existential types.These can be used to "bundle" some data together with operations on that data, in such a way that the operations can be used without exposing the specific type of the underlying data. Such a value is very similar to anobjectas found inobject-oriented programminglanguages.
  • Data types that do not actually contain any values. These can be useful to represent data in type-levelmetaprogramming.
  • Type families:user-defined functions from types to types. Whereas parametric polymorphism provides the same structure for every type instantiation, type families providead hocpolymorphism with implementations that can differ between instantiations. Use cases include content-aware optimizing containers and type-level metaprogramming.
  • Implicit function parameters that have dynamicscope.These are represented in types in much the same way as type class constraints.
  • Linear types(GHC 9.0)

Extensions relating totype classesinclude:

  • A type class may be parametrized on more than one type. Thus a type class can describe not only a set of types, but ann-aryrelationon types.
  • Functional dependencies,which constrain parts of that relation to be a mathematicalfunctionon types. That is, the constraint specifies that some type class parameter is completely determined once some other set of parameters is fixed. This guides the process oftype inferencein situations where otherwise there would be ambiguity.
  • Significantly relaxed rules regarding the allowable shape of type class instances. When these are enabled in full, the type class system becomes aTuring-completelanguage forlogic programmingat compile time.
  • Type families, as described above, may also be associated with a type class.
  • The automatic generation of certain type class instances is extended in several ways. New type classes forgeneric programmingand common recursion patterns are supported. Also, when a new type is declared asisomorphicto an existing type, any type class instance declared for the underlying type may be lifted to the new type "for free".

Portability

[edit]

Versions of GHC are available for several system orcomputing platform,includingWindowsand most varieties ofUnix(such asLinux,FreeBSD,OpenBSD,andmacOS).[22]GHC has also beenportedto several differentprocessor architectures.[22]

See also

[edit]

References

[edit]
  1. ^ab "The GHC Team".Haskell.org.Retrieved1 September2016.
  2. ^abc Hudak, P.; Hughes, J.;Peyton Jones, S.;Wadler, P. (June 2007)."A History of Haskell: Being Lazy With Class"(PDF).Procedures of the Third ACM SIGPLAN History of Programming Languages Conference (HOPL-III).Retrieved1 September2016.
  3. ^ "Download – The Glasgow Haskell Compiler".Haskell.org.
  4. ^ab"Deprecation of 32-bit Darwin and Windows platforms".GHC Team.
  5. ^ "The Glorious Glasgow Haskell Compilation System User's Guide".Haskell.org.Retrieved27 July2014.
  6. ^ "2017 state of Haskell survey results".taylor.fausak.me.15 November 2017.Retrieved11 December2017.
  7. ^ "Industrial Haskell Group".Haskell.org.2014.Retrieved1 September2016.
  8. ^"GHC The Glasgow Haskell Compiler".Haskell.org.Retrieved14 January2022.
  9. ^"Repository: configure.ac".gitlab.haskell.org.12 January 2022.Retrieved14 January2022.
  10. ^"The Glorious Glasgow Haskell Compilation System User's Guide, Version 7.6.3".downloads.haskell.org.Retrieved14 January2022.
  11. ^"ghc-0.29-src.tar.gz"(targzip).downloads.haskell.org.File: ghc-0.29/ghc/PATCHLEVEL.Retrieved14 January2022.
  12. ^ "GHC Commentary: The Compiler".Haskell.org.23 March 2016. Archived fromthe originalon 23 March 2016.Retrieved26 May2016.
  13. ^ab Sulzmann, M.; Chakravarty, M. M. T.;Peyton Jones, S.;Donnelly, K. (January 2007)."System F with Type Equality Coercions".Procedures of the ACM Workshop on Types in Language Design and Implementation (TLDI).
  14. ^ Peyton Jones, S.(April 1996)."Compiling Haskell by program transformation: a report from the trenches".Procedures of the European Symposium on Programming (ESOP).
  15. ^ Peyton Jones, S.(April 1992)."Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine, Version 2.5".Journal of Functional Programming.2(2): 127–202.doi:10.1017/S0956796800000319.
  16. ^ "Haskell 98 Language and Libraries: The Revised Report".Haskell.org.Retrieved28 January2007.
  17. ^ "Haskell 2010 Language Report".Haskell.org.Retrieved30 August2012.
  18. ^"Welcome to Haskell' (Haskell Prime)".Haskell.org.Archived fromthe originalon 20 February 2016.Retrieved26 May2016.
  19. ^"GHC Language Features".Haskell.org.Archived fromthe originalon 29 June 2016.Retrieved25 May2016.
  20. ^ Coutts, D.; Leshchinskiy, R.; Stewart, D. (April 2007)."Stream Fusion: From Lists to Streams to Nothing at All".Procedures of the ACM SIGPLAN International Conference on Functional Programming (ICFP).Archived fromthe originalon 23 September 2007.
  21. ^ Mitchell, Neil; Fletcher, Shayne (3 May 2020)."Record Dot Syntax".ghc-proposals.GitHub.Retrieved30 June2020.
  22. ^abPlatformsat gitlab.haskell.org
[edit]