Chapter 7. GHC Language Features

Table of Contents

7.1. Language options
7.2. Unboxed types and primitive operations
7.2.1. Unboxed types
7.2.2. Unboxed Tuples
7.3. Syntactic extensions
7.3.1. Hierarchical Modules
7.3.2. Pattern guards
7.3.3. The recursive do-notation
7.3.4. Parallel List Comprehensions
7.3.5. Rebindable syntax
7.3.6. Postfix operators
7.4. Type system extensions
7.4.1. Data types and type synonyms
7.4.1.1. Data types with no constructors
7.4.1.2. Infix type constructors, classes, and type variables
7.4.1.3. Liberalised type synonyms
7.4.1.4. Existentially quantified data constructors
7.4.2. Class declarations
7.4.2.1. Multi-parameter type classes
7.4.2.2. The superclasses of a class declaration
7.4.2.3. Class method types
7.4.3. Functional dependencies
7.4.3.1. Rules for functional dependencies
7.4.3.2. Background on functional dependencies
7.4.4. Instance declarations
7.4.4.1. Relaxed rules for instance declarations
7.4.4.2. Undecidable instances
7.4.4.3. Overlapping instances
7.4.4.4. Type synonyms in the instance head
7.4.5. Type signatures
7.4.5.1. The context of a type signature
7.4.5.2. For-all hoisting
7.4.6. Implicit parameters
7.4.6.1. Implicit-parameter type constraints
7.4.6.2. Implicit-parameter bindings
7.4.6.3. Implicit parameters and polymorphic recursion
7.4.6.4. Implicit parameters and monomorphism
7.4.7. Explicitly-kinded quantification
7.4.8. Arbitrary-rank polymorphism
7.4.8.1. Examples
7.4.8.2. Type inference
7.4.8.3. Implicit quantification
7.4.9. Impredicative polymorphism
7.4.10. Lexically scoped type variables
7.4.10.1. Overview
7.4.10.2. Declaration type signatures
7.4.10.3. Expression type signatures
7.4.10.4. Pattern type signatures
7.4.10.5. Class and instance declarations
7.4.11. Deriving clause for classes Typeable and Data
7.4.12. Generalised derived instances for newtypes
7.4.12.1. Generalising the deriving clause
7.4.12.2. A more precise specification
7.4.13. Generalised typing of mutually recursive bindings
7.5. Generalised Algebraic Data Types (GADTs)
7.6. Template Haskell
7.6.1. Syntax
7.6.2. Using Template Haskell
7.6.3. A Template Haskell Worked Example
7.6.4. Using Template Haskell with Profiling
7.7. Arrow notation
7.7.1. do-notation for commands
7.7.2. Conditional commands
7.7.3. Defining your own control structures
7.7.4. Primitive constructs
7.7.5. Differences with the paper
7.7.6. Portability
7.8. Bang patterns
7.8.1. Informal description of bang patterns
7.8.2. Syntax and semantics
7.9. Assertions
7.10. Pragmas
7.10.1. DEPRECATED pragma
7.10.2. INCLUDE pragma
7.10.3. INLINE and NOINLINE pragmas
7.10.3.1. INLINE pragma
7.10.3.2. NOINLINE pragma
7.10.3.3. Phase control
7.10.4. LANGUAGE pragma
7.10.5. LINE pragma
7.10.6. OPTIONS_GHC pragma
7.10.7. RULES pragma
7.10.8. SPECIALIZE pragma
7.10.9. SPECIALIZE instance pragma
7.10.10. UNPACK pragma
7.11. Rewrite rules
7.11.1. Syntax
7.11.2. Semantics
7.11.3. List fusion
7.11.4. Specialisation
7.11.5. Controlling what's going on
7.11.6. CORE pragma
7.12. Special built-in functions
7.12.1. The seq function
7.12.2. The inline function
7.12.3. The lazy function
7.12.4. The unsafeCoerce# function
7.13. Generic classes
7.13.1. Using generics
7.13.2. Changes wrt the paper
7.13.3. Terminology and restrictions
7.13.4. Another example
7.14. Control over monomorphism
7.14.1. Switching off the dreaded Monomorphism Restriction
7.14.2. Monomorphic pattern bindings
7.15. Concurrent and Parallel Haskell
7.15.1. Concurrent Haskell
7.15.2. Software Transactional Memory
7.15.3. Parallel Haskell
7.15.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.

7.1. Language options

These flags control what variation of the language are permitted. Leaving out all of them gives you standard Haskell 98.

NB. 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 7, GHC Language Features, except where otherwise noted.

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

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

-ffi and -fffi:

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

New reserved words: foreign.

-fno-monomorphism-restriction,-fno-mono-pat-binds:

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

-fextended-default-rules:

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

-fallow-overlapping-instances , -fallow-undecidable-instances , -fallow-incoherent-instances , -fcontext-stack=N

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

-finline-phase

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

-farrows

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

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

Other syntax stolen: (|, |).

-fgenerics

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

-fno-implicit-prelude

GHC normally imports Prelude.hi files for you. If you'd rather it didn't, then give it a -fno-implicit-prelude 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, -fno-implicit-prelude does change the handling of certain built-in syntax: see Section 7.3.5, “Rebindable syntax”.

-fimplicit-params

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

Syntax stolen: ?varid, %varid.

-fscoped-type-variables

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

-fth

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

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