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. The magic hash
8.3.2. New qualified operator syntax
8.3.3. Hierarchical Modules
8.3.4. Pattern guards
8.3.5. View patterns
8.3.6. The recursive do-notation
8.3.7. Parallel List Comprehensions
8.3.8. Generalised (SQL-Like) List Comprehensions
8.3.9. Rebindable syntax and the implicit Prelude import
8.3.10. Postfix operators
8.3.11. Record field disambiguation
8.3.12. Record puns
8.3.13. Record wildcards
8.3.14. Local Fixity Declarations
8.3.15. Package-qualified imports
8.3.16. Summary of stolen syntax
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 Why existential? Existentials and type classes Record Constructors 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 Generalising the deriving clause A more precise specification
8.6. Class and instances declarations
8.6.1. Class declarations Multi-parameter type classes The superclasses of a class declaration Class method types
8.6.2. Functional dependencies Rules for functional dependencies Background on functional dependencies
8.6.3. Instance declarations Relaxed rules for instance declarations Undecidable instances Overlapping instances Type synonyms in the instance head
8.6.4. Overloaded string literals
8.7. Type families
8.7.1. Data families Data family declarations Data instance declarations Import and export
8.7.2. Synonym families Type family declarations Type instance declarations Equality constraints
8.8. Other type system extensions
8.8.1. Type signatures The context of a type signature
8.8.2. Implicit parameters Implicit-parameter type constraints Implicit-parameter bindings Implicit parameters and polymorphic recursion Implicit parameters and monomorphism
8.8.3. Explicitly-kinded quantification
8.8.4. Arbitrary-rank polymorphism Examples Type inference Implicit quantification
8.8.5. Impredicative polymorphism
8.8.6. Lexically scoped type variables Overview Declaration type signatures Expression type signatures Pattern type signatures Class and instance declarations
8.8.7. Generalised typing of mutually recursive bindings
8.9. Template Haskell
8.9.1. Syntax
8.9.2. Using Template Haskell
8.9.3. A Template Haskell Worked Example
8.9.4. Using Template Haskell with Profiling
8.9.5. Template Haskell Quasi-quotation
8.10. Arrow notation
8.10.1. do-notation for commands
8.10.2. Conditional commands
8.10.3. Defining your own control structures
8.10.4. Primitive constructs
8.10.5. Differences with the paper
8.10.6. Portability
8.11. Bang patterns
8.11.1. Informal description of bang patterns
8.11.2. Syntax and semantics
8.12. Assertions
8.13. Pragmas
8.13.1. LANGUAGE pragma
8.13.2. OPTIONS_GHC pragma
8.13.3. INCLUDE pragma
8.13.4. WARNING and DEPRECATED pragmas
8.13.5. INLINE and NOINLINE pragmas INLINE pragma NOINLINE pragma Phase control
8.13.6. LINE pragma
8.13.7. RULES pragma
8.13.8. SPECIALIZE pragma
8.13.9. SPECIALIZE instance pragma
8.13.10. UNPACK pragma
8.13.11. SOURCE pragma
8.14. Rewrite rules
8.14.1. Syntax
8.14.2. Semantics
8.14.3. List fusion
8.14.4. Specialisation
8.14.5. Controlling what's going on
8.14.6. CORE pragma
8.15. Special built-in functions
8.16. Generic classes
8.16.1. Using generics
8.16.2. Changes wrt the paper
8.16.3. Terminology and restrictions
8.16.4. Another example
8.17. Control over monomorphism
8.17.1. Switching off the dreaded Monomorphism Restriction
8.17.2. Monomorphic pattern bindings
8.18. Concurrent and Parallel Haskell
8.18.1. Concurrent Haskell
8.18.2. Software Transactional Memory
8.18.3. Parallel Haskell
8.18.4. Annotating pure code for parallelism
8.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.

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.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.