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.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. Class method types
7.4.3. Type signatures
7.4.3.1. The context of a type signature
7.4.3.2. For-all hoisting
7.4.4. Instance declarations
7.4.4.1. Overlapping instances
7.4.4.2. Type synonyms in the instance head
7.4.4.3. Undecidable instances
7.4.5. Implicit parameters
7.4.5.1. Implicit-parameter type constraints
7.4.5.2. Implicit-parameter bindings
7.4.5.3. Implicit parameters and polymorphic recursion
7.4.5.4. Implicit parameters and monomorphism
7.4.6. Linear implicit parameters
7.4.6.1. Warnings
7.4.6.2. Recursive functions
7.4.7. Functional dependencies
7.4.8. Explicitly-kinded quantification
7.4.9. Arbitrary-rank polymorphism
7.4.9.1. Examples
7.4.9.2. Type inference
7.4.9.3. Implicit quantification
7.4.10. Scoped type variables
7.4.10.1. What a scoped type variable means
7.4.10.2. Scope and implicit quantification
7.4.10.3. Declaration type signatures
7.4.10.4. Where a pattern type signature can occur
7.4.10.5. Result type signatures
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.5. Generalised Algebraic Data Types
7.6. Template Haskell
7.6.1. Syntax
7.6.2. Using Template Haskell
7.6.3. A Template Haskell Worked Example
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. Assertions
7.9. Pragmas
7.9.1. DEPRECATED pragma
7.9.2. INCLUDE pragma
7.9.3. INLINE and NOINLINE pragmas
7.9.3.1. INLINE pragma
7.9.3.2. NOINLINE pragma
7.9.3.3. Phase control
7.9.4. LINE pragma
7.9.5. OPTIONS_GHC pragma
7.9.6. RULES pragma
7.9.7. SPECIALIZE pragma
7.9.8. SPECIALIZE instance pragma
7.9.9. UNPACK pragma
7.10. Rewrite rules
7.10.1. Syntax
7.10.2. Semantics
7.10.3. List fusion
7.10.4. Specialisation
7.10.5. Controlling what's going on
7.10.6. CORE pragma
7.11. Generic classes
7.11.1. Using generics
7.11.2. Changes wrt the paper
7.11.3. Terminology and restrictions
7.11.4. Another example
7.12. Concurrent and Parallel Haskell
7.12.1. Features specific to Parallel Haskell
7.12.1.1. The Parallel interface (recommended)
7.12.1.2. Underlying functions and primitives
7.12.1.3. Scheduling policy for concurrent threads
7.12.1.4. Scheduling policy for parallel threads

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 plus deprecated syntax of previous versions of the FFI for backwards compatibility.

New reserved words: foreign.

-fno-monomorphism-restriction:

Switch off the Haskell 98 monomorphism restriction. Independent of the -fglasgow-exts flag.

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

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

-finline-phase

See Section 7.10, “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.11, “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.5, “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, “Scoped type variables ”). Implied by -fglasgow-exts.

-fth

Enables Template Haskell (see Section 7.6, “Template Haskell”). Currently also implied by -fglasgow-exts.

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