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. Unicode syntax
7.3.2. The magic hash
7.3.3. Hierarchical Modules
7.3.4. Pattern guards
7.3.5. View patterns
7.3.6. n+k patterns
7.3.7. Traditional record syntax
7.3.8. The recursive do-notation Recursive binding groups The mdo notation
7.3.9. Parallel List Comprehensions
7.3.10. Generalised (SQL-Like) List Comprehensions
7.3.11. Monad comprehensions
7.3.12. Rebindable syntax and the implicit Prelude import
7.3.13. Postfix operators
7.3.14. Tuple sections
7.3.15. Lambda-case
7.3.16. Multi-way if-expressions
7.3.17. Record field disambiguation
7.3.18. Record puns
7.3.19. Record wildcards
7.3.20. Local Fixity Declarations
7.3.21. Package-qualified imports
7.3.22. Safe imports
7.3.23. Summary of stolen syntax
7.4. Extensions to data types and type synonyms
7.4.1. Data types with no constructors
7.4.2. Data type contexts
7.4.3. Infix type constructors, classes, and type variables
7.4.4. Liberalised type synonyms
7.4.5. Existentially quantified data constructors Why existential? Existentials and type classes Record Constructors Restrictions
7.4.6. Declaring data types with explicit constructor signatures
7.4.7. 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 extra classes (Typeable, Data, etc)
7.5.4. Generalised derived instances for newtypes Generalising the deriving clause A more precise specification
7.6. Class and instances declarations
7.6.1. Class declarations Multi-parameter type classes The superclasses of a class declaration Class method types Default method signatures
7.6.2. Functional dependencies Rules for functional dependencies Background on functional dependencies
7.6.3. Instance declarations Relaxed rules for the instance head Relaxed rules for instance contexts Undecidable instances Overlapping instances Type signatures in instance declarations
7.6.4. Overloaded string literals
7.7. Type families
7.7.1. Data families Data family declarations Data instance declarations Overlap of data instances
7.7.2. Synonym families Type family declarations Type instance declarations Overlap of type synonym instances Decidability of type synonym instances
7.7.3. Associated data and type families Associated instances Associated type synonym defaults Scoping of class parameters
7.7.4. Import and export Examples Instances
7.7.5. Type families and instance declarations
7.8. Kind polymorphism
7.8.1. Overview of kind polymorphism
7.8.2. Overview
7.8.3. Polymorphic kind recursion and complete kind signatures
7.9. Datatype promotion
7.9.1. Motivation
7.9.2. Overview
7.9.3. Distinguishing between types and constructors
7.9.4. Promoted lists and tuples types
7.9.5. Promoted Literals
7.9.6. Promoting existential data constructors
7.10. Equality constraints
7.11. The Constraint kind
7.12. Other type system extensions
7.12.1. Explicit universal quantification (forall)
7.12.2. The context of a type signature
7.12.3. Implicit parameters Implicit-parameter type constraints Implicit-parameter bindings Implicit parameters and polymorphic recursion Implicit parameters and monomorphism
7.12.4. Explicitly-kinded quantification
7.12.5. Arbitrary-rank polymorphism Examples Type inference Implicit quantification
7.12.6. Impredicative polymorphism
7.12.7. Lexically scoped type variables Overview Declaration type signatures Expression type signatures Pattern type signatures Class and instance declarations
7.12.8. Generalised typing of mutually recursive bindings
7.12.9. Monomorphic local bindings
7.13. Deferring type errors to runtime
7.13.1. Enabling deferring of type errors
7.13.2. Deferred type errors in GHCi
7.14. Template Haskell
7.14.1. Syntax
7.14.2. Using Template Haskell
7.14.3. A Template Haskell Worked Example
7.14.4. Using Template Haskell with Profiling
7.14.5. Template Haskell Quasi-quotation
7.15. Arrow notation
7.15.1. do-notation for commands
7.15.2. Conditional commands
7.15.3. Defining your own control structures
7.15.4. Primitive constructs
7.15.5. Differences with the paper
7.15.6. Portability
7.16. Bang patterns
7.16.1. Informal description of bang patterns
7.16.2. Syntax and semantics
7.17. Assertions
7.18. Pragmas
7.18.1. LANGUAGE pragma
7.18.2. OPTIONS_GHC pragma
7.18.3. INCLUDE pragma
7.18.4. WARNING and DEPRECATED pragmas
7.18.5. INLINE and NOINLINE pragmas INLINE pragma INLINABLE pragma NOINLINE pragma CONLIKE modifier Phase control
7.18.6. LINE pragma
7.18.7. RULES pragma
7.18.8. SPECIALIZE pragma SPECIALIZE INLINE SPECIALIZE for imported functions Obsolete SPECIALIZE syntax
7.18.9. SPECIALIZE instance pragma
7.18.10. UNPACK pragma
7.18.11. NOUNPACK pragma
7.18.12. SOURCE pragma
7.19. Rewrite rules
7.19.1. Syntax
7.19.2. Semantics
7.19.3. How rules interact with INLINE/NOINLINE and CONLIKE pragmas
7.19.4. List fusion
7.19.5. Specialisation
7.19.6. Controlling what's going on in rewrite rules
7.19.7. CORE pragma
7.20. Special built-in functions
7.21. Generic classes
7.22. Generic programming
7.22.1. Deriving representations
7.22.2. Writing generic functions
7.22.3. Generic defaults
7.22.4. More information
7.23. Control over monomorphism
7.23.1. Switching off the dreaded Monomorphism Restriction
7.23.2. Monomorphic pattern bindings
7.24. Concurrent and Parallel Haskell
7.24.1. Concurrent Haskell
7.24.2. Software Transactional Memory
7.24.3. Parallel Haskell
7.24.4. Annotating pure code for parallelism
7.24.5. Data Parallel Haskell
7.25. Safe Haskell
7.25.1. Uses of Safe Haskell Strict type-safety (good style) Building secure systems (restricted IO Monads)
7.25.2. Safe Language
7.25.3. Safe Imports
7.25.4. Trust and Safe Haskell Modes Trust check (-fpackage-trust disabled) Trust check (-fpackage-trust enabled) Example Trustworthy Requirements Package Trust
7.25.5. Safe Haskell Inference
7.25.6. Safe Haskell Flag Summary

