X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=fb21918e2583303d1a611f91dbb982a1b1b71342;hp=f63c90ebdb8205bc6db399961f8f975c15e4b220;hb=1e436f2bb208a6c990743afaf17b7c2a93c31742;hpb=483cdff0899f01688eff0163c5f297f5bc52e00c diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index f63c90e..fb21918 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -109,7 +109,7 @@ While you really can use this stuff to write fast code, All these primitive data types and operations are exported by the library GHC.Prim, for which there is -detailed online documentation. +detailed online documentation. (This documentation is generated from the file compiler/prelude/primops.txt.pp.) @@ -210,22 +210,20 @@ in a top-level binding. in a recursive binding. You may bind unboxed variables in a (non-recursive, -non-top-level) pattern binding, but any such variable causes the entire -pattern-match -to become strict. For example: +non-top-level) pattern binding, but you must make any such pattern-match +strict. For example, rather than: data Foo = Foo Int Int# f x = let (Foo a b, w) = ..rhs.. in ..body.. -Since b has type Int#, the entire pattern -match -is strict, and the program behaves as if you had written +you must write: data Foo = Foo Int Int# - f x = case ..rhs.. of { (Foo a b, w) -> ..body.. } + f x = let !(Foo a b, w) = ..rhs.. in ..body.. +since b has type Int#. @@ -842,6 +840,19 @@ y) will not be coalesced. + + + +n+k patterns + + + +n+k pattern support is enabled by default. To disable +it, you can use the flag. + + + + @@ -876,7 +887,7 @@ As you can guess justOnes will evaluate to Just [1,1 -The Control.Monad.Fix library introduces the MonadFix class. It's definition is: +The Control.Monad.Fix library introduces the MonadFix class. Its definition is: class Monad m => MonadFix m where @@ -1003,11 +1014,12 @@ This name is not supported by GHC. Generalised list comprehensions are a further enhancement to the - list comprehension syntatic sugar to allow operations such as sorting + list comprehension syntactic sugar to allow operations such as sorting and grouping which are familiar from SQL. They are fully described in the paper Comprehensive comprehensions: comprehensions with "order by" and "group by", except that the syntax we use differs slightly from the paper. +The extension is enabled with the flag . Here is an example: employees = [ ("Simon", "MS", 80) @@ -1043,7 +1055,7 @@ then f This statement requires that f have the type - forall a. [a] -> [a]. You can see an example of it's use in the + forall a. [a] -> [a]. You can see an example of its use in the motivating example, as this form is used to apply take 5. @@ -1270,6 +1282,44 @@ definitions; you must define such a function in prefix form. + +Tuple sections + + + The flag enables Python-style partially applied + tuple constructors. For example, the following program + + (, True) + + is considered to be an alternative notation for the more unwieldy alternative + + \x -> (x, True) + +You can omit any combination of arguments to the tuple, as in the following + + (, "I", , , "Love", , 1337) + +which translates to + + \a b c d -> (a, "I", b, c, "Love", d, 1337) + + + + + If you have unboxed tuples enabled, tuple sections + will also be available for them, like so + + (# , True #) + +Because there is no unboxed unit tuple, the following expression + + (# #) + +continues to stand for the unboxed singleton tuple data constructor. + + + + Record field disambiguation @@ -1286,7 +1336,6 @@ module Foo where 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 @@ -1311,6 +1360,37 @@ 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. + +Some details: + + +Field disambiguation can be combined with punning (see ). For exampe: + +module Foo where + import M + x=True + ok3 (MkS { x }) = x+1 -- Uses both disambiguation and punning + + + + +With you can use unqualifed +field names even if the correponding selector is only in scope qualified +For example, assuming the same module M as in our earlier example, this is legal: + +module Foo where + import qualified M -- Note qualified + + ok4 (M.MkS { x = n }) = n+1 -- Unambiguous + +Since the constructore MkS is only in scope qualified, you must +name it M.MkS, but the field x does not need +to be qualified even though M.x is in scope but x +is not. (In effect, it is qualified by the constructor.) + + + + @@ -1347,16 +1427,9 @@ a for the same name a. -Note that puns and other patterns can be mixed in the same record: - -data C = C {a :: Int, b :: Int} -f (C {a, b = 4}) = a - -and that puns can be used wherever record patterns occur (e.g. in -let bindings or at the top-level). - - - +Note that: + + Record punning can also be used in an expression, writing, for example, let a = 1 in C {a} @@ -1365,12 +1438,41 @@ instead of let a = 1 in C {a = a} - -Note that this expansion is purely syntactic, so the record pun +The expansion is purely syntactic, so the expanded right-hand side expression refers to the nearest enclosing variable that is spelled the same as the field name. + + + +Puns and other patterns can be mixed in the same record: + +data C = C {a :: Int, b :: Int} +f (C {a, b = 4}) = a + + + + +Puns can be used wherever record patterns occur (e.g. in +let bindings or at the top-level). + + + +A pun on a qualified field name is expanded by stripping off the module qualifier. +For example: + +f (C {M.a}) = a + +means + +f (M.C {M.a = a}) = a + +(This is useful if the field selector a for constructor M.C +is only in scope in qualified form.) + + + @@ -1381,6 +1483,7 @@ same as the field name. Record wildcards are enabled by the flag -XRecordWildCards. +This flag implies -XDisambiguateRecordFields. @@ -1393,7 +1496,7 @@ f (C {a = 1, b = b, c = c, d = d}) = b + c + d -Record wildcard syntax permits a (..) in a record +Record wildcard syntax permits a ".." in a record pattern, where each elided field f is replaced by the pattern f = f. For example, the above pattern can be written as @@ -1403,7 +1506,10 @@ f (C {a = 1, ..}) = b + c + d -Note that wildcards can be mixed with other patterns, including puns +More details: + + +Wildcards can be mixed with other patterns, including puns (); for example, in a pattern C {a = 1, b, ..}). Additionally, record wildcards can be used wherever record patterns occur, including in let @@ -1413,24 +1519,38 @@ C {a = 1, ..} = e defines b, c, and d. - + - + Record wildcards can also be used in expressions, writing, for example, - let {a = 1; b = 2; c = 3; d = 4} in C {..} - in place of - let {a = 1; b = 2; c = 3; d = 4} in C {a=a, b=b, c=c, d=d} - -Note that this expansion is purely syntactic, so the record wildcard +The expansion is purely syntactic, so the record wildcard expression refers to the nearest enclosing variables that are spelled the same as the omitted field names. + + + +The ".." expands to the missing +in-scope record fields, where "in scope" +includes both unqualified and qualified-only. +Any fields that are not in scope are not filled in. For example + +module M where + data R = R { a,b,c :: Int } +module X where + import qualified M( R(a,b) ) + f a b = R { .. } + +The {..} expands to {M.a=a,M.b=b}, +omitting c since it is not in scope at all. + + @@ -2353,16 +2473,46 @@ otherwise is a generalised data type ( +As with other type signatures, you can give a single signature for several data constructors. +In this example we give a single signature for T1 and T2: + + data T a where + T1,T2 :: a -> T a + T3 :: T a + + + + The type signature of each constructor is independent, and is implicitly universally quantified as usual. -Different constructors may have different universally-quantified type variables -and different type-class constraints. -For example, this is fine: +In particular, the type variable(s) in the "data T a where" header +have no scope, and different constructors may have different universally-quantified type variables: + + data T a where -- The 'a' has no scope + T1,T2 :: b -> T b -- Means forall b. b -> T b + T3 :: T a -- Means forall a. T a + + + + +A constructor signature may mention type class constraints, which can differ for +different constructors. For example, this is fine: data T a where - T1 :: Eq b => b -> T b + T1 :: Eq b => b -> b -> T b T2 :: (Show c, Ix c) => c -> [c] -> T c +When patten matching, these constraints are made available to discharge constraints +in the body of the match. For example: + + f :: T a -> String + f (T1 x y) | x==y = "yes" + | otherwise = "no" + f (T2 a b) = show a + +Note that f is not overloaded; the Eq constraint arising +from the use of == is discharged by the pattern match on T1 +and similarly the Show constraint arising from the use of show. @@ -2374,12 +2524,12 @@ have no scope. Indeed, one can write a kind signature instead: or even a mixture of the two: - data Foo a :: (* -> *) -> * where ... + data Bar a :: (* -> *) -> * where ... The type variables (if given) may be explicitly kinded, so we could also write the header for Foo like this: - data Foo a (b :: * -> *) where ... + data Bar a (b :: * -> *) where ... @@ -2410,27 +2560,48 @@ declaration. For example, these two declarations are equivalent +The type signature may have quantified type variables that do not appear +in the result type: + + data Foo where + MkFoo :: a -> (a->Bool) -> Foo + Nil :: Foo + +Here the type variable a does not appear in the result type +of either constructor. +Although it is universally quantified in the type of the constructor, such +a type variable is often called "existential". +Indeed, the above declaration declares precisely the same type as +the data Foo in . + +The type may contain a class context too, of course: + + data Showable where + MkShowable :: Show a => a -> Showable + + + + You can use record syntax on a GADT-style data type declaration: data Person where - Adult { name :: String, children :: [Person] } :: Person - Child { name :: String } :: Person + Adult :: { name :: String, children :: [Person] } -> Person + Child :: Show a => { name :: !String, funny :: a } -> Person As usual, for every constructor that has a field f, the type of field f must be the same (modulo alpha conversion). - - -At the moment, record updates are not yet possible with GADT-style declarations, -so support is limited to record construction, selection and pattern matching. -For example - - aPerson = Adult { name = "Fred", children = [] } +The Child constructor above shows that the signature +may have a context, existentially-quantified variables, and strictness annotations, +just as in the non-record case. (NB: the "type" that follows the double-colon +is not really a type, because of the record syntax and strictness annotations. +A "type" of this form can appear only in a constructor signature.) + - shortName :: Person -> Bool - hasChildren (Adult { children = kids }) = not (null kids) - hasChildren (Child {}) = False - + +Record updates are allowed with GADT-style declarations, +only fields that have the following property: the type of the field +mentions no existential type variables. @@ -2529,7 +2700,7 @@ constructor). -It's is permitted to declare an ordinary algebraic data type using GADT-style syntax. +It is permitted to declare an ordinary algebraic data type using GADT-style syntax. What makes a GADT into a GADT is not the syntax, but rather the presence of data constructors whose result type is not just T a b. @@ -2641,16 +2812,22 @@ GHC now allows stand-alone deriving declarations, enabled by The syntax is identical to that of an ordinary instance declaration apart from (a) the keyword deriving, and (b) the absence of the where part. -You must supply a context (in the example the context is (Eq a)), +Note the following points: + + +You must supply an explicit context (in the example the context is (Eq a)), exactly as you would in an ordinary instance declaration. -(In contrast the context is inferred in a deriving clause -attached to a data type declaration.) +(In contrast, in a deriving clause +attached to a data type declaration, the context is inferred.) + + A deriving instance declaration must obey the same rules concerning form and termination as ordinary instance declarations, controlled by the same flags; see . - - + + + Unlike a deriving declaration attached to a data declaration, the instance can be more specific than the data type (assuming you also use @@ -2664,8 +2841,31 @@ for example This will generate a derived instance for (Foo [a]) and (Foo (Maybe a)), but other types such as (Foo (Int,Bool)) will not be an instance of Eq. + + + +Unlike a deriving +declaration attached to a data declaration, +GHC does not restrict the form of the data type. Instead, GHC simply generates the appropriate +boilerplate code for the specified class, and typechecks it. If there is a type error, it is +your problem. (GHC will show you the offending code if it has a type error.) +The merit of this is that you can derive instances for GADTs and other exotic +data types, providing only that the boilerplate code does indeed typecheck. For example: + + data T a where + T1 :: T Int + T2 :: T Bool + + deriving instance Show (T a) + +In this example, you cannot say ... deriving( Show ) on the +data type declaration for T, +because T is a GADT, but you can generate +the instance declaration using stand-alone deriving. + + The stand-alone syntax is generalised for newtypes in exactly the same way that ordinary deriving clauses are generalised (). For example: @@ -2676,13 +2876,14 @@ For example: GHC always treats the last parameter of the instance (Foo in this example) as the type whose instance is being derived. - + + -Deriving clause for classes <literal>Typeable</literal> and <literal>Data</literal> +Deriving clause for extra classes (<literal>Typeable</literal>, <literal>Data</literal>, etc) Haskell 98 allows the programmer to add "deriving( Eq, Ord )" to a data type @@ -2692,11 +2893,11 @@ classes Eq, Ord, Enum, Ix, Bounded, Read, and Show. -GHC extends this list with two more classes that may be automatically derived -(provided the flag is specified): -Typeable, and Data. These classes are defined in the library -modules Data.Typeable and Data.Generics respectively, and the -appropriate class must be in scope before it can be mentioned in the deriving clause. +GHC extends this list with several more classes that may be automatically derived: + + With , you can derive instances of the classes +Typeable, and Data, defined in the library +modules Data.Typeable and Data.Generics respectively. An instance of Typeable can only be derived if the data type has seven or fewer type parameters, all of kind *. @@ -2712,6 +2913,26 @@ In other cases, there is nothing to stop the programmer writing a Typab class, whose kind suits that of the data type constructor, and then writing the data type instance by hand. + + + With , you can derive instances of +the class Functor, +defined in GHC.Base. + + + With , you can derive instances of +the class Foldable, +defined in Data.Foldable. + + + With , you can derive instances of +the class Traversable, +defined in Data.Traversable. + + +In each case the appropriate class must be in scope before it +can be mentioned in the deriving clause. + @@ -2936,7 +3157,8 @@ All the extensions are enabled by the flag. Multi-parameter type classes -Multi-parameter type classes are permitted. For example: +Multi-parameter type classes are permitted, with flag . +For example: @@ -2948,13 +3170,17 @@ Multi-parameter type classes are permitted. For example: - + The superclasses of a class declaration -There are no restrictions on the context in a class declaration -(which introduces superclasses), except that the class hierarchy must -be acyclic. So these class declarations are OK: +In Haskell 98 the context of a class declaration (which introduces superclasses) +must be simple; that is, each predicate must consist of a class applied to +type variables. The flag +() +lifts this restriction, +so that the only restriction on the context in a class declaration is +that the class hierarchy must be acyclic. So these class declarations are OK: @@ -4228,7 +4454,7 @@ type family Elem c example, consider the following declaration: type family F a b :: * -> * -- F's arity is 2, - -- although it's overall kind is * -> * -> * -> * + -- although its overall kind is * -> * -> * -> * Given this declaration the following are examples of well-formed and malformed types: @@ -4502,7 +4728,11 @@ these type signatures are perfectly OK g :: Eq [a] => ... g :: Ord (T a ()) => ... +The flag also lifts the corresponding +restriction on class declarations () and instance declarations +(). + GHC imposes the following restrictions on the constraints in a type signature. Consider the type: @@ -5833,12 +6063,13 @@ Wiki page. an expression; the spliced expression must have type Q Exp - a list of top-level declarations; the spliced expression must have type Q [Dec] + an type; the spliced expression must + have type Q Typ + a list of top-level declarations; the spliced expression + must have type Q [Dec] - Inside a splice you can can only call functions defined in imported modules, - not functions defined elsewhere in the same module. - + not functions defined elsewhere in the same module. A expression quotation is written in Oxford brackets, thus: @@ -5855,7 +6086,7 @@ Wiki page. A quasi-quotation can appear in either a pattern context or an expression context and is also written in Oxford brackets: - [:varid| ... |], + [$varid| ... |], where the "..." is an arbitrary string; a full description of the quasi-quotation facility is given in . @@ -5876,12 +6107,31 @@ Wiki page. + You may omit the $(...) in a top-level declaration splice. + Simply writing an expression (rather than a declaration) implies a splice. For example, you can write + +module Foo where +import Bar + +f x = x + +$(deriveStuff 'f) -- Uses the $(...) notation + +g y = y+1 + +deriveStuff 'g -- Omits the $(...) + +h z = z-1 + + This abbreviation makes top-level declaration slices quieter and less intimidating. + + (Compared to the original paper, there are many differences of detail. The syntax for a declaration splice uses "$" not "splice". The type of the enclosed expression must be Q [Dec], not [Q Dec]. -Type splices are not implemented, and neither are pattern splices or quotations. +Pattern splices and quotations are not implemented.) @@ -7040,24 +7290,11 @@ Assertion failures can be caught, see the documentation for the INCLUDE pragma - The INCLUDE pragma is for specifying the names - of C header files that should be #include'd into - the C source code generated by the compiler for the current module (if - compiling via C). For example: - - -{-# INCLUDE "foo.h" #-} -{-# INCLUDE <stdio.h> #-} - - INCLUDE is a file-header pragma (see ). - - An INCLUDE pragma is the preferred alternative - to the option (), because the - INCLUDE pragma is understood by other - compilers. Yet another alternative is to add the include file to each - foreign import declaration in your code, but we - don't recommend using this approach with GHC. + The INCLUDE used to be necessary for + specifying header files to be included when using the FFI and + compiling via C. It is no longer required for GHC, but is + accepted (and ignored) for compatibility with other + compilers.