X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=43e84394c085b4b308f3bf210fdbb589d81dba87;hp=31206017e7b76cc6b7ff0fa4b12e0de7fee36ddf;hb=58521c72cec262496dabf5fffb057d25ab17a0f7;hpb=5eb2190d2aebc6e1a11780a43d31cbc7e831dd78 diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index 3120601..43e8439 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#. @@ -876,7 +874,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 +1001,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 +1042,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 +1269,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 @@ -2353,16 +2390,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 +2441,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 +2477,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 +2617,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. @@ -4248,7 +4336,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: @@ -5853,6 +5941,8 @@ Wiki page. an expression; the spliced expression must have type Q Exp + an type; the spliced expression must + have type Q Typ a list of top-level declarations; the spliced expression must have type Q [Dec] @@ -5901,7 +5991,7 @@ Wiki page. (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.)