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. Negative literals
7.3.4. Fractional looking integer literals
7.3.5. Binary integer literals
7.3.6. Hierarchical Modules
7.3.7. Pattern guards
7.3.8. View patterns
7.3.9. Pattern synonyms
7.3.9.1. Syntax and scoping of pattern synonyms
7.3.9.2. Import and export of pattern synonyms
7.3.9.3. Typing of pattern synonyms
7.3.9.4. Matching of pattern synonyms
7.3.10. n+k patterns
7.3.11. Traditional record syntax
7.3.12. The recursive do-notation
7.3.12.1. Recursive binding groups
7.3.12.2. The mdo notation
7.3.13. Parallel List Comprehensions
7.3.14. Generalised (SQL-Like) List Comprehensions
7.3.15. Monad comprehensions
7.3.16. Rebindable syntax and the implicit Prelude import
7.3.17. Postfix operators
7.3.18. Tuple sections
7.3.19. Lambda-case
7.3.20. Empty case alternatives
7.3.21. Multi-way if-expressions
7.3.22. Record field disambiguation
7.3.23. Record puns
7.3.24. Record wildcards
7.3.25. Local Fixity Declarations
7.3.26. Import and export extensions
7.3.26.1. Hiding things the imported module doesn't export
7.3.26.2. Package-qualified imports
7.3.26.3. Safe imports
7.3.26.4. Explicit namespaces in import/export
7.3.27. 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. Type operators
7.4.5. Liberalised type synonyms
7.4.6. Existentially quantified data constructors
7.4.6.1. Why existential?
7.4.6.2. Existentials and type classes
7.4.6.3. Record Constructors
7.4.6.4. Restrictions
7.4.7. Declaring data types with explicit constructor signatures
7.4.8. 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 instances of extra classes (Data, etc)
7.5.4. Deriving Typeable instances
7.5.5. Generalised derived instances for newtypes
7.5.5.1. Generalising the deriving clause
7.5.5.2. A more precise specification
7.5.6. Deriving any other class
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.1.4. Default method signatures
7.6.1.5. Nullary type classes
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. Instance resolution
7.6.3.2. Relaxed rules for the instance head
7.6.3.3. Relaxed rules for instance contexts
7.6.3.4. Undecidable instances
7.6.3.5. Overlapping instances
7.6.3.6. Instance signatures: type signatures in instance declarations
7.6.4. Overloaded string literals
7.6.5. Overloaded lists
7.6.5.1. The IsList class
7.6.5.2. Rebindable syntax
7.6.5.3. Defaulting
7.6.5.4. Speculation about the future
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. Overlap of data instances
7.7.2. Synonym families
7.7.2.1. Type family declarations
7.7.2.2. Type instance declarations
7.7.2.3. Closed type families
7.7.2.4. Type family examples
7.7.2.5. Compatibility and apartness of type family equations
7.7.2.6. Decidability of type synonym instances
7.7.3. Associated data and type families
7.7.3.1. Associated instances
7.7.3.2. Associated type synonym defaults
7.7.3.3. Scoping of class parameters
7.7.3.4. Instance contexts and associated type and data instances
7.7.4. Import and export
7.7.4.1. Examples
7.7.4.2. 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.8.4. Kind inference in closed type families
7.8.5. Kind inference in class instance declarations
7.9. Datatype promotion
7.9.1. Motivation
7.9.2. Overview
7.9.3. Distinguishing between types and constructors
7.9.4. Promoted list and tuple types
7.9.5. Promoting existential data constructors
7.9.6. Promoting type operators
7.10. Type-Level Literals
7.10.1. Runtime Values for Type-Level Literals
7.10.2. Computing With Type-Level Naturals
7.11. Equality constraints
7.11.1. The Coercible constraint
7.12. The Constraint kind
7.13. Other type system extensions
7.13.1. Explicit universal quantification (forall)
7.13.2. The context of a type signature
7.13.3. Ambiguous types and the ambiguity check
7.13.4. Implicit parameters
7.13.4.1. Implicit-parameter type constraints
7.13.4.2. Implicit-parameter bindings
7.13.4.3. Implicit parameters and polymorphic recursion
7.13.4.4. Implicit parameters and monomorphism
7.13.5. Explicitly-kinded quantification
7.13.6. Arbitrary-rank polymorphism
7.13.6.1. Examples
7.13.6.2. Type inference
7.13.6.3. Implicit quantification
7.13.7. Impredicative polymorphism
7.13.8. Lexically scoped type variables
7.13.8.1. Overview
7.13.8.2. Declaration type signatures
7.13.8.3. Expression type signatures
7.13.8.4. Pattern type signatures
7.13.8.5. Class and instance declarations
7.13.9. Bindings and generalisation
7.13.9.1. Switching off the dreaded Monomorphism Restriction
7.13.9.2. Generalised typing of mutually recursive bindings
7.13.9.3. Let-generalisation
7.14. Typed Holes
7.15. Partial Type Signatures
7.15.1. Syntax
7.15.1.1. Type Wildcards
7.15.1.2. Named Wildcards
7.15.1.3. Extra-Constraints Wildcard
7.15.2. Where can they occur?
7.16. Deferring type errors to runtime
7.16.1. Enabling deferring of type errors
7.16.2. Deferred type errors in GHCi
7.17. Template Haskell
7.17.1. Syntax
7.17.2. Using Template Haskell
7.17.3. Viewing Template Haskell generated code
7.17.4. A Template Haskell Worked Example
7.17.5. Using Template Haskell with Profiling
7.17.6. Template Haskell Quasi-quotation
7.18. Arrow notation
7.18.1. do-notation for commands
7.18.2. Conditional commands
7.18.3. Defining your own control structures
7.18.4. Primitive constructs
7.18.5. Differences with the paper
7.18.6. Portability
7.19. Bang patterns
7.19.1. Informal description of bang patterns
7.19.2. Syntax and semantics
7.20. Assertions
7.21. Static pointers
7.21.1. Using static pointers
7.21.2. Static semantics of static pointers
7.22. Pragmas
7.22.1. LANGUAGE pragma
7.22.2. OPTIONS_GHC pragma
7.22.3. INCLUDE pragma
7.22.4. WARNING and DEPRECATED pragmas
7.22.5. MINIMAL pragma
7.22.6. INLINE and NOINLINE pragmas
7.22.6.1. INLINE pragma
7.22.6.2. INLINABLE pragma
7.22.6.3. NOINLINE pragma
7.22.6.4. CONLIKE modifier
7.22.6.5. Phase control
7.22.7. LINE pragma
7.22.8. RULES pragma
7.22.9. SPECIALIZE pragma
7.22.9.1. SPECIALIZE INLINE
7.22.9.2. SPECIALIZE for imported functions
7.22.9.3. Obsolete SPECIALIZE syntax
7.22.10. SPECIALIZE instance pragma
7.22.11. UNPACK pragma
7.22.12. NOUNPACK pragma
7.22.13. SOURCE pragma
7.22.14. OVERLAPPING, OVERLAPPABLE, OVERLAPS, and INCOHERENT pragmas
7.23. Rewrite rules
7.23.1. Syntax
7.23.2. Semantics
7.23.3. How rules interact with INLINE/NOINLINE pragmas
7.23.4. How rules interact with CONLIKE pragmas
7.23.5. List fusion
7.23.6. Specialisation
7.23.7. Controlling what's going on in rewrite rules
7.24. Special built-in functions
7.25. Generic classes
7.26. Generic programming
7.26.1. Deriving representations
7.26.2. Writing generic functions
7.26.3. Generic defaults
7.26.4. More information
7.27. Roles
7.27.1. Nominal, Representational, and Phantom
7.27.2. Role inference
7.27.3. Role annotations
7.28. Concurrent and Parallel Haskell
7.28.1. Concurrent Haskell
7.28.2. Software Transactional Memory
7.28.3. Parallel Haskell
7.28.4. Annotating pure code for parallelism
7.28.5. Data Parallel Haskell
7.29. Safe Haskell
7.29.1. Uses of Safe Haskell
7.29.1.1. Strict type-safety (good style)
7.29.1.2. Building secure systems (restricted IO Monads)
7.29.2. Safe Language
7.29.3. Safe Imports
7.29.4. Trust and Safe Haskell Modes
7.29.4.1. Trust check (-fpackage-trust disabled)
7.29.4.2. Trust check (-fpackage-trust enabled)
7.29.4.3. Example
7.29.4.4. Trustworthy Requirements
7.29.4.5. Package Trust
7.29.5. Safe Haskell Inference
7.29.6. Safe Haskell Flag Summary
7.29.7. Safe Compilation

As with all known Haskell systems, GHC implements some extensions to the language. They can all be enabled or disabled by command line 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.22.1, “LANGUAGE pragma”).

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