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. The magic hash
7.3.2. New qualified operator syntax
7.3.3. Hierarchical Modules
7.3.4. Pattern guards
7.3.5. View patterns
7.3.6. The recursive do-notation
7.3.7. Parallel List Comprehensions
7.3.8. Generalised (SQL-Like) List Comprehensions
7.3.9. Rebindable syntax and the implicit Prelude import
7.3.10. Postfix operators
7.3.11. Record field disambiguation
7.3.12. Record puns
7.3.13. Record wildcards
7.3.14. Local Fixity Declarations
7.3.15. Package-qualified imports
7.3.16. Summary of stolen syntax
7.4. Extensions to data types and type synonyms
7.4.1. Data types with no constructors
7.4.2. Infix type constructors, classes, and type variables
7.4.3. Liberalised type synonyms
7.4.4. Existentially quantified data constructors
7.4.4.1. Why existential?
7.4.4.2. Existentials and type classes
7.4.4.3. Record Constructors
7.4.4.4. Restrictions
7.4.5. Declaring data types with explicit constructor signatures
7.4.6. Generalised Algebraic Data Types (GADTs)
7.5. Extensions to the "deriving" mechanism
7.5.1. Inferred context for deriving clauses
7.5.2. Stand-alone deriving declarations
7.5.3. Deriving clause for classes Typeable and Data
7.5.4. Generalised derived instances for newtypes
7.5.4.1. Generalising the deriving clause
7.5.4.2. A more precise specification
7.6. Class and instances declarations
7.6.1. Class declarations
7.6.1.1. Multi-parameter type classes
7.6.1.2. The superclasses of a class declaration
7.6.1.3. Class method types
7.6.2. Functional dependencies
7.6.2.1. Rules for functional dependencies
7.6.2.2. Background on functional dependencies
7.6.3. Instance declarations
7.6.3.1. Relaxed rules for instance declarations
7.6.3.2. Undecidable instances
7.6.3.3. Overlapping instances
7.6.3.4. Type synonyms in the instance head
7.6.4. Overloaded string literals
7.7. Type families
7.7.1. Data families
7.7.1.1. Data family declarations
7.7.1.2. Data instance declarations
7.7.1.3. Import and export
7.7.2. Synonym families
7.7.2.1. Type family declarations
7.7.2.2. Type instance declarations
7.7.2.3. Equality constraints
7.8. Other type system extensions
7.8.1. Type signatures
7.8.1.1. The context of a type signature
7.8.2. Implicit parameters
7.8.2.1. Implicit-parameter type constraints
7.8.2.2. Implicit-parameter bindings
7.8.2.3. Implicit parameters and polymorphic recursion
7.8.2.4. Implicit parameters and monomorphism
7.8.3. Explicitly-kinded quantification
7.8.4. Arbitrary-rank polymorphism
7.8.4.1. Examples
7.8.4.2. Type inference
7.8.4.3. Implicit quantification
7.8.5. Impredicative polymorphism
7.8.6. Lexically scoped type variables
7.8.6.1. Overview
7.8.6.2. Declaration type signatures
7.8.6.3. Expression type signatures
7.8.6.4. Pattern type signatures
7.8.6.5. Class and instance declarations
7.8.7. Generalised typing of mutually recursive bindings
7.9. Template Haskell
7.9.1. Syntax
7.9.2. Using Template Haskell
7.9.3. A Template Haskell Worked Example
7.9.4. Using Template Haskell with Profiling
7.9.5. Template Haskell Quasi-quotation
7.10. Arrow notation
7.10.1. do-notation for commands
7.10.2. Conditional commands
7.10.3. Defining your own control structures
7.10.4. Primitive constructs
7.10.5. Differences with the paper
7.10.6. Portability
7.11. Bang patterns
7.11.1. Informal description of bang patterns
7.11.2. Syntax and semantics
7.12. Assertions
7.13. Pragmas
7.13.1. LANGUAGE pragma
7.13.2. OPTIONS_GHC pragma
7.13.3. INCLUDE pragma
7.13.4. WARNING and DEPRECATED pragmas
7.13.5. INLINE and NOINLINE pragmas
7.13.5.1. INLINE pragma
7.13.5.2. NOINLINE pragma
7.13.5.3. Phase control
7.13.6. LINE pragma
7.13.7. RULES pragma
7.13.8. SPECIALIZE pragma
7.13.9. SPECIALIZE instance pragma
7.13.10. UNPACK pragma
7.13.11. SOURCE pragma
7.14. Rewrite rules
7.14.1. Syntax
7.14.2. Semantics
7.14.3. List fusion
7.14.4. Specialisation
7.14.5. Controlling what's going on
7.14.6. CORE pragma
7.15. Special built-in functions
7.16. Generic classes
7.16.1. Using generics
7.16.2. Changes wrt the paper
7.16.3. Terminology and restrictions
7.16.4. Another example
7.17. Control over monomorphism
7.17.1. Switching off the dreaded Monomorphism Restriction
7.17.2. Monomorphic pattern bindings
7.18. Concurrent and Parallel Haskell
7.18.1. Concurrent Haskell
7.18.2. Software Transactional Memory
7.18.3. Parallel Haskell
7.18.4. Annotating pure code for parallelism
7.18.5. Data Parallel Haskell

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

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 7.13.1, “LANGUAGE pragma”>).

The flag -fglasgow-exts is equivalent to enabling the following extensions: -XPrintExplicitForalls, -XForeignFunctionInterface, -XUnliftedFFITypes, -XGADTs, -XImplicitParams, -XScopedTypeVariables, -XUnboxedTuples, -XTypeSynonymInstances, -XStandaloneDeriving, -XDeriveDataTypeable, -XFlexibleContexts, -XFlexibleInstances, -XConstrainedClassMethods, -XMultiParamTypeClasses, -XFunctionalDependencies, -XMagicHash, -XPolymorphicComponents, -XExistentialQuantification, -XUnicodeSyntax, -XPostfixOperators, -XPatternGuards, -XLiberalTypeSynonyms, -XRankNTypes, -XImpredicativeTypes, -XTypeOperators, -XRecursiveDo, -XParallelListComp, -XEmptyDataDecls, -XKindSignatures, -XGeneralizedNewtypeDeriving, -XTypeFamilies. Enabling these options is the only effect of -fglasgow-exts. We are trying to move away from this portmanteau flag, and towards enabling features individually.