Chapter 8. GHC Language Features

Table of Contents

8.1. Language options
8.2. Unboxed types and primitive operations
8.2.1. Unboxed types
8.2.2. Unboxed Tuples
8.3. Syntactic extensions
8.3.1. Hierarchical Modules
8.3.2. Pattern guards
8.3.3. The recursive do-notation
8.3.4. Parallel List Comprehensions
8.3.5. Rebindable syntax
8.3.6. Postfix operators
8.3.7. Record field disambiguation
8.4. Extensions to data types and type synonyms
8.4.1. Data types with no constructors
8.4.2. Infix type constructors, classes, and type variables
8.4.3. Liberalised type synonyms
8.4.4. Existentially quantified data constructors
8.4.4.1. Why existential?
8.4.4.2. Existentials and type classes
8.4.4.3. Record Constructors
8.4.4.4. Restrictions
8.4.5. Declaring data types with explicit constructor signatures
8.4.6. Generalised Algebraic Data Types (GADTs)
8.5. Extensions to the "deriving" mechanism
8.5.1. Inferred context for deriving clauses
8.5.2. Stand-alone deriving declarations
8.5.3. Deriving clause for classes Typeable and Data
8.5.4. Generalised derived instances for newtypes
8.5.4.1. Generalising the deriving clause
8.5.4.2. A more precise specification
8.6. Class and instances declarations
8.6.1. Class declarations
8.6.1.1. Multi-parameter type classes
8.6.1.2. The superclasses of a class declaration
8.6.1.3. Class method types
8.6.2. Functional dependencies
8.6.2.1. Rules for functional dependencies
8.6.2.2. Background on functional dependencies
8.6.3. Instance declarations
8.6.3.1. Relaxed rules for instance declarations
8.6.3.2. Undecidable instances
8.6.3.3. Overlapping instances
8.6.3.4. Type synonyms in the instance head
8.6.4. Overloaded string literals
8.7. Other type system extensions
8.7.1. Type signatures
8.7.1.1. The context of a type signature
8.7.2. Implicit parameters
8.7.2.1. Implicit-parameter type constraints
8.7.2.2. Implicit-parameter bindings
8.7.2.3. Implicit parameters and polymorphic recursion
8.7.2.4. Implicit parameters and monomorphism
8.7.3. Explicitly-kinded quantification
8.7.4. Arbitrary-rank polymorphism
8.7.4.1. Examples
8.7.4.2. Type inference
8.7.4.3. Implicit quantification
8.7.5. Impredicative polymorphism
8.7.6. Lexically scoped type variables
8.7.6.1. Overview
8.7.6.2. Declaration type signatures
8.7.6.3. Expression type signatures
8.7.6.4. Pattern type signatures
8.7.6.5. Class and instance declarations
8.7.7. Generalised typing of mutually recursive bindings
8.7.8. Type families
8.8. Template Haskell
8.8.1. Syntax
8.8.2. Using Template Haskell
8.8.3. A Template Haskell Worked Example
8.8.4. Using Template Haskell with Profiling
8.9. Arrow notation
8.9.1. do-notation for commands
8.9.2. Conditional commands
8.9.3. Defining your own control structures
8.9.4. Primitive constructs
8.9.5. Differences with the paper
8.9.6. Portability
8.10. Bang patterns
8.10.1. Informal description of bang patterns
8.10.2. Syntax and semantics
8.11. Assertions
8.12. Pragmas
8.12.1. LANGUAGE pragma
8.12.2. OPTIONS_GHC pragma
8.12.3. INCLUDE pragma
8.12.4. DEPRECATED pragma
8.12.5. INLINE and NOINLINE pragmas
8.12.5.1. INLINE pragma
8.12.5.2. NOINLINE pragma
8.12.5.3. Phase control
8.12.6. LINE pragma
8.12.7. RULES pragma
8.12.8. SPECIALIZE pragma
8.12.9. SPECIALIZE instance pragma
8.12.10. UNPACK pragma
8.13. Rewrite rules
8.13.1. Syntax
8.13.2. Semantics
8.13.3. List fusion
8.13.4. Specialisation
8.13.5. Controlling what's going on
8.13.6. CORE pragma
8.14. Special built-in functions
8.15. Generic classes
8.15.1. Using generics
8.15.2. Changes wrt the paper
8.15.3. Terminology and restrictions
8.15.4. Another example
8.16. Control over monomorphism
8.16.1. Switching off the dreaded Monomorphism Restriction
8.16.2. Monomorphic pattern bindings
8.17. Concurrent and Parallel Haskell
8.17.1. Concurrent Haskell
8.17.2. Software Transactional Memory
8.17.3. Parallel Haskell
8.17.4. Annotating pure code for parallelism

As with all known Haskell systems, GHC implements some extensions to the language. They are all enabled by options; by default GHC understands only plain Haskell 98.

