liveBook · Manning

Language extension

As explained in Learn Haskell by Example

A language extension is an optional compiler-supported addition that expands a programming language's syntax, semantics, or available features.

A language extension is an optional compiler-supported addition that changes Haskell's default syntax or behavior. Extensions can apply globally through compiler flags or locally through a module pragma. section 6.2.2

Language extensions support practical programming tasks such as:

  • Enabling ScopedTypeVariables so a type variable introduced by forall can be reused consistently across a function declaration. section 6.2.2
  • Using NamedFieldPuns to access selected record fields without assigning each field a new name. section 7.3
  • Using RecordWildCards to make record fields available by name, avoiding a long list of field bindings. section 7.3

Enabling ScopedTypeVariables

Place the pragma at the beginning of a module:\n\n{-# LANGUAGE ScopedTypeVariables #-}\n\nmodule Graph (...) where\n\nWith the extension enabled, an explicit forall introduces lexically scoped type variables. For example, f :: forall a. a -> a allows the annotation (x :: a) inside the function's where binding to refer to the same a.

Listing 7.7 Functions to compute the number of rows and columns in a CSV file
numberOfRows :: Csv -> Int
numberOfRows Csv {..} =  #1
  case csvColumns of
    [] -> 0  #2
    (x : _) -> length x  #3

numberOfColumns :: Csv -> Int
numberOfColumns Csv {..} = length csvColumns  #4

A complete CSV example shows RecordWildCards in functions that compute row and column counts. section 7.3

How Learn Haskell by Example covers it

How deep it goes
Explains global and per-file enabling with worked examples, while warning that forall meanings vary across extensions.
Examples
Uses ScopedTypeVariables in a graph module and record-pattern extensions in CSV row and column functions.
What it assumes
Assumes familiarity with Haskell declarations, type variables, forall, and record syntax rather than teaching them from zero.
What you can do after
Apply named Haskell extensions through LANGUAGE pragmas to control type-variable scoping and shorten record access code.
How it connects
Connects language extensions to type variables, forall, lexical scoping, record syntax, NamedFieldPuns, and RecordWildCards.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.