A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/explicit_forall.html below:

6.11.1. Explicit universal quantification (forall) — Glasgow Haskell Compiler 9.12.2 User's Guide

Glasgow Haskell Compiler 6.11.1. Explicit universal quantification (forall)¶
ExplicitForAll¶
Implied by:

ScopedTypeVariables, LiberalTypeSynonyms, RankNTypes, ExistentialQuantification

Since:

6.12.1

Status:

Included in GHC2024, GHC2021

Allow use of the forall keyword in places where universal quantification is implicit.

Haskell type signatures are implicitly quantified. When the language option ExplicitForAll is used, the keyword forall allows us to say exactly what this means. For example:

means this:

The two are treated identically, except that the latter may bring type variables into scope (see Lexically scoped type variables).

This extension also enables explicit quantification of type and kind variables in Data instance declarations, Type instance declarations, Closed type families, Associated instances, and Rewrite rules.

Notes:

6.11.1.1. The forall-or-nothing rule¶

In certain forms of types, type variables obey what is known as the “forall-or-nothing” rule: if a type has an outermost, explicit, invisible forall, then all of the type variables in the type must be explicitly quantified. These two examples illustrate how the rule works:

f  :: forall a b. a -> b -> b         -- OK, `a` and `b` are explicitly bound
g  :: forall a. a -> forall b. b -> b -- OK, `a` and `b` are explicitly bound
h  :: forall a. a -> b -> b           -- Rejected, `b` is not in scope

The type signatures for f, g, and h all begin with an outermost invisible forall, so every type variable in these signatures must be explicitly bound by a forall. Both f and g obey the forall-or-nothing rule, since they explicitly quantify a and b. On the other hand, h does not explicitly quantify b, so GHC will reject its type signature for being improperly scoped.

In places where the forall-or-nothing rule takes effect, if a type does not have an outermost invisible forall, then any type variables that are not explicitly bound by a forall become implicitly quantified. For example:

i :: a -> b -> b             -- `a` and `b` are implicitly quantified
j :: a -> forall b. b -> b   -- `a` is implicitly quantified
k :: (forall a. a -> b -> b) -- `b` is implicitly quantified
type L :: forall a -> b -> b -- `b` is implicitly quantified

GHC will accept i, j, and k’s type signatures, as well as L’s kind signature. Note that:

The forall-or-nothing rule takes effect in the following places:

Notes:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4