X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fglasgow_exts.xml;h=a25d42e89712e066a786d6f5cda416e9ea96dd2b;hb=8e14697c6e2cccc0a632233ffe95505765db2eef;hp=a3e1d01064ffbf1f20ded0c2da1c8c3370da7da6;hpb=df87e59ebe63776ef33b2587b50930baf9614931;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/glasgow_exts.xml b/ghc/docs/users_guide/glasgow_exts.xml index a3e1d01..a25d42e 100644 --- a/ghc/docs/users_guide/glasgow_exts.xml +++ b/ghc/docs/users_guide/glasgow_exts.xml @@ -219,6 +219,28 @@ documentation describes all the libraries that come with GHC. + + + Enables implicit parameters (see ). Currently also implied by + . + + Syntax stolen: + ?varid, + %varid. + + + + + + + Enables lexically-scoped type variables (see ). Implied by + . + + + + Enables Template Haskell (see describes all the libraries that come with GHC. - - - - Enables implicit parameters (see ). Currently also implied by - . - - Syntax stolen: - ?varid, - %varid. - - - @@ -270,7 +279,7 @@ became out of date, and wrong information is worse than none. The Real Truth about what primitive types there are, and what operations work over those types, is held in the file -fptools/ghc/compiler/prelude/primops.txt. +fptools/ghc/compiler/prelude/primops.txt.pp. This file is used directly to generate GHC's primitive-operation definitions, so it is always correct! It is also intended for processing into text. @@ -334,11 +343,16 @@ it is accidental that it is represented by a pointer. If a pointer represents a primitive value, then it really does point to that value: no unevaluated thunks, no indirections…nothing can be at the other end of the pointer than the primitive value. +A numerically-intensive program using unboxed types can +go a lot faster than its “standard” +counterpart—we saw a threefold speedup on one example. -There are some restrictions on the use of primitive types, the main -one being that you can't pass a primitive value to a polymorphic +There are some restrictions on the use of primitive types: + +The main restriction +is that you can't pass a primitive value to a polymorphic function or store one in a polymorphic data type. This rules out things like [Int#] (i.e. lists of primitive integers). The reason for this restriction is that polymorphic @@ -350,11 +364,33 @@ attempt to dereference the pointer, with disastrous results. Even worse, the unboxed value might be larger than a pointer (Double# for instance). + + You cannot bind a variable with an unboxed type +in a top-level binding. + + You cannot bind a variable with an unboxed type +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: + + data Foo = Foo Int Int# - -Nevertheless, A numerically-intensive program using unboxed types can -go a lot faster than its “standard” -counterpart—we saw a threefold speedup on one example. + 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 + + data Foo = Foo Int Int# + + f x = case ..rhs.. of { (Foo a b, w) -> ..body.. } + + + + @@ -389,21 +425,19 @@ values, but they avoid the heap allocation normally associated with using fully-fledged tuples. When an unboxed tuple is returned, the components are put directly into registers or on the stack; the unboxed tuple itself does not have a composite representation. Many -of the primitive operations listed in this section return unboxed +of the primitive operations listed in primops.txt.pp return unboxed tuples. +In particular, the IO and ST monads use unboxed +tuples to avoid unnecessary allocation during sequences of operations. There are some pretty stringent restrictions on the use of unboxed tuples: - - - - - Unboxed tuple types are subject to the same restrictions as +Values of unboxed tuple types are subject to the same restrictions as other unboxed types; i.e. they may not be stored in polymorphic data structures or passed to polymorphic functions. @@ -412,56 +446,46 @@ structures or passed to polymorphic functions. - Unboxed tuples may only be constructed as the direct result of -a function, and may only be deconstructed with a case expression. -eg. the following are valid: +No variable can have an unboxed tuple type, nor may a constructor or function +argument have an unboxed tuple type. The following are all illegal: -f x y = (# x+1, y-1 #) -g x = case f x x of { (# a, b #) -> a + b } - - - -but the following are invalid: - - - -f x y = g (# x, y #) -g (# x, y #) = x + y - - - - - - + data Foo = Foo (# Int, Int #) - - No variable can have an unboxed tuple type. This is illegal: + f :: (# Int, Int #) -> (# Int, Int #) + f x = x + g :: (# Int, Int #) -> Int + g (# a,b #) = a - -f :: (# Int, Int #) -> (# Int, Int #) -f x = x + h x = let y = (# x,x #) in ... - - -because x has an unboxed tuple type. - - - - -Note: we may relax some of these restrictions in the future. - - - -The IO and ST monads use unboxed -tuples to avoid unnecessary allocation during sequences of operations. +The typical use of unboxed tuples is simply to return multiple values, +binding those multiple results with a case expression, thus: + + f x y = (# x+1, y-1 #) + g x = case f x x of { (# a, b #) -> a + b } + +You can have an unboxed tuple in a pattern binding, thus + + f x = let (# p,q #) = h x in ..body.. + +If the types of p and q are not unboxed, +the resulting binding is lazy like any other Haskell pattern binding. The +above example desugars like this: + + f x = let t = case h x o f{ (# p,q #) -> (p,q) + p = fst t + q = snd t + in ..body.. + +Indeed, the bindings can even be recursive. @@ -901,18 +925,34 @@ Nevertheless, they can be useful when defining "phantom types". -Infix type constructors +Infix type constructors and classes -GHC allows type constructors to be operators, and to be written infix, very much +GHC allows type constructors and classes to be operators, and to be written infix, very much like expressions. More specifically: - A type constructor can be an operator, beginning with a colon; e.g. :*:. + A type constructor or class can be an operator, beginning with a colon; e.g. :*:. The lexical syntax is the same as that for data constructors. - Types can be written infix. For example Int :*: Bool. + Data type and type-synonym declarations can be written infix, parenthesised + if you want further arguments. E.g. + + data a :*: b = Foo a b + type a :+: b = Either a b + class a :=: b where ... + + data (a :**: b) x = Baz a b x + type (a :++: b) y = Either (a,b) y + + + + Types, and class constraints, can be written infix. For example + + x :: Int :*: Bool + f :: (a :=: b) => a -> b + Back-quotes work @@ -920,7 +960,7 @@ like expressions. More specifically: Int `a` Bool. Similarly, parentheses work the same; e.g. (:*:) Int Bool. - Fixities may be declared for type constructors just as for data constructors. However, + Fixities may be declared for type constructors, or classes, just as for data constructors. However, one cannot distinguish between the two in a fixity declaration; a fixity declaration sets the fixity for a data constructor and the corresponding type constructor. For example: @@ -934,13 +974,6 @@ like expressions. More specifically: Function arrow is infixr with fixity 0. (This might change; I'm not sure what it should be.) - Data type and type-synonym declarations can be written infix. E.g. - - data a :*: b = Foo a b - type a :+: b = Either a b - - - The only thing that differs between operators in types and operators in expressions is that ordinary non-constructor operators, such as + and * are not allowed in types. Reason: the uniform thing to do would be to make them type @@ -1698,99 +1731,67 @@ means Instance declarations - + Overlapping instances -In general, instance declarations may not overlap. The two instance -declarations - - - - instance context1 => C type1 where ... - instance context2 => C type2 where ... - - -"overlap" if type1 and type2 unify. - - -However, if you give the command line option --fallow-overlapping-instances -option then overlapping instance declarations are permitted. -However, GHC arranges never to commit to using an instance declaration -if another instance declaration also applies, either now or later. - - - - - - EITHER type1 and type2 do not unify - - - - - - OR type2 is a substitution instance of type1 -(but not identical to type1), or vice versa. - - - -Notice that these rules - - - - - make it clear which instance decl to use -(pick the most specific one that matches) - - - - - - - do not mention the contexts context1, context2 -Reason: you can pick which instance decl -"matches" based on the type. - - - - -However the rules are over-conservative. Two instance declarations can overlap, -but it can still be clear in particular situations which to use. For example: - - instance C (Int,a) where ... - instance C (a,Bool) where ... - -These are rejected by GHC's rules, but it is clear what to do when trying -to solve the constraint C (Int,Int) because the second instance -cannot apply. Yell if this restriction bites you. - - -GHC is also conservative about committing to an overlapping instance. For example: - - class C a where { op :: a -> a } - instance C [Int] where ... - instance C a => C [a] where ... - - f :: C b => [b] -> [b] - f x = op x - -From the RHS of f we get the constraint C [b]. But -GHC does not commit to the second instance declaration, because in a particular -call of f, b might be instantiate to Int, so the first instance declaration -would be appropriate. So GHC rejects the program. If you add -GHC will instead silently pick the second instance, without complaining about +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: +-fallow-overlapping-instances + +and +-fallow-incoherent-instances +, as this section discusses. + +When GHC tries to resolve, say, the constraint C Int Bool, +it tries to match every instance declaration against the +constraint, +by instantiating the head of the instance declaration. For example, consider +these declarations: + + instance context1 => C Int a where ... -- (A) + instance context2 => C a Bool where ... -- (B) + instance context3 => C Int [a] where ... -- (C) + instance context4 => C Int [Int] where ... -- (D) + +The instances (A) and (B) match the constraint C Int Bool, but (C) and (D) do not. When matching, GHC takes +no account of the context of the instance declaration +(context1 etc). +GHC's default behaviour is that exactly one instance must match the +constraint it is trying to resolve. +It is fine for there to be a potential of overlap (by +including both declarations (A) and (B), say); an error is only reported if a +particular constraint matches more than one. + + + +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 +most-specific match, the program is rejected. + + +However, GHC is conservative about committing to an overlapping instance. For example: + + f :: [b] -> [b] + f x = ... + +Suppose that from the RHS of f we get the constraint +C Int [b]. But +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 , +GHC will instead pick (C), without complaining about the problem of subsequent instantiations. -Regrettably, GHC doesn't guarantee to detect overlapping instance -declarations if they appear in different modules. GHC can "see" the -instance declarations in the transitive closure of all the modules -imported by the one being compiled, so it can "see" all instance decls -when it is compiling Main. However, it currently chooses not -to look at ones that can't possibly be of use in the module currently -being compiled, in the interests of efficiency. (Perhaps we should -change that decision, at least for Main.) - +Because overlaps are checked and reported lazily, as described above, you need +the in the module that calls +the overloaded function, rather than in the module that defines it. + @@ -2677,22 +2678,19 @@ for rank-2 types. -A pattern type signature can introduce a scoped type -variable. For example - - - - +A lexically scoped type variable can be bound by: + +A declaration type signature () +A pattern type signature () +A result type signature () + +For example: f (xs::[a]) = ys ++ ys where ys :: [a] ys = reverse 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. @@ -2700,8 +2698,6 @@ 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 mentioned in the type signature that are not in scope are implicitly universally quantified. (If there are no type variables in @@ -2724,10 +2720,10 @@ So much for the basic idea. Here are the details. -What a pattern type signature means +What a scoped type variable means -A type variable brought into scope by a pattern type signature is simply -the name for a type. The restriction they express is that all occurrences +A lexically-scoped type variable is simply +the name for a type. The restriction it expresses is that all occurrences of the same name mean the same type. For example: f :: [Int] -> Int -> Int @@ -2897,7 +2893,33 @@ scope over the methods defined in the where part. For exampl - + +Declaration type signatures +A declaration type signature that has explicit +quantification (using forall) brings into scope the +explicitly-quantified +type variables, in the definition of the named function(s). For example: + + f :: forall a. [a] -> [a] + f (x:xs) = xs ++ [ x :: a ] + +The "forall a" brings "a" into scope in +the definition of "f". + +This only happens if the quantification in f's type +signature is explicit. For example: + + g :: [a] -> [a] + g (x:xs) = xs ++ [ x :: a ] + +This program will be rejected, because "a" does not scope +over the definition of "f", so "x::a" +means "x::forall a. a" by Haskell's usual implicit +quantification rules. + + + + Where a pattern type signature can occur @@ -3014,10 +3036,12 @@ in f4's scope. +Pattern type signatures are completely orthogonal to ordinary, separate +type signatures. The two can be used independently or together. - + Result type signatures @@ -3538,7 +3562,7 @@ module Printf where -- you intend to use it. -- Import some Template Haskell syntax -import Language.Haskell.TH.Syntax +import Language.Haskell.TH -- Describe a format string data Format = D | S | L String @@ -4228,6 +4252,31 @@ 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> #-} + + The INCLUDE pragma(s) must appear at the top of + your source file with any OPTIONS_GHC + pragma(s). + + 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. + + INLINE and NOINLINE pragmas @@ -4401,16 +4450,19 @@ key_function :: Int -> String -> (Bool, Double) - OPTIONS pragma - OPTIONS + OPTIONS_GHC pragma + OPTIONS_GHC - pragmaOPTIONS + pragmaOPTIONS_GHC - The OPTIONS pragma is used to specify + The OPTIONS_GHC pragma is used to specify additional options that are given to the compiler when compiling this source file. See for details. + + Previous versions of GHC accepted OPTIONS rather + than OPTIONS_GHC, but that is now deprecated.