Changes between Version 8 and Version 9 of ScopedTypeVariables


Ignore:
Timestamp:
Jan 7, 2006 2:45:09 PM (8 years ago)
Author:
ross@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ScopedTypeVariables

    v8 v9  
    66== Brief Explanation == 
    77 
    8 Type annotations, which Haskell 98 allows in expressions, are also allowed in patterns and (currently in GHC only) in result type signatures, attached to the left side of a function definition.  New type variables in these annotations scope over the function body, permitting locally defined variables to be given signatures in situations where it would be impossible in Haskell 98, e.g. 
     8In Haskell 98, it is sometimes impossible to give a signature for a locally defined variable, e.g. `cmp` in 
    99{{{ 
    1010sortImage :: Ord b => (a -> b) -> [a] -> [a] 
     11sortImage f = sortBy cmp 
     12  where cmp x y = compare (f x) (f y) 
     13}}} 
     14The argument type of `cmp` is the type `a` in the signature of `sortImage`, but there is no way to refer to it in a type signature. 
     15Quantification of type variables over expressions is needed. 
     16 
     17GHC provides three extensions that bind type variables: 
     18 
     19 * Explicit `forall`s in type signature declarations. 
     20   The bound variables scope over the function body, e.g. 
     21   {{{ 
     22sortImage :: forall a b. Ord b => (a -> b) -> [a] -> [a] 
     23sortImage f = sortBy cmp 
     24  where cmp :: a -> a -> Ordering 
     25        cmp x y = compare (f x) (f y) 
     26}}} 
     27 
     28 * Pattern type signatures. 
     29   Free variables in the type stand for new types in the scope of the pattern, e.g. 
     30   {{{ 
    1131sortImage (f::a->b) = sortBy cmp 
    1232  where cmp :: a -> a -> Ordering 
    1333        cmp x y = compare (f x) (f y) 
    1434}}} 
     35 
     36 * Result type signatures, giving the type of both sides of the equation. 
     37   Free variables in the type stand for new types in the scope, e.g. 
     38   {{{ 
     39sortImage f :: [a] -> [a] = sortBy cmp 
     40  where cmp :: a -> a -> Ordering 
     41        cmp x y = compare (f x) (f y) 
     42}}} 
     43In the latter two cases, the variable can stand for any type, not necessarily a type variable is in these examples, i.e. the variable is existentially quantified. 
     44Hugs supports only pattern type signatures, with the type variables universally quantified. 
     45 
     46In GHC, type variables in instances heads also scope over the body of the instance. 
    1547 
    1648== References ==