[project @ 1999-03-26 19:50:31 by sof]
[ghc-hetmet.git] / ghc / docs / users_guide / glasgow_exts.vsgml
1
2 % $Id: glasgow_exts.vsgml,v 1.7 1999/03/26 19:50:31 sof 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 <sect2>The partial-application restriction
1202 <p>
1203
1204 There is really only one way in which data structures with polymorphic
1205 components might surprise you: you must not partially apply them.
1206 For example, this is illegal:
1207
1208 <tscreen><verb>
1209         map MkSwizzle [sort, reverse]
1210 </verb></tscreen>
1211
1212 The restriction is this: <em>every subexpression of the program must
1213 have a type that has no for-alls, except that in a function
1214 application (f e1 ... en) the partial applications are not subject to
1215 this rule</em>.  The restriction makes type inference feasible.
1216
1217 In the illegal example, the sub-expression <tt>MkSwizzle</tt> has the
1218 polymorphic type <tt>(Ord b => [b] -> [b]) -> Swizzle</tt> and is not
1219 a sub-expression of an enclosing application.  On the other hand, this
1220 expression is OK:
1221
1222 <tscreen><verb>
1223         map (T1 (\a b -> a)) [1,2,3]
1224 </verb></tscreen>
1225
1226 even though it involves a partial application of <tt>T1</tt>, because
1227 the sub-expression <tt>T1 (\a b -> a)</tt> has type <tt>Int -> T
1228 Int</tt>.
1229
1230 <sect2>Type signatures
1231 <label id="sigs">
1232 <p>
1233
1234 Once you have data constructors with universally-quantified fields, or
1235 constants such as <tt>runST</tt> that have rank-2 types, it isn't long
1236 before you discover that you need more!  Consider:
1237
1238 <tscreen><verb>
1239   mkTs f x y = [T1 f x, T1 f y]
1240 </verb></tscreen>
1241
1242 <tt>mkTs</tt> is a fuction that constructs some values of type
1243 <tt>T</tt>, using some pieces passed to it.  The trouble is that since
1244 <tt>f</tt> is a function argument, Haskell assumes that it is
1245 monomorphic, so we'll get a type error when applying <tt>T1</tt> to
1246 it.  This is a rather silly example, but the problem really bites in
1247 practice.  Lots of people trip over the fact that you can't make
1248 "wrappers functions" for <tt>runST</tt> for exactly the same reason.
1249 In short, it is impossible to build abstractions around functions with
1250 rank-2 types.
1251
1252 The solution is fairly clear.  We provide the ability to give a rank-2
1253 type signature for <em>ordinary</em> functions (not only data
1254 constructors), thus:
1255
1256 <tscreen><verb>
1257   mkTs :: (forall b. b -> b -> b) -> a -> [T a]
1258   mkTs f x y = [T1 f x, T1 f y]
1259 </verb></tscreen>
1260
1261 This type signature tells the compiler to attribute <tt>f</tt> with
1262 the polymorphic type <tt>(forall b. b -> b -> b)</tt> when type
1263 checking the body of <tt>mkTs</tt>, so now the application of
1264 <tt>T1</tt> is fine.
1265
1266 There are two restrictions:
1267
1268 <itemize>
1269 <item> You can only define a rank 2 type, specified by the following
1270 grammar:
1271
1272 <tscreen><verb>
1273    rank2type ::= [forall tyvars .] [context =>] funty
1274    funty     ::= ([forall tyvars .] [context =>] ty) -> funty
1275                | ty
1276    ty        ::= ...current Haskell monotype syntax...
1277 </verb></tscreen>
1278
1279 Informally, the universal quantification must all be right at the beginning, 
1280 or at the top level of a function argument.
1281
1282 <item> There is a restriction on the definition of a function whose
1283 type signature is a rank-2 type: the polymorphic arguments must be
1284 matched on the left hand side of the "<tt>=</tt>" sign.  You can't
1285 define <tt>mkTs</tt> like this:
1286
1287 <tscreen><verb>
1288   mkTs :: (forall b. b -> b -> b) -> a -> [T a]
1289   mkTs = \ f x y -> [T1 f x, T1 f y]
1290 </verb></tscreen>
1291
1292
1293 The same partial-application rule applies to ordinary functions with
1294 rank-2 types as applied to data constructors.  
1295
1296 </itemize>
1297
1298 % -----------------------------------------------------------------------------
1299 <sect1>Existentially quantified data constructors
1300 <label id="existential-quantification">
1301 <p>
1302
1303 The idea of using existential quantification in data type declarations
1304 was suggested by Laufer (I believe, thought doubtless someone will
1305 correct me), and implemented in Hope+. It's been in Lennart
1306 Augustsson's <tt>hbc</tt> Haskell compiler for several years, and
1307 proved very useful.  Here's the idea.  Consider the declaration:
1308
1309 <tscreen><verb>
1310   data Foo = forall a. MkFoo a (a -> Bool)
1311            | Nil
1312 </verb></tscreen>
1313
1314 The data type <tt>Foo</tt> has two constructors with types:
1315
1316 <tscreen><verb>
1317   MkFoo :: forall a. a -> (a -> Bool) -> Foo
1318   Nil   :: Foo
1319 </verb></tscreen>
1320
1321 Notice that the type variable <tt>a</tt> in the type of <tt>MkFoo</tt>
1322 does not appear in the data type itself, which is plain <tt>Foo</tt>.
1323 For example, the following expression is fine:
1324
1325 <tscreen><verb>
1326   [MkFoo 3 even, MkFoo 'c' isUpper] :: [Foo]
1327 </verb></tscreen>
1328
1329 Here, <tt>(MkFoo 3 even)</tt> packages an integer with a function
1330 <tt>even</tt> that maps an integer to <tt>Bool</tt>; and <tt>MkFoo 'c'
1331 isUpper</tt> packages a character with a compatible function.  These
1332 two things are each of type <tt>Foo</tt> and can be put in a list.
1333
1334 What can we do with a value of type <tt>Foo</tt>?.  In particular,
1335 what happens when we pattern-match on <tt>MkFoo</tt>?
1336
1337 <tscreen><verb>
1338   f (MkFoo val fn) = ???
1339 </verb></tscreen>
1340
1341 Since all we know about <tt>val</tt> and <tt>fn</tt> is that they
1342 are compatible, the only (useful) thing we can do with them is to
1343 apply <tt>fn</tt> to <tt>val</tt> to get a boolean.  For example:
1344
1345 <tscreen><verb>
1346   f :: Foo -> Bool
1347   f (MkFoo val fn) = fn val
1348 </verb></tscreen>
1349
1350 What this allows us to do is to package heterogenous values
1351 together with a bunch of functions that manipulate them, and then treat
1352 that collection of packages in a uniform manner.  You can express
1353 quite a bit of object-oriented-like programming this way.
1354
1355 <sect2>Why existential?
1356 <label id="existential">
1357 <p>
1358
1359 What has this to do with <em>existential</em> quantification?
1360 Simply that <tt>MkFoo</tt> has the (nearly) isomorphic type
1361
1362 <tscreen><verb>
1363   MkFoo :: (exists a . (a, a -> Bool)) -> Foo
1364 </verb></tscreen>
1365
1366 But Haskell programmers can safely think of the ordinary
1367 <em>universally</em> quantified type given above, thereby avoiding
1368 adding a new existential quantification construct.
1369
1370 <sect2>Type classes
1371 <p>
1372
1373 An easy extension (implemented in <tt>hbc</tt>) is to allow 
1374 arbitrary contexts before the constructor.  For example:
1375
1376 <tscreen><verb>
1377   data Baz = forall a. Eq a => Baz1 a a
1378            | forall b. Show b => Baz2 b (b -> b)
1379 </verb></tscreen>
1380
1381 The two constructors have the types you'd expect:
1382
1383 <tscreen><verb>
1384   Baz1 :: forall a. Eq a => a -> a -> Baz
1385   Baz2 :: forall b. Show b => b -> (b -> b) -> Baz
1386 </verb></tscreen>
1387
1388 But when pattern matching on <tt>Baz1</tt> the matched values can be compared
1389 for equality, and when pattern matching on <tt>Baz2</tt> the first matched
1390 value can be converted to a string (as well as applying the function to it).  
1391 So this program is legal:
1392
1393 <tscreen><verb>
1394   f :: Baz -> String
1395   f (Baz1 p q) | p == q    = "Yes"
1396                | otherwise = "No"
1397   f (Baz1 v fn)            = show (fn v)
1398 </verb></tscreen>
1399
1400 Operationally, in a dictionary-passing implementation, the
1401 constructors <tt>Baz1</tt> and <tt>Baz2</tt> must store the
1402 dictionaries for <tt>Eq</tt> and <tt>Show</tt> respectively, and
1403 extract it on pattern matching.
1404
1405 Notice the way that the syntax fits smoothly with that used for
1406 universal quantification earlier.
1407
1408 <sect2>Restrictions
1409 <p>
1410
1411 There are several restrictions on the ways in which existentially-quantified
1412 constructors can be use.
1413
1414 <itemize>
1415
1416 <item> When pattern matching, each pattern match introduces a new,
1417 distinct, type for each existential type variable.  These types cannot
1418 be unified with any other type, nor can they escape from the scope of
1419 the pattern match.  For example, these fragments are incorrect:
1420
1421 <tscreen><verb>
1422   f1 (MkFoo a f) = a
1423 </verb></tscreen>
1424
1425 Here, the type bound by <tt>MkFoo</tt> "escapes", because <tt>a</tt>
1426 is the result of <tt>f1</tt>.  One way to see why this is wrong is to
1427 ask what type <tt>f1</tt> has:
1428
1429 <tscreen><verb>
1430   f1 :: Foo -> a             -- Weird!
1431 </verb></tscreen>
1432
1433 What is this "<tt>a</tt>" in the result type? Clearly we don't mean
1434 this:
1435
1436 <tscreen><verb>
1437   f1 :: forall a. Foo -> a   -- Wrong!
1438 </verb></tscreen>
1439
1440 The original program is just plain wrong.  Here's another sort of error
1441
1442 <tscreen><verb>
1443   f2 (Baz1 a b) (Baz1 p q) = a==q
1444 </verb></tscreen>
1445
1446 It's ok to say <tt>a==b</tt> or <tt>p==q</tt>, but
1447 <tt>a==q</tt> is wrong because it equates the two distinct types arising
1448 from the two <tt>Baz1</tt> constructors.
1449
1450
1451 <item>You can't pattern-match on an existentially quantified
1452 constructor in a <tt>let</tt> or <tt>where</tt> group of
1453 bindings. So this is illegal:
1454
1455 <tscreen><verb>
1456   f3 x = a==b where { Baz1 a b = x }
1457 </verb></tscreen>
1458
1459 You can only pattern-match
1460 on an existentially-quantified constructor in a <tt>case</tt> expression or
1461 in the patterns of a function definition.
1462
1463 The reason for this restriction is really an implementation one.
1464 Type-checking binding groups is already a nightmare without
1465 existentials complicating the picture.  Also an existential pattern
1466 binding at the top level of a module doesn't make sense, because it's
1467 not clear how to prevent the existentially-quantified type "escaping".
1468 So for now, there's a simple-to-state restriction.  We'll see how
1469 annoying it is.  
1470
1471 <item>You can't use existential quantification for <tt>newtype</tt> 
1472 declarations.  So this is illegal:
1473
1474 <tscreen><verb>
1475   newtype T = forall a. Ord a => MkT a
1476 </verb></tscreen>
1477
1478 Reason: a value of type <tt>T</tt> must be represented as a pair
1479 of a dictionary for <tt>Ord t</tt> and a value of type <tt>t</tt>.
1480 That contradicts the idea that <tt>newtype</tt> should have no 
1481 concrete representation.  You can get just the same efficiency and effect
1482 by using <tt>data</tt> instead of <tt>newtype</tt>.  If there is no
1483 overloading involved, then there is more of a case for allowing
1484 an existentially-quantified <tt>newtype</tt>, because the <tt>data</tt>
1485 because the <tt>data</tt> version does carry an implementation cost,
1486 but single-field existentially quantified constructors aren't much
1487 use.  So the simple restriction (no existential stuff on <tt>newtype</tt>)
1488 stands, unless there are convincing reasons to change it.
1489 </itemize>
1490
1491
1492 <sect1> <idx/Assertions/ 
1493 <label id="sec:assertions">
1494 <p>
1495
1496 If you want to use assertions in your standard Haskell code, you
1497 could define something like the following:
1498
1499 <tscreen><verb>
1500 assert :: Bool -> a -> a
1501 assert False x = error "assertion failed!"
1502 assert _     x = x
1503 </verb></tscreen>
1504
1505 which works, but gives you back a less than useful error message --
1506 an assertion failed, but which?
1507
1508 One way out is to define an extended <tt/assert/ function which also
1509 takes a descriptive string to include in the error message and
1510 perhaps combine this with the use of a pre-processor which inserts
1511 the source location where <tt/assert/ was used.
1512
1513 Ghc offers a helping hand here, doing all of this for you. For every
1514 use of <tt/assert/ in the user's source:
1515
1516 <tscreen><verb>
1517 kelvinToC :: Double -> Double
1518 kelvinToC k = assert (k &gt;= 0.0) (k+273.15)
1519 </verb></tscreen>
1520
1521 Ghc will rewrite this to also include the source location where the
1522 assertion was made, 
1523
1524 <tscreen><verb>
1525 assert pred val ==> assertError "Main.hs,15" pred val
1526 </verb></tscreen>
1527
1528 The rewrite is only performed by the compiler when applications of
1529 <tt>Exception.assert</tt> are spotted, so you can still define and use
1530 your own versions of <tt/assert/, should you so wish. If not, import
1531 <tt/Exception/ to use <tt/assert/ in your code.
1532
1533 Assertion failures can be caught, see the documentation for the
1534 Hugs/GHC Exception library for information of how.
1535
1536 % -----------------------------------------------------------------------------
1537 <sect1>Scoped Type Variables
1538 <label id="scoped-type-variables">
1539 <p>
1540
1541 A <em/pattern type signature/ can introduce a <em/scoped type
1542 variable/.  For example
1543
1544 <tscreen><verb>
1545 f (xs::[a]) = ys ++ ys
1546            where
1547               ys :: [a]
1548               ys = reverse xs 
1549 </verb></tscreen>
1550
1551 The pattern @(xs::[a])@ includes a type signature for @xs@.
1552 This brings the type variable @a@ into scope; it scopes over
1553 all the patterns and right hand sides for this equation for @f@.
1554 In particular, it is in scope at the type signature for @y@.
1555
1556 At ordinary type signatures, such as that for @ys@, any type variables
1557 mentioned in the type signature <em/that are not in scope/ are
1558 implicitly universally quantified.  (If there are no type variables in
1559 scope, all type variables mentioned in the signature are universally
1560 quantified, which is just as in Haskell 98.)  In this case, since @a@
1561 is in scope, it is not universally quantified, so the type of @ys@ is
1562 the same as that of @xs@.  In Haskell 98 it is not possible to declare
1563 a type for @ys@; a major benefit of scoped type variables is that
1564 it becomes possible to do so.
1565
1566 Scoped type variables are implemented in both GHC and Hugs.  Where the
1567 implementations differ from the specification below, those differences
1568 are noted.
1569
1570 So much for the basic idea.  Here are the details.
1571
1572 <sect2>Scope and implicit quantification
1573 <p>
1574
1575 <itemize>
1576 <item> All the type variables mentioned in the patterns for a single 
1577 function definition equation, that are not already in scope,
1578 are brought into scope by the patterns.  We describe this set as
1579 the <em/type variables bound by the equation/.
1580
1581 <item> The type variables thus brought into scope may be mentioned
1582 in ordinary type signatures or pattern type signatures anywhere within
1583 their scope.
1584
1585 <item> In ordinary type signatures, any type variable mentioned in the
1586 signature that is in scope is <em/not/ universally quantified.
1587
1588 <item> Ordinary type signatures do not bring any new type variables
1589 into scope (except in the type signature itself!). So this is illegal:
1590
1591 <tscreen><verb>
1592   f :: a -> a
1593   f x = x::a
1594 </verb></tscreen>
1595
1596 It's illegal because @a@ is not in scope in the body of @f@,
1597 so the ordinary signature @x::a@ is equivalent to @x::forall a.a@;
1598 and that is an incorrect typing.
1599
1600 <item> There is no implicit universal quantification on pattern type
1601 signatures, nor may one write an explicit @forall@ type in a pattern
1602 type signature.  The pattern type signature is a monotype.
1603
1604 <item> 
1605 The type variables in the head of a @class@ or @instance@ declaration
1606 scope over the methods defined in the @where@ part.  For example:
1607
1608 <tscreen><verb>
1609   class C a where
1610     op :: [a] -> a
1611
1612     op xs = let ys::[a]
1613                 ys = reverse xs
1614             in
1615             head ys
1616 </verb></tscreen>
1617
1618 (Not implemented in Hugs yet, Dec 98).
1619 </itemize>
1620
1621 <sect2>Polymorphism
1622 <p>
1623
1624 <itemize>
1625 <item> Pattern type signatures are completely orthogonal to ordinary, separate
1626 type signatures.  The two can be used independently or together.  There is
1627 no scoping associated with the names of the type variables in a separate type signature.
1628
1629 <tscreen><verb>
1630    f :: [a] -> [a]
1631    f (xs::[b]) = reverse xs
1632 </verb></tscreen>
1633
1634 <item> The function must be polymorphic in the type variables
1635 bound by all its equations.  Operationally, the type variables bound
1636 by one equation must not:
1637
1638 <itemize>
1639 <item> Be unified with a type (such as @Int@, or @[a]@).
1640 <item> Be unified with a type variable free in the environment.
1641 <item> Be unified with each other.  (They may unify with the type variables 
1642 bound by another equation for the same function, of course.)
1643 </itemize>
1644
1645 For example, the following all fail to type check:
1646
1647 <tscreen><verb>
1648   f (x::a) (y::b) = [x,y]       -- a unifies with b
1649
1650   g (x::a) = x + 1::Int         -- a unifies with Int
1651
1652   h x = let k (y::a) = [x,y]    -- a is free in the
1653         in k x                  -- environment
1654
1655   k (x::a) True    = ...        -- a unifies with Int
1656   k (x::Int) False = ...
1657
1658   w :: [b] -> [b]
1659   w (x::a) = x                  -- a unifies with [b]
1660 </verb></tscreen>
1661
1662 <item> The pattern-bound type variable may, however, be constrained
1663 by the context of the principal type, thus:
1664
1665 <tscreen><verb>
1666   f (x::a) (y::a) = x+y*2
1667 </verb></tscreen>
1668
1669 gets the inferred type: @forall a. Num a => a -> a -> a@.
1670 </itemize>
1671
1672 <sect2>Result type signatures
1673 <p>
1674
1675 <itemize>
1676 <item> The result type of a function can be given a signature,
1677 thus:
1678
1679 <tscreen><verb>
1680   f (x::a) :: [a] = [x,x,x]
1681 </verb></tscreen>
1682
1683 The final @":: [a]"@ after all the patterns gives a signature to the
1684 result type.  Sometimes this is the only way of naming the type variable
1685 you want:
1686
1687 <tscreen><verb>
1688   f :: Int -> [a] -> [a]
1689   f n :: ([a] -> [a]) = let g (x::a, y::a) = (y,x)
1690                         in \xs -> map g (reverse xs `zip` xs)
1691 </verb></tscreen>
1692
1693 </itemize>
1694
1695 Result type signatures are not yet implemented in Hugs.
1696
1697 <sect2>Pattern signatures on other constructs
1698 <p>
1699
1700 <itemize>
1701 <item> A pattern type signature can be on an arbitrary sub-pattern, not
1702 just on a variable:
1703
1704 <tscreen><verb>
1705   f ((x,y)::(a,b)) = (y,x) :: (b,a)
1706 </verb></tscreen>
1707
1708 <item> Pattern type signatures, including the result part, can be used
1709 in lambda abstractions:
1710
1711 <tscreen><verb>
1712   (\ (x::a, y) :: a -> x)
1713 </verb></tscreen>
1714
1715 Type variables bound by these patterns must be polymorphic in
1716 the sense defined above.
1717 For example:
1718
1719 <tscreen><verb>
1720   f1 (x::c) = f1 x      -- ok
1721   f2 = \(x::c) -> f2 x  -- not ok
1722 </verb></tscreen>
1723
1724 Here, @f1@ is OK, but @f2@ is not, because @c@ gets unified
1725 with a type variable free in the environment, in this
1726 case, the type of @f2@, which is in the environment when
1727 the lambda abstraction is checked.
1728
1729 <item> Pattern type signatures, including the result part, can be used
1730 in @case@ expressions:
1731
1732 <tscreen><verb>
1733   case e of { (x::a, y) :: a -> x } 
1734 </verb></tscreen>
1735
1736 The pattern-bound type variables must, as usual, 
1737 be polymorphic in the following sense: each case alternative,
1738 considered as a lambda abstraction, must be polymorphic.
1739 Thus this is OK:
1740
1741 <tscreen><verb>
1742   case (True,False) of { (x::a, y) -> x }
1743 </verb></tscreen>
1744
1745 Even though the context is that of a pair of booleans, 
1746 the alternative itself is polymorphic.  Of course, it is
1747 also OK to say:
1748
1749 <tscreen><verb>
1750   case (True,False) of { (x::Bool, y) -> x }
1751 </verb></tscreen>
1752
1753 <item>
1754 To avoid ambiguity, the type after the ``@::@'' in a result
1755 pattern signature on a lambda or @case@ must be atomic (i.e. a single
1756 token or a parenthesised type of some sort).  To see why, 
1757 consider how one would parse this:
1758
1759 <tscreen><verb>
1760   \ x :: a -> b -> x
1761 </verb></tscreen>
1762
1763 <item> Pattern type signatures that bind new type variables
1764 may not be used in pattern bindings at all.
1765 So this is illegal:
1766
1767 <tscreen><verb>
1768   f x = let (y, z::a) = x in ...
1769 </verb></tscreen>
1770
1771 But these are OK, because they do not bind fresh type variables:
1772
1773 <tscreen><verb>
1774   f1 x            = let (y, z::Int) = x in ...
1775   f2 (x::(Int,a)) = let (y, z::a)   = x in ...
1776 </verb></tscreen>
1777
1778 However a single variable is considered a degenerate function binding,
1779 rather than a degerate pattern binding, so this is permitted, even
1780 though it binds a type variable:
1781
1782 <tscreen><verb>
1783   f :: (b->b) = \(x::b) -> x
1784 </verb></tscreen>
1785
1786 </itemize>
1787 Such degnerate function bindings do not fall under the monomorphism
1788 restriction.  Thus:
1789
1790 <tscreen><verb>
1791   g :: a -> a -> Bool = \x y. x==y
1792 </verb></tscreen>
1793
1794 Here @g@ has type @forall a. Eq a => a -> a -> Bool@, just as if
1795 @g@ had a separate type signature.  Lacking a type signature, @g@
1796 would get a monomorphic type.
1797
1798 <sect2>Existentials
1799 <p>
1800
1801 <itemize>
1802 <item> Pattern type signatures can bind existential type variables.
1803 For example:
1804
1805 <tscreen><verb>
1806   data T = forall a. MkT [a]
1807
1808   f :: T -> T
1809   f (MkT [t::a]) = MkT t3
1810                  where
1811                    t3::[a] = [t,t,t]
1812 </verb></tscreen>
1813
1814 </itemize>
1815
1816 %-----------------------------------------------------------------------------
1817 <sect1>Pragmas
1818 <label id="pragmas">
1819 <p>
1820
1821 GHC supports several pragmas, or instructions to the compiler placed
1822 in the source code.  Pragmas don't affect the meaning of the program,
1823 but they might affect the efficiency of the generated code.
1824
1825 <sect2>INLINE pragma
1826 <label id="inline-pragma">
1827 <nidx>INLINE pragma</nidx>
1828 <nidx>pragma, INLINE</nidx>
1829 <p>
1830
1831 GHC (with @-O@, as always) tries to inline (or ``unfold'')
1832 functions/values that are ``small enough,'' thus avoiding the call
1833 overhead and possibly exposing other more-wonderful optimisations.
1834
1835 You will probably see these unfoldings (in Core syntax) in your
1836 interface files.
1837
1838 Normally, if GHC decides a function is ``too expensive'' to inline, it
1839 will not do so, nor will it export that unfolding for other modules to
1840 use.
1841
1842 The sledgehammer you can bring to bear is the
1843 @INLINE@<nidx>INLINE pragma</nidx> pragma, used thusly:
1844 <tscreen><verb>
1845 key_function :: Int -> String -> (Bool, Double) 
1846
1847 #ifdef __GLASGOW_HASKELL__
1848 {-# INLINE key_function #-}
1849 #endif
1850 </verb></tscreen>
1851 (You don't need to do the C pre-processor carry-on unless you're going
1852 to stick the code through HBC---it doesn't like @INLINE@ pragmas.)
1853
1854 The major effect of an @INLINE@ pragma is to declare a function's
1855 ``cost'' to be very low.  The normal unfolding machinery will then be
1856 very keen to inline it.
1857
1858 An @INLINE@ pragma for a function can be put anywhere its type
1859 signature could be put.
1860
1861 @INLINE@ pragmas are a particularly good idea for the
1862 @then@/@return@ (or @bind@/@unit@) functions in a monad.
1863 For example, in GHC's own @UniqueSupply@ monad code, we have:
1864 <tscreen><verb>
1865 #ifdef __GLASGOW_HASKELL__
1866 {-# INLINE thenUs #-}
1867 {-# INLINE returnUs #-}
1868 #endif
1869 </verb></tscreen>
1870
1871 <sect2>NOINLINE pragma
1872 <label id="noinline-pragma">
1873 <p>
1874 <nidx>NOINLINE pragma</nidx>
1875 <nidx>pragma, NOINLINE</nidx>
1876
1877 The @NOINLINE@ pragma does exactly what you'd expect: it stops the
1878 named function from being inlined by the compiler.  You shouldn't ever
1879 need to do this, unless you're very cautious about code size.
1880
1881 <sect2>SPECIALIZE pragma
1882 <label id="specialize-pragma">
1883 <p>
1884 <nidx>SPECIALIZE pragma</nidx>
1885 <nidx>pragma, SPECIALIZE</nidx>
1886 <nidx>overloading, death to</nidx>
1887
1888 (UK spelling also accepted.)  For key overloaded functions, you can
1889 create extra versions (NB: more code space) specialised to particular
1890 types.  Thus, if you have an overloaded function:
1891
1892 <tscreen><verb>
1893 hammeredLookup :: Ord key => [(key, value)] -> key -> value
1894 </verb></tscreen>
1895
1896 If it is heavily used on lists with @Widget@ keys, you could
1897 specialise it as follows:
1898 <tscreen><verb>
1899 {-# SPECIALIZE hammeredLookup :: [(Widget, value)] -> Widget -> value #-}
1900 </verb></tscreen>
1901
1902 To get very fancy, you can also specify a named function to use for
1903 the specialised value, by adding @= blah@, as in:
1904 <tscreen><verb>
1905 {-# SPECIALIZE hammeredLookup :: ...as before... = blah #-}
1906 </verb></tscreen>
1907 It's <em>Your Responsibility</em> to make sure that @blah@ really
1908 behaves as a specialised version of @hammeredLookup@!!!
1909
1910 NOTE: the @=blah@ feature isn't implemented in GHC 4.xx.
1911
1912 An example in which the @= blah@ form will Win Big:
1913 <tscreen><verb>
1914 toDouble :: Real a => a -> Double
1915 toDouble = fromRational . toRational
1916
1917 {-# SPECIALIZE toDouble :: Int -> Double = i2d #-}
1918 i2d (I# i) = D# (int2Double# i) -- uses Glasgow prim-op directly
1919 </verb></tscreen>
1920 The @i2d@ function is virtually one machine instruction; the
1921 default conversion---via an intermediate @Rational@---is obscenely
1922 expensive by comparison.
1923
1924 By using the US spelling, your @SPECIALIZE@ pragma will work with
1925 HBC, too.  Note that HBC doesn't support the @= blah@ form.
1926
1927 A @SPECIALIZE@ pragma for a function can be put anywhere its type
1928 signature could be put.
1929
1930 <sect2>SPECIALIZE instance pragma
1931 <label id="specialize-instance-pragma">
1932 <p>
1933 <nidx>SPECIALIZE pragma</nidx>
1934 <nidx>overloading, death to</nidx>
1935 Same idea, except for instance declarations.  For example:
1936 <tscreen><verb>
1937 instance (Eq a) => Eq (Foo a) where { ... usual stuff ... }
1938
1939 {-# SPECIALIZE instance Eq (Foo [(Int, Bar)] #-}
1940 </verb></tscreen>
1941 Compatible with HBC, by the way.
1942
1943 <sect2>LINE pragma
1944 <label id="line-pragma">
1945 <p>
1946 <nidx>LINE pragma</nidx>
1947 <nidx>pragma, LINE</nidx>
1948
1949 This pragma is similar to C's @#line@ pragma, and is mainly for use in
1950 automatically generated Haskell code.  It lets you specify the line
1951 number and filename of the original code; for example
1952
1953 <tscreen><verb>
1954 {-# LINE 42 "Foo.vhs" #-}
1955 </verb></tscreen>
1956
1957 if you'd generated the current file from something called @Foo.vhs@
1958 and this line corresponds to line 42 in the original.  GHC will adjust
1959 its error messages to refer to the line/file named in the @LINE@
1960 pragma.
1961