X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fusers_guide%2Fglasgow_exts.xml;h=0a9c7e8d5195d597537dbe1f9ed24643180fa1c5;hb=a37dfa428490086770a1acb92c9b6694bbc35a55;hp=beaaad616a393caf66fc20809d502363266087fb;hpb=0065d5ab628975892cea1ec7303f968c3338cbe1;p=ghc-hetmet.git
diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml
index beaaad6..0a9c7e8 100644
--- a/docs/users_guide/glasgow_exts.xml
+++ b/docs/users_guide/glasgow_exts.xml
@@ -116,11 +116,22 @@ documentation describes all the libraries that come with GHC.
- :
-
+ ,:
- Switch off the Haskell 98 monomorphism restriction.
+ These two flags control how generalisation is done.
+ See .
+
+
+
+
+
+
+ :
+
+
+
+ Use GHCi's extended default rules in a regular module ().
Independent of the
flag.
@@ -140,7 +151,7 @@ documentation describes all the libraries that come with GHC.
-
+
@@ -244,7 +255,8 @@ documentation describes all the libraries that come with GHC.
Enables Template Haskell (see ). Currently also implied by
+ linkend="template-haskell"/>). This flag must
+ be given explicitly; it is no longer implied by
.Syntax stolen: [|,
@@ -616,7 +628,7 @@ clunky env var1 var1 = case lookup env var1 of
Nothing -> fail
Just val2 -> val1 + val2
where
- fail = val1 + val2
+ fail = var1 + var2
@@ -1009,8 +1021,8 @@ in a type synonym, thus:
f :: Discard a
f x y = (x, show y)
- g :: Discard Int -> (Int,Bool) -- A rank-2 type
- g f = f Int True
+ g :: Discard Int -> (Int,String) -- A rank-2 type
+ g f = f 3 True
@@ -2021,6 +2033,11 @@ something more specific does not:
op = ... -- Default
+You can find lots of background material about the reason for these
+restrictions in the paper
+Understanding functional dependencies via Constraint Handling Rules.
+
@@ -2089,7 +2106,7 @@ option, you can use arbitrary
types in both an instance context and instance head. Termination is ensured by having a
fixed-depth recursion stack. If you exceed the stack depth you get a
sort of backtrace, and the opportunity to increase the stack depth
-with N.
+with N.
@@ -2106,7 +2123,9 @@ can be modified by two flags:
and
-fallow-incoherent-instances
-, as this section discusses.
+, 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 ().
When GHC tries to resolve, say, the constraint C Int Bool,
it tries to match every instance declaration against the
@@ -2463,7 +2482,7 @@ function that called it. For example, our sort function might
to pick out the least value in a list:
least :: (?cmp :: a -> a -> Bool) => [a] -> a
- least xs = fst (sort xs)
+ least xs = head (sort xs)
Without lifting a finger, the ?cmp parameter is
propagated to become a parameter of least as well. With explicit
@@ -2623,6 +2642,11 @@ inner binding of ?x, so (f 9) will return
+
+
Explicitly-kinded quantification
@@ -3742,20 +3768,17 @@ Derived instance declarations are constructed as follows. Consider the
declaration (after expansion of any type synonyms)
- newtype T v1...vn = T' (S t1...tk vk+1...vn) deriving (c1...cm)
+ newtype T v1...vn = T' (t vk+1...vn) deriving (c1...cm)
where
- S is a type constructor,
-
-
- The t1...tk are types,
+ The type t is an arbitrary type
- The vk+1...vn are type variables which do not occur in any of
- the ti, and
+ The vk+1...vn are type variables which do not occur in
+ t, and
The ci are partial applications of
@@ -3773,7 +3796,7 @@ where
Then, for each ci, the derived instance
declaration is:
- instance ci (S t1...tk vk+1...v) => ci (T v1...vp)
+ instance ci (t vk+1...v) => ci (T v1...vp)
where p is chosen so that T v1...vp is of the
right kind for the last parameter of class Ci.
@@ -4089,9 +4112,8 @@ Tim Sheard is going to expand it.)
constructions. You need to use the flag
to switch these syntactic extensions on
- ( is currently implied by
- , but you are encouraged to
- specify it explicitly).
+ ( is no longer implied by
+ ).
@@ -4239,7 +4261,46 @@ Hello
+
+
+Using Template Haskell with Profiling
+profilingwith Template Haskell
+Template Haskell relies on GHC's built-in bytecode compiler and
+interpreter to run the splice expressions. The bytecode interpreter
+runs the compiled expression on top of the same runtime on which GHC
+itself is running; this means that the compiled code referred to by
+the interpreted expression must be compatible with this runtime, and
+in particular this means that object code that is compiled for
+profiling cannot be loaded and used by a splice
+expression, because profiled object code is only compatible with the
+profiling version of the runtime.
+
+This causes difficulties if you have a multi-module program
+containing Template Haskell code and you need to compile it for
+profiling, because GHC cannot load the profiled object code and use it
+when executing the splices. Fortunately GHC provides a workaround.
+The basic idea is to compile the program twice:
+
+
+
+ Compile the program or library first the normal way, without
+ .
+
+
+ Then compile it again with , and
+ additionally use
+ to name the object files differentliy (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
+ building with and Template Haskell is used,
+ GHC will emit an error message.
+
+
+
+
@@ -4734,6 +4795,150 @@ Because the preprocessor targets Haskell (rather than Core),
+
+
+
+Bang patterns
+Bang patterns
+
+GHC supports an extension of pattern matching called bang
+patterns. Bang patterns are under consideration for Haskell Prime.
+The Haskell
+prime feature description contains more discussion and examples
+than the material below.
+
+
+Bang patterns are enabled by the flag .
+
+
+
+Informal description of bang patterns
+
+
+The main idea is to add a single new production to the syntax of patterns:
+
+ pat ::= !pat
+
+Matching an expression e against a pattern !p is done by first
+evaluating e (to WHNF) and then matching the result against p.
+Example:
+
+f1 !x = True
+
+This definition makes f1 is strict in x,
+whereas without the bang it would be lazy.
+Bang patterns can be nested of course:
+
+f2 (!x, y) = [x,y]
+
+Here, f2 is strict in x but not in
+y.
+A bang only really has an effect if it precedes a variable or wild-card pattern:
+
+f3 !(x,y) = [x,y]
+f4 (x,y) = [x,y]
+
+Here, f3 and f4 are identical; putting a bang before a pattern that
+forces evaluation anyway does nothing.
+
+Bang patterns work in case expressions too, of course:
+
+g5 x = let y = f x in body
+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
+result, and then evaluates body.
+
+Bang patterns work in let and where
+definitions too. For example:
+
+let ![x,y] = e in b
+
+is a strict pattern: operationally, it evaluates e, matches
+it against the pattern [x,y], and then evaluates b
+The "!" should not be regarded as part of the pattern; after all,
+in a function argument ![x,y] means the
+same as [x,y]. Rather, the "!"
+is part of the syntax of let bindings.
+
+
+
+
+
+Syntax and semantics
+
+
+
+We add a single new production to the syntax of patterns:
+
+ pat ::= !pat
+
+There is one problem with syntactic ambiguity. Consider:
+
+f !x = 3
+
+Is this a definition of the infix function "(!)",
+or of the "f" with a bang pattern? GHC resolves this
+ambiguity inf favour of the latter. If you want to define
+(!) with bang-patterns enabled, you have to do so using
+prefix notation:
+
+(!) f x = 3
+
+The semantics of Haskell pattern matching is described in
+Section 3.17.2 of the Haskell Report. To this description add
+one extra item 10, saying:
+Matching
+the pattern !pat against a value v behaves as follows:
+if v is bottom, the match diverges
+ otherwise, pat is matched against
+ v
+
+
+Similarly, in Figure 4 of
+Section 3.17.3, add a new case (t):
+
+case v of { !pat -> e; _ -> e' }
+ = v `seq` case v of { pat -> e; _ -> e' }
+
+
+That leaves let expressions, whose translation is given in
+Section
+3.12
+of the Haskell Report.
+In the translation box, first apply
+the following transformation: for each pattern pi that is of
+form !qi = ei, transform it to (xi,!qi) = ((),ei), and and replace e0
+by (xi `seq` e0). Then, when none of the left-hand-side patterns
+have a bang at the top, apply the rules in the existing box.
+
+The effect of the let rule is to force complete matching of the pattern
+qi before evaluation of the body is begun. The bang is
+retained in the translated form in case qi is a variable,
+thus:
+
+ let !y = f x in b
+
+
+
+
+The let-binding can be recursive. However, it is much more common for
+the let-binding to be non-recursive, in which case the following law holds:
+(let !p = rhs in body)
+ is equivalent to
+(case rhs of !p -> body)
+
+
+A pattern with a bang at the outermost level is not allowed at the top level of
+a module.
+
+
+
+
@@ -5013,63 +5218,58 @@ key_function :: Int -> String -> (Bool, Double)
If you use you'll see the
sequence of phase numbers for successive runs of the
simplifier. In an INLINE pragma you can optionally specify a
- phase number, thus:
-
+ phase number, thus:
- You can say "inline f in Phase 2
- and all subsequent phases":
-
- {-# INLINE [2] f #-}
-
-
-
-
+ "INLINE[k] f" means: do not inline
+ f
+ until phase k, but from phase
+ k onwards be very keen to inline it.
+
- You can say "inline g in all
- phases up to, but not including, Phase 3":
-
- {-# INLINE [~3] g #-}
-
-
-
-
+ "INLINE[~k] f" means: be very keen to inline
+ f
+ until phase k, but from phase
+ k onwards do not inline it.
+
- If you omit the phase indicator, you mean "inline in
- all phases".
-
+ "NOINLINE[k] f" means: do not inline
+ f
+ until phase k, but from phase
+ k onwards be willing to inline it (as if
+ there was no pragma).
+
+
+ "INLINE[~k] f" means: be willing to inline
+ f
+ until phase k, but from phase
+ k onwards do not inline it.
+
-
- You can use a phase number on a NOINLINE pragma too:
-
-
-
- You can say "do not inline f
- until Phase 2; in Phase 2 and subsequently behave as if
- there was no pragma at all":
+The same information is summarised here:
- {-# NOINLINE [2] f #-}
-
-
-
+ -- Before phase 2 Phase 2 and later
+ {-# INLINE [2] f #-} -- No Yes
+ {-# INLINE [~2] f #-} -- Yes No
+ {-# NOINLINE [2] f #-} -- No Maybe
+ {-# NOINLINE [~2] f #-} -- Maybe No
-
- You can say "do not inline g in
- Phase 3 or any subsequent phase; before that, behave as if
- there was no pragma":
-
- {-# NOINLINE [~3] g #-}
+ {-# INLINE f #-} -- Yes Yes
+ {-# NOINLINE f #-} -- No No
-
-
-
-
- If you omit the phase indicator, you mean "never
- inline this function".
-
-
-
- The same phase-numbering control is available for RULES
+By "Maybe" we mean that the usual heuristic inlining rules apply (if the
+function body is small, or it is applied to interesting-looking arguments etc).
+Another way to understand the semantics is this:
+
+For both INLINE and NOINLINE, the phase number says
+when inlining is allowed at all.
+The INLINE pragma has the additional effect of making the
+function body look small, so that when inlining is allowed it is very likely to
+happen.
+
+
+
+The same phase-numbering control is available for RULES
().
@@ -5347,7 +5547,9 @@ The programmer can specify rewrite rules as part of the source program
(in a pragma). GHC applies these rewrite rules wherever it can, provided (a)
the flag () is on,
and (b) the flag
-() is not specified.
+() is not specified, and (c) the
+ ()
+flag is active.
@@ -5669,7 +5871,7 @@ The following are good producers:
- filter
+take, filter
@@ -5732,7 +5934,7 @@ The following are good consumers:
- filter
+take, filter
@@ -5997,6 +6199,89 @@ r) ->
+
+Special built-in functions
+GHC has a few built-in funcions with special behaviour,
+described in this section. All are exported by
+GHC.Exts.
+
+The inline function
+
+The inline function is somewhat experimental.
+
+ inline :: a -> a
+
+The call (inline f) arranges that f
+is inlined, regardless of its size. More precisely, the call
+(inline f) rewrites to the right-hand side of f's
+definition.
+This allows the programmer to control inlining from
+a particular call site
+rather than the definition site of the function
+(c.f. INLINE pragmas ).
+
+
+This inlining occurs regardless of the argument to the call
+or the size of f's definition; it is unconditional.
+The main caveat is that f's definition must be
+visible to the compiler. That is, f must be
+let-bound in the current scope.
+If no inlining takes place, the inline function
+expands to the identity function in Phase zero; so its use imposes
+no overhead.
+
+ If the function is defined in another
+module, GHC only exposes its inlining in the interface file if the
+function is sufficiently small that it might be
+inlined by the automatic mechanism. There is currently no way to tell
+GHC to expose arbitrarily-large functions in the interface file. (This
+shortcoming is something that could be fixed, with some kind of pragma.)
+
+
+
+The lazy function
+
+The lazy function restrains strictness analysis a little:
+
+ lazy :: a -> a
+
+The call (lazy e) means the same as e,
+but lazy has a magical property so far as strictness
+analysis is concerned: it is lazy in its first argument,
+even though its semantics is strict. After strictness analysis has run,
+calls to lazy are inlined to be the identity function.
+
+
+This behaviour is occasionally useful when controlling evaluation order.
+Notably, lazy is used in the library definition of
+Control.Parallel.par:
+
+ par :: a -> b -> b
+ par x y = case (par# x) of { _ -> lazy y }
+
+If lazy were not lazy, par would
+look strict in y which would defeat the whole
+purpose of par.
+
+
+
+The unsafeCoerce# function
+
+The function unsafeCoerce# allows you to side-step the
+typechecker entirely. It has type
+
+ unsafeCoerce# :: a -> b
+
+That is, it allows you to coerce any type into any other type. If you use this
+function, you had better get it right, otherwise segmentation faults await.
+It is generally used when you want to write a program that you know is
+well-typed, but where Haskell's type system is not expressive enough to prove
+that it is well typed.
+
+
+
+
+
Generic classes
@@ -6253,6 +6538,51 @@ Just to finish with, here's another example I rather like:
+
+Control over monomorphism
+
+GHC supports two flags that control the way in which generalisation is
+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.
+ A pattern binding is a binding whose LHS has no function arguments,
+ and is not a simple variable. For example:
+
+ f x = x -- Not a pattern binding
+ f = \x -> x -- Not a pattern binding
+ f :: Int -> Int = \x -> x -- Not a pattern binding
+
+ (g,h) = e -- A pattern binding
+ (f) = e -- A pattern binding
+ [x] = e -- A pattern binding
+
+Experimentally, GHC now makes pattern bindings monomorphic by
+default. Use to recover the
+standard behaviour.
+
+
+
+