Personal tools

Scoped type variables

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
 
m
Line 1: Line 1:
Scoped Type Variables are an extension to Haskell's type system that allow free type variables to be re-used in the scope of a function. The are also described in the [http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables GHC documentation].
+
Scoped Type Variables are an extension to Haskell's type system that allow free type variables to be re-used in the scope of a function. They are also described in the [http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables GHC documentation].
   
 
As an example, consider the following functions:
 
As an example, consider the following functions:

Revision as of 06:36, 13 August 2009

Scoped Type Variables are an extension to Haskell's type system that allow free type variables to be re-used in the scope of a function. They are also described in the GHC documentation.

As an example, consider the following functions:

{-# OPTIONS_GHC -XScopedTypeVariables #-}
 
...
 
mkpair1 :: forall a b. a -> b -> (a,b)
mkpair1 aa bb = (ida aa, bb)
    where
      ida :: a -> a -- This refers to a in the function's type signature
      ida = id
 
mkpair2 :: forall a b. a -> b -> (a,b)
mkpair2 aa bb = (ida aa, bb)
    where
      ida :: b -> b -- Illegal, because refers to b in type signature
      ida = id
 
mkpair3 :: a -> b -> (a,b)
mkpair3 aa bb = (ida aa, bb)
    where
      ida :: b -> b -- Legal, because b is now a free variable
      ida = id

Scoped type variables make it possible to specify the particular type of a function in situations where it is not otherwise possible, which can in turn help avoid problems with the Monomorphism_restriction.

This feature should be better documented in the Wiki, but this is a start.