Some of the Glasgow extensions serve to give you access to the underlying facilities with which we implement Haskell. Thus, you can get at the Raw Iron, if you are willing to write some non-portable code at a more primitive level. You need not be “stuck” on performance because of the implementation costs of Haskell's “high-level” features—you can always code “under” them. In an extreme case, you can write all your time-critical code in C, and then just glue it together with Haskell!

Before you get too carried away working at the lowest level (e.g., sloshing MutableByteArray#s around your program), you may wish to check if there are libraries that provide a “Haskellised veneer” over the features you want. The separate libraries documentation describes all the libraries that come with GHC.

8.1. Language options

The language option flag control what variation of the language are permitted. Leaving out all of them gives you standard Haskell 98.

Generally speaking, all the language options are introduced by "-X", e.g. -XTemplateHaskell.

All the language options can be turned off by using the prefix "No"; e.g. "-XNoTemplateHaskell".

Language options recognised by Cabal can also be enabled using the LANGUAGE pragma, thus {-# LANGUAGE TemplateHaskell #-} (see Section 8.12.1, “LANGUAGE pragma”>).

Turning on an option that enables special syntax might cause working Haskell 98 code to fail to compile, perhaps because it uses a variable name which has become a reserved word. So, together with each option below, we list the special syntax which is enabled by this option. We use notation and nonterminal names from the Haskell 98 lexical syntax (see the Haskell 98 Report). There are two classes of special syntax:

  • New reserved words and symbols: character sequences which are no longer available for use as identifiers in the program.

  • Other special syntax: sequences of characters that have a different meaning when this particular option is turned on.

We are only listing syntax changes here that might affect existing working programs (i.e. "stolen" syntax). Many of these extensions will also enable new context-free syntax, but in all cases programs written to use the new syntax would not be compilable without the option enabled.

-fglasgow-exts:

This simultaneously enables all of the extensions to Haskell 98 described in Chapter 8, GHC Language Features, except where otherwise noted. We are trying to move away from this portmanteau flag, and towards enabling features individaully.

New reserved words: forall (only in types), mdo.

Other syntax stolen: varid{#}, char#, string#, integer#, float#, float##, (#, #), |), {|.

Implies these specific language options: -XForeignFunctionInterface, -XImplicitParams, -XScopedTypeVariables, -XGADTs, -XTypeFamilies.

-XForeignFunctionInterface:

This option enables the language extension defined in the Haskell 98 Foreign Function Interface Addendum.

New reserved words: foreign.

-XMonomorphismRestriction,-XMonoPatBinds:

These two flags control how generalisation is done. See Section 8.16, “Control over monomorphism”.

-XExtendedDefaultRules:

Use GHCi's extended default rules in a regular module (Section 3.4.5, “Type defaulting in GHCi”). Independent of the -fglasgow-exts flag.

-XOverlappingInstances , -XUndecidableInstances , -XIncoherentInstances , -fcontext-stack=N

See Section 8.6.3, “Instance declarations”. Only relevant if you also use -fglasgow-exts.

-finline-phase

See Section 8.13, “Rewrite rules ”. Only relevant if you also use -fglasgow-exts.

-XArrows

See Section 8.9, “Arrow notation ”. Independent of -fglasgow-exts.

New reserved words/symbols: rec, proc, -<, >-, -<<, >>-.

Other syntax stolen: (|, |).

-XGenerics

See Section 8.15, “Generic classes”. Independent of -fglasgow-exts.

-XNoImplicitPrelude

GHC normally imports Prelude.hi files for you. If you'd rather it didn't, then give it a -XNoImplicitPrelude option. The idea is that you can then import a Prelude of your own. (But don't call it Prelude; the Haskell module namespace is flat, and you must not conflict with any Prelude module.)

Even though you have not imported the Prelude, most of the built-in syntax still refers to the built-in Haskell Prelude types and values, as specified by the Haskell Report. For example, the type [Int] still means Prelude.[] Int; tuples continue to refer to the standard Prelude tuples; the translation for list comprehensions continues to use Prelude.map etc.

However, -XNoImplicitPrelude does change the handling of certain built-in syntax: see Section 8.3.5, “Rebindable syntax”.

-XImplicitParams

Enables implicit parameters (see Section 8.7.2, “Implicit parameters”). Currently also implied by -fglasgow-exts.

Syntax stolen: ?varid, %varid.

-XOverloadedStrings

Enables overloaded string literals (see Section 8.6.4, “Overloaded string literals ”).

-XScopedTypeVariables

Enables lexically-scoped type variables (see Section 8.7.6, “Lexically scoped type variables ”). Implied by -fglasgow-exts.

-XTemplateHaskell

Enables Template Haskell (see Section 8.8, “Template Haskell”). This flag must be given explicitly; it is no longer implied by -fglasgow-exts.

Syntax stolen: [|, [e|, [p|, [d|, [t|, $(, $varid.