[project @ 1999-04-29 12:21:50 by simonpj]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.vsgml
1
2 % $Id: glasgow_exts.vsgml,v 1.9 1999/04/29 12:21:50 simonpj Exp $
3 %
4 % GHC Language Extensions.
5 %
6
7 As with all known Haskell systems, GHC implements some extensions to
8 the language.  To use them, you'll need to give a @-fglasgow-exts@%
9 <nidx>-fglasgow-exts option</nidx> option.
10
11 Virtually all of the Glasgow extensions serve to give you access to
12 the underlying facilities with which we implement Haskell.  Thus, you
13 can get at the Raw Iron, if you are willing to write some non-standard
14 code at a more primitive level.  You need not be ``stuck'' on
15 performance because of the implementation costs of Haskell's
16 ``high-level'' features---you can always code ``under'' them.  In an
17 extreme case, you can write all your time-critical code in C, and then
18 just glue it together with Haskell!
19
20 Executive summary of our extensions:
21
22 <descrip>
23
24 <tag>Unboxed types and primitive operations:</tag> 
25
26 You can get right down to the raw machine types and operations;
27 included in this are ``primitive arrays'' (direct access to Big Wads
28 of Bytes).  Please see Section <ref name="Unboxed types"
29 id="glasgow-unboxed"> and following.
30
31 <tag>Multi-parameter type classes:</tag>
32
33 GHC's type system supports extended type classes with multiple
34 parameters.  Please see Section <ref name="Mult-parameter type
35 classes" id="multi-param-type-classes">.
36
37 <tag>Local universal quantification:</tag>
38
39 GHC's type system supports explicit unversal quantification in
40 constructor fields and function arguments.  This is useful for things
41 like defining @runST@ from the state-thread world.  See Section <ref
42 name="Local universal quantification" id="universal-quantification">.
43
44 <tag>Extistentially quantification in data types:</tag>
45
46 Some or all of the type variables in a datatype declaration may be
47 <em>existentially quantified</em>.  More details in Section <ref
48 name="Existential Quantification" id="existential-quantification">.
49
50 <tag>Scoped type variables:</tag>
51
52 Scoped type variables enable the programmer to supply type signatures
53 for some nested declarations, where this would not be legal in Haskell
54 98.  Details in Section <ref name="Scoped Type Variables"
55 id="scoped-type-variables">.
56
57 <tag>Calling out to C:</tag> 
58
59 Just what it sounds like.  We provide <em>lots</em> of rope that you
60 can dangle around your neck.  Please see Section <ref name="Calling~C
61 directly from Haskell" id="glasgow-ccalls">.
62
63 <tag>Pragmas</tag>
64
65 Pragmas are special instructions to the compiler placed in the source
66 file.  The pragmas GHC supports are described in Section <ref
67 name="Pragmas" id="pragmas">.
68
69 </descrip>
70
71 Before you get too carried away working at the lowest level (e.g.,
72 sloshing @MutableByteArray#@s around your program), you may wish to
73 check if there are system libraries that provide a ``Haskellised
74 veneer'' over the features you want.  See Section <ref name="GHC
75 Prelude and libraries" id="ghc-prelude">.
76
77 %************************************************************************
78 %*                                                                      *
79 <sect1>Unboxed types
80 <label id="glasgow-unboxed">
81 <p>
82 <nidx>Unboxed types (Glasgow extension)</nidx>
83 %*                                                                      *
84 %************************************************************************
85
86 These types correspond to the ``raw machine'' types you would use in
87 C: @Int#@ (long int), @Double#@ (double), @Addr#@ (void *), etc.  The
88 <em>primitive operations</em> (PrimOps) on these types are what you
89 might expect; e.g., @(+#)@ is addition on @Int#@s, and is the
90 machine-addition that we all know and love---usually one instruction.
91
92 There are some restrictions on the use of unboxed types, the main one
93 being that you can't pass an unboxed value to a polymorphic function
94 or store one in a polymorphic data type.  This rules out things like
95 @[Int#]@ (ie. lists of unboxed integers).  The reason for this
96 restriction is that polymorphic arguments and constructor fields are
97 assumed to be pointers: if an unboxed integer is stored in one of
98 these, the garbage collector would attempt to follow it, leading to
99 unpredictable space leaks.  Or a @seq@ operation on the polymorphic
100 component may attempt to dereference the pointer, with disastrous
101 results.  Even worse, the unboxed value might be larger than a pointer
102 (@Double#@ for instance).
103
104 Nevertheless, A numerically-intensive program using unboxed types can
105 go a <em>lot</em> faster than its ``standard'' counterpart---we saw a
106 threefold speedup on one example.
107
108 Please see Section <ref name="The module PrelGHC: really primitive
109 stuff" id="ghc-libs-ghc"> for the details of unboxed types and the
110 operations on them.
111
112 %************************************************************************
113 %*                                                                      *
114 <sect1>Primitive state-transformer monad
115 <label id="glasgow-ST-monad">
116 <p>
117 <nidx>state transformers (Glasgow extensions)</nidx>
118 <nidx>ST monad (Glasgow extension)</nidx>
119 %*                                                                      *
120 %************************************************************************
121
122 This monad underlies our implementation of arrays, mutable and
123 immutable, and our implementation of I/O, including ``C calls''.
124
125 The @ST@ library, which provides access to the @ST@ monad, is a
126 GHC/Hugs extension library and is described in the separate <htmlurl
127 name="GHC/Hugs Extension Libraries" url="libs.html"> document.
128
129 %************************************************************************
130 %*                                                                      *
131 <sect1>Primitive arrays, mutable and otherwise
132 <label id="glasgow-prim-arrays">
133 <p>
134 <nidx>primitive arrays (Glasgow extension)</nidx>
135 <nidx>arrays, primitive (Glasgow extension)</nidx>
136 %*                                                                      *
137 %************************************************************************
138
139 GHC knows about quite a few flavours of Large Swathes of Bytes.
140
141 First, GHC distinguishes between primitive arrays of (boxed) Haskell
142 objects (type @Array# obj@) and primitive arrays of bytes (type
143 @ByteArray#@).
144
145 Second, it distinguishes between...
146 <descrip>
147 <tag>Immutable:</tag>
148 Arrays that do not change (as with ``standard'' Haskell arrays); you
149 can only read from them.  Obviously, they do not need the care and
150 attention of the state-transformer monad.
151
152 <tag>Mutable:</tag>
153 Arrays that may be changed or ``mutated.''  All the operations on them
154 live within the state-transformer monad and the updates happen
155 <em>in-place</em>.
156
157 <tag>``Static'' (in C land):</tag>
158 A C routine may pass an @Addr#@ pointer back into Haskell land.  There
159 are then primitive operations with which you may merrily grab values
160 over in C land, by indexing off the ``static'' pointer.
161
162 <tag>``Stable'' pointers:</tag>
163 If, for some reason, you wish to hand a Haskell pointer (i.e.,
164 <em>not</em> an unboxed value) to a C routine, you first make the
165 pointer ``stable,'' so that the garbage collector won't forget that it
166 exists.  That is, GHC provides a safe way to pass Haskell pointers to
167 C.
168
169 Please see Section <ref name="Subverting automatic unboxing with
170 ``stable pointers''" id="glasgow-stablePtrs"> for more details.
171
172 <tag>``Foreign objects'':</tag>
173 A ``foreign object'' is a safe way to pass an external object (a
174 C-allocated pointer, say) to Haskell and have Haskell do the Right
175 Thing when it no longer references the object.  So, for example, C
176 could pass a large bitmap over to Haskell and say ``please free this
177 memory when you're done with it.'' 
178
179 Please see Section <ref name="Pointing outside the Haskell heap"
180 id="glasgow-foreignObjs"> for more details.
181
182 </descrip>
183
184 The libraries section gives more details on all these ``primitive
185 array'' types and the operations on them, Section <ref name="The GHC
186 Prelude and Libraries" id="ghc-prelude">.  Some of these extensions
187 are also supported by Hugs, and the supporting libraries are described
188 in the <htmlurl name="GHC/Hugs Extension Libraries" url="libs.html">
189 document.
190
191 %************************************************************************
192 %*                                                                      *
193 <sect1>Calling~C directly from Haskell
194 <label id="glasgow-ccalls">
195 <p>
196 <nidx>C calls (Glasgow extension)</nidx>
197 <nidx>_ccall_ (Glasgow extension)</nidx>
198 <nidx>_casm_ (Glasgow extension)</nidx>
199 %*                                                                      *
200 %************************************************************************
201
202 GOOD ADVICE: Because this stuff is not Entirely Stable as far as names
203 and things go, you would be well-advised to keep your C-callery
204 corraled in a few modules, rather than sprinkled all over your code.
205 It will then be quite easy to update later on.
206
207 %************************************************************************
208 %*                                                                      *
209 <sect2>@_ccall_@ and @_casm_@: an introduction
210 <label id="ccall-intro">
211 <p>
212 %*                                                                      *
213 %************************************************************************
214
215 The simplest way to use a simple C function
216
217 <tscreen><verb>
218 double fooC( FILE *in, char c, int i, double d, unsigned int u )
219 </verb></tscreen>
220
221 is to provide a Haskell wrapper:
222
223 <tscreen><verb>
224 fooH :: Char -> Int -> Double -> Word -> IO Double
225 fooH c i d w = _ccall_ fooC (``stdin''::Addr) c i d w
226 </verb></tscreen>
227
228 The function @fooH@ will unbox all of its arguments, call the C
229 function @fooC@ and box the corresponding arguments.
230
231 One of the annoyances about @_ccall_@s is when the C types don't quite
232 match the Haskell compiler's ideas.  For this, the @_casm_@ variant
233 may be just the ticket (NB: <em>no chance</em> of such code going
234 through a native-code generator):
235
236 <tscreen><verb>
237 oldGetEnv name
238   = _casm_ ``%r = getenv((char *) %0);'' name >>= \ litstring@(A# str#) ->
239     return (
240         if (litstring == ``NULL'') then
241             Left ("Fail:oldGetEnv:"++name)
242         else
243             Right (unpackCString# str#)
244     )
245 </verb></tscreen>
246
247 The first literal-literal argument to a @_casm_@ is like a @printf@
248 format: @%r@ is replaced with the ``result,'' @%0@--@%n-1@ are
249 replaced with the 1st--nth arguments.  As you can see above, it is an
250 easy way to do simple C~casting.  Everything said about @_ccall_@ goes
251 for @_casm_@ as well.
252
253 The use of @_casm_@ in your code does pose a problem to the compiler
254 when it comes to generating an interface file for a freshly compiled
255 module. Included in an interface file is the unfolding (if any) of a
256 declaration. However, if a declaration's unfolding happens to contain
257 a @_casm_@, its unfolding will <em/not/ be emitted into the interface
258 file even if it qualifies by all the other criteria. The reason why
259 the compiler prevents this from happening is that unfolding @_casm_@s
260 into an interface file unduly constrains how code that import your
261 module have to be compiled. If an imported declaration is unfolded and
262 it contains a @_casm_@, you now have to be using a compiler backend
263 capable of dealing with it (i.e., the C compiler backend). If you are
264 using the C compiler backend, the unfolded @_casm_@ may still cause you
265 problems since the C code snippet it contains may mention CPP symbols
266 that were in scope when compiling the original module are not when
267 compiling the importing module.
268
269 If you're willing to put up with the drawbacks of doing cross-module
270 inlining of C code (GHC - A Better C Compiler :-), the option
271 @-funfold-casms-in-hi-file@ will turn off the default behaviour.
272 <nidx>-funfold-casms-in-hi-file option</nidx>
273
274 %************************************************************************
275 %*                                                                      *
276 <sect2>Literal-literals
277 <label id="glasgow-literal-literals">
278 <p>
279 <nidx>Literal-literals</nidx>
280 %*                                                                      *
281 %************************************************************************
282
283 The literal-literal argument to @_casm_@ can be made use of separately
284 from the @_casm_@ construct itself. Indeed, we've already used it:
285
286 <tscreen><verb>
287 fooH :: Char -> Int -> Double -> Word -> IO Double
288 fooH c i d w = _ccall_ fooC (``stdin''::Addr) c i d w
289 </verb></tscreen>
290
291 The first argument that's passed to @fooC@ is given as a literal-literal, 
292 that is, a literal chunk of C code that will be inserted into the generated
293 @.hc@ code at the right place.
294
295 A literal-literal is restricted to having a type that's an instance of
296 the @CCallable@ class, see <ref name="CCallable" id="ccall-gotchas">
297 for more information.
298
299 Notice that literal-literals are by their very nature unfriendly to
300 native code generators, so exercise judgement about whether or not to
301 make use of them in your code.
302
303 %************************************************************************
304 %*                                                                      *
305 <sect2>Using function headers
306 <label id="glasgow-foreign-headers">
307 <p>
308 <nidx>C calls, function headers</nidx>
309 %*                                                                      *
310 %************************************************************************
311
312 When generating C (using the @-fvia-C@ directive), one can assist the
313 C compiler in detecting type errors by using the @-#include@ directive
314 to provide @.h@ files containing function headers.
315
316 For example,
317
318 <tscreen><verb>
319 typedef unsigned long *StgForeignObj;
320 typedef long StgInt;
321
322 void          initialiseEFS (StgInt size);
323 StgInt        terminateEFS (void);
324 StgForeignObj emptyEFS(void);
325 StgForeignObj updateEFS (StgForeignObj a, StgInt i, StgInt x);
326 StgInt        lookupEFS (StgForeignObj a, StgInt i);
327 </verb></tscreen>
328
329 You can find appropriate definitions for @StgInt@, @StgForeignObj@,
330 etc using @gcc@ on your architecture by consulting
331 @ghc/includes/StgTypes.h@.  The following table summarises the
332 relationship between Haskell types and C types.
333
334 <tabular ca="ll">
335 <bf>C type name</bf>      | <bf>Haskell Type</bf> @@
336 @@
337 @StgChar@          | @Char#@ @@               
338 @StgInt@           | @Int#@ @@                
339 @StgWord@          | @Word#@ @@               
340 @StgAddr@          | @Addr#@ @@               
341 @StgFloat@         | @Float#@ @@              
342 @StgDouble@        | @Double#@ @@             
343
344 @StgArray@         | @Array#@ @@              
345 @StgByteArray@     | @ByteArray#@ @@          
346 @StgArray@         | @MutableArray#@ @@       
347 @StgByteArray@     | @MutableByteArray#@ @@   
348
349 @StgStablePtr@     | @StablePtr#@ @@
350 @StgForeignObj@    | @ForeignObj#@
351 </tabular>
352
353 Note that this approach is only <em>essential</em> for returning
354 @float@s (or if @sizeof(int) != sizeof(int *)@ on your
355 architecture) but is a Good Thing for anyone who cares about writing
356 solid code.  You're crazy not to do it.
357
358 %************************************************************************
359 %*                                                                      *
360 <sect2>Subverting automatic unboxing with ``stable pointers''
361 <label id="glasgow-stablePtrs">
362 <p>
363 <nidx>stable pointers (Glasgow extension)</nidx>
364 %*                                                                      *
365 %************************************************************************
366
367 The arguments of a @_ccall_@ are automatically unboxed before the
368 call.  There are two reasons why this is usually the Right Thing to
369 do:
370
371 <itemize>
372 <item>
373 C is a strict language: it would be excessively tedious to pass
374 unevaluated arguments and require the C programmer to force their
375 evaluation before using them.
376
377 <item> Boxed values are stored on the Haskell heap and may be moved
378 within the heap if a garbage collection occurs---that is, pointers
379 to boxed objects are not <em>stable</em>.
380 </itemize>
381
382 It is possible to subvert the unboxing process by creating a ``stable
383 pointer'' to a value and passing the stable pointer instead.  For
384 example, to pass/return an integer lazily to C functions @storeC@ and
385 @fetchC@, one might write:
386
387 <tscreen><verb>
388 storeH :: Int -> IO ()
389 storeH x = makeStablePtr x              >>= \ stable_x ->
390            _ccall_ storeC stable_x
391
392 fetchH :: IO Int
393 fetchH x = _ccall_ fetchC               >>= \ stable_x ->
394            deRefStablePtr stable_x      >>= \ x ->
395            freeStablePtr stable_x       >>
396            return x
397 </verb></tscreen>
398
399 The garbage collector will refrain from throwing a stable pointer away
400 until you explicitly call one of the following from C or Haskell.
401
402 <tscreen><verb>
403 void freeStablePointer( StgStablePtr stablePtrToToss )
404 freeStablePtr :: StablePtr a -> IO ()
405 </verb></tscreen>
406
407 As with the use of @free@ in C programs, GREAT CARE SHOULD BE
408 EXERCISED to ensure these functions are called at the right time: too
409 early and you get dangling references (and, if you're lucky, an error
410 message from the runtime system); too late and you get space leaks.
411
412 And to force evaluation of the argument within @fooC@, one would
413 call one of the following C functions (according to type of argument).
414
415 <tscreen><verb>
416 void     performIO  ( StgStablePtr stableIndex /* StablePtr s (IO ()) */ );
417 StgInt   enterInt   ( StgStablePtr stableIndex /* StablePtr s Int */ );
418 StgFloat enterFloat ( StgStablePtr stableIndex /* StablePtr s Float */ );
419 </verb></tscreen>
420
421 <nidx>performIO</nidx>
422 <nidx>enterInt</nidx>
423 <nidx>enterFloat</nidx>
424
425 Note Bene: @_ccall_GC_@<nidx>_ccall_GC_</nidx> must be used if any of
426 these functions are used.
427
428 %************************************************************************
429 %*                                                                      *
430 <sect2>Foreign objects: pointing outside the Haskell heap
431 <label id="glasgow-foreignObjs">
432 <p>
433 <nidx>foreign objects (Glasgow extension)</nidx>
434 %*                                                                      *
435 %************************************************************************
436
437 There are two types that @ghc@ programs can use to reference
438 (heap-allocated) objects outside the Haskell world: @Addr@ and
439 @ForeignObj@.
440
441 If you use @Addr@, it is up to you to the programmer to arrange
442 allocation and deallocation of the objects.
443
444 If you use @ForeignObj@, @ghc@'s garbage collector will call upon the
445 user-supplied <em>finaliser</em> function to free the object when the
446 Haskell world no longer can access the object.  (An object is
447 associated with a finaliser function when the abstract
448  Haskell type @ForeignObj@ is created). The finaliser function is
449 expressed in C, and is passed as argument the object:
450
451 <tscreen><verb>
452 void foreignFinaliser ( StgForeignObj fo )
453 </verb></tscreen>
454
455 when the Haskell world can no longer access the object.  Since
456 @ForeignObj@s only get released when a garbage collection occurs, we
457 provide ways of triggering a garbage collection from within C and from
458 within Haskell.
459
460 <tscreen><verb>
461 void GarbageCollect()
462 performGC :: IO ()
463 </verb></tscreen>
464
465 More information on the programmers' interface to @ForeignObj@ can be
466 found in the library documentation.
467
468 %************************************************************************
469 %*                                                                      *
470 <sect2>Avoiding monads
471 <label id="glasgow-avoiding-monads">
472 <p>
473 <nidx>C calls to `pure C'</nidx>
474 <nidx>unsafePerformIO</nidx>
475 %*                                                                      *
476 %************************************************************************
477
478 The @_ccall_@ construct is part of the @IO@ monad because 9 out of 10
479 uses will be to call imperative functions with side effects such as
480 @printf@.  Use of the monad ensures that these operations happen in a
481 predictable order in spite of laziness and compiler optimisations.
482
483 To avoid having to be in the monad to call a C function, it is
484 possible to use @unsafePerformIO@, which is available from the
485 @IOExts@ module.  There are three situations where one might like to
486 call a C function from outside the IO world:
487
488 <itemize>
489 <item>
490 Calling a function with no side-effects:
491 <tscreen><verb>
492 atan2d :: Double -> Double -> Double
493 atan2d y x = unsafePerformIO (_ccall_ atan2d y x)
494
495 sincosd :: Double -> (Double, Double)
496 sincosd x = unsafePerformIO $ do
497         da <- newDoubleArray (0, 1)
498         _casm_ ``sincosd( %0, &((double *)%1[0]), &((double *)%1[1]) );'' x da
499         s <- readDoubleArray da 0
500         c <- readDoubleArray da 1
501         return (s, c)
502 </verb></tscreen>
503
504 <item> Calling a set of functions which have side-effects but which can
505 be used in a purely functional manner.
506
507 For example, an imperative implementation of a purely functional
508 lookup-table might be accessed using the following functions.
509
510 <tscreen><verb>
511 empty  :: EFS x
512 update :: EFS x -> Int -> x -> EFS x
513 lookup :: EFS a -> Int -> a
514
515 empty = unsafePerformIO (_ccall_ emptyEFS)
516
517 update a i x = unsafePerformIO $
518         makeStablePtr x         >>= \ stable_x ->
519         _ccall_ updateEFS a i stable_x
520
521 lookup a i = unsafePerformIO $
522         _ccall_ lookupEFS a i   >>= \ stable_x ->
523         deRefStablePtr stable_x
524 </verb></tscreen>
525
526 You will almost always want to use @ForeignObj@s with this.
527
528 <item> Calling a side-effecting function even though the results will
529 be unpredictable.  For example the @trace@ function is defined by:
530
531 <tscreen><verb>
532 trace :: String -> a -> a
533 trace string expr
534   = unsafePerformIO (
535         ((_ccall_ PreTraceHook sTDERR{-msg-}):: IO ())  >>
536         fputs sTDERR string                             >>
537         ((_ccall_ PostTraceHook sTDERR{-msg-}):: IO ()) >>
538         return expr )
539   where
540     sTDERR = (``stderr'' :: Addr)
541 </verb></tscreen>
542
543 (This kind of use is not highly recommended --- it is only really
544 useful in debugging code.)
545 </itemize>
546
547 %************************************************************************
548 %*                                                                      *
549 <sect2>C-calling ``gotchas'' checklist
550 <label id="ccall-gotchas">
551 <p>
552 <nidx>C call dangers</nidx>
553 <nidx>CCallable</nidx>
554 <nidx>CReturnable</nidx>
555 %*                                                                      *
556 %************************************************************************
557
558 And some advice, too.
559
560 <itemize>
561 <item> For modules that use @_ccall_@s, etc., compile with
562 @-fvia-C@.<nidx>-fvia-C option</nidx> You don't have to, but you should.
563
564 Also, use the @-#include "prototypes.h"@ flag (hack) to inform the C
565 compiler of the fully-prototyped types of all the C functions you
566 call.  (Section <ref name="Using function headers"
567 id="glasgow-foreign-headers"> says more about this...)
568
569 This scheme is the <em>only</em> way that you will get <em>any</em>
570 typechecking of your @_ccall_@s.  (It shouldn't be that way, but...).
571 GHC will pass the flag @-Wimplicit@ to gcc so that you'll get warnings
572 if any @_ccall_@ed functions have no prototypes.
573
574 <item>
575 Try to avoid @_ccall_@s to C~functions that take @float@
576 arguments or return @float@ results.  Reason: if you do, you will
577 become entangled in (ANSI?) C's rules for when arguments/results are
578 promoted to @doubles@.  It's a nightmare and just not worth it.
579 Use @doubles@ if possible.
580
581 If you do use @floats@, check and re-check that the right thing is
582 happening.  Perhaps compile with @-keep-hc-file-too@ and look at
583 the intermediate C (@.hc@ file).
584
585 <item> The compiler uses two non-standard type-classes when
586 type-checking the arguments and results of @_ccall_@: the arguments
587 (respectively result) of @_ccall_@ must be instances of the class
588 @CCallable@ (respectively @CReturnable@).  Both classes may be
589 imported from the module @CCall@, but this should only be
590 necessary if you want to define a new instance.  (Neither class
591 defines any methods --- their only function is to keep the
592 type-checker happy.)
593
594 The type checker must be able to figure out just which of the
595 C-callable/returnable types is being used.  If it can't, you have to
596 add type signatures. For example,
597
598 <tscreen><verb>
599 f x = _ccall_ foo x
600 </verb></tscreen>
601
602 is not good enough, because the compiler can't work out what type @x@
603 is, nor what type the @_ccall_@ returns.  You have to write, say:
604
605 <tscreen><verb>
606 f :: Int -> IO Double
607 f x = _ccall_ foo x
608 </verb></tscreen>
609
610 This table summarises the standard instances of these classes.
611
612 % ToDo: check this table against implementation!
613
614 <tabular ca="llll">
615 <bf>Type</bf>       |<bf>CCallable</bf>|<bf>CReturnable</bf> | <bf>Which is probably...</bf> @@
616
617 @Char@              | Yes  | Yes   | @unsigned char@ @@
618 @Int@               | Yes  | Yes   | @long int@ @@
619 @Word@              | Yes  | Yes   | @unsigned long int@ @@
620 @Addr@              | Yes  | Yes   | @void *@ @@
621 @Float@             | Yes  | Yes   | @float@ @@
622 @Double@            | Yes  | Yes   | @double@ @@
623 @()@                | No   | Yes   | @void@ @@
624 @[Char]@            | Yes  | No    | @char *@ (null-terminated) @@
625                                       
626 @Array@             | Yes  | No    | @unsigned long *@ @@
627 @ByteArray@         | Yes  | No    | @unsigned long *@ @@
628 @MutableArray@      | Yes  | No    | @unsigned long *@ @@
629 @MutableByteArray@  | Yes  | No    | @unsigned long *@ @@
630                                        
631 @State@             | Yes  | Yes   | nothing!@@
632                                        
633 @StablePtr@         | Yes  | Yes   | @unsigned long *@ @@
634 @ForeignObjs@       | Yes  | Yes   | see later @@
635 </tabular>
636
637 Actually, the @Word@ type is defined as being the same size as a
638 pointer on the target architecture, which is <em>probably</em>
639 @unsigned long int@.  
640
641 The brave and careful programmer can add their own instances of these
642 classes for the following types:
643
644 <itemize>
645 <item>
646 A <em>boxed-primitive</em> type may be made an instance of both
647 @CCallable@ and @CReturnable@.  
648
649 A boxed primitive type is any data type with a
650 single unary constructor with a single primitive argument.  For
651 example, the following are all boxed primitive types:
652
653 <tscreen><verb>
654 Int
655 Double
656 data XDisplay = XDisplay Addr#
657 data EFS a = EFS# ForeignObj#
658 </verb></tscreen>
659
660 <tscreen><verb>
661 instance CCallable   (EFS a)
662 instance CReturnable (EFS a)
663 </verb></tscreen>
664
665 <item> Any datatype with a single nullary constructor may be made an
666 instance of @CReturnable@.  For example:
667
668 <tscreen><verb>
669 data MyVoid = MyVoid
670 instance CReturnable MyVoid
671 </verb></tscreen>
672
673 <item> As at version 2.09, @String@ (i.e., @[Char]@) is still
674 not a @CReturnable@ type.
675
676 Also, the now-builtin type @PackedString@ is neither
677 @CCallable@ nor @CReturnable@.  (But there are functions in
678 the PackedString interface to let you get at the necessary bits...)
679 </itemize>
680
681 <item> The code-generator will complain if you attempt to use @%r@ in
682 a @_casm_@ whose result type is @IO ()@; or if you don't use @%r@
683 <em>precisely</em> once for any other result type.  These messages are
684 supposed to be helpful and catch bugs---please tell us if they wreck
685 your life.
686
687 <item> If you call out to C code which may trigger the Haskell garbage
688 collector or create new threads (examples of this later...), then you
689 must use the @_ccall_GC_@<nidx>_ccall_GC_ primitive</nidx> or
690 @_casm_GC_@<nidx>_casm_GC_ primitive</nidx> variant of C-calls.  (This
691 does not work with the native code generator - use @\fvia-C@.) This
692 stuff is hairy with a capital H!  </itemize>
693
694 <sect1> Multi-parameter type classes
695 <label id="multi-param-type-classes">
696 <p>
697
698 This section documents GHC's implementation of multi-paramter type
699 classes.  There's lots of background in the paper <url name="Type
700 classes: exploring the design space"
701 url="http://www.dcs.gla.ac.uk/~simonpj/multi.ps.gz"> (Simon Peyton
702 Jones, Mark Jones, Erik Meijer).
703
704 I'd like to thank people who reported shorcomings in the GHC 3.02
705 implementation.  Our default decisions were all conservative ones, and
706 the experience of these heroic pioneers has given useful concrete
707 examples to support several generalisations.  (These appear below as
708 design choices not implemented in 3.02.)
709
710 I've discussed these notes with Mark Jones, and I believe that Hugs
711 will migrate towards the same design choices as I outline here.
712 Thanks to him, and to many others who have offered very useful
713 feedback.
714
715 <sect2>Types
716 <p>
717
718 There are the following restrictions on the form of a qualified 
719 type:
720
721 <tscreen><verb>
722   forall tv1..tvn (c1, ...,cn) => type
723 </verb></tscreen>
724
725 (Here, I write the "foralls" explicitly, although the Haskell source
726 language omits them; in Haskell 1.4, all the free type variables of an
727 explicit source-language type signature are universally quantified,
728 except for the class type variables in a class declaration.  However,
729 in GHC, you can give the foralls if you want.  See Section <ref
730 name="Explicit universal quantification"
731 id="universal-quantification">).
732
733 <enum>
734
735 <item> <bf>Each universally quantified type variable 
736 @tvi@ must be mentioned (i.e. appear free) in @type@</bf>.
737
738 The reason for this is that a value with a type that does not obey
739 this restriction could not be used without introducing
740 ambiguity. Here, for example, is an illegal type:
741
742 <tscreen><verb>
743   forall a. Eq a => Int
744 </verb></tscreen>
745
746 When a value with this type was used, the constraint <tt>Eq tv</tt>
747 would be introduced where <tt>tv</tt> is a fresh type variable, and
748 (in the dictionary-translation implementation) the value would be
749 applied to a dictionary for <tt>Eq tv</tt>.  The difficulty is that we
750 can never know which instance of <tt>Eq</tt> to use because we never
751 get any more information about <tt>tv</tt>.
752
753 <item> <bf>Every constraint @ci@ must mention at least one of the
754 universally quantified type variables @tvi@</bf>.
755
756 For example, this type is OK because <tt>C a b</tt> mentions the
757 universally quantified type variable <tt>b</tt>:
758
759 <tscreen><verb>
760   forall a. C a b => burble
761 </verb></tscreen>
762
763 The next type is illegal because the constraint <tt>Eq b</tt> does not
764 mention <tt>a</tt>:
765
766 <tscreen><verb>
767   forall a. Eq b => burble
768 </verb></tscreen>
769
770 The reason for this restriction is milder than the other one.  The
771 excluded types are never useful or necessary (because the offending
772 context doesn't need to be witnessed at this point; it can be floated
773 out).  Furthermore, floating them out increases sharing. Lastly,
774 excluding them is a conservative choice; it leaves a patch of
775 territory free in case we need it later.
776
777 </enum>
778
779 These restrictions apply to all types, whether declared in a type signature
780 or inferred.
781
782 Unlike Haskell 1.4, constraints in types do <bf>not</bf> have to be of
783 the form <em>(class type-variables)</em>.  Thus, these type signatures
784 are perfectly OK
785
786 <tscreen><verb>
787   f :: Eq (m a) => [m a] -> [m a]
788   g :: Eq [a] => ...
789 </verb></tscreen>
790
791 This choice recovers principal types, a property that Haskell 1.4 does not have.
792
793 <sect2>Class declarations
794 <p>
795
796 <enum>
797
798 <item> <bf>Multi-parameter type classes are permitted</bf>. For example:
799
800 <tscreen><verb>
801   class Collection c a where
802     union :: c a -> c a -> c a
803     ...etc..
804 </verb></tscreen>
805
806
807 <item> <bf>The class hierarchy must be acyclic</bf>.  However, the definition
808 of "acyclic" involves only the superclass relationships.  For example,
809 this is OK:
810
811 <tscreen><verb>
812   class C a where { 
813     op :: D b => a -> b -> b
814   }
815
816   class C a => D a where { ... }
817 </verb></tscreen>
818
819 Here, <tt>C</tt> is a superclass of <tt>D</tt>, but it's OK for a
820 class operation <tt>op</tt> of <tt>C</tt> to mention <tt>D</tt>.  (It
821 would not be OK for <tt>D</tt> to be a superclass of <tt>C</tt>.)
822
823 <item> <bf>There are no restrictions on the context in a class declaration
824 (which introduces superclasses), except that the class hierarchy must
825 be acyclic</bf>.  So these class declarations are OK:
826
827 <tscreen><verb>
828   class Functor (m k) => FiniteMap m k where
829     ...
830
831   class (Monad m, Monad (t m)) => Transform t m where
832     lift :: m a -> (t m) a
833 </verb></tscreen>
834
835 <item> <bf>In the signature of a class operation, every constraint
836 must mention at least one type variable that is not a class type
837 variable</bf>.
838
839 Thus:
840
841 <tscreen><verb>
842   class Collection c a where
843     mapC :: Collection c b => (a->b) -> c a -> c b
844 </verb></tscreen>
845
846 is OK because the constraint <tt>(Collection a b)</tt> mentions
847 <tt>b</tt>, even though it also mentions the class variable
848 <tt>a</tt>.  On the other hand:
849
850 <tscreen><verb>
851   class C a where
852     op :: Eq a => (a,b) -> (a,b)
853 </verb></tscreen>
854
855 is not OK because the constraint <tt>(Eq a)</tt> mentions on the class
856 type variable <tt>a</tt>, but not <tt>b</tt>.  However, any such
857 example is easily fixed by moving the offending context up to the
858 superclass context:
859
860 <tscreen><verb>
861   class Eq a => C a where
862     op ::(a,b) -> (a,b)
863 </verb></tscreen>
864
865 A yet more relaxed rule would allow the context of a class-op signature
866 to mention only class type variables.  However, that conflicts with
867 Rule 1(b) for types above.
868
869 <item> <bf>The type of each class operation must mention <em/all/ of
870 the class type variables</bf>.  For example:
871
872 <tscreen><verb>
873   class Coll s a where
874     empty  :: s
875     insert :: s -> a -> s
876 </verb></tscreen>
877
878 is not OK, because the type of <tt>empty</tt> doesn't mention
879 <tt>a</tt>.  This rule is a consequence of Rule 1(a), above, for
880 types, and has the same motivation.
881
882 Sometimes, offending class declarations exhibit misunderstandings.  For
883 example, <tt>Coll</tt> might be rewritten
884
885 <tscreen><verb>
886   class Coll s a where
887     empty  :: s a
888     insert :: s a -> a -> s a
889 </verb></tscreen>
890
891 which makes the connection between the type of a collection of
892 <tt>a</tt>'s (namely <tt>(s a)</tt>) and the element type <tt>a</tt>.
893 Occasionally this really doesn't work, in which case you can split the
894 class like this:
895
896 <tscreen><verb>
897   class CollE s where
898     empty  :: s
899
900   class CollE s => Coll s a where
901     insert :: s -> a -> s
902 </verb></tscreen>
903
904 </enum>
905
906 <sect2>Instance declarations
907 <p>
908
909 <enum>
910
911 <item> <bf>Instance declarations may not overlap</bf>.  The two instance
912 declarations
913
914 <tscreen><verb>
915   instance context1 => C type1 where ...
916   instance context2 => C type2 where ...
917 </verb></tscreen>
918
919 "overlap" if @type1@ and @type2@ unify
920
921 However, if you give the command line option
922 @-fallow-overlapping-instances@<nidx>-fallow-overlapping-instances
923 option</nidx> then two overlapping instance declarations are permitted
924 iff
925
926 <itemize>
927 <item> EITHER @type1@ and @type2@ do not unify
928 <item> OR @type2@ is a substitution instance of @type1@
929                 (but not identical to @type1@)
930 <item> OR vice versa
931 </itemize>
932
933 Notice that these rules
934
935 <itemize>
936 <item> make it clear which instance decl to use
937            (pick the most specific one that matches)
938
939 <item> do not mention the contexts @context1@, @context2@
940             Reason: you can pick which instance decl
941             "matches" based on the type.
942 </itemize>
943
944 Regrettably, GHC doesn't guarantee to detect overlapping instance
945 declarations if they appear in different modules.  GHC can "see" the
946 instance declarations in the transitive closure of all the modules
947 imported by the one being compiled, so it can "see" all instance decls
948 when it is compiling <tt>Main</tt>.  However, it currently chooses not
949 to look at ones that can't possibly be of use in the module currently
950 being compiled, in the interests of efficiency.  (Perhaps we should
951 change that decision, at least for <tt>Main</tt>.)
952
953 <item> <bf>There are no restrictions on the type in an instance
954 <em/head/, except that at least one must not be a type variable</bf>.
955 The instance "head" is the bit after the "=>" in an instance decl. For
956 example, these are OK:
957
958 <tscreen><verb>
959   instance C Int a where ...
960
961   instance D (Int, Int) where ...
962
963   instance E [[a]] where ...
964 </verb></tscreen>
965
966 Note that instance heads <bf>may</bf> contain repeated type variables.
967 For example, this is OK:
968
969 <tscreen><verb>
970   instance Stateful (ST s) (MutVar s) where ...
971 </verb></tscreen>
972
973 The "at least one not a type variable" restriction is to ensure that
974 context reduction terminates: each reduction step removes one type
975 constructor.  For example, the following would make the type checker
976 loop if it wasn't excluded:
977
978 <tscreen><verb>
979   instance C a => C a where ...
980 </verb></tscreen>
981
982 There are two situations in which the rule is a bit of a pain. First,
983 if one allows overlapping instance declarations then it's quite
984 convenient to have a "default instance" declaration that applies if
985 something more specific does not:
986
987 <tscreen><verb>
988   instance C a where
989     op = ... -- Default
990 </verb></tscreen>
991
992 Second, sometimes you might want to use the following to get the
993 effect of a "class synonym":
994
995 <tscreen><verb>
996   class (C1 a, C2 a, C3 a) => C a where { }
997
998   instance (C1 a, C2 a, C3 a) => C a where { }
999 </verb></tscreen>
1000
1001 This allows you to write shorter signatures:
1002
1003 <tscreen><verb>
1004   f :: C a => ...
1005 </verb></tscreen>
1006
1007 instead of
1008
1009 <tscreen><verb>
1010   f :: (C1 a, C2 a, C3 a) => ...
1011 </verb></tscreen>
1012
1013 I'm on the lookout for a simple rule that preserves decidability while
1014 allowing these idioms.  The experimental flag
1015 @-fallow-undecidable-instances@<nidx>-fallow-undecidable-instances
1016 option</nidx> lifts this restriction, allowing all the types in an
1017 instance head to be type variables.
1018
1019 <item> <bf>Unlike Haskell 1.4, instance heads may use type
1020 synonyms</bf>.  As always, using a type synonym is just shorthand for
1021 writing the RHS of the type synonym definition.  For example:
1022
1023 <tscreen><verb>
1024   type Point = (Int,Int) 
1025   instance C Point   where ...
1026   instance C [Point] where ...
1027 </verb></tscreen>
1028
1029 is legal.  However, if you added
1030
1031 <tscreen><verb>
1032   instance C (Int,Int) where ...
1033 </verb></tscreen>
1034
1035 as well, then the compiler will complain about the overlapping
1036 (actually, identical) instance declarations.  As always, type synonyms
1037 must be fully applied.  You cannot, for example, write:
1038
1039 <tscreen><verb>
1040   type P a = [[a]]
1041   instance Monad P where ...
1042 </verb></tscreen>
1043
1044 This design decision is independent of all the others, and easily
1045 reversed, but it makes sense to me.
1046
1047 <item><bf>The types in an instance-declaration <em/context/ must all
1048 be type variables</bf>. Thus
1049
1050 <tscreen><verb>
1051   instance C a b => Eq (a,b) where ...
1052 </verb></tscreen>
1053
1054 is OK, but
1055
1056 <tscreen><verb>
1057   instance C Int b => Foo b where ...
1058 </verb></tscreen>
1059
1060 is not OK.  Again, the intent here is to make sure that context
1061 reduction terminates.
1062
1063 Voluminous correspondence on the Haskell mailing list has convinced me
1064 that it's worth experimenting with a more liberal rule.  If you use
1065 the flag <tt>-fallow-undecidable-instances</tt> you can use arbitrary
1066 types in an instance context.  Termination is ensured by having a
1067 fixed-depth recursion stack.  If you exceed the stack depth you get a
1068 sort of backtrace, and the opportunity to increase the stack depth
1069 with <tt>-fcontext-stack</tt><em/N/.
1070
1071 </enum>
1072
1073 % -----------------------------------------------------------------------------
1074 <sect1>Explicit universal quantification
1075 <label id="universal-quantification">
1076 <p>
1077
1078 GHC now allows you to write explicitly quantified types.  GHC's
1079 syntax for this now agrees with Hugs's, namely:
1080
1081 <tscreen><verb>
1082         forall a b. (Ord a, Eq  b) => a -> b -> a
1083 </verb></tscreen>
1084
1085 The context is, of course, optional.  You can't use <tt>forall</tt> as
1086 a type variable any more!
1087
1088 Haskell type signatures are implicitly quantified.  The <tt>forall</tt>
1089 allows us to say exactly what this means.  For example:
1090
1091 <tscreen><verb>
1092         g :: b -> b
1093 </verb></tscreen>
1094
1095 means this:
1096
1097 <tscreen><verb>
1098         g :: forall b. (b -> b)
1099 </verb></tscreen>
1100
1101 The two are treated identically.
1102
1103 <sect2>Universally-quantified data type fields
1104 <label id="univ">
1105 <p>
1106
1107 In a <tt>data</tt> or <tt>newtype</tt> declaration one can quantify
1108 the types of the constructor arguments.  Here are several examples:
1109
1110 <tscreen><verb>
1111 data T a = T1 (forall b. b -> b -> b) a
1112
1113 data MonadT m = MkMonad { return :: forall a. a -> m a,
1114                           bind   :: forall a b. m a -> (a -> m b) -> m b
1115                         }
1116
1117 newtype Swizzle = MkSwizzle (Ord a => [a] -> [a])
1118 </verb></tscreen>
1119
1120 The constructors now have so-called <em/rank 2/ polymorphic
1121 types, in which there is a for-all in the argument types.:
1122
1123 <tscreen><verb>
1124 T1 :: forall a. (forall b. b -> b -> b) -> a -> T1 a
1125 MkMonad :: forall m. (forall a. a -> m a)
1126                   -> (forall a b. m a -> (a -> m b) -> m b)
1127                   -> MonadT m
1128 MkSwizzle :: (Ord a => [a] -> [a]) -> Swizzle
1129 </verb></tscreen>
1130
1131 Notice that you don't need to use a <tt>forall</tt> if there's an
1132 explicit context.  For example in the first argument of the
1133 constructor <tt>MkSwizzle</tt>, an implicit "<tt>forall a.</tt>" is
1134 prefixed to the argument type.  The implicit <tt>forall</tt>
1135 quantifies all type variables that are not already in scope, and are
1136 mentioned in the type quantified over.
1137
1138 As for type signatures, implicit quantification happens for non-overloaded
1139 types too.  So if you write this:
1140 <tscreen><verb>
1141   data T a = MkT (Either a b) (b -> b)
1142 </verb></tscreen>
1143 it's just as if you had written this:
1144 <tscreen><verb>
1145   data T a = MkT (forall b. Either a b) (forall b. b -> b)
1146 </verb></tscreen>
1147 That is, since the type variable <tt>b</tt> isn't in scope, it's
1148 implicitly universally quantified.  (Arguably, it would be better
1149 to <em>require</em> explicit quantification on constructor arguments
1150 where that is what is wanted.  Feedback welcomed.)
1151
1152 <sect2> Construction 
1153 <p>
1154
1155 You construct values of types <tt>T1, MonadT, Swizzle</tt> by applying
1156 the constructor to suitable values, just as usual.  For example,
1157
1158 <tscreen><verb>
1159 (T1 (\xy->x) 3) :: T Int
1160
1161 (MkSwizzle sort)    :: Swizzle
1162 (MkSwizzle reverse) :: Swizzle
1163
1164 (let r x = Just x
1165      b m k = case m of
1166                 Just y -> k y
1167                 Nothing -> Nothing
1168   in
1169   MkMonad r b) :: MonadT Maybe
1170 </verb></tscreen>
1171
1172 The type of the argument can, as usual, be more general than the type
1173 required, as <tt>(MkSwizzle reverse)</tt> shows.  (<tt>reverse</tt>
1174 does not need the <tt>Ord</tt> constraint.)
1175
1176 <sect2>Pattern matching
1177 <p>
1178
1179 When you use pattern matching, the bound variables may now have
1180 polymorphic types.  For example:
1181
1182 <tscreen><verb>
1183         f :: T a -> a -> (a, Char)
1184         f (T1 f k) x = (f k x, f 'c' 'd')
1185
1186         g :: (Ord a, Ord b) => Swizzle -> [a] -> (a -> b) -> [b]
1187         g (MkSwizzle s) xs f = s (map f (s xs))
1188
1189         h :: MonadT m -> [m a] -> m [a]
1190         h m [] = return m []
1191         h m (x:xs) = bind m x           $ \y ->
1192                       bind m (h m xs)   $ \ys ->
1193                       return m (y:ys)
1194 </verb></tscreen>
1195
1196 In the function <tt>h</tt> we use the record selectors <tt>return</tt>
1197 and <tt>bind</tt> to extract the polymorphic bind and return functions
1198 from the <tt>MonadT</tt> data structure, rather than using pattern
1199 matching.
1200
1201 You cannot pattern-match against an argument that is polymorphic.
1202 For example:
1203 <tscreen><verb>
1204         newtype TIM s a = TIM (ST s (Maybe a))
1205
1206         runTIM :: (forall s. TIM s a) -> Maybe a
1207         runTIM (TIM m) = runST m
1208 </verb></tscreen>
1209
1210 Here the pattern-match fails, because you can't pattern-match against
1211 an argument of type <tt>(forall s. TIM s a)</tt>.  Instead you 
1212 must bind the variable and pattern match in the right hand side:
1213 <tscreen><verb>
1214         runTIM :: (forall s. TIM s a) -> Maybe a
1215         runTIM tm = case tm of { TIM m -> runST m }
1216 </verb></tscreen>
1217 The <tt>tm</tt> on the right hand side is (invisibly) instantiated, like
1218 any polymorphic value at its occurrence site, and now you can pattern-match
1219 against it.
1220
1221 <sect2>The partial-application restriction
1222 <p>
1223
1224 There is really only one way in which data structures with polymorphic
1225 components might surprise you: you must not partially apply them.
1226 For example, this is illegal:
1227
1228 <tscreen><verb>
1229         map MkSwizzle [sort, reverse]
1230 </verb></tscreen>
1231
1232 The restriction is this: <em>every subexpression of the program must
1233 have a type that has no for-alls, except that in a function
1234 application (f e1 ... en) the partial applications are not subject to
1235 this rule</em>.  The restriction makes type inference feasible.
1236
1237 In the illegal example, the sub-expression <tt>MkSwizzle</tt> has the
1238 polymorphic type <tt>(Ord b => [b] -> [b]) -> Swizzle</tt> and is not
1239 a sub-expression of an enclosing application.  On the other hand, this
1240 expression is OK:
1241
1242 <tscreen><verb>
1243         map (T1 (\a b -> a)) [1,2,3]
1244 </verb></tscreen>
1245
1246 even though it involves a partial application of <tt>T1</tt>, because
1247 the sub-expression <tt>T1 (\a b -> a)</tt> has type <tt>Int -> T
1248 Int</tt>.
1249
1250 <sect2>Type signatures
1251 <label id="sigs">
1252 <p>
1253
1254 Once you have data constructors with universally-quantified fields, or
1255 constants such as <tt>runST</tt> that have rank-2 types, it isn't long
1256 before you discover that you need more!  Consider:
1257
1258 <tscreen><verb>
1259   mkTs f x y = [T1 f x, T1 f y]
1260 </verb></tscreen>
1261
1262 <tt>mkTs</tt> is a fuction that constructs some values of type
1263 <tt>T</tt>, using some pieces passed to it.  The trouble is that since
1264 <tt>f</tt> is a function argument, Haskell assumes that it is
1265 monomorphic, so we'll get a type error when applying <tt>T1</tt> to
1266 it.  This is a rather silly example, but the problem really bites in
1267 practice.  Lots of people trip over the fact that you can't make
1268 "wrappers functions" for <tt>runST</tt> for exactly the same reason.
1269 In short, it is impossible to build abstractions around functions with
1270 rank-2 types.
1271
1272 The solution is fairly clear.  We provide the ability to give a rank-2
1273 type signature for <em>ordinary</em> functions (not only data
1274 constructors), thus:
1275
1276 <tscreen><verb>
1277   mkTs :: (forall b. b -> b -> b) -> a -> [T a]
1278   mkTs f x y = [T1 f x, T1 f y]
1279 </verb></tscreen>
1280
1281 This type signature tells the compiler to attribute <tt>f</tt> with
1282 the polymorphic type <tt>(forall b. b -> b -> b)</tt> when type
1283 checking the body of <tt>mkTs</tt>, so now the application of
1284 <tt>T1</tt> is fine.
1285
1286 There are two restrictions:
1287
1288 <itemize>
1289 <item> You can only define a rank 2 type, specified by the following
1290 grammar:
1291
1292 <tscreen><verb>
1293    rank2type ::= [forall tyvars .] [context =>] funty
1294    funty     ::= ([forall tyvars .] [context =>] ty) -> funty
1295                | ty
1296    ty        ::= ...current Haskell monotype syntax...
1297 </verb></tscreen>
1298
1299 Informally, the universal quantification must all be right at the beginning, 
1300 or at the top level of a function argument.
1301
1302 <item> There is a restriction on the definition of a function whose
1303 type signature is a rank-2 type: the polymorphic arguments must be
1304 matched on the left hand side of the "<tt>=</tt>" sign.  You can't
1305 define <tt>mkTs</tt> like this:
1306
1307 <tscreen><verb>
1308   mkTs :: (forall b. b -> b -> b) -> a -> [T a]
1309   mkTs = \ f x y -> [T1 f x, T1 f y]
1310 </verb></tscreen>
1311
1312
1313 The same partial-application rule applies to ordinary functions with
1314 rank-2 types as applied to data constructors.  
1315
1316 </itemize>
1317
1318 % -----------------------------------------------------------------------------
1319 <sect1>Existentially quantified data constructors
1320 <label id="existential-quantification">
1321 <p>
1322
1323 The idea of using existential quantification in data type declarations
1324 was suggested by Laufer (I believe, thought doubtless someone will
1325 correct me), and implemented in Hope+. It's been in Lennart
1326 Augustsson's <tt>hbc</tt> Haskell compiler for several years, and
1327 proved very useful.  Here's the idea.  Consider the declaration:
1328
1329 <tscreen><verb>
1330   data Foo = forall a. MkFoo a (a -> Bool)
1331            | Nil
1332 </verb></tscreen>
1333
1334 The data type <tt>Foo</tt> has two constructors with types:
1335
1336 <tscreen><verb>
1337   MkFoo :: forall a. a -> (a -> Bool) -> Foo
1338   Nil   :: Foo
1339 </verb></tscreen>
1340
1341 Notice that the type variable <tt>a</tt> in the type of <tt>MkFoo</tt>
1342 does not appear in the data type itself, which is plain <tt>Foo</tt>.
1343 For example, the following expression is fine:
1344
1345 <tscreen><verb>
1346   [MkFoo 3 even, MkFoo 'c' isUpper] :: [Foo]
1347 </verb></tscreen>
1348
1349 Here, <tt>(MkFoo 3 even)</tt> packages an integer with a function
1350 <tt>even</tt> that maps an integer to <tt>Bool</tt>; and <tt>MkFoo 'c'
1351 isUpper</tt> packages a character with a compatible function.  These
1352 two things are each of type <tt>Foo</tt> and can be put in a list.
1353
1354 What can we do with a value of type <tt>Foo</tt>?.  In particular,
1355 what happens when we pattern-match on <tt>MkFoo</tt>?
1356
1357 <tscreen><verb>
1358   f (MkFoo val fn) = ???
1359 </verb></tscreen>
1360
1361 Since all we know about <tt>val</tt> and <tt>fn</tt> is that they
1362 are compatible, the only (useful) thing we can do with them is to
1363 apply <tt>fn</tt> to <tt>val</tt> to get a boolean.  For example:
1364
1365 <tscreen><verb>
1366   f :: Foo -> Bool
1367   f (MkFoo val fn) = fn val
1368 </verb></tscreen>
1369
1370 What this allows us to do is to package heterogenous values
1371 together with a bunch of functions that manipulate them, and then treat
1372 that collection of packages in a uniform manner.  You can express
1373 quite a bit of object-oriented-like programming this way.
1374
1375 <sect2>Why existential?
1376 <label id="existential">
1377 <p>
1378
1379 What has this to do with <em>existential</em> quantification?
1380 Simply that <tt>MkFoo</tt> has the (nearly) isomorphic type
1381
1382 <tscreen><verb>
1383   MkFoo :: (exists a . (a, a -> Bool)) -> Foo
1384 </verb></tscreen>
1385
1386 But Haskell programmers can safely think of the ordinary
1387 <em>universally</em> quantified type given above, thereby avoiding
1388 adding a new existential quantification construct.
1389
1390 <sect2>Type classes
1391 <p>
1392
1393 An easy extension (implemented in <tt>hbc</tt>) is to allow 
1394 arbitrary contexts before the constructor.  For example:
1395
1396 <tscreen><verb>
1397   data Baz = forall a. Eq a => Baz1 a a
1398            | forall b. Show b => Baz2 b (b -> b)
1399 </verb></tscreen>
1400
1401 The two constructors have the types you'd expect:
1402
1403 <tscreen><verb>
1404   Baz1 :: forall a. Eq a => a -> a -> Baz
1405   Baz2 :: forall b. Show b => b -> (b -> b) -> Baz
1406 </verb></tscreen>
1407
1408 But when pattern matching on <tt>Baz1</tt> the matched values can be compared
1409 for equality, and when pattern matching on <tt>Baz2</tt> the first matched
1410 value can be converted to a string (as well as applying the function to it).  
1411 So this program is legal:
1412
1413 <tscreen><verb>
1414   f :: Baz -> String
1415   f (Baz1 p q) | p == q    = "Yes"
1416                | otherwise = "No"
1417   f (Baz1 v fn)            = show (fn v)
1418 </verb></tscreen>
1419
1420 Operationally, in a dictionary-passing implementation, the
1421 constructors <tt>Baz1</tt> and <tt>Baz2</tt> must store the
1422 dictionaries for <tt>Eq</tt> and <tt>Show</tt> respectively, and
1423 extract it on pattern matching.
1424
1425 Notice the way that the syntax fits smoothly with that used for
1426 universal quantification earlier.
1427
1428 <sect2>Restrictions
1429 <p>
1430
1431 There are several restrictions on the ways in which existentially-quantified
1432 constructors can be use.
1433
1434 <itemize>
1435
1436 <item> When pattern matching, each pattern match introduces a new,
1437 distinct, type for each existential type variable.  These types cannot
1438 be unified with any other type, nor can they escape from the scope of
1439 the pattern match.  For example, these fragments are incorrect:
1440
1441 <tscreen><verb>
1442   f1 (MkFoo a f) = a
1443 </verb></tscreen>
1444
1445 Here, the type bound by <tt>MkFoo</tt> "escapes", because <tt>a</tt>
1446 is the result of <tt>f1</tt>.  One way to see why this is wrong is to
1447 ask what type <tt>f1</tt> has:
1448
1449 <tscreen><verb>
1450   f1 :: Foo -> a             -- Weird!
1451 </verb></tscreen>
1452
1453 What is this "<tt>a</tt>" in the result type? Clearly we don't mean
1454 this:
1455
1456 <tscreen><verb>
1457   f1 :: forall a. Foo -> a   -- Wrong!
1458 </verb></tscreen>
1459
1460 The original program is just plain wrong.  Here's another sort of error
1461
1462 <tscreen><verb>
1463   f2 (Baz1 a b) (Baz1 p q) = a==q
1464 </verb></tscreen>
1465
1466 It's ok to say <tt>a==b</tt> or <tt>p==q</tt>, but
1467 <tt>a==q</tt> is wrong because it equates the two distinct types arising
1468 from the two <tt>Baz1</tt> constructors.
1469
1470
1471 <item>You can't pattern-match on an existentially quantified
1472 constructor in a <tt>let</tt> or <tt>where</tt> group of
1473 bindings. So this is illegal:
1474
1475 <tscreen><verb>
1476   f3 x = a==b where { Baz1 a b = x }
1477 </verb></tscreen>
1478
1479 You can only pattern-match
1480 on an existentially-quantified constructor in a <tt>case</tt> expression or
1481 in the patterns of a function definition.
1482
1483 The reason for this restriction is really an implementation one.
1484 Type-checking binding groups is already a nightmare without
1485 existentials complicating the picture.  Also an existential pattern
1486 binding at the top level of a module doesn't make sense, because it's
1487 not clear how to prevent the existentially-quantified type "escaping".
1488 So for now, there's a simple-to-state restriction.  We'll see how
1489 annoying it is.  
1490
1491 <item>You can't use existential quantification for <tt>newtype</tt> 
1492 declarations.  So this is illegal:
1493
1494 <tscreen><verb>
1495   newtype T = forall a. Ord a => MkT a
1496 </verb></tscreen>
1497
1498 Reason: a value of type <tt>T</tt> must be represented as a pair
1499 of a dictionary for <tt>Ord t</tt> and a value of type <tt>t</tt>.
1500 That contradicts the idea that <tt>newtype</tt> should have no 
1501 concrete representation.  You can get just the same efficiency and effect
1502 by using <tt>data</tt> instead of <tt>newtype</tt>.  If there is no
1503 overloading involved, then there is more of a case for allowing
1504 an existentially-quantified <tt>newtype</tt>, because the <tt>data</tt>
1505 because the <tt>data</tt> version does carry an implementation cost,
1506 but single-field existentially quantified constructors aren't much
1507 use.  So the simple restriction (no existential stuff on <tt>newtype</tt>)
1508 stands, unless there are convincing reasons to change it.
1509 </itemize>
1510
1511
1512 <sect1> <idx/Assertions/ 
1513 <label id="sec:assertions">
1514 <p>
1515
1516 If you want to make use of assertions in your standard Haskell code, you
1517 could define a function like the following:
1518
1519 <tscreen><verb>
1520 assert :: Bool -> a -> a
1521 assert False x = error "assertion failed!"
1522 assert _     x = x
1523 </verb></tscreen>
1524
1525 which works, but gives you back a less than useful error message --
1526 an assertion failed, but which and where?
1527
1528 One way out is to define an extended <tt/assert/ function which also
1529 takes a descriptive string to include in the error message and
1530 perhaps combine this with the use of a pre-processor which inserts
1531 the source location where <tt/assert/ was used.
1532
1533 Ghc offers a helping hand here, doing all of this for you. For every
1534 use of <tt/assert/ in the user's source:
1535
1536 <tscreen><verb>
1537 kelvinToC :: Double -> Double
1538 kelvinToC k = assert (k &gt;= 0.0) (k+273.15)
1539 </verb></tscreen>
1540
1541 Ghc will rewrite this to also include the source location where the
1542 assertion was made, 
1543
1544 <tscreen><verb>
1545 assert pred val ==> assertError "Main.hs|15" pred val
1546 </verb></tscreen>
1547
1548 The rewrite is only performed by the compiler when applications of
1549 <tt>Exception.assert</tt> are spotted, so you can still define and use
1550 your own versions of <tt/assert/, should you so wish. If not, import
1551 <tt/Exception/ to make use <tt/assert/ in your code.
1552
1553 To have the compiler ignore uses of assert, use the compiler option
1554 @-fignore-asserts@. <nidx>-fignore-asserts option</nidx> That is,
1555 expressions of the form @assert pred e@ will be rewritten to @e@.
1556
1557 Assertion failures can be caught, see the documentation for the
1558 Hugs/GHC Exception library for information of how.
1559
1560 % -----------------------------------------------------------------------------
1561 <sect1>Scoped Type Variables
1562 <label id="scoped-type-variables">
1563 <p>
1564
1565 A <em/pattern type signature/ can introduce a <em/scoped type
1566 variable/.  For example
1567
1568 <tscreen><verb>
1569 f (xs::[a]) = ys ++ ys
1570            where
1571               ys :: [a]
1572               ys = reverse xs 
1573 </verb></tscreen>
1574
1575 The pattern @(xs::[a])@ includes a type signature for @xs@.
1576 This brings the type variable @a@ into scope; it scopes over
1577 all the patterns and right hand sides for this equation for @f@.
1578 In particular, it is in scope at the type signature for @y@.
1579
1580 At ordinary type signatures, such as that for @ys@, any type variables
1581 mentioned in the type signature <em/that are not in scope/ are
1582 implicitly universally quantified.  (If there are no type variables in
1583 scope, all type variables mentioned in the signature are universally
1584 quantified, which is just as in Haskell 98.)  In this case, since @a@
1585 is in scope, it is not universally quantified, so the type of @ys@ is
1586 the same as that of @xs@.  In Haskell 98 it is not possible to declare
1587 a type for @ys@; a major benefit of scoped type variables is that
1588 it becomes possible to do so.
1589
1590 Scoped type variables are implemented in both GHC and Hugs.  Where the
1591 implementations differ from the specification below, those differences
1592 are noted.
1593
1594 So much for the basic idea.  Here are the details.
1595
1596 <sect2>Scope and implicit quantification
1597 <p>
1598
1599 <itemize>
1600 <item> All the type variables mentioned in the patterns for a single 
1601 function definition equation, that are not already in scope,
1602 are brought into scope by the patterns.  We describe this set as
1603 the <em/type variables bound by the equation/.
1604
1605 <item> The type variables thus brought into scope may be mentioned
1606 in ordinary type signatures or pattern type signatures anywhere within
1607 their scope.
1608
1609 <item> In ordinary type signatures, any type variable mentioned in the
1610 signature that is in scope is <em/not/ universally quantified.
1611
1612 <item> Ordinary type signatures do not bring any new type variables
1613 into scope (except in the type signature itself!). So this is illegal:
1614
1615 <tscreen><verb>
1616   f :: a -> a
1617   f x = x::a
1618 </verb></tscreen>
1619
1620 It's illegal because @a@ is not in scope in the body of @f@,
1621 so the ordinary signature @x::a@ is equivalent to @x::forall a.a@;
1622 and that is an incorrect typing.
1623
1624 <item> There is no implicit universal quantification on pattern type
1625 signatures, nor may one write an explicit @forall@ type in a pattern
1626 type signature.  The pattern type signature is a monotype.
1627
1628 <item> 
1629 The type variables in the head of a @class@ or @instance@ declaration
1630 scope over the methods defined in the @where@ part.  For example:
1631
1632 <tscreen><verb>
1633   class C a where
1634     op :: [a] -> a
1635
1636     op xs = let ys::[a]
1637                 ys = reverse xs
1638             in
1639             head ys
1640 </verb></tscreen>
1641
1642 (Not implemented in Hugs yet, Dec 98).
1643 </itemize>
1644
1645 <sect2>Polymorphism
1646 <p>
1647
1648 <itemize>
1649 <item> Pattern type signatures are completely orthogonal to ordinary, separate
1650 type signatures.  The two can be used independently or together.  There is
1651 no scoping associated with the names of the type variables in a separate type signature.
1652
1653 <tscreen><verb>
1654    f :: [a] -> [a]
1655    f (xs::[b]) = reverse xs
1656 </verb></tscreen>
1657
1658 <item> The function must be polymorphic in the type variables
1659 bound by all its equations.  Operationally, the type variables bound
1660 by one equation must not:
1661
1662 <itemize>
1663 <item> Be unified with a type (such as @Int@, or @[a]@).
1664 <item> Be unified with a type variable free in the environment.
1665 <item> Be unified with each other.  (They may unify with the type variables 
1666 bound by another equation for the same function, of course.)
1667 </itemize>
1668
1669 For example, the following all fail to type check:
1670
1671 <tscreen><verb>
1672   f (x::a) (y::b) = [x,y]       -- a unifies with b
1673
1674   g (x::a) = x + 1::Int         -- a unifies with Int
1675
1676   h x = let k (y::a) = [x,y]    -- a is free in the
1677         in k x                  -- environment
1678
1679   k (x::a) True    = ...        -- a unifies with Int
1680   k (x::Int) False = ...
1681
1682   w :: [b] -> [b]
1683   w (x::a) = x                  -- a unifies with [b]
1684 </verb></tscreen>
1685
1686 <item> The pattern-bound type variable may, however, be constrained
1687 by the context of the principal type, thus:
1688
1689 <tscreen><verb>
1690   f (x::a) (y::a) = x+y*2
1691 </verb></tscreen>
1692
1693 gets the inferred type: @forall a. Num a => a -> a -> a@.
1694 </itemize>
1695
1696 <sect2>Result type signatures
1697 <p>
1698
1699 <itemize>
1700 <item> The result type of a function can be given a signature,
1701 thus:
1702
1703 <tscreen><verb>
1704   f (x::a) :: [a] = [x,x,x]
1705 </verb></tscreen>
1706
1707 The final @":: [a]"@ after all the patterns gives a signature to the
1708 result type.  Sometimes this is the only way of naming the type variable
1709 you want:
1710
1711 <tscreen><verb>
1712   f :: Int -> [a] -> [a]
1713   f n :: ([a] -> [a]) = let g (x::a, y::a) = (y,x)
1714                         in \xs -> map g (reverse xs `zip` xs)
1715 </verb></tscreen>
1716
1717 </itemize>
1718
1719 Result type signatures are not yet implemented in Hugs.
1720
1721 <sect2>Pattern signatures on other constructs
1722 <p>
1723
1724 <itemize>
1725 <item> A pattern type signature can be on an arbitrary sub-pattern, not
1726 just on a variable:
1727
1728 <tscreen><verb>
1729   f ((x,y)::(a,b)) = (y,x) :: (b,a)
1730 </verb></tscreen>
1731
1732 <item> Pattern type signatures, including the result part, can be used
1733 in lambda abstractions:
1734
1735 <tscreen><verb>
1736   (\ (x::a, y) :: a -> x)
1737 </verb></tscreen>
1738
1739 Type variables bound by these patterns must be polymorphic in
1740 the sense defined above.
1741 For example:
1742
1743 <tscreen><verb>
1744   f1 (x::c) = f1 x      -- ok
1745   f2 = \(x::c) -> f2 x  -- not ok
1746 </verb></tscreen>
1747
1748 Here, @f1@ is OK, but @f2@ is not, because @c@ gets unified
1749 with a type variable free in the environment, in this
1750 case, the type of @f2@, which is in the environment when
1751 the lambda abstraction is checked.
1752
1753 <item> Pattern type signatures, including the result part, can be used
1754 in @case@ expressions:
1755
1756 <tscreen><verb>
1757   case e of { (x::a, y) :: a -> x } 
1758 </verb></tscreen>
1759
1760 The pattern-bound type variables must, as usual, 
1761 be polymorphic in the following sense: each case alternative,
1762 considered as a lambda abstraction, must be polymorphic.
1763 Thus this is OK:
1764
1765 <tscreen><verb>
1766   case (True,False) of { (x::a, y) -> x }
1767 </verb></tscreen>
1768
1769 Even though the context is that of a pair of booleans, 
1770 the alternative itself is polymorphic.  Of course, it is
1771 also OK to say:
1772
1773 <tscreen><verb>
1774   case (True,False) of { (x::Bool, y) -> x }
1775 </verb></tscreen>
1776
1777 <item>
1778 To avoid ambiguity, the type after the ``@::@'' in a result
1779 pattern signature on a lambda or @case@ must be atomic (i.e. a single
1780 token or a parenthesised type of some sort).  To see why, 
1781 consider how one would parse this:
1782
1783 <tscreen><verb>
1784   \ x :: a -> b -> x
1785 </verb></tscreen>
1786
1787 <item> Pattern type signatures that bind new type variables
1788 may not be used in pattern bindings at all.
1789 So this is illegal:
1790
1791 <tscreen><verb>
1792   f x = let (y, z::a) = x in ...
1793 </verb></tscreen>
1794
1795 But these are OK, because they do not bind fresh type variables:
1796
1797 <tscreen><verb>
1798   f1 x            = let (y, z::Int) = x in ...
1799   f2 (x::(Int,a)) = let (y, z::a)   = x in ...
1800 </verb></tscreen>
1801
1802 However a single variable is considered a degenerate function binding,
1803 rather than a degerate pattern binding, so this is permitted, even
1804 though it binds a type variable:
1805
1806 <tscreen><verb>
1807   f :: (b->b) = \(x::b) -> x
1808 </verb></tscreen>
1809
1810 </itemize>
1811 Such degnerate function bindings do not fall under the monomorphism
1812 restriction.  Thus:
1813
1814 <tscreen><verb>
1815   g :: a -> a -> Bool = \x y. x==y
1816 </verb></tscreen>
1817
1818 Here @g@ has type @forall a. Eq a => a -> a -> Bool@, just as if
1819 @g@ had a separate type signature.  Lacking a type signature, @g@
1820 would get a monomorphic type.
1821
1822 <sect2>Existentials
1823 <p>
1824
1825 <itemize>
1826 <item> Pattern type signatures can bind existential type variables.
1827 For example:
1828
1829 <tscreen><verb>
1830   data T = forall a. MkT [a]
1831
1832   f :: T -> T
1833   f (MkT [t::a]) = MkT t3
1834                  where
1835                    t3::[a] = [t,t,t]
1836 </verb></tscreen>
1837
1838 </itemize>
1839
1840 %-----------------------------------------------------------------------------
1841 <sect1>Pragmas
1842 <label id="pragmas">
1843 <p>
1844
1845 GHC supports several pragmas, or instructions to the compiler placed
1846 in the source code.  Pragmas don't affect the meaning of the program,
1847 but they might affect the efficiency of the generated code.
1848
1849 <sect2>INLINE pragma
1850 <label id="inline-pragma">
1851 <nidx>INLINE pragma</nidx>
1852 <nidx>pragma, INLINE</nidx>
1853 <p>
1854
1855 GHC (with @-O@, as always) tries to inline (or ``unfold'')
1856 functions/values that are ``small enough,'' thus avoiding the call
1857 overhead and possibly exposing other more-wonderful optimisations.
1858
1859 You will probably see these unfoldings (in Core syntax) in your
1860 interface files.
1861
1862 Normally, if GHC decides a function is ``too expensive'' to inline, it
1863 will not do so, nor will it export that unfolding for other modules to
1864 use.
1865
1866 The sledgehammer you can bring to bear is the
1867 @INLINE@<nidx>INLINE pragma</nidx> pragma, used thusly:
1868 <tscreen><verb>
1869 key_function :: Int -> String -> (Bool, Double) 
1870
1871 #ifdef __GLASGOW_HASKELL__
1872 {-# INLINE key_function #-}
1873 #endif
1874 </verb></tscreen>
1875 (You don't need to do the C pre-processor carry-on unless you're going
1876 to stick the code through HBC---it doesn't like @INLINE@ pragmas.)
1877
1878 The major effect of an @INLINE@ pragma is to declare a function's
1879 ``cost'' to be very low.  The normal unfolding machinery will then be
1880 very keen to inline it.
1881
1882 An @INLINE@ pragma for a function can be put anywhere its type
1883 signature could be put.
1884
1885 @INLINE@ pragmas are a particularly good idea for the
1886 @then@/@return@ (or @bind@/@unit@) functions in a monad.
1887 For example, in GHC's own @UniqueSupply@ monad code, we have:
1888 <tscreen><verb>
1889 #ifdef __GLASGOW_HASKELL__
1890 {-# INLINE thenUs #-}
1891 {-# INLINE returnUs #-}
1892 #endif
1893 </verb></tscreen>
1894
1895 <sect2>NOINLINE pragma
1896 <label id="noinline-pragma">
1897 <p>
1898 <nidx>NOINLINE pragma</nidx>
1899 <nidx>pragma, NOINLINE</nidx>
1900
1901 The @NOINLINE@ pragma does exactly what you'd expect: it stops the
1902 named function from being inlined by the compiler.  You shouldn't ever
1903 need to do this, unless you're very cautious about code size.
1904
1905 <sect2>SPECIALIZE pragma
1906 <label id="specialize-pragma">
1907 <p>
1908 <nidx>SPECIALIZE pragma</nidx>
1909 <nidx>pragma, SPECIALIZE</nidx>
1910 <nidx>overloading, death to</nidx>
1911
1912 (UK spelling also accepted.)  For key overloaded functions, you can
1913 create extra versions (NB: more code space) specialised to particular
1914 types.  Thus, if you have an overloaded function:
1915
1916 <tscreen><verb>
1917 hammeredLookup :: Ord key => [(key, value)] -> key -> value
1918 </verb></tscreen>
1919
1920 If it is heavily used on lists with @Widget@ keys, you could
1921 specialise it as follows:
1922 <tscreen><verb>
1923 {-# SPECIALIZE hammeredLookup :: [(Widget, value)] -> Widget -> value #-}
1924 </verb></tscreen>
1925
1926 To get very fancy, you can also specify a named function to use for
1927 the specialised value, by adding @= blah@, as in:
1928 <tscreen><verb>
1929 {-# SPECIALIZE hammeredLookup :: ...as before... = blah #-}
1930 </verb></tscreen>
1931 It's <em>Your Responsibility</em> to make sure that @blah@ really
1932 behaves as a specialised version of @hammeredLookup@!!!
1933
1934 NOTE: the @=blah@ feature isn't implemented in GHC 4.xx.
1935
1936 An example in which the @= blah@ form will Win Big:
1937 <tscreen><verb>
1938 toDouble :: Real a => a -> Double
1939 toDouble = fromRational . toRational
1940
1941 {-# SPECIALIZE toDouble :: Int -> Double = i2d #-}
1942 i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly
1943 </verb></tscreen>
1944 The @i2d@ function is virtually one machine instruction; the
1945 default conversion---via an intermediate @Rational@---is obscenely
1946 expensive by comparison.
1947
1948 By using the US spelling, your @SPECIALIZE@ pragma will work with
1949 HBC, too.  Note that HBC doesn't support the @= blah@ form.
1950
1951 A @SPECIALIZE@ pragma for a function can be put anywhere its type
1952 signature could be put.
1953
1954 <sect2>SPECIALIZE instance pragma
1955 <label id="specialize-instance-pragma">
1956 <p>
1957 <nidx>SPECIALIZE pragma</nidx>
1958 <nidx>overloading, death to</nidx>
1959 Same idea, except for instance declarations.  For example:
1960 <tscreen><verb>
1961 instance (Eq a) => Eq (Foo a) where { ... usual stuff ... }
1962
1963 {-# SPECIALIZE instance Eq (Foo [(Int, Bar)] #-}
1964 </verb></tscreen>
1965 Compatible with HBC, by the way.
1966
1967 <sect2>LINE pragma
1968 <label id="line-pragma">
1969 <p>
1970 <nidx>LINE pragma</nidx>
1971 <nidx>pragma, LINE</nidx>
1972
1973 This pragma is similar to C's @#line@ pragma, and is mainly for use in
1974 automatically generated Haskell code.  It lets you specify the line
1975 number and filename of the original code; for example
1976
1977 <tscreen><verb>
1978 {-# LINE 42 "Foo.vhs" #-}
1979 </verb></tscreen>
1980
1981 if you'd generated the current file from something called @Foo.vhs@
1982 and this line corresponds to line 42 in the original.  GHC will adjust
1983 its error messages to refer to the line/file named in the @LINE@
1984 pragma.
1985