Chapter 8. GHC Language Features
Prev |  | Next

## Chapter 8. GHC Language Features

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

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 8, GHC Language Features, except where otherwise noted. We are trying to move away from this portmanteau flag, and towards enabling features individually.

  New reserved words: `forall` (only in types), `mdo`.

  Other syntax stolen: `varid`{`#`}, `char``#`, `string``#`, `integer``#`, `float``#`, `float``##`, `(#`, `#)`, `|)`, `{|`.

  Implies these specific language options: `-XForeignFunctionInterface`, `-XImplicitParams`, `-XScopedTypeVariables`, `-XGADTs`, `-XTypeFamilies`.

`-XForeignFunctionInterface`:

  This option enables the language extension defined in the Haskell 98 Foreign Function Interface Addendum.

  New reserved words: `foreign`.

`-XMonomorphismRestriction`,`-XMonoPatBinds`:

  These two flags control how generalisation is done. See Section 8.16, “Control over monomorphism”.

`-XExtendedDefaultRules`:

  Use GHCi's extended default rules in a regular module (Section 3.4.5, “Type defaulting in GHCi”). Independent of the `-fglasgow-exts` flag.

`-XOverlappingInstances` , `-XUndecidableInstances` , `-XIncoherentInstances` , `-fcontext-stack=N`

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

`-finline-phase`

  See Section 8.13, “Rewrite rules ”. Only relevant if you also use `-fglasgow-exts`.

`-XArrows`

  See Section 8.9, “Arrow notation ”. Independent of `-fglasgow-exts`.

  New reserved words/symbols: `rec`, `proc`, `-<`, `>-`, `-<<`, `>>-`.

  Other syntax stolen: `(|`, `|)`.

`-XGenerics`

  See Section 8.15, “Generic classes”. Independent of `-fglasgow-exts`.

`-XNoImplicitPrelude`

  GHC normally imports `Prelude.hi` files for you. If you'd rather it didn't, then give it a `-XNoImplicitPrelude` 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, `-XNoImplicitPrelude` does change the handling of certain built-in syntax: see Section 8.3.5, “Rebindable syntax”.

`-XImplicitParams`

  Enables implicit parameters (see Section 8.7.2, “Implicit parameters”). Currently also implied by `-fglasgow-exts`.

  Syntax stolen: `?`varid``, `%`varid``.

`-XOverloadedStrings`

  Enables overloaded string literals (see Section 8.6.4, “Overloaded string literals ”).

`-XScopedTypeVariables`

  Enables lexically-scoped type variables (see Section 8.7.6, “Lexically scoped type variables ”). Implied by `-fglasgow-exts`.

`-XTemplateHaskell`

  Enables Template Haskell (see Section 8.8, “Template Haskell”). This flag must be given explicitly; it is no longer implied by `-fglasgow-exts`.

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

Prev |  | Next
7.4. Thriftier: producing a program that gobbles less heap space | Home | 8.2. Unboxed types and primitive operations