X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=a27a9185c02ba09becdfc3dfefaca8efc09bdf08;hb=97169c5dd31537b28f5f8ad08cd6cdf82c1ecefd;hp=7e78c7299c9a69de6afc5bc800e1e6bd85711f93;hpb=6a05ec5ef5373f61b7f9f5bdc344483417fa801b;p=ghc-hetmet.git
diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml
index 7e78c72..a27a918 100644
--- a/docs/users_guide/glasgow_exts.xml
+++ b/docs/users_guide/glasgow_exts.xml
@@ -1152,16 +1152,16 @@ GHC allows a small extension to the syntax of left operator sections, which
allows you to define postfix operators. The extension is this: the left section
(e !)
-
+
is equivalent (from the point of view of both type checking and execution) to the expression
((!) e)
-
+
(for any expression e and operator (!).
The strict Haskell 98 interpretation is that the section is equivalent to
(\y -> (!) e y)
-
+
That is, the operator must be a function of two arguments. GHC allows it to
take only one argument, and that in turn allows you to write the function
postfix.
@@ -1689,8 +1689,8 @@ adding a new existential quantification construct.
-
-Type classes
+
+Existentials and type classes
An easy extension is to allow
@@ -1744,11 +1744,6 @@ dictionaries for Eq and Show respectively,
extract it on pattern matching.
-
-Notice the way that the syntax fits smoothly with that used for
-universal quantification earlier.
-
-
@@ -2021,19 +2016,8 @@ In the example, the equality dictionary is used to satisfy the equality constrai
generated by the call to elem, so that the type of
insert itself has no Eq constraint.
-This behaviour contrasts with Haskell 98's peculiar treament of
-contexts on a data type declaration (Section 4.2.1 of the Haskell 98 Report).
-In Haskell 98 the defintion
-
- data Eq a => Set' a = MkSet' [a]
-
-gives MkSet' the same type as MkSet above. But instead of
-making available an (Eq a) constraint, pattern-matching
-on MkSet'requires an (Eq a) constraint!
-GHC faithfully implements this behaviour, odd though it is. But for GADT-style declarations,
-GHC's behaviour is much more useful, as well as much more intuitive.
-For example, a possible application of GHC's behaviour is to reify dictionaries:
+For example, one possible application is to reify dictionaries:
data NumInst a where
MkNumInst :: Num a => NumInst a
@@ -2047,6 +2031,38 @@ For example, a possible application of GHC's behaviour is to reify dictionaries:
Here, a value of type NumInst a is equivalent
to an explicit (Num a) dictionary.
+
+All this applies to constructors declared using the syntax of .
+For example, the NumInst data type above could equivalently be declared
+like this:
+
+ data NumInst a
+ = Num a => MkNumInst (NumInst a)
+
+Notice that, unlike the situation when declaring an existental, there is
+no forall, because the Num constrains the
+data type's univerally quantified type variable a.
+A constructor may have both universal and existential type variables: for example,
+the following two declarations are equivalent:
+
+ data T1 a
+ = forall b. (Num a, Eq b) => MkT1 a b
+ data T2 a where
+ MkT2 :: (Num a, Eq b) => a -> b -> T2 a
+
+
+All this behaviour contrasts with Haskell 98's peculiar treatment of
+contexts on a data type declaration (Section 4.2.1 of the Haskell 98 Report).
+In Haskell 98 the definition
+
+ data Eq a => Set' a = MkSet' [a]
+
+gives MkSet' the same type as MkSet above. But instead of
+making available an (Eq a) constraint, pattern-matching
+on MkSet'requires an (Eq a) constraint!
+GHC faithfully implements this behaviour, odd though it is. But for GADT-style declarations,
+GHC's behaviour is much more useful, as well as much more intuitive.
+
The rest of this section gives further details about GADT-style data
@@ -2132,7 +2148,7 @@ 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 exmaple
+For example
aPerson = Adult { name = "Fred", children = [] }
@@ -2239,7 +2255,7 @@ constructor).
You cannot use a deriving clause for a GADT; only for
-an ordianary data type.
+an ordinary data type.
@@ -2307,7 +2323,7 @@ The third is not Haskell 98, and risks losing termination of instances.
GHC takes a conservative position: it accepts the first two, but not the third. The rule is this:
each constraint in the inferred instance context must consist only of type variables,
-with no repititions.
+with no repetitions.
This rule is applied regardless of flags. If you want a more exotic context, you can write
@@ -2343,7 +2359,7 @@ For example:
deriving instance MonadState Int Foo
GHC always treats the last parameter of the instance
-(Foo in this exmample) as the type whose instance is being derived.
+(Foo in this example) as the type whose instance is being derived.
@@ -2393,14 +2409,14 @@ Haskell 98, you can inherit instances of Eq, Ord
+
newtype Dollars = Dollars Int
-
+
and you want to use arithmetic on Dollars, you have to
explicitly define an instance of Num:
-
+
instance Num Dollars where
Dollars a + Dollars b = Dollars (a+b)
...
@@ -2418,17 +2434,17 @@ dictionary, only slower!
GHC now permits such instances to be derived instead,
using the flag ,
so one can write
-
+
newtype Dollars = Dollars Int deriving (Eq,Show,Num)
-
+
and the implementation uses the sameNum dictionary
for Dollars as for Int. Notionally, the compiler
derives an instance declaration of the form
-
+
instance Num Int => Num Dollars
-
+
which just adds or removes the newtype constructor according to the type.
@@ -2438,27 +2454,27 @@ We can also derive instances of constructor classes in a similar
way. For example, suppose we have implemented state and failure monad
transformers, such that
-
+
instance Monad m => Monad (State s m)
instance Monad m => Monad (Failure m)
-
+
In Haskell 98, we can define a parsing monad by
-
+
type Parser tok m a = State [tok] (Failure m) a
-
+
which is automatically a monad thanks to the instance declarations
above. With the extension, we can make the parser type abstract,
without needing to write an instance of class Monad, via
-
+
newtype Parser tok m a = Parser (State [tok] (Failure m) a)
deriving Monad
In this case the derived instance declaration is of the form
-
+
instance Monad (State [tok] (Failure m)) => Monad (Parser tok m)
-
+
Notice that, since Monad is a constructor class, the
instance is a partial application of the new type, not the
@@ -2473,12 +2489,12 @@ newtype is the last class parameter. In this case, a ``partial
application'' of the class appears in the deriving
clause. For example, given the class
-
+
class StateMonad s m | m -> s where ...
instance Monad m => StateMonad s (State s m) where ...
-
+
then we can derive an instance of StateMonad for Parsers by
-
+
newtype Parser tok m a = Parser (State [tok] (Failure m) a)
deriving (Monad, StateMonad [tok])
@@ -2486,7 +2502,7 @@ then we can derive an instance of StateMonad for Par
The derived instance is obtained by completing the application of the
class to the new type:
-
+
instance StateMonad [tok] (State [tok] (Failure m)) =>
StateMonad [tok] (Parser tok m)
@@ -2506,9 +2522,9 @@ the newtype and its representation.
Derived instance declarations are constructed as follows. Consider the
declaration (after expansion of any type synonyms)
-
+
newtype T v1...vn = T' (t vk+1...vn) deriving (c1...cm)
-
+
where
@@ -2537,17 +2553,17 @@ where
Then, for each ci, the derived instance
declaration is:
-
+
instance ci t => ci (T v1...vk)
As an example which does not work, consider
-
+
newtype NonMonad m s = NonMonad (State s m s) deriving Monad
-
+
Here we cannot derive the instance
-
+
instance Monad (State s m) => Monad (NonMonad m)
-
+
because the type variable s occurs in State s m,
and so cannot be "eta-converted" away. It is a good thing that this
@@ -2561,7 +2577,7 @@ Notice also that the order of class parameters becomes
important, since we can only derive instances for the last one. If the
StateMonad class above were instead defined as
-
+
class StateMonad m s | m -> s where ...
@@ -2673,7 +2689,7 @@ class type variable, thus:
The type of elem is illegal in Haskell 98, because it
contains the constraint Eq a, constrains only the
class type variable (in this case a).
-GHC lifts this restriction.
+GHC lifts this restriction (flag ).
@@ -3020,7 +3036,7 @@ must be of the form C a where a
is a type variable that occurs in the head.
-The flag loosens these restrictions
+The flag loosens these restrictions
considerably. Firstly, multi-parameter type classes are permitted. Secondly,
the context and head of the instance declaration can each consist of arbitrary
(well-kinded) assertions (C t1 ... tn) subject only to the
@@ -3149,7 +3165,7 @@ typechecker loop:
class F a b | a->b
instance F [a] [[a]]
instance (D c, F a c) => D [a] -- 'c' is not mentioned in the head
-
+
Similarly, it can be tempting to lift the coverage condition:
class Mul a b c | a b -> c where
@@ -3259,7 +3275,7 @@ 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
+and flags when that module is
being defined. Neither flag is required in a module that imports and uses the
instance declaration. Specifically, during the lookup process:
@@ -3372,7 +3388,7 @@ instance IsString [Char] where
fromString cs = cs
The class IsString is not in scope by default. If you want to mention
-it explicitly (for exmaple, to give an instance declaration for it), you can import it
+it explicitly (for example, to give an instance declaration for it), you can import it
from module GHC.Exts.
@@ -3426,7 +3442,7 @@ to work since it gets translated into an equality comparison.
Type signatures
-The context of a type signature
+The context of a type signature
Unlike Haskell 98, constraints in types do not have to be of
the form (class type-variable) or
@@ -3553,7 +3569,7 @@ J Lewis, MB Shields, E Meijer, J Launchbury,
Boston, Jan 2000.
-(Most of the following, stil rather incomplete, documentation is
+(Most of the following, still rather incomplete, documentation is
due to Jeff Lewis.)Implicit parameter support is enabled with the option
@@ -3733,7 +3749,7 @@ In the former case, len_acc1 is monomorphic in its own
right-hand side, so the implicit parameter ?acc is not
passed to the recursive call. In the latter case, because len_acc2
has a type signature, the recursive call is made to the
-polymoprhic version, which takes ?acc
+polymorphic version, which takes ?acc
as an implicit parameter. So we get the following results in GHCi:
Prog> len1 "hello"
@@ -3858,7 +3874,7 @@ Other points:
'?x' and '%x'
are entirely distinct implicit parameters: you
- can use them together and they won't intefere with each other.
+ can use them together and they won't interfere with each other. You can bind linear implicit parameters in 'with' clauses.
@@ -4062,9 +4078,18 @@ The function f3 has a rank-3 type;
it has rank-2 types on the left of a function arrow.
-GHC allows types of arbitrary rank; you can nest foralls
-arbitrarily deep in function arrows. (GHC used to be restricted to rank 2, but
-that restriction has now been lifted.)
+GHC has three flags to control higher-rank types:
+
+
+ : data constructors (only) can have polymorphic argment types.
+
+
+ : any function (including data constructors) can have a rank-2 type.
+
+
+ : any function (including data constructors) can have an arbitrary-rank type.
+That is, you can nest foralls
+arbitrarily deep in function arrows.
In particular, a forall-type (also called a "type scheme"),
including an operational type class context, is legal:
@@ -4076,6 +4101,8 @@ field type signatures. As the type of an implicit parameter In a pattern type signature (see )
+
+
Of course forall becomes a keyword; you can't use forall as
a type variable any more!
@@ -4371,7 +4398,7 @@ a type. (This is a change from GHC's earlier
design.)
Furthermore, distinct lexical type variables stand for distinct
type variables. This means that every programmer-written type signature
-(includin one that contains free scoped type variables) denotes a
+(including one that contains free scoped type variables) denotes a
rigid type; that is, the type is fully known to the type
checker, and no inference is involved.Lexical type variables may be alpha-renamed freely, without
@@ -4388,7 +4415,7 @@ A lexically scoped type variable can be bound by:
-In Haskell, a programmer-written type signature is implicitly quantifed over
+In Haskell, a programmer-written type signature is implicitly quantified over
its free type variables (Section
4.1.2
@@ -4455,7 +4482,7 @@ type variable s into scope, in the annotated expression
Pattern type signatures
A type signature may occur in any pattern; this is a pattern type
-signature.
+signature.
For example:
-- f and g assume that 'a' is already in scope
@@ -4463,14 +4490,32 @@ For example:
g (x::a) = x
h ((x,y) :: (Int,Bool)) = (y,x)
-In the case where all the type variables in the pattern type sigature are
+In the case where all the type variables in the pattern type signature are
already in scope (i.e. bound by the enclosing context), matters are simple: the
signature simply constrains the type of the pattern in the obvious way.
-There is only one situation in which you can write a pattern type signature that
-mentions a type variable that is not already in scope, namely in pattern match
-of an existential data constructor. For example:
+Unlike expression and declaration type signatures, pattern type signatures are not implictly generalised.
+The pattern in a patterm binding may only mention type variables
+that are already in scope. For example:
+
+ f :: forall a. [a] -> (Int, [a])
+ f xs = (n, zs)
+ where
+ (ys::[a], n) = (reverse xs, length xs) -- OK
+ zs::[a] = xs ++ ys -- OK
+
+ Just (v::b) = ... -- Not OK; b is not in scope
+
+Here, the pattern signatures for ys and zs
+are fine, but the one for v is not because b is
+not in scope.
+
+
+However, in all patterns other than pattern bindings, a pattern
+type signature may mention a type variable that is not in scope; in this case,
+the signature brings that type variable into scope.
+This is particularly important for existential data constructors. For example:
data T = forall a. MkT [a]
@@ -4480,16 +4525,21 @@ of an existential data constructor. For example:
t3::[a] = [t,t,t]
Here, the pattern type signature (t::a) mentions a lexical type
-variable that is not already in scope. Indeed, it cannot already be in scope,
+variable that is not already in scope. Indeed, it cannot already be in scope,
because it is bound by the pattern match. GHC's rule is that in this situation
(and only then), a pattern type signature can mention a type variable that is
not already in scope; the effect is to bring it into scope, standing for the
existentially-bound type variable.
-If this seems a little odd, we think so too. But we must have
+When a pattern type signature binds a type variable in this way, GHC insists that the
+type variable is bound to a rigid, or fully-known, type variable.
+This means that any user-written type signature always stands for a completely known type.
+
+
+If all this seems a little odd, we think so too. But we must have
some way to bring such type variables into scope, else we
-could not name existentially-bound type variables in subequent type signatures.
+could not name existentially-bound type variables in subsequent type signatures.
This is (now) the only situation in which a pattern type
@@ -4614,12 +4664,12 @@ This is rejected by Haskell 98, but under Jones's scheme the definition for
g is typechecked first, separately from that for
f,
because the reference to f in g's right
-hand side is ingored by the dependency analysis. Then g's
+hand side is ignored by the dependency analysis. Then g's
type is generalised, to get
g :: Ord a => a -> Bool
-Now, the defintion for f is typechecked, with this type for
+Now, the definition for f is typechecked, with this type for
g in the type environment.
@@ -4910,7 +4960,7 @@ The basic idea is to compile the program twice:Then compile it again with , and
additionally use
- to name the object files differentliy (you can choose any suffix
+ to name the object files differently (you can choose any suffix
that isn't the normal object suffix here). GHC will automatically
load the object files built in the first step when executing splice
expressions. If you omit the flag when
@@ -5468,7 +5518,7 @@ g6 x = case f x of { y -> body }
g7 x = case f x of { !y -> body }
The functions g5 and g6 mean exactly the same thing.
-But g7 evalutes (f x), binds y to the
+But g7 evaluates (f x), binds y to the
result, and then evaluates body.
Bang patterns work in let and where
@@ -5781,7 +5831,7 @@ Assertion failures can be caught, see the documentation for the
When you compile any module that imports and uses any
of the specified entities, GHC will print the specified
message.
- You can only depecate entities declared at top level in the module
+ You can only deprecate entities declared at top level in the module
being compiled, and you can only use unqualified names in the list of
entities being deprecated. A capitalised name, such as T
refers to either the type constructor T
@@ -5840,8 +5890,26 @@ key_function :: Int -> String -> (Bool, Double)
The major effect of an INLINE pragma
is to declare a function's “cost” to be very low.
The normal unfolding machinery will then be very keen to
- inline it.
-
+ inline it. However, an INLINE pragma for a
+ function "f" has a number of other effects:
+
+
+No funtions are inlined into f. Otherwise
+GHC might inline a big function into f's right hand side,
+making f big; and then inline f blindly.
+
+
+The float-in, float-out, and common-sub-expression transformations are not
+applied to the body of f.
+
+
+An INLINE function is not worker/wrappered by strictness analysis.
+It's going to be inlined wholesale instead.
+
+
+All of these effects are aimed at ensuring that what gets inlined is
+exactly what you asked for, no more and no less.
+Syntactically, an INLINE pragma for a
function can be put anywhere its type signature could be
put.
@@ -6013,7 +6081,7 @@ happen.
{-# SPECIALIZE f :: <type> #-}
- is valid if and only if the defintion
+ is valid if and only if the definition
f_spec :: <type>
f_spec = f
@@ -6029,7 +6097,7 @@ happen.
h :: Eq a => a -> a -> a
{-# SPECIALISE h :: (Eq a) => [a] -> [a] -> [a] #-}
-
+
The last of these examples will generate a
RULE with a somewhat-complex left-hand side (try it yourself), so it might not fire very
well. If you use this kind of specialisation, let us know how well it works.
@@ -6038,7 +6106,7 @@ well. If you use this kind of specialisation, let us know how well it works.
A SPECIALIZE pragma can optionally be followed with a
INLINE or NOINLINE pragma, optionally
followed by a phase, as described in .
-The INLINE pragma affects the specialised verison of the
+The INLINE pragma affects the specialised version of the
function (only), and applies even if the function is recursive. The motivating
example is this:
@@ -6734,7 +6802,7 @@ If you add you get a more detailed listing.
- The definition of (say) build in GHC/Base.lhs looks llike this:
+ The definition of (say) build in GHC/Base.lhs looks like this:
build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
@@ -6831,7 +6899,7 @@ r) ->
Special built-in functions
-GHC has a few built-in funcions with special behaviour. These
+GHC has a few built-in functions with special behaviour. These
are now described in the module GHC.Prim
in the library documentation.
@@ -7001,7 +7069,7 @@ So this too is illegal:
op2 :: a -> Bool
op2 {| p :*: q |} (x :*: y) = False
-(The reason for this restriction is that we gather all the equations for a particular type consructor
+(The reason for this restriction is that we gather all the equations for a particular type constructor
into a single generic instance declaration.)
@@ -7032,7 +7100,7 @@ Here, op1, op2, op3 are OK, but op4 is rejected, because it has a type variable
inside a list.
-This restriction is an implementation restriction: we just havn't got around to
+This restriction is an implementation restriction: we just haven't got around to
implementing the necessary bidirectional maps over arbitrary type constructors.
It would be relatively easy to add specific type constructors, such as Maybe and list,
to the ones that are allowed.
@@ -7130,7 +7198,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.