X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fglasgow_exts.sgml;h=1c42f5d1c6945dd331f8d1c1dacf2e72cfccd7fa;hb=2dfd507259664e6f28df4a9467a8de34d01d70a0;hp=30de1d27420d8a049eb8231a2d102c5935121f7b;hpb=dc801dc275fb8f81d482535b4d6317e234bb10f8;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/glasgow_exts.sgml b/ghc/docs/users_guide/glasgow_exts.sgml index 30de1d2..1c42f5d 100644 --- a/ghc/docs/users_guide/glasgow_exts.sgml +++ b/ghc/docs/users_guide/glasgow_exts.sgml @@ -77,7 +77,7 @@ documentation describes all the libraries that come with GHC. This simultaneously enables all of the extensions to Haskell 98 described in , except where otherwise + linkend="ghc-language-features"/>, except where otherwise noted. New reserved words: forall (only in @@ -128,7 +128,7 @@ documentation describes all the libraries that come with GHC. - See . Only relevant + See . Only relevant if you also use . @@ -137,7 +137,7 @@ documentation describes all the libraries that come with GHC. - See . Only relevant if + See . Only relevant if you also use . @@ -146,7 +146,7 @@ documentation describes all the libraries that come with GHC. - See . Independent of + See . Independent of . New reserved words/symbols: rec, @@ -163,7 +163,7 @@ documentation describes all the libraries that come with GHC. - See . Independent of + See . Independent of . @@ -192,7 +192,7 @@ documentation describes all the libraries that come with GHC. However, does change the handling of certain built-in syntax: see . + linkend="rebindable-syntax"/>. @@ -200,7 +200,7 @@ documentation describes all the libraries that come with GHC. Enables Template Haskell (see ). Currently also implied by + linkend="template-haskell"/>). Currently also implied by . Syntax stolen: [|, @@ -215,7 +215,7 @@ documentation describes all the libraries that come with GHC. Enables implicit parameters (see ). Currently also implied by + linkend="implicit-parameters"/>). Currently also implied by . Syntax stolen: @@ -485,7 +485,7 @@ import qualified Control.Monad.ST.Strict as ST For details on how GHC searches for source and interface files in the presence of hierarchical modules, see . + linkend="search-path"/>. GHC comes with a large collection of libraries arranged hierarchically; see the accompanying library documentation. @@ -505,7 +505,7 @@ import qualified Control.Monad.ST.Strict as ST Pattern guards (Glasgow extension) -The discussion that follows is an abbreviated version of Simon Peyton Jones's original proposal. (Note that the proposal was written before pattern guards were implemented, so refers to them as unimplemented.) +The discussion that follows is an abbreviated version of Simon Peyton Jones's original proposal. (Note that the proposal was written before pattern guards were implemented, so refers to them as unimplemented.) @@ -517,11 +517,11 @@ lookup :: FiniteMap -> Int -> Maybe Int The lookup returns Nothing if the supplied key is not in the domain of the mapping, and (Just v) otherwise, -where v is the value that the key maps to. Now consider the following definition: +where v is the value that the key maps to. Now consider the following definition: -clunky env var1 var2 | ok1 && ok2 = val1 + val2 +clunky env var1 var2 | ok1 && ok2 = val1 + val2 | otherwise = var1 + var2 where m1 = lookup env var1 @@ -547,12 +547,12 @@ expectJust Nothing = error "Unexpected Nothing" -What is clunky doing? The guard ok1 && +What is clunky doing? The guard ok1 && ok2 checks that both lookups succeed, using maybeToBool to convert the Maybe types to booleans. The (lazily evaluated) expectJust calls extract the values from the results of the lookups, and binds the -returned values to val1 and val2 +returned values to val1 and val2 respectively. If either lookup fails, then clunky takes the otherwise case and returns the sum of its arguments. @@ -615,9 +615,9 @@ with among the pattern guards. For example: -f x | [y] <- x +f x | [y] <- x , y > 3 - , Just z <- h y + , Just z <- h y = ... @@ -651,7 +651,7 @@ Here is a simple (yet contrived) example: import Control.Monad.Fix -justOnes = mdo xs <- Just (1:xs) +justOnes = mdo xs <- Just (1:xs) return xs @@ -731,7 +731,7 @@ This name is not supported by GHC. example, the following zips together two lists: - [ (x, y) | x <- xs | y <- ys ] + [ (x, y) | x <- xs | y <- ys ] The behavior of parallel list comprehensions follows that of @@ -744,8 +744,8 @@ This name is not supported by GHC. Given a parallel comprehension of the form: - [ e | p1 <- e11, p2 <- e12, ... - | q1 <- e21, q2 <- e22, ... + [ e | p1 <- e11, p2 <- e12, ... + | q1 <- e21, q2 <- e22, ... ... ] @@ -753,8 +753,8 @@ This name is not supported by GHC. This will be translated to: - [ e | ((p1,p2), (q1,q2), ...) <- zipN [(p1,p2) | p1 <- e11, p2 <- e12, ...] - [(q1,q2) | q1 <- e21, q2 <- e22, ...] + [ e | ((p1,p2), (q1,q2), ...) <- zipN [(p1,p2) | p1 <- e11, p2 <- e12, ...] + [(q1,q2) | q1 <- e21, q2 <- e22, ...] ... ] @@ -860,7 +860,7 @@ a data type with no constructors. For example: Syntactically, the declaration lacks the "= constrs" part. The type can be parameterised over types of any kind, but if the kind is not * then an explicit kind annotation must be used -(see ). +(see ). Such data types have only one value, namely bottom. Nevertheless, they can be useful when defining "phantom types". @@ -1020,7 +1020,7 @@ because GHC does not allow unboxed tuples on the left of a function arrow. The idea of using existential quantification in data type declarations was suggested by Laufer (I believe, thought doubtless someone will correct me), and implemented in Hope+. It's been in Lennart -Augustsson's hbc Haskell compiler for several years, and +Augustsson's hbc Haskell compiler for several years, and proved very useful. Here's the idea. Consider the declaration: @@ -1131,7 +1131,7 @@ adding a new existential quantification construct. Type classes -An easy extension (implemented in hbc) is to allow +An easy extension (implemented in hbc) is to allow arbitrary contexts before the constructor. For example: @@ -1329,7 +1329,7 @@ instance Eq T where (MkT a) == (MkT b) = ??? -But a and b have distinct types, and so can't be compared. +But a and b have distinct types, and so can't be compared. It's just about possible to imagine examples in which the derived instance would make sense, but it seems altogether simpler simply to prohibit such declarations. Define your own instances! @@ -1352,14 +1352,14 @@ declarations. Define your own instances! This section documents GHC's implementation of multi-parameter type -classes. There's lots of background in the paper Type -classes: exploring the design space (Simon Peyton Jones, Mark +classes: exploring the design space (Simon Peyton Jones, Mark Jones, Erik Meijer). There are the following constraints on class declarations: - + @@ -1423,7 +1423,7 @@ be acyclic. So these class declarations are OK: All of the class type variables must be reachable (in the sense -mentioned in ) +mentioned in ) from the free varibles of each method type . For example: @@ -1468,7 +1468,7 @@ class like this: - + @@ -1519,12 +1519,12 @@ Consider the type: language omits them; in Haskell 98, all the free type variables of an explicit source-language type signature are universally quantified, except for the class type variables in a class declaration. However, -in GHC, you can give the foralls if you want. See ). +in GHC, you can give the foralls if you want. See ). - + @@ -1557,7 +1557,7 @@ Note that the reachability condition is weaker than saying that a is functionally dependendent on a type variable free in type (see ). The reason for this is there +linkend="functional-dependencies"/>). The reason for this is there might be a "hidden" dependency, in a superclass perhaps. So "reachable" is a conservative approximation to "functionally dependent". For example, consider: @@ -1604,7 +1604,7 @@ territory free in case we need it later. - + @@ -1612,7 +1612,7 @@ territory free in case we need it later. For-all hoisting -It is often convenient to use generalised type synonyms (see ) at the right hand +It is often convenient to use generalised type synonyms (see ) at the right hand end of an arrow, thus: type Discard a = forall b. a -> b -> a @@ -1844,7 +1844,7 @@ instance C Int b => Foo b where ... is not OK. - + These restrictions ensure that context reduction terminates: each reduction step removes one type constructor. For example, the following would make the type checker @@ -2029,7 +2029,7 @@ For example, we define the min function by binding cmp. min :: [a] -> a - min = let ?cmp = (<=) in least + min = let ?cmp = (<=) in least @@ -2278,10 +2278,10 @@ to give the kind explicitly as (machine-checked) documentation, just as it is nice to give a type signature for a function. On some occasions, it is essential to do so. For example, in his paper "Restricted Data Types in Haskell" (Haskell Workshop 1999) John Hughes had to define the data type: - + data Set cxt a = Set [a] | Unused (cxt a -> ()) - + The only use for the Unused constructor was to force the correct kind for the type variable cxt. @@ -2290,21 +2290,21 @@ GHC now instead allows you to specify the kind of a type variable directly, wher a type variable is explicitly bound. Namely: data declarations: - + data Set (cxt :: * -> *) a = Set [a] - + type declarations: - + type T (f :: * -> *) = f Int - + class declarations: - + class (Eq a) => C (f :: * -> *) a where ... - + forall's in type signatures: - + f :: forall (cxt :: * -> *). Set cxt Int - + @@ -2318,14 +2318,14 @@ single lexeme in Haskell. As part of the same extension, you can put kind annotations in types as well. Thus: - + f :: (Int :: *) -> Int g :: forall a. a -> (a :: *) - + The syntax is - + atype ::= '(' ctype '::' kind ') - + The parentheses are required. @@ -2386,12 +2386,12 @@ In particular, a forall-type (also called a "type scheme"), including an operational type class context, is legal: On the left of a function arrow - On the right of a function arrow (see ) + On the right of a function arrow (see ) As the argument of a constructor, or type of a field, in a data type declaration. For example, any of the f1,f2,f3,g1,g2 above would be valid field type signatures. As the type of an implicit parameter - In a pattern type signature (see ) + In a pattern type signature (see ) There is one place you cannot put a forall: you cannot instantiate a type variable with a forall-type. So you cannot @@ -2557,7 +2557,7 @@ that x's type has no foralls in it. What does it mean to "provide" an explicit type for x? You can do that by giving a type signature for x directly, using a pattern type signature -(), thus: +(), thus: \ f :: (forall a. a->a) -> (f True, f 'c') @@ -2659,23 +2659,23 @@ f (xs::[a]) = ys ++ ys -The pattern (xs::[a]) includes a type signature for xs. +The pattern (xs::[a]) includes a type signature for xs. This brings the type variable a into scope; it scopes over all the patterns and right hand sides for this equation for f. -In particular, it is in scope at the type signature for y. +In particular, it is in scope at the type signature for y. Pattern type signatures are completely orthogonal to ordinary, separate type signatures. The two can be used independently or together. -At ordinary type signatures, such as that for ys, any type variables +At ordinary type signatures, such as that for ys, any type variables mentioned in the type signature that are not in scope are implicitly universally quantified. (If there are no type variables in scope, all type variables mentioned in the signature are universally -quantified, which is just as in Haskell 98.) In this case, since a -is in scope, it is not universally quantified, so the type of ys is -the same as that of xs. In Haskell 98 it is not possible to declare -a type for ys; a major benefit of scoped type variables is that +quantified, which is just as in Haskell 98.) In this case, since a +is in scope, it is not universally quantified, so the type of ys is +the same as that of xs. In Haskell 98 it is not possible to declare +a type for ys; a major benefit of scoped type variables is that it becomes possible to do so. @@ -2762,9 +2762,9 @@ The type variable(s) bound by the pattern have the same scope as the term variable(s) bound by the pattern. For example: let - f (x::a) = <...rhs of f...> + f (x::a) = <...rhs of f...> (p::b, q::b) = (1,2) - in <...body of let...> + in <...body of let...> Here, the type variable a scopes over the right hand side of f, just like x does; while the type variable b scopes over the @@ -2804,7 +2804,7 @@ into scope (except in the type signature itself!). So this is illegal: f x = x::a -It's illegal because a is not in scope in the body of f, +It's illegal because a is not in scope in the body of f, so the ordinary signature x::a is equivalent to x::forall a.a; and that is an incorrect typing. @@ -3607,7 +3607,7 @@ arr (\ x -> (x, x)) >>> returnA Note that variables not used later in the composition are projected out. -After simplification using rewrite rules (see ) +After simplification using rewrite rules (see ) defined in the Control.Arrow module, this reduces to @@ -3847,7 +3847,7 @@ though the results would be somewhat clumsy. For example, we could simulate do-notation by defining bind :: Arrow a => a e b -> a (e,b) c -> a e c -u `bind` f = returnA &&& u >>> f +u `bind` f = returnA &&& u >>> f bind_ :: Arrow a => a e b -> a e c -> a e c u `bind_` f = u `bind` (arr fst >>> f) @@ -4142,7 +4142,7 @@ key_function :: Int -> String -> (Bool, Double) See also the NOINLINE pragma (). + linkend="noinline-pragma"/>). @@ -4231,7 +4231,7 @@ key_function :: Int -> String -> (Bool, Double) The same phase-numbering control is available for RULES - (). + (). @@ -4265,7 +4265,7 @@ key_function :: Int -> String -> (Bool, Double) The OPTIONS pragma is used to specify additional options that are given to the compiler when compiling - this source file. See for + this source file. See for details. @@ -4273,7 +4273,7 @@ key_function :: Int -> String -> (Bool, Double) RULES pragma The RULES pragma lets you specify rewrite rules. It is - described in . + described in . @@ -4305,7 +4305,7 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value A SPECIALIZE has the effect of generating (a) a specialised version of the function and (b) a rewrite rule - (see ) that rewrites a call to the + (see ) that rewrites a call to the un-specialised function into a call to the specialised one. In earlier versions of GHC, it was possible to provide your own @@ -4316,7 +4316,7 @@ hammeredLookup :: Ord key => [(key, value)] -> key -> value This feature has been removed, as it is now subsumed by the - RULES pragma (see ). + RULES pragma (see ). @@ -4355,14 +4355,14 @@ of the pragma. the constructor itself, removing a level of indirection. For example: - + data T = T {-# UNPACK #-} !Float {-# UNPACK #-} !Float - + will create a constructor T containing two unboxed floats. This may not always be an optimisation: if - the T constructor is scrutinised and the + the T constructor is scrutinised and the floats passed to a non-strict function for example, they will have to be reboxed (this is done automatically by the compiler). @@ -4372,33 +4372,33 @@ data T = T {-# UNPACK #-} !Float unfoldings to the compiler so the reboxing can be removed as often as possible. For example: - + f :: T -> Float f (T f1 f2) = f1 + f2 - + - The compiler will avoid reboxing f1 - and f2 by inlining + + The compiler will avoid reboxing f1 + and f2 by inlining + on floats, but only when is on. Any single-constructor data is eligible for unpacking; for example - + data T = T {-# UNPACK #-} !(Int,Int) - + will store the two Ints directly in the - T constructor, by flattening the pair. + T constructor, by flattening the pair. Multi-level unpacking is also supported: - + data T = T {-# UNPACK #-} !S data S = S {-# UNPACK #-} !Int {-# UNPACK #-} !Int - + will store two unboxed Int#s - directly in the T constructor. The + directly in the T constructor. The unpacker can see through newtypes, too. If a field cannot be unpacked, you will not get a warning, @@ -4425,9 +4425,9 @@ data S = S {-# UNPACK #-} !Int {-# UNPACK #-} !Int The programmer can specify rewrite rules as part of the source program (in a pragma). GHC applies these rewrite rules wherever it can, provided (a) -the flag () is on, +the flag () is on, and (b) the flag -() is not specified. +() is not specified. @@ -4465,7 +4465,7 @@ no significance at all. It is only used when reporting how many times the rule -A rule may optionally have a phase-control number (see ), +A rule may optionally have a phase-control number (see ), immediately after the name of the rule. Thus: {-# RULES @@ -4623,7 +4623,7 @@ But not beta conversion (that's called higher-order matching). Matching is carried out on GHC's intermediate language, which includes type abstractions and applications. So a rule only matches if the -types match too. See below. +types match too. See below. @@ -4640,8 +4640,8 @@ For example, consider: The expression s (t xs) does not match the rule "map/map", but GHC -will substitute for s and t, giving an expression which does match. -If s or t was (a) used more than once, and (b) large or a redex, then it would +will substitute for s and t, giving an expression which does match. +If s or t was (a) used more than once, and (b) large or a redex, then it would not be substituted, and the rule would not fire. @@ -4982,7 +4982,7 @@ If you add you get a more detailed listing. - The defintion of (say) build in GHC/Base.lhs looks llike this: + The defintion of (say) build in GHC/Base.lhs looks llike this: build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a] @@ -5041,7 +5041,7 @@ g x = show x However, when external for is generated (via ), there will be Notes attached to the - expressions show and x. + expressions show and x. The core function declaration for f is: @@ -5069,8 +5069,8 @@ r) -> Here, we can see that the function show (which has been expanded out to a case expression over the Show dictionary) has a %note attached to it, as does the - expression eta (which used to be called - x). + expression eta (which used to be called + x).