X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=90333b42c457a0777ffed1a265d3e90ed15c1c9c;hb=73c987fea0bf61b543dcb176eea1b2cc785f9d9d;hp=e7858cea81eddefcca1fb933eeaec217f3702ee0;hpb=5e05865dffed03c40b5d15831d26f903d5d73ede;p=ghc-hetmet.git diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index e7858ce..90333b4 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -42,23 +42,16 @@ documentation describes all the libraries that come with GHC. permitted. Leaving out all of them gives you standard Haskell 98. - Generally speaking, all the language options are introduced by "" or ""; - e.g. . Before anything else is done, the string following - "" is normalised by removing hyphens and converting - to lower case. So , , and - are all equivalent. + Generally speaking, all the language options are introduced by "", + e.g. . All the language options can be turned off by using the prefix ""; - e.g. "". + e.g. "". Language options recognised by Cabal can also be enabled using the LANGUAGE pragma, thus {-# LANGUAGE TemplateHaskell #-} (see >). - All the language options can be introduced with "" as well as "", - but this is a deprecated feature for backward compatibility. Use the "" - or LANGUAGE-pragma form. - Turning on an option that enables special syntax might cause working Haskell 98 code to fail to compile, perhaps because it uses a variable name which has @@ -115,18 +108,18 @@ documentation describes all the libraries that come with GHC. |), {|. Implies these specific language options: - , - , - , - , - . + , + , + , + , + . - and : - + : + This option enables the language extension defined in the @@ -138,7 +131,7 @@ documentation describes all the libraries that come with GHC. - ,: + ,: These two flags control how generalisation is done. @@ -149,8 +142,8 @@ documentation describes all the libraries that come with GHC. - : - + : + Use GHCi's extended default rules in a regular module (). @@ -161,16 +154,16 @@ documentation describes all the libraries that come with GHC. - - + + - - + + - - + + @@ -195,8 +188,8 @@ documentation describes all the libraries that come with GHC. - - + + See . Independent of @@ -214,8 +207,8 @@ documentation describes all the libraries that come with GHC. - - + + See . Independent of @@ -224,13 +217,13 @@ documentation describes all the libraries that come with GHC. - + - -XnoImplicitPrelude + -XNoImplicitPrelude option GHC normally imports Prelude.hi files for you. If you'd rather it didn't, then give it a - option. The idea is + option. The idea is that you can then import a Prelude of your own. (But don't call it Prelude; the Haskell module namespace is flat, and you must not conflict with any @@ -245,14 +238,14 @@ documentation describes all the libraries that come with GHC. translation for list comprehensions continues to use Prelude.map etc. - However, does + However, does change the handling of certain built-in syntax: see . - + Enables implicit parameters (see ). Currently also implied by @@ -265,7 +258,7 @@ documentation describes all the libraries that come with GHC. - + Enables overloaded string literals (see ). @@ -273,7 +266,7 @@ documentation describes all the libraries that come with GHC. - + Enables lexically-scoped type variables (see ). Implied by @@ -282,7 +275,7 @@ documentation describes all the libraries that come with GHC. - , + Enables Template Haskell (see ). This flag must @@ -724,9 +717,11 @@ qualifier list has just one element, a boolean expression. The recursive do-notation (also known as mdo-notation) is implemented as described in -"A recursive do for Haskell", -Levent Erkok, John Launchbury", +A recursive do for Haskell, +by Levent Erkok, John Launchbury, Haskell Workshop 2002, pages: 29-37. Pittsburgh, Pennsylvania. +This paper is essential reading for anyone making non-trivial use of mdo-notation, +and we do not repeat it here. The do-notation of Haskell does not allow recursive bindings, @@ -757,11 +752,18 @@ class Monad m => MonadFix m where The function mfix -dictates how the required recursion operation should be performed. If recursive bindings are required for a monad, -then that monad must be declared an instance of the MonadFix class. -For details, see the above mentioned reference. +dictates how the required recursion operation should be performed. For example, +justOnes desugars as follows: + +justOnes = mfix (\xs' -> do { xs <- Just (1:xs'); return xs } + +For full details of the way in which mdo is typechecked and desugared, see +the paper A recursive do for Haskell. +In particular, GHC implements the segmentation technique described in Section 3.2 of the paper. +If recursive bindings are required for a monad, +then that monad must be declared an instance of the MonadFix class. The following instances of MonadFix are automatically provided: List, Maybe, IO. Furthermore, the Control.Monad.ST and Control.Monad.ST.Lazy modules provide the instances of the MonadFix class for Haskell's internal state monad (strict and lazy, respectively). @@ -859,7 +861,7 @@ This name is not supported by GHC. hierarchy. It completely defeats that purpose if the literal "1" means "Prelude.fromInteger 1", which is what the Haskell Report specifies. - So the flag causes + So the flag causes the following pieces of built-in syntax to refer to whatever is in scope, not the Prelude versions: @@ -962,6 +964,48 @@ definitions; you must define such a function in prefix form. + +Record field disambiguation + +In record construction and record pattern matching +it is entirely unambiguous which field is referred to, even if there are two different +data types in scope with a common field name. For example: + +module M where + data S = MkS { x :: Int, y :: Bool } + +module Foo where + import M + + data T = MkT { x :: Int } + + ok1 (MkS { x = n }) = n+1 -- Unambiguous + + ok2 n = MkT { x = n+1 } -- Unambiguous + + bad1 k = k { x = 3 } -- Ambiguous + bad2 k = x k -- Ambiguous + +Even though there are two x's in scope, +it is clear that the x in the pattern in the +definition of ok1 can only mean the field +x from type S. Similarly for +the function ok2. However, in the record update +in bad1 and the record selection in bad2 +it is not clear which of the two types is intended. + + +Haskell 98 regards all four as ambiguous, but with the + flag, GHC will accept +the former two. The rules are precisely the same as those for instance +declarations in Haskell 98, where the method names on the left-hand side +of the method bindings in an instance declaration refer unambiguously +to the method of that class (provided they are in scope at all), even +if there are other variables in scope with the same name. +This reduces the clutter of qualified names when you import two +records from different modules that use the same field name. + + @@ -1805,7 +1849,7 @@ may use different notation to that implemented in GHC. The rest of this section outlines the extensions to GHC that support GADTs. The extension is enabled with -. +. A GADT can only be declared using GADT-style syntax (); @@ -2708,8 +2752,8 @@ makes instance inference go into a loop, because it requires the constraint Nevertheless, GHC allows you to experiment with more liberal rules. If you use -the experimental flag --X=AllowUndecidableInstances, +the experimental flag +-XUndecidableInstances, both the Paterson Conditions and the Coverage Condition (described in ) are lifted. Termination is ensured by having a fixed-depth recursion stack. If you exceed the stack depth you get a @@ -2726,11 +2770,11 @@ with N. In general, GHC requires that that it be unambiguous which instance declaration should be used to resolve a type-class constraint. This behaviour -can be modified by two flags: --X=AllowOverlappingInstances +can be modified by two flags: +-XOverlappingInstances -and --X=AllowIncoherentInstances +and +-XIncoherentInstances , as this section discusses. Both these flags are dynamic flags, and can be set on a per-module basis, using an OPTIONS_GHC pragma if desired (). @@ -2758,7 +2802,7 @@ particular constraint matches more than one. -The flag instructs GHC to allow +The flag instructs GHC to allow more than one instance to match, provided there is a most specific one. For example, the constraint C Int [Int] matches instances (A), (C) and (D), but the last is more specific, and hence is chosen. If there is no @@ -2775,30 +2819,45 @@ Suppose that from the RHS of f we get the constraint GHC does not commit to instance (C), because in a particular call of f, b might be instantiate to Int, in which case instance (D) would be more specific still. -So GHC rejects the program. If you add the flag , +So GHC rejects the program. +(If you add the flag , GHC will instead pick (C), without complaining about -the problem of subsequent instantiations. +the problem of subsequent instantiations.) + + +Notice that we gave a type signature to f, so GHC had to +check that f has the specified type. +Suppose instead we do not give a type signature, asking GHC to infer +it instead. In this case, GHC will refrain from +simplifying the constraint C Int [Int] (for the same reason +as before) but, rather than rejecting the program, it will infer the type + + f :: C Int b => [b] -> [b] + +That postpones the question of which instance to pick to the +call site for f +by which time more is known about the type b. The willingness to be overlapped or incoherent is a property of the instance declaration itself, controlled by the -presence or otherwise of the -and flags when that mdodule is +presence or otherwise of the +and flags when that mdodule is being defined. Neither flag is required in a module that imports and uses the instance declaration. Specifically, during the lookup process: An instance declaration is ignored during the lookup process if (a) a more specific match is found, and (b) the instance declaration was compiled with -. The flag setting for the +. The flag setting for the more-specific instance does not matter. -Suppose an instance declaration does not matche the constraint being looked up, but +Suppose an instance declaration does not match the constraint being looked up, but does unify with it, so that it might match when the constraint is further instantiated. Usually GHC will regard this as a reason for not committing to some other constraint. But if the instance declaration was compiled with -, GHC will skip the "does-it-unify?" +, GHC will skip the "does-it-unify?" check for that declaration. @@ -2807,18 +2866,18 @@ overlapping instances without the library client having to know. If an instance declaration is compiled without -, +, then that instance can never be overlapped. This could perhaps be inconvenient. Perhaps the rule should instead say that the overlapping instance declaration should be compiled in this way, rather than the overlapped one. Perhaps overlap at a usage site should be permitted regardless of how the instance declarations -are compiled, if the flag is +are compiled, if the flag is used at the usage site. (Mind you, the exact usage site can occasionally be hard to pin down.) We are interested to receive feedback on these points. -The flag implies the - flag, but not vice versa. +The flag implies the + flag, but not vice versa. @@ -3001,7 +3060,7 @@ Boston, Jan 2000. due to Jeff Lewis.) Implicit parameter support is enabled with the option -. +. A variable is called dynamically bound when it is bound by the calling @@ -4039,7 +4098,7 @@ and all others are monomorphic until the group is generalised Following a suggestion of Mark Jones, in his paper Typing Haskell in Haskell, -GHC implements a more general scheme. If is +GHC implements a more general scheme. If is specified: the dependency analysis ignores references to variables that have an explicit type signature. @@ -4068,7 +4127,7 @@ Now, the defintion for f is typechecked, with this type for The same refined dependency analysis also allows the type signatures of mutually-recursive functions to have different contexts, something that is illegal in Haskell 98 (Section 4.5.2, last sentence). With - + GHC only insists that the type signatures of a refined group have identical type signatures; in practice this means that only variables bound by the same pattern binding must have the same context. For example, this is fine: @@ -4089,7 +4148,7 @@ pattern binding must have the same context. For example, this is fine: GHC supports overloaded string literals. Normally a string literal has type String, but with overloaded string -literals enabled (with -X=OverloadedStrings) +literals enabled (with -XOverloadedStrings) a string literal has type (IsString a) => a. @@ -4115,7 +4174,7 @@ it explicitly (for exmaple, to give an instance declaration for it), you can imp from module GHC.Exts. -Haskell's defaulting mechanism is extended to cover string literals, when is specified. +Haskell's defaulting mechanism is extended to cover string literals, when is specified. Specifically: @@ -4178,7 +4237,7 @@ wiki page on type families. The material will be moved to this user's guide when it has stabilised. -Type families are enabled by the flag . +Type families are enabled by the flag . @@ -4227,10 +4286,10 @@ Tim Sheard is going to expand it.) Template Haskell has the following new syntactic constructions. You need to use the flag - or - + + to switch these syntactic extensions on - ( is no longer implied by + ( is no longer implied by ). @@ -4262,7 +4321,7 @@ Tim Sheard is going to expand it.) the quotation has type Expr. [d| ... |], where the "..." is a list of top-level declarations; the quotation has type Q [Dec]. - [Planned, but not implemented yet.] [t| ... |], where the "..." is a type; + [t| ... |], where the "..." is a type; the quotation has type Type. @@ -4376,7 +4435,7 @@ pr s = gen (parse s) Now run the compiler (here we are a Cygwin prompt on Windows): -$ ghc --make -X=TemplateHaskell main.hs -o main.exe +$ ghc --make -XTemplateHaskell main.hs -o main.exe Run "main.exe" and here is your output: @@ -4465,7 +4524,7 @@ Palgrave, 2003. and the arrows web page at http://www.haskell.org/arrows/. -With the flag, GHC supports the arrow +With the flag, GHC supports the arrow notation described in the second of these papers. What follows is a brief introduction to the notation; it won't make much sense unless you've read Hughes's paper. @@ -4935,7 +4994,7 @@ prime feature description contains more discussion and examples than the material below. -Bang patterns are enabled by the flag . +Bang patterns are enabled by the flag . @@ -6483,7 +6542,7 @@ where clause and over-ride whichever methods you please. Use the flags (to enable the extra syntax), - (to generate extra per-data-type code), + (to generate extra per-data-type code), and (to make the Generics library available. @@ -6692,21 +6751,21 @@ carried out at let and where bindings. Switching off the dreaded Monomorphism Restriction - + Haskell's monomorphism restriction (see Section 4.5.5 of the Haskell Report) can be completely switched off by -. +. Monomorphic pattern bindings - - + + As an experimental change, we are exploring the possibility of making pattern bindings monomorphic; that is, not generalised at all. @@ -6722,7 +6781,7 @@ can be completely switched off by [x] = e -- A pattern binding Experimentally, GHC now makes pattern bindings monomorphic by -default. Use to recover the +default. Use to recover the standard behaviour.