569727d2cd0fb22ef3b8ed8f0034bdb6b3c2786c
[ghc-hetmet.git] / ghc / docs / users_guide / libraries.lit
1 \begin{onlystandalone}
2 \documentstyle[a4wide,grasp]{article}
3 \begin{rawlatex}
4 \renewcommand{\textfraction}{0.1}
5 \renewcommand{\floatpagefraction}{0.9}
6 \renewcommand{\dblfloatpagefraction}{0.9}
7
8 \sloppy
9 \renewcommand{\today}{November 1997}
10 \end{rawlatex}
11
12 \begin{document}
13 \title{The GHC Prelude and Libraries}
14 \author{Simon L Peyton Jones \and Simon Marlow \and Will Partain}
15
16 \maketitle
17 \begin{rawlatex}
18 \tableofcontents
19 \end{rawlatex}
20 \end{onlystandalone}
21
22 \section[ghc-prelude]{The GHC prelude and libraries}
23
24 This document describes GHC's prelude and libraries.  The basic story is that of
25 the Haskell 1.4 Report and Libraries document (which we do not reproduce here),
26 but this document describes in addition:
27 \begin{itemize}
28 \item   GHC's additional non-standard libraries and types, such as
29         state transformers, packed strings, foreign objects, stable
30         pointers, and so on.
31
32 \item   GHC's primitive types and operations.  The standard Haskell
33         functions are implemented on top of these, and it is sometimes
34         useful to use them directly.
35
36 \item   The organisation of these libraries into directories.
37 \item   Short description of programmer interface to the non-standard,
38         `built-in' libraries provided in addition to the standard
39         prelude and libraries.
40 \end{itemize}
41
42 In addition to the GHC prelude libraries, GHC comes with a number of
43 system libraries, which are presented in Section \ref{syslibs}.
44
45 \subsection{Prelude library organisation}
46
47 {\em Probably only of interest to implementors...}
48
49 The prelude libraries are organised into the following three groups,
50 each of which is kept in a separate sub-directory of GHC's source
51 @lib/@ directory: 
52 \begin{description}
53 \item[@lib/required/@]  These are the libraries {\em required} by the
54 Haskell definition.  All are defined by the Haskell Report, or by the
55 Haskell Libraries Report. 
56 They currently comprise:
57 \begin{itemize}
58 \item @Array@: monolithic arrays.
59 \item @Char@: more functions on characters.
60 \item @Complex@: interface defining complex number type and functions
61 over it.
62 \item @CPUTime@: get the CPU time used by the program.
63 \item @Directory@: basic functions for accessing the file system.
64 \item @Ix@: the @Ix@ class of indexing operations.
65 \item @IO@: additional input/output functions.
66 \item @List@: more functions on lists.
67 \item @Locale@: localisation functions.
68 \item @Maybe@: more functions on @Maybe@ types.
69 \item @Monad@: functions on monads.
70 \item @Numeric@: operations for reading and showing number values.
71 \item @Prelude@: standard prelude interface.
72 \item @Random@: pseudo-random number generator.
73 \item @Ratio@: functions on rational numbers.
74 \item @System@: basic operating-system interface functions.
75 \item @Time@: operations on time.
76 \end{itemize}
77
78 \item[@lib/glaExts@]  Extension libraries, currently comprising:
79 \begin{itemize}
80 \item @Addr@: primitive pointer type.
81 \item @Bits@: a class of bitwise operations.
82 \item @ByteArray@: operations over immutable chunks of (heap allocated) bytes.
83 \item @CCall@: classes @CCallable@ and @CReturnable@ for calling C.
84 \item @Foreign@: types and operations for GHC's foreign-language
85 interface.
86 \item @GlaExts@: interface for extensions that are only implemented in
87 GHC: namely unboxed types and primitive operations.
88 \item @IOExts@: extensions to the @IO@ library.
89 \item @Int@: 8, 16, 32 and 64-bit integers with bit operations.
90 \item @LazyST@: a lazy version of the @ST@ monad.
91 \item @MutableArray@: operations over mutable arrays.
92 \item @ST@: the state transformer monad, @STRef@s and @STArray@s.
93 \item @Word@: 8, 16, 32 and 64-bit naturals with bit operations.
94 \end{itemize}
95
96 \item[@lib/concurrent@] GHC extension libraries to support Concurrent Haskell, currently comprising:
97 \begin{itemize}
98 \item @Concurrent@: main library.
99 \item @Parallel@: stuff for multi-processor parallelism.
100 \item @Channel@
101 \item @ChannelVar@
102 \item @Merge@
103 \item @SampleVar@
104 \item @Semaphore@
105 \end{itemize}
106
107 \item[@lib/ghc@] These libraries are the pieces on which all the
108 others are built.  They aren't typically imported by Joe Programmer,
109 but there's nothing to stop you doing so if you want.  In general, the
110 modules prefixed by @Prel@ are pieces that go towards building
111 @Prelude@.
112
113 \begin{itemize}
114 \item @GHC@: this ``library'' brings into scope all the primitive
115 types and operations, such as @Int#@, @+#@, @encodeFloat#@, etc etc.
116 It is unique in that there is no Haskell source code for it.  Details
117 in Section \ref{sect:ghc}.
118
119 \item @PrelBase@: defines the basic types and classes without which
120 very few Haskell programs can work.  The classes are: @Eq@, @Ord@,
121 @Enum@, @Bounded@, @Num@, @Show@, @Eval@, @Monad@, @MonadZero@,
122 @MonadPlus@.  The types are: list, @Bool@, @Char@, @Ordering@,
123 @String@, @Int@, @Integer@.
124
125 \item @PrelMaybe@: defines the @Maybe@ type.
126
127 \item @PrelEither@: defines the @Either@ type.
128
129 \item @PrelTup@: defines tuples and their instances.
130
131 \item @PrelList@: defines most of the list operations required by
132 @Prelude@.  (A few are in @PrelBase@, to avoid gratuitous mutual
133 recursion between modules.)
134
135 \item @PrelNum@ defines: the numeric classes beyond @Num@, namely
136 @Real@, @Integral@, @Fractional@, @Floating@, @RealFrac@, @RealFloat@;
137 instances for appropriate classes for @Int@ and @Integer@; the types
138 @Float@, @Double@, and @Ratio@ and their instances.
139
140 \item @PrelRead@: the @Read@ class and all its instances.  It's kept
141 separate because many programs don't use @Read@ at all, so we don't
142 even want to link in its code. (If the prelude libraries are built by
143 splitting the object files, this is all a non-issue)
144
145 \item @ConcBase@: substrate stuff for Concurrent Haskell.
146
147 \item @IOBase@: substrate stuff for the main I/O libraries.
148 \item @IOHandle@: large blob of code for doing I/O on handles.
149 \item @PrelIO@: the remaining small pieces to produce the I/O stuff
150 needed by @Prelude@.
151
152 \item @STBase@: substrate stuff for @ST@.
153 \item @ArrBase@: substrate stuff for @Array@.
154
155 \item @GHCerr@: error reporting code, called from code that the
156 compiler plants in compiled programs.
157
158 \item @GHCmain@: the definition of @mainIO@, which is what {\em
159         really} gets called by the runtime system.  @mainIO@ in turn
160         calls @main@.
161
162 \item @PackBase@: low-level packing/unpacking operations.
163
164 \item @Error@: the definition of @error@, placed in its own module
165 with a hand-written @.hi-boot@ file in order to break recursive
166 dependencies in the libraries (everything needs @error@, but the
167 definition of @error@ itself needs a few things...).
168 \end{itemize}
169 \end{description}
170
171 The @...Base@ modules generally export representation information that
172 is hidden from the public interface.  For example the module @STBase@
173 exports the type @ST@ including its representation, whereas the module
174 @ST@ exports @ST@ abstractly.
175
176 None of these modules are involved in any mutual recursion, with the
177 sole exception that many modules import @Error.error@.
178
179 \subsection[ghc-libs-ghc]{The module @GHC@: really primitive stuff}
180 \label{sect:ghc}
181
182 This section defines all the types which are primitive in Glasgow
183 Haskell, and the operations provided for them.
184
185 A primitive type is one which cannot be defined in Haskell, and which
186 is therefore built into the language and compiler.  Primitive types
187 are always unboxed; that is, a value of primitive type cannot be
188 bottom.
189
190 Primitive values are often represented by a simple bit-pattern, such
191 as @Int#@, @Float#@, @Double#@.  But this is not necessarily the case:
192 a primitive value might be represented by a pointer to a
193 heap-allocated object.  Examples include @Array#@, the type of
194 primitive arrays.  You might think this odd: doesn't being
195 heap-allocated mean that it has a box?  No, it does not.  A primitive
196 array is heap-allocated because it is too big a value to fit in a
197 register, and would be too expensive to copy around; in a sense, it is
198 accidental that it is represented by a pointer.  If a pointer
199 represents a primitive value, then it really does point to that value:
200 no unevaluated thunks, no indirections...nothing can be at the other
201 end of the pointer than the primitive value.
202
203 This section also describes a few non-primitive types, which are needed 
204 to express the result types of some primitive operations.
205
206 \subsubsection{Character and numeric types}
207
208 There are the following obvious primitive types:
209 \begin{verbatim}
210 type Char#
211 type Int#       -- see also Word# and Addr#, later
212 type Float#
213 type Double#
214 \end{verbatim}
215 If you really want to know their exact equivalents in C, see
216 @ghc/includes/StgTypes.lh@ in the GHC source tree.
217
218 Literals for these types may be written as follows:
219 \begin{verbatim}
220 1#              an Int#
221 1.2#            a Float#
222 1.34##          a Double#
223 'a'#            a Char#; for weird characters, use '\o<octal>'#
224 "a"#            an Addr# (a `char *')
225 \end{verbatim}
226
227 \subsubsubsection{Comparison operations}
228 \begin{verbatim}
229 {>,>=,==,/=,<,<=}# :: Int# -> Int# -> Bool
230
231 {gt,ge,eq,ne,lt,le}Char# :: Char# -> Char# -> Bool
232     -- ditto for Word#, Float#, Double#, and Addr#
233 \end{verbatim}
234
235 \subsubsubsection{Unboxed-character operations}
236 \begin{verbatim}
237 ord# :: Char# -> Int#
238 chr# :: Int# -> Char#
239 \end{verbatim}
240
241
242 \subsubsubsection{Unboxed-@Int@ operations}
243 \begin{verbatim}
244 {+,-,*,quotInt,remInt}# :: Int# -> Int# -> Int#
245 negateInt# :: Int# -> Int#
246
247 iShiftL#, iShiftRA#, iShiftRL# :: Int# -> Int# -> Int#
248         -- shift left, right arithmetic, right logical
249 \end{verbatim}
250
251 {\bf Note:} No error/overflow checking!
252
253 \subsubsubsection{Unboxed-@Double@ and @Float@ operations}
254 \begin{verbatim}
255 {plus,minus,times,divide}Double# :: Double# -> Double# -> Double#
256 negateDouble# :: Double# -> Double#
257
258 float2Int#      :: Double# -> Int#   -- just a cast, no checking!
259 int2Double#     :: Int# -> Double#
260
261 expDouble#      :: Double# -> Double#
262 logDouble#      :: Double# -> Double#
263 sqrtDouble#     :: Double# -> Double#
264 sinDouble#      :: Double# -> Double#
265 cosDouble#      :: Double# -> Double#
266 tanDouble#      :: Double# -> Double#
267 asinDouble#     :: Double# -> Double#
268 acosDouble#     :: Double# -> Double#
269 atanDouble#     :: Double# -> Double#
270 sinhDouble#     :: Double# -> Double#
271 coshDouble#     :: Double# -> Double#
272 tanhDouble#     :: Double# -> Double#
273 powerDouble#    :: Double# -> Double# -> Double#
274 \end{verbatim}
275
276 There's an exactly-matching set of unboxed-@Float@ ops; replace
277 @Double#@ with @Float#@ in the list above.  There are two
278 coercion functions for @Float#@/@Double#@:
279 \begin{verbatim}
280 float2Double#   :: Float# -> Double#
281 double2Float#   :: Double# -> Float#
282 \end{verbatim}
283
284 The primitive versions of @encodeDouble@/@decodeDouble@:
285 \begin{verbatim}
286 encodeDouble#   :: Int# -> Int# -> ByteArray#   -- Integer mantissa
287                 -> Int#                         -- Int exponent
288                 -> Double#
289
290 decodeDouble#   :: Double# -> PrelNum.ReturnIntAndGMP
291 \end{verbatim}
292
293 (And the same for @Float#@s.)
294
295 \subsubsection{Operations on/for @Integers@ (interface to GMP)}
296 \label{sect:horrid-Integer-pairing-types}
297
298 We implement @Integers@ (arbitrary-precision integers) using the GNU
299 multiple-precision (GMP) package (version 1.3.2).
300
301 {\bf Note:} some of this might change when we upgrade to using GMP~2.x.
302
303 The data type for @Integer@ must mirror that for @MP_INT@ in @gmp.h@
304 (see @gmp.info@ in \tr{ghc/includes/runtime/gmp}).  It comes out as:
305 \begin{verbatim}
306 data Integer = J# Int# Int# ByteArray#
307 \end{verbatim}
308
309 So, @Integer@ is really just a ``pairing'' type for a particular
310 collection of primitive types.
311
312 The operations in the GMP return other combinations of
313 GMP-plus-something, so we need ``pairing'' types for those, too:
314 \begin{verbatim}
315 data Return2GMPs     = Return2GMPs Int# Int# ByteArray# Int# Int# ByteArray#
316 data ReturnIntAndGMP = ReturnIntAndGMP Int# Int# Int# ByteArray#
317
318 -- ????? something to return a string of bytes (in the heap?)
319 \end{verbatim}
320
321 The primitive ops to support @Integers@ use the ``pieces'' of the
322 representation, and are as follows:
323 \begin{verbatim}
324 negateInteger#  :: Int# -> Int# -> ByteArray# -> Integer
325
326 {plus,minus,times}Integer# :: Int# -> Int# -> ByteArray#
327                            -> Int# -> Int# -> ByteArray#
328                            -> Integer
329
330 cmpInteger# :: Int# -> Int# -> ByteArray#
331             -> Int# -> Int# -> ByteArray#
332             -> Int# -- -1 for <; 0 for ==; +1 for >
333
334 divModInteger#, quotRemInteger#
335         :: Int# -> Int# -> ByteArray#
336         -> Int# -> Int# -> ByteArray#
337         -> PrelNum.Return2GMPs
338
339 integer2Int# :: Int# -> Int# -> ByteArray# -> Int# 
340
341 int2Integer#  :: Int#  -> Integer -- NB: no error-checking on these two!
342 word2Integer# :: Word# -> Integer
343
344 addr2Integer# :: Addr# -> Integer
345         -- the Addr# is taken to be a `char *' string
346         -- to be converted into an Integer.
347 \end{verbatim}
348
349
350 \subsubsection{Words and addresses}
351
352 A @Word#@ is used for bit-twiddling operations.  It is the same size as
353 an @Int#@, but has no sign nor any arithmetic operations.
354 \begin{verbatim}
355 type Word#      -- Same size/etc as Int# but *unsigned*
356 type Addr#      -- A pointer from outside the "Haskell world" (from C, probably);
357                 -- described under "arrays"
358 \end{verbatim}
359
360 @Word#@s and @Addr#@s have the usual comparison operations.
361 Other unboxed-@Word@ ops (bit-twiddling and coercions):
362 \begin{verbatim}
363 and#, or#, xor# :: Word# -> Word# -> Word#
364         -- standard bit ops.
365
366 quotWord#, remWord# :: Word# -> Word# -> Word#
367         -- word (i.e. unsigned) versions are different from int
368         -- versions, so we have to provide these explicitly.
369
370 not# :: Word# -> Word#
371
372 shiftL#, shiftRA#, shiftRL# :: Word# -> Int# -> Word#
373         -- shift left, right arithmetic, right logical
374
375 int2Word#       :: Int#  -> Word# -- just a cast, really
376 word2Int#       :: Word# -> Int#
377 \end{verbatim}
378
379 Unboxed-@Addr@ ops (C casts, really):
380 \begin{verbatim}
381 int2Addr#       :: Int#  -> Addr#
382 addr2Int#       :: Addr# -> Int#
383 \end{verbatim}
384
385 The casts between @Int#@, @Word#@ and @Addr#@ correspond to null
386 operations at the machine level, but are required to keep the Haskell
387 type checker happy.
388
389 Operations for indexing off of C pointers (@Addr#@s) to snatch values
390 are listed under ``arrays''.
391
392 \subsubsection{Arrays}
393
394 The type @Array# elt@ is the type of primitive, unboxed arrays of
395 values of type @elt@.
396
397 \begin{verbatim}
398 type Array# elt
399 \end{verbatim}
400
401 @Array#@ is more primitive than a Haskell array --- indeed, the
402 Haskell @Array@ interface is implemented using @Array#@ --- in that an
403 @Array#@ is indexed only by @Int#@s, starting at zero.  It is also
404 more primitive by virtue of being unboxed.  That doesn't mean that it
405 isn't a heap-allocated object --- of course, it is.  Rather, being
406 unboxed means that it is represented by a pointer to the array itself,
407 and not to a thunk which will evaluate to the array (or to bottom).
408 The components of an @Array#@ are themselves boxed.
409
410 The type @ByteArray#@ is similar to @Array#@, except that it contains
411 just a string of (non-pointer) bytes.
412
413 \begin{verbatim}
414 type ByteArray#
415 \end{verbatim}
416
417 Arrays of these types are useful when a Haskell program wishes to
418 construct a value to pass to a C procedure. It is also possible to
419 use them to build (say) arrays of unboxed characters for internal use
420 in a Haskell program.  Given these uses, @ByteArray#@ is deliberately
421 a bit vague about the type of its components.  Operations are provided
422 to extract values of type @Char#@, @Int#@, @Float#@, @Double#@, and
423 @Addr#@ from arbitrary offsets within a @ByteArray#@.  (For type
424 @Foo#@, the $i$th offset gets you the $i$th @Foo#@, not the @Foo#@ at
425 byte-position $i$.  Mumble.)  (If you want a @Word#@, grab an @Int#@,
426 then coerce it.)
427
428 Lastly, we have static byte-arrays, of type @Addr#@ [mentioned
429 previously].  (Remember the duality between arrays and pointers in C.)
430 Arrays of this types are represented by a pointer to an array in the
431 world outside Haskell, so this pointer is not followed by the garbage
432 collector.  In other respects they are just like @ByteArray#@.  They
433 are only needed in order to pass values from C to Haskell.
434
435 \subsubsubsection{Reading and writing.}
436
437 Primitive arrays are linear, and indexed starting at zero.
438
439 The size and indices of a @ByteArray#@, @Addr#@, and
440 @MutableByteArray#@ are all in bytes.  It's up to the program to
441 calculate the correct byte offset from the start of the array.  This
442 allows a @ByteArray#@ to contain a mixture of values of different
443 type, which is often needed when preparing data for and unpicking
444 results from C.  (Umm... not true of indices... WDP 95/09)
445
446 {\em Should we provide some @sizeOfDouble#@ constants?}
447
448 Out-of-range errors on indexing should be caught by the code which
449 uses the primitive operation; the primitive operations themselves do
450 {\em not} check for out-of-range indexes. The intention is that the
451 primitive ops compile to one machine instruction or thereabouts.
452
453 We use the terms ``reading'' and ``writing'' to refer to accessing
454 {\em mutable} arrays (see Section~\ref{sect:mutable}), and
455 ``indexing'' to refer to reading a value from an {\em immutable}
456 array.
457
458 Immutable byte arrays are straightforward to index (all indices in bytes):
459 \begin{verbatim}
460 indexCharArray#   :: ByteArray# -> Int# -> Char#
461 indexIntArray#    :: ByteArray# -> Int# -> Int#
462 indexAddrArray#   :: ByteArray# -> Int# -> Addr#
463 indexFloatArray#  :: ByteArray# -> Int# -> Float#
464 indexDoubleArray# :: ByteArray# -> Int# -> Double#
465
466 indexCharOffAddr#   :: Addr# -> Int# -> Char#
467 indexIntOffAddr#    :: Addr# -> Int# -> Int#
468 indexFloatOffAddr#  :: Addr# -> Int# -> Float#
469 indexDoubleOffAddr# :: Addr# -> Int# -> Double#
470 indexAddrOffAddr#   :: Addr# -> Int# -> Addr#   
471  -- Get an Addr# from an Addr# offset
472 \end{verbatim}
473 \index{indexCharArray# function}
474 \index{indexIntArray# function}
475 \index{indexAddrArray# function}
476 \index{indexFloatArray# function}
477 \index{indexDoubleArray# function}
478 \index{indexCharOffAddr# function}
479 \index{indexIntOffAddr# function}
480 \index{indexFloatOffAddr# function}
481 \index{indexDoubleOffAddr# function}
482 \index{indexAddrOffAddr# function}
483
484 The last of these, @indexAddrOffAddr#@, extracts an @Addr#@ using an offset
485 from another @Addr#@, thereby providing the ability to follow a chain of
486 C pointers.
487
488 Something a bit more interesting goes on when indexing arrays of boxed
489 objects, because the result is simply the boxed object. So presumably
490 it should be entered --- we never usually return an unevaluated
491 object!  This is a pain: primitive ops aren't supposed to do
492 complicated things like enter objects.  The current solution is to
493 return a lifted value, but I don't like it!
494 \begin{verbatim}
495 indexArray#       :: Array# elt -> Int# -> PrelBase.Lift elt  -- Yuk!
496 \end{verbatim}
497
498
499 \subsubsection{The state type}
500 \index{State# type}
501
502 The primitive type @State#@ represents the state of a state transformer.
503 It is parameterised on the desired type of state, which serves to keep
504 states from distinct threads distinct from one another.  But the {\em only}
505 effect of this parameterisation is in the type system: all values of type
506 @State#@ are represented in the same way.  Indeed, they are all 
507 represented by nothing at all!  The code generator ``knows'' to generate no 
508 code, and allocate no registers etc, for primitive states.
509 \begin{verbatim}
510 type State# s
511 \end{verbatim}
512
513
514 The type @GHC.RealWorld@ is truly opaque: there are no values defined
515 of this type, and no operations over it.  It is ``primitive'' in that
516 sense---but it is {\em not unboxed!} Its only role in life is to be
517 the type which distinguishes the @IO@ state transformer.
518 \begin{verbatim}
519 data RealWorld
520 \end{verbatim}
521
522 \subsubsubsection{State of the world}
523
524 A single, primitive, value of type @State# RealWorld@ is provided.
525 \begin{verbatim}
526 realWorld# :: State# GHC.RealWorld
527 \end{verbatim}
528 \index{realWorld# state object}
529
530 (Note: in the compiler, not a @PrimOp@; just a mucho magic
531 @Id@. Exported from @GHC@, though).
532
533 \subsubsection{State pairing types}
534 \label{sect:horrid-pairing-types}
535
536 This subsection defines some types which, while they aren't quite
537 primitive because we can define them in Haskell, are very nearly so.
538 They define constructors which pair a primitive state with a value of
539 each primitive type.  They are required to express the result type of
540 the primitive operations in the state monad.
541 \begin{verbatim}
542 data StateAndPtr#    s elt = StateAndPtr#    (State# s) elt 
543
544 data StateAndChar#   s     = StateAndChar#   (State# s) Char# 
545 data StateAndInt#    s     = StateAndInt#    (State# s) Int# 
546 data StateAndWord#   s     = StateAndWord#   (State# s) Word#
547 data StateAndFloat#  s     = StateAndFloat#  (State# s) Float# 
548 data StateAndDouble# s     = StateAndDouble# (State# s) Double#  
549 data StateAndAddr#   s     = StateAndAddr#   (State# s) Addr#
550
551 data StateAndStablePtr# s a = StateAndStablePtr#  (State# s) (StablePtr# a)
552 data StateAndForeignObj# s  = StateAndForeignObj# (State# s) ForeignObj#
553 data StateAndSynchVar#  s a = StateAndSynchVar#  (State# s) (SynchVar# a)
554
555 data StateAndArray#            s elt = StateAndArray#        (State# s) (Array# elt) 
556 data StateAndMutableArray#     s elt = StateAndMutableArray# (State# s) (MutableArray# s elt)  
557 data StateAndByteArray#        s = StateAndByteArray#        (State# s) ByteArray# 
558 data StateAndMutableByteArray# s = StateAndMutableByteArray# (State# s) (MutableByteArray# s)
559 \end{verbatim}
560
561 Hideous.
562
563 \subsubsection{Mutable arrays}
564 \label{sect:mutable}
565 \index{Mutable arrays}
566
567 Corresponding to @Array#@ and @ByteArray#@, we have the types of
568 mutable versions of each.  In each case, the representation is a
569 pointer to a suitable block of (mutable) heap-allocated storage.
570 \begin{verbatim}
571 type MutableArray# s elt
572 type MutableByteArray# s
573 \end{verbatim}
574
575 \subsubsubsection{Allocation}
576 \index{Mutable arrays, allocation}
577 \index{Allocation, of mutable arrays}
578
579 Mutable arrays can be allocated. Only pointer-arrays are initialised;
580 arrays of non-pointers are filled in by ``user code'' rather than by
581 the array-allocation primitive.  Reason: only the pointer case has to
582 worry about GC striking with a partly-initialised array.
583
584 \begin{verbatim}
585 newArray#       :: Int# -> elt -> State# s -> StateAndMutableArray# s elt 
586
587 newCharArray#   :: Int# -> State# s -> StateAndMutableByteArray# s 
588 newIntArray#    :: Int# -> State# s -> StateAndMutableByteArray# s 
589 newAddrArray#   :: Int# -> State# s -> StateAndMutableByteArray# s 
590 newFloatArray#  :: Int# -> State# s -> StateAndMutableByteArray# s 
591 newDoubleArray# :: Int# -> State# s -> StateAndMutableByteArray# s 
592 \end{verbatim}
593
594 The size of a @ByteArray#@ is given in bytes.
595
596 \subsubsubsection{Reading and writing}
597
598 %OLD: Remember, offsets in a @MutableByteArray#@ are in bytes.
599 \begin{verbatim}
600 readArray#       :: MutableArray# s elt -> Int# -> State# s -> StateAndPtr#    s elt
601 readCharArray#   :: MutableByteArray# s -> Int# -> State# s -> StateAndChar#   s
602 readIntArray#    :: MutableByteArray# s -> Int# -> State# s -> StateAndInt#    s
603 readAddrArray#   :: MutableByteArray# s -> Int# -> State# s -> StateAndAddr#   s 
604 readFloatArray#  :: MutableByteArray# s -> Int# -> State# s -> StateAndFloat#  s 
605 readDoubleArray# :: MutableByteArray# s -> Int# -> State# s -> StateAndDouble# s 
606
607 writeArray#       :: MutableArray# s elt -> Int# -> elt     -> State# s -> State# s 
608 writeCharArray#   :: MutableByteArray# s -> Int# -> Char#   -> State# s -> State# s 
609 writeIntArray#    :: MutableByteArray# s -> Int# -> Int#    -> State# s -> State# s 
610 writeAddrArray#   :: MutableByteArray# s -> Int# -> Addr#   -> State# s -> State# s 
611 writeFloatArray#  :: MutableByteArray# s -> Int# -> Float#  -> State# s -> State# s 
612 writeDoubleArray# :: MutableByteArray# s -> Int# -> Double# -> State# s -> State# s 
613 \end{verbatim}
614
615
616 \subsubsubsection{Equality.}
617
618 One can take ``equality'' of mutable arrays.  What is compared is the
619 {\em name} or reference to the mutable array, not its contents.
620 \begin{verbatim}
621 sameMutableArray#     :: MutableArray# s elt -> MutableArray# s elt -> Bool
622 sameMutableByteArray# :: MutableByteArray# s -> MutableByteArray# s -> Bool
623 \end{verbatim}
624
625
626 \subsubsubsection{Freezing mutable arrays}
627
628 Only unsafe-freeze has a primitive.  (Safe freeze is done directly in Haskell 
629 by copying the array and then using @unsafeFreeze@.) 
630 \begin{verbatim}
631 unsafeFreezeArray#     :: MutableArray# s elt -> State# s -> StateAndArray#     s elt
632 unsafeFreezeByteArray# :: MutableByteArray# s -> State# s -> StateAndByteArray# s
633 \end{verbatim}
634
635
636 \subsubsection{Stable pointers}
637 \index{Stable pointers}
638
639 A stable pointer is a name for a Haskell object which can be passed to
640 the external world.  It is ``stable'' in the sense that the name does
641 not change when the Haskell garbage collector runs --- in contrast to
642 the address of the object which may well change.
643
644 The stable pointer type is parameterised by the type of the thing
645 which is named.
646 \begin{verbatim}
647 type StablePtr# a
648 \end{verbatim}
649
650 A stable pointer is represented by an index into the (static)
651 @StablePointerTable@.  The Haskell garbage collector treats the
652 @StablePointerTable@ as a source of roots for GC.
653
654 The @makeStablePointer@ function converts a value into a stable
655 pointer.  It is part of the @IO@ monad, because we want to be sure
656 we don't allocate one twice by accident, and then only free one of the
657 copies.
658 \begin{verbatim}
659 makeStablePointer#  :: a -> State# RealWorld -> StateAndStablePtr# RealWorld a
660 freeStablePointer#  :: StablePtr# a -> State# RealWorld -> State# RealWorld
661 deRefStablePointer# :: StablePtr# a -> State# RealWorld -> StateAndPtr RealWorld a
662 \end{verbatim}
663
664 There is also a C procedure @FreeStablePtr@ which frees a stable pointer.
665
666 %{\em Andy's comment.} {\bf Errors:} The following is not strictly true: the current
667 %implementation is not as polymorphic as claimed.  The reason for this
668 %is that the C programmer will have to use a different entry-routine
669 %for each type of stable pointer.  At present, we only supply a very
670 %limited number (3) of these routines.  It might be possible to
671 %increase the range of these routines by providing general purpose
672 %entry points to apply stable pointers to (stable pointers to)
673 %arguments and to enter (stable pointers to) boxed primitive values.
674 %{\em End of Andy's comment.}
675
676 %
677 % Rewritten and updated for MallocPtr++ -- 4/96 SOF
678 %
679 \subsubsection{Foreign objects}
680 \index{Foreign objects}
681 \index{ForeignObj type}
682
683 A @ForeignObj#@ is a reference to an object outside the Haskell world
684 (i.e., from the C world, or a reference to an object on another
685 machine completely.), where the Haskell world has been told ``Let me
686 know when you're finished with this ...''.
687
688 \begin{verbatim}
689 type ForeignObj#
690 \end{verbatim}
691
692 GHC provides two primitives on @ForeignObj#@:
693
694 \begin{verbatim}
695 makeForeignObj# 
696         :: Addr# -- foreign reference
697         -> Addr# -- pointer to finalisation routine
698         -> StateAndForeignObj# RealWorld ForeignObj#
699 writeForeignObj 
700         :: ForeignObj#        -- foreign object
701         -> Addr#              -- datum
702         -> State# RealWorld
703         -> State# RealWorld
704 \end{verbatim}
705
706 The module @Foreign@ (Section \ref{sec:foreign-obj}) provides a more
707 programmer-friendly interface to foreign objects.
708
709 \subsubsection{Synchronizing variables (M-vars)}
710 \index{Synchronising variables (M-vars)}
711 \index{M-Vars}
712
713 Synchronising variables are the primitive type used to implement
714 Concurrent Haskell's MVars (see the Concurrent Haskell paper for
715 the operational behaviour of these operations).
716
717 \begin{verbatim}
718 type SynchVar# s elt    -- primitive
719
720 newSynchVar#:: State# s -> StateAndSynchVar# s elt
721 takeMVar#   :: SynchVar# s elt -> State# s -> StateAndPtr# s elt
722 putMVar#    :: SynchVar# s elt -> State# s -> State# s
723 \end{verbatim}
724
725 \subsubsection{@spark#@ primitive operation (for parallel execution)}
726
727 {\em ToDo: say something}  It's used in the unfolding for @par@.
728
729 \subsubsection{The @errorIO#@ primitive operation}
730
731 The @errorIO#@ primitive takes an argument much like @IO@.  It aborts
732 execution of the current program, and continues instead by performing
733 the given @IO@-like value on the current state of the world.
734
735 \begin{verbatim}
736 errorIO# :: (State# RealWorld# -> a -> a
737 \end{verbatim}
738
739 \subsection{GHC/Hugs Extension Libraries}
740
741 The extension libraries provided by both GHC and Hugs are described in
742 a separate document ``The Hugs-GHC Extension Libraries''.
743
744 \subsection{GHC-only Extension Libraries}
745
746 If you rely on the implicit @import Prelude@ that GHC normally does
747 for you, and if you don't use any weird flags (notably
748 @-fglasgow-exts@), and if you don't import the Glasgow extensions
749 interface, @GlaExts@, then GHC should work {\em exactly} as the
750 Haskell report says, and the full user namespaces should be available
751 to you.
752
753 If you mess about with @import Prelude@... innocent hiding, e.g.,
754
755 \begin{verbatim}
756 import Prelude hiding ( fromIntegral )
757 \end{verbatim}
758
759 should work just fine.
760
761 % this should work now?? -- SDM
762
763 %There are some things you can do that will make GHC crash, e.g.,
764 %hiding a standard class:
765 %\begin{verbatim}
766 %import Prelude hiding ( Eq(..) )
767 %\end{verbatim}
768 %
769 %Don't do that.
770
771 If you turn on @-fglasgow-exts@, the compiler will recognise and parse
772 unboxed values properly. To get at the primitive operations described
773 herein, import the relevant interfaces.
774
775 \subsubsection{The @GlaExts@ interface}
776 \index{GlaExts interface (GHC extensions)}
777
778 The @GlaExts@ interface provides access to extensions that only GHC
779 implements.  These currently are: unboxed types, including the
780 representations of the primitive types (Int, Float, etc.), and the
781 GHC primitive operations (@+#@, @==#@, etc.).
782
783 This module used to provide access to all the Glasgow extensions, but
784 these have since been moved into separate libraries for compatibility
785 with Hugs (version 2.09: in fact, you can still get at this stuff via
786 @GlaExts@ for compatibility, but this facility will likely be removed
787 in the future).
788
789 \begin{verbatim}
790 -- the representation of some basic types:
791 data Char    = C# Char#
792 data Int     = I# Int#
793 data Addr    = A# Addr#
794 data Word    = W# Word#
795 data Float   = F# Float#
796 data Double  = D# Double#
797 data Integer = J# Int# Int# ByteArray#
798
799 module GHC  -- all primops and primitive types.
800 \end{verbatim}
801
802 \subsubsection{The @MutableArray@ interface}
803 \label{sec:mutable-array}
804 \index{MutableArray interface (GHC extensions)}
805
806 The @MutableArray@ interface defines a general set of operations over
807 mutable arrays (@MutableArray@) and mutable chunks of memory
808 (@MutableByteArray@):
809
810 \begin{verbatim}
811 data MutableArray s ix elt -- abstract
812 data MutableByteArray s ix -- abstract
813                            -- instance of : CCallable
814 -- Creators:
815 newArray           :: Ix ix => (ix,ix) -> elt -> ST s (MutableArray s ix elt)
816 newCharArray       :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix) 
817 newAddrArray       :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix) 
818 newIntArray        :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix) 
819 newFloatArray      :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix) 
820 newDoubleArray     :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix) 
821
822 boundsOfArray      :: Ix ix => MutableArray s ix elt -> (ix, ix)  
823 boundsOfByteArray  :: Ix ix => MutableByteArray s ix -> (ix, ix)
824
825
826 readArray          :: Ix ix => MutableArray s ix elt -> ix -> ST s elt 
827
828 readCharArray      :: Ix ix => MutableByteArray s ix -> ix -> ST s Char 
829 readIntArray       :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
830 readAddrArray      :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
831 readFloatArray     :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
832 readDoubleArray    :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
833
834 writeArray         :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s () 
835 writeCharArray     :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s () 
836 writeIntArray      :: Ix ix => MutableByteArray s ix -> ix -> Int  -> ST s () 
837 writeAddrArray     :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s () 
838 writeFloatArray    :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s () 
839 writeDoubleArray   :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s () 
840
841 freezeArray        :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
842 freezeCharArray    :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
843 freezeIntArray     :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
844 freezeAddrArray    :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
845 freezeFloatArray   :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
846 freezeDoubleArray  :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
847
848 unsafeFreezeArray     :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)  
849 unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
850 thawArray             :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
851 \end{verbatim}
852
853 \subsubsection{The @ByteArray@ interface}
854 \label{sec:byte-array}
855 \index{ByteArray interface (GHC extensions)}
856
857 @ByteArray@s are chunks of immutable Haskell heap:
858
859 \begin{verbatim}
860 data ByteArray ix -- abstract
861                   -- instance of: CCallable
862
863 indexCharArray     :: Ix ix => ByteArray ix -> ix -> Char 
864 indexIntArray      :: Ix ix => ByteArray ix -> ix -> Int
865 indexAddrArray     :: Ix ix => ByteArray ix -> ix -> Addr
866 indexFloatArray    :: Ix ix => ByteArray ix -> ix -> Float
867 indexDoubleArray   :: Ix ix => ByteArray ix -> ix -> Double
868
869 indexCharOffAddr   :: Addr -> Int -> Char
870 indexIntOffAddr    :: Addr -> Int -> Int
871 indexAddrOffAddr   :: Addr -> Int -> Addr
872 indexFloatOffAddr  :: Addr -> Int -> Float
873 indexDoubleOffAddr :: Addr -> Int -> Double
874 \end{verbatim}
875
876 \subsubsection{Stable pointers}
877
878 Nothing exciting here, just simple boxing up.
879 \begin{verbatim}
880 data StablePtr a = StablePtr (StablePtr# a)
881
882 makeStablePointer :: a -> StablePtr a
883 freeStablePointer :: StablePtr a -> IO ()
884 \end{verbatim}
885
886 \subsubsection{Foreign objects}
887 \label{sec:foreign-obj}
888 \index{Foreign objects}
889
890 This module provides the @ForeignObj@ type and wrappers around the
891 primitive operations on foreign objects.
892
893 \begin{verbatim}
894 data ForeignObj = ForeignObj ForeignObj#
895
896 makeForeignObj 
897         :: Addr   -- object to be boxed up as a ForeignObj
898         -> Addr   -- finaliser 
899         -> IO ForeignObj
900
901 writeForeignObj 
902         :: ForeignObj   -- previously created foreign object
903         -> Addr         -- new value
904         -> IO ()
905
906 \end{verbatim}
907 \index{ForeignObj type}
908 \index{makeForeignObj function}
909 \index{writeForeignObj function}
910
911 A typical use of @ForeignObj@ is in constructing Haskell bindings
912 to external libraries. A good example is that of writing a binding to
913 an image-processing library (which was actually the main motivation
914 for implementing @ForeignObj@'s precursor, @MallocPtr#@). The
915 images manipulated are not stored in the Haskell heap, either because
916 the library insist on allocating them internally or we (sensibly)
917 decide to spare the GC from having to heave heavy images around.
918
919 \begin{verbatim}
920 data Image = Image ForeignObj
921 \end{verbatim}
922
923 The @ForeignObj@ type is then used to refer to the externally
924 allocated image, and to acheive some type safety, the Haskell binding
925 defines the @Image@ data type. So, a value of type @ForeignObj@ is
926 used to ``box'' up an external reference into a Haskell heap object
927 that we can then indirectly reference:
928
929 \begin{verbatim}
930 createImage :: (Int,Int) -> IO Image
931 \end{verbatim}
932
933 So far, this looks just like an @Addr@ type, but @ForeignObj@ offers a
934 bit more, namely that we can specify a {\em finalisation routine} to
935 invoke when the @ForeignObj@ is discarded by the GC. The garbage
936 collector invokes the finalisation routine associated with the
937 @ForeignObj@, saying `` Thanks, I'm through with this now..'' For the
938 image-processing library, the finalisation routine could for the
939 images free up memory allocated for them. The finalisation routine has
940 currently to be written in C (the finalisation routine can in turn
941 call on @FreeStablePtr@ to deallocate a stable pointer).
942
943 Associating a finalisation routine with an external object is done by
944 calling @makeForeignObj@.  {\bf Note:} the foreign object value and
945 its finaliser are contained in the @ForeignObj@, so there's no danger
946 of an aggressive optimiser somehow separating the two (with the result
947 that the foreign reference would not be freed).
948
949 (Implementation: a linked list of all @ForeignObj#@s is maintained to
950  allow the garbage collector to detect when a @ForeignObj#@ becomes
951  garbage.)
952
953 Like @Array@, @ForeignObj#@s are represented by heap objects.
954
955 Upon controlled termination of the Haskell program, all @ForeignObjs@
956 are freed, invoking their respective finalisers before terminating.
957
958 \subsubsection{The @CCall@ module}
959
960 The @CCall@ module defines the classes @CCallable@ and @CReturnable@,
961 along with instances for the primitive types (@Int@, @Int#@, @Float@,
962 @Float#@ etc.)  GHC knows to import this module if you use @_ccall_@,
963 but if you need to define your own instances of these classes, you
964 will need to import @CCall@ explicitly.
965
966 More information on how to use @_ccall_@ can be found in Section
967 \ref{glasgow-ccalls}.
968
969 \begin{onlystandalone}
970 \end{document}
971 \end{onlystandalone}
972