As with all known Haskell systems, GHC implements some extensions to the language. They can all be enabled or disabled by commandline flags or language pragmas. By default GHC understands the most recent Haskell version it supports, plus a handful of extensions.

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 flags control what variation of the language are permitted.

Language options can be controlled in two ways:

  • Every language option can switched on by a command-line flag "-X..." (e.g. -XTemplateHaskell), and switched off by the flag "-XNo..."; (e.g. -XNoTemplateHaskell).

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

The flag -fglasgow-exts is equivalent to enabling the following extensions: -XForeignFunctionInterface, -XUnliftedFFITypes, -XImplicitParams, -XScopedTypeVariables, -XUnboxedTuples, -XTypeSynonymInstances, -XStandaloneDeriving, -XDeriveDataTypeable, -XDeriveFunctor, -XDeriveFoldable, -XDeriveTraversable, -XDeriveGeneric, -XFlexibleContexts, -XFlexibleInstances, -XConstrainedClassMethods, -XMultiParamTypeClasses, -XFunctionalDependencies, -XMagicHash, -XPolymorphicComponents, -XExistentialQuantification, -XUnicodeSyntax, -XPostfixOperators, -XPatternGuards, -XLiberalTypeSynonyms, -XRankNTypes, -XTypeOperators, -XExplicitNamespaces, -XRecursiveDo, -XParallelListComp, -XEmptyDataDecls, -XKindSignatures, -XGeneralizedNewtypeDeriving. 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.