1 <!-- UNBOXED TYPES AND PRIMITIVE OPERATIONS -->
3 <sect1 id="primitives">
4 <title>Unboxed types and primitive operations</title>
5 <indexterm><primary>PrelGHC module</primary></indexterm>
7 <para>This module defines all the types which are primitive in
8 Glasgow Haskell, and the operations provided for them.</para>
10 <para>Note: while you really can use this stuff to write fast code,
11 we generally find it a lot less painful, and more satisfying in the
12 long run, to use higher-level language features and libraries. With
13 any luck, the code you write will be optimised to the efficient
14 unboxed version in any case. And if it isn't, we'd like to know
17 <sect2 id="glasgow-unboxed">
22 <indexterm><primary>Unboxed types (Glasgow extension)</primary></indexterm>
25 <para>Most types in GHC are <firstterm>boxed</firstterm>, which means
26 that values of that type are represented by a pointer to a heap
27 object. The representation of a Haskell <literal>Int</literal>, for
28 example, is a two-word heap object. An <firstterm>unboxed</firstterm>
29 type, however, is represented by the value itself, no pointers or heap
30 allocation are involved.
34 Unboxed types correspond to the “raw machine” types you
35 would use in C: <literal>Int#</literal> (long int),
36 <literal>Double#</literal> (double), <literal>Addr#</literal>
37 (void *), etc. The <emphasis>primitive operations</emphasis>
38 (PrimOps) on these types are what you might expect; e.g.,
39 <literal>(+#)</literal> is addition on
40 <literal>Int#</literal>s, and is the machine-addition that we all
41 know and love—usually one instruction.
45 Primitive (unboxed) types cannot be defined in Haskell, and are
46 therefore built into the language and compiler. Primitive types are
47 always unlifted; that is, a value of a primitive type cannot be
48 bottom. We use the convention that primitive types, values, and
49 operations have a <literal>#</literal> suffix.
53 Primitive values are often represented by a simple bit-pattern, such
54 as <literal>Int#</literal>, <literal>Float#</literal>,
55 <literal>Double#</literal>. But this is not necessarily the case:
56 a primitive value might be represented by a pointer to a
57 heap-allocated object. Examples include
58 <literal>Array#</literal>, the type of primitive arrays. A
59 primitive array is heap-allocated because it is too big a value to fit
60 in a register, and would be too expensive to copy around; in a sense,
61 it is accidental that it is represented by a pointer. If a pointer
62 represents a primitive value, then it really does point to that value:
63 no unevaluated thunks, no indirections…nothing can be at the
64 other end of the pointer than the primitive value.
68 There are some restrictions on the use of primitive types, the main
69 one being that you can't pass a primitive value to a polymorphic
70 function or store one in a polymorphic data type. This rules out
71 things like <literal>[Int#]</literal> (i.e. lists of primitive
72 integers). The reason for this restriction is that polymorphic
73 arguments and constructor fields are assumed to be pointers: if an
74 unboxed integer is stored in one of these, the garbage collector would
75 attempt to follow it, leading to unpredictable space leaks. Or a
76 <function>seq</function> operation on the polymorphic component may
77 attempt to dereference the pointer, with disastrous results. Even
78 worse, the unboxed value might be larger than a pointer
79 (<literal>Double#</literal> for instance).
83 Nevertheless, A numerically-intensive program using unboxed types can
84 go a <emphasis>lot</emphasis> faster than its “standard”
85 counterpart—we saw a threefold speedup on one example.
90 <sect2 id="unboxed-tuples">
95 Unboxed tuples aren't really exported by <literal>PrelGHC</literal>,
96 they're available by default with <option>-fglasgow-exts</option>. An
97 unboxed tuple looks like this:
109 where <literal>e_1..e_n</literal> are expressions of any
110 type (primitive or non-primitive). The type of an unboxed tuple looks
115 Unboxed tuples are used for functions that need to return multiple
116 values, but they avoid the heap allocation normally associated with
117 using fully-fledged tuples. When an unboxed tuple is returned, the
118 components are put directly into registers or on the stack; the
119 unboxed tuple itself does not have a composite representation. Many
120 of the primitive operations listed in this section return unboxed
125 There are some pretty stringent restrictions on the use of unboxed tuples:
134 Unboxed tuple types are subject to the same restrictions as
135 other unboxed types; i.e. they may not be stored in polymorphic data
136 structures or passed to polymorphic functions.
143 Unboxed tuples may only be constructed as the direct result of
144 a function, and may only be deconstructed with a <literal>case</literal> expression.
145 eg. the following are valid:
149 f x y = (# x+1, y-1 #)
150 g x = case f x x of { (# a, b #) -> a + b }
154 but the following are invalid:
168 No variable can have an unboxed tuple type. This is illegal:
172 f :: (# Int, Int #) -> (# Int, Int #)
177 because <literal>x</literal> has an unboxed tuple type.
187 Note: we may relax some of these restrictions in the future.
191 The <literal>IO</literal> and <literal>ST</literal> monads use unboxed
192 tuples to avoid unnecessary allocation during sequences of operations.
198 <title>Character and numeric types</title>
200 <indexterm><primary>character types, primitive</primary></indexterm>
201 <indexterm><primary>numeric types, primitive</primary></indexterm>
202 <indexterm><primary>integer types, primitive</primary></indexterm>
203 <indexterm><primary>floating point types, primitive</primary></indexterm>
205 There are the following obvious primitive types:
219 <indexterm><primary><literal>Char#</literal></primary></indexterm>
220 <indexterm><primary><literal>Int#</literal></primary></indexterm>
221 <indexterm><primary><literal>Word#</literal></primary></indexterm>
222 <indexterm><primary><literal>Addr#</literal></primary></indexterm>
223 <indexterm><primary><literal>Float#</literal></primary></indexterm>
224 <indexterm><primary><literal>Double#</literal></primary></indexterm>
225 <indexterm><primary><literal>Int64#</literal></primary></indexterm>
226 <indexterm><primary><literal>Word64#</literal></primary></indexterm>
229 If you really want to know their exact equivalents in C, see
230 <filename>ghc/includes/StgTypes.h</filename> in the GHC source tree.
234 Literals for these types may be written as follows:
243 'a'# a Char#; for weird characters, use e.g. '\o<octal>'#
244 "a"# an Addr# (a `char *'); only characters '\0'..'\255' allowed
247 <indexterm><primary>literals, primitive</primary></indexterm>
248 <indexterm><primary>constants, primitive</primary></indexterm>
249 <indexterm><primary>numbers, primitive</primary></indexterm>
255 <title>Comparison operations</title>
258 <indexterm><primary>comparisons, primitive</primary></indexterm>
259 <indexterm><primary>operators, comparison</primary></indexterm>
265 {>,>=,==,/=,<,<=}# :: Int# -> Int# -> Bool
267 {gt,ge,eq,ne,lt,le}Char# :: Char# -> Char# -> Bool
268 -- ditto for Word# and Addr#
271 <indexterm><primary><literal>>#</literal></primary></indexterm>
272 <indexterm><primary><literal>>=#</literal></primary></indexterm>
273 <indexterm><primary><literal>==#</literal></primary></indexterm>
274 <indexterm><primary><literal>/=#</literal></primary></indexterm>
275 <indexterm><primary><literal><#</literal></primary></indexterm>
276 <indexterm><primary><literal><=#</literal></primary></indexterm>
277 <indexterm><primary><literal>gt{Char,Word,Addr}#</literal></primary></indexterm>
278 <indexterm><primary><literal>ge{Char,Word,Addr}#</literal></primary></indexterm>
279 <indexterm><primary><literal>eq{Char,Word,Addr}#</literal></primary></indexterm>
280 <indexterm><primary><literal>ne{Char,Word,Addr}#</literal></primary></indexterm>
281 <indexterm><primary><literal>lt{Char,Word,Addr}#</literal></primary></indexterm>
282 <indexterm><primary><literal>le{Char,Word,Addr}#</literal></primary></indexterm>
288 <title>Primitive-character operations</title>
291 <indexterm><primary>characters, primitive operations</primary></indexterm>
292 <indexterm><primary>operators, primitive character</primary></indexterm>
298 ord# :: Char# -> Int#
299 chr# :: Int# -> Char#
302 <indexterm><primary><literal>ord#</literal></primary></indexterm>
303 <indexterm><primary><literal>chr#</literal></primary></indexterm>
309 <title>Primitive-<literal>Int</literal> operations</title>
312 <indexterm><primary>integers, primitive operations</primary></indexterm>
313 <indexterm><primary>operators, primitive integer</primary></indexterm>
319 {+,-,*,quotInt,remInt,gcdInt}# :: Int# -> Int# -> Int#
320 negateInt# :: Int# -> Int#
322 iShiftL#, iShiftRA#, iShiftRL# :: Int# -> Int# -> Int#
323 -- shift left, right arithmetic, right logical
325 addIntC#, subIntC#, mulIntC# :: Int# -> Int# -> (# Int#, Int# #)
326 -- add, subtract, multiply with carry
329 <indexterm><primary><literal>+#</literal></primary></indexterm>
330 <indexterm><primary><literal>-#</literal></primary></indexterm>
331 <indexterm><primary><literal>*#</literal></primary></indexterm>
332 <indexterm><primary><literal>quotInt#</literal></primary></indexterm>
333 <indexterm><primary><literal>remInt#</literal></primary></indexterm>
334 <indexterm><primary><literal>gcdInt#</literal></primary></indexterm>
335 <indexterm><primary><literal>iShiftL#</literal></primary></indexterm>
336 <indexterm><primary><literal>iShiftRA#</literal></primary></indexterm>
337 <indexterm><primary><literal>iShiftRL#</literal></primary></indexterm>
338 <indexterm><primary><literal>addIntC#</literal></primary></indexterm>
339 <indexterm><primary><literal>subIntC#</literal></primary></indexterm>
340 <indexterm><primary><literal>mulIntC#</literal></primary></indexterm>
341 <indexterm><primary>shift operations, integer</primary></indexterm>
345 <emphasis>Note:</emphasis> No error/overflow checking!
351 <title>Primitive-<literal>Double</literal> and <literal>Float</literal> operations</title>
354 <indexterm><primary>floating point numbers, primitive</primary></indexterm>
355 <indexterm><primary>operators, primitive floating point</primary></indexterm>
361 {+,-,*,/}## :: Double# -> Double# -> Double#
362 {<,<=,==,/=,>=,>}## :: Double# -> Double# -> Bool
363 negateDouble# :: Double# -> Double#
364 double2Int# :: Double# -> Int#
365 int2Double# :: Int# -> Double#
367 {plus,minux,times,divide}Float# :: Float# -> Float# -> Float#
368 {gt,ge,eq,ne,lt,le}Float# :: Float# -> Float# -> Bool
369 negateFloat# :: Float# -> Float#
370 float2Int# :: Float# -> Int#
371 int2Float# :: Int# -> Float#
377 <indexterm><primary><literal>+##</literal></primary></indexterm>
378 <indexterm><primary><literal>-##</literal></primary></indexterm>
379 <indexterm><primary><literal>*##</literal></primary></indexterm>
380 <indexterm><primary><literal>/##</literal></primary></indexterm>
381 <indexterm><primary><literal><##</literal></primary></indexterm>
382 <indexterm><primary><literal><=##</literal></primary></indexterm>
383 <indexterm><primary><literal>==##</literal></primary></indexterm>
384 <indexterm><primary><literal>=/##</literal></primary></indexterm>
385 <indexterm><primary><literal>>=##</literal></primary></indexterm>
386 <indexterm><primary><literal>>##</literal></primary></indexterm>
387 <indexterm><primary><literal>negateDouble#</literal></primary></indexterm>
388 <indexterm><primary><literal>double2Int#</literal></primary></indexterm>
389 <indexterm><primary><literal>int2Double#</literal></primary></indexterm>
393 <indexterm><primary><literal>plusFloat#</literal></primary></indexterm>
394 <indexterm><primary><literal>minusFloat#</literal></primary></indexterm>
395 <indexterm><primary><literal>timesFloat#</literal></primary></indexterm>
396 <indexterm><primary><literal>divideFloat#</literal></primary></indexterm>
397 <indexterm><primary><literal>gtFloat#</literal></primary></indexterm>
398 <indexterm><primary><literal>geFloat#</literal></primary></indexterm>
399 <indexterm><primary><literal>eqFloat#</literal></primary></indexterm>
400 <indexterm><primary><literal>neFloat#</literal></primary></indexterm>
401 <indexterm><primary><literal>ltFloat#</literal></primary></indexterm>
402 <indexterm><primary><literal>leFloat#</literal></primary></indexterm>
403 <indexterm><primary><literal>negateFloat#</literal></primary></indexterm>
404 <indexterm><primary><literal>float2Int#</literal></primary></indexterm>
405 <indexterm><primary><literal>int2Float#</literal></primary></indexterm>
409 And a full complement of trigonometric functions:
415 expDouble# :: Double# -> Double#
416 logDouble# :: Double# -> Double#
417 sqrtDouble# :: Double# -> Double#
418 sinDouble# :: Double# -> Double#
419 cosDouble# :: Double# -> Double#
420 tanDouble# :: Double# -> Double#
421 asinDouble# :: Double# -> Double#
422 acosDouble# :: Double# -> Double#
423 atanDouble# :: Double# -> Double#
424 sinhDouble# :: Double# -> Double#
425 coshDouble# :: Double# -> Double#
426 tanhDouble# :: Double# -> Double#
427 powerDouble# :: Double# -> Double# -> Double#
430 <indexterm><primary>trigonometric functions, primitive</primary></indexterm>
434 similarly for <literal>Float#</literal>.
438 There are two coercion functions for <literal>Float#</literal>/<literal>Double#</literal>:
444 float2Double# :: Float# -> Double#
445 double2Float# :: Double# -> Float#
448 <indexterm><primary><literal>float2Double#</literal></primary></indexterm>
449 <indexterm><primary><literal>double2Float#</literal></primary></indexterm>
453 The primitive version of <function>decodeDouble</function>
454 (<function>encodeDouble</function> is implemented as an external C
461 decodeDouble# :: Double# -> PrelNum.ReturnIntAndGMP
464 <indexterm><primary><literal>encodeDouble#</literal></primary></indexterm>
465 <indexterm><primary><literal>decodeDouble#</literal></primary></indexterm>
469 (And the same for <literal>Float#</literal>s.)
474 <sect2 id="integer-operations">
475 <title>Operations on/for <literal>Integers</literal> (interface to GMP)
479 <indexterm><primary>arbitrary precision integers</primary></indexterm>
480 <indexterm><primary>Integer, operations on</primary></indexterm>
484 We implement <literal>Integers</literal> (arbitrary-precision
485 integers) using the GNU multiple-precision (GMP) package (version
490 The data type for <literal>Integer</literal> is either a small
491 integer, represented by an <literal>Int</literal>, or a large integer
492 represented using the pieces required by GMP's
493 <literal>MP_INT</literal> in <filename>gmp.h</filename> (see
494 <filename>gmp.info</filename> in
495 <filename>ghc/includes/runtime/gmp</filename>). It comes out as:
501 data Integer = S# Int# -- small integers
502 | J# Int# ByteArray# -- large integers
505 <indexterm><primary>Integer type</primary></indexterm> The primitive
506 ops to support large <literal>Integers</literal> use the
507 “pieces” of the representation, and are as follows:
513 negateInteger# :: Int# -> ByteArray# -> Integer
515 {plus,minus,times}Integer#, gcdInteger#,
516 quotInteger#, remInteger#, divExactInteger#
517 :: Int# -> ByteArray#
518 -> Int# -> ByteArray#
519 -> (# Int#, ByteArray# #)
522 :: Int# -> ByteArray#
523 -> Int# -> ByteArray#
524 -> Int# -- -1 for <; 0 for ==; +1 for >
527 :: Int# -> ByteArray#
529 -> Int# -- -1 for <; 0 for ==; +1 for >
532 :: Int# -> ByteArray#
536 divModInteger#, quotRemInteger#
537 :: Int# -> ByteArray#
538 -> Int# -> ByteArray#
539 -> (# Int#, ByteArray#,
542 integer2Int# :: Int# -> ByteArray# -> Int#
544 int2Integer# :: Int# -> Integer -- NB: no error-checking on these two!
545 word2Integer# :: Word# -> Integer
547 addr2Integer# :: Addr# -> Integer
548 -- the Addr# is taken to be a `char *' string
549 -- to be converted into an Integer.
552 <indexterm><primary><literal>negateInteger#</literal></primary></indexterm>
553 <indexterm><primary><literal>plusInteger#</literal></primary></indexterm>
554 <indexterm><primary><literal>minusInteger#</literal></primary></indexterm>
555 <indexterm><primary><literal>timesInteger#</literal></primary></indexterm>
556 <indexterm><primary><literal>quotInteger#</literal></primary></indexterm>
557 <indexterm><primary><literal>remInteger#</literal></primary></indexterm>
558 <indexterm><primary><literal>gcdInteger#</literal></primary></indexterm>
559 <indexterm><primary><literal>gcdIntegerInt#</literal></primary></indexterm>
560 <indexterm><primary><literal>divExactInteger#</literal></primary></indexterm>
561 <indexterm><primary><literal>cmpInteger#</literal></primary></indexterm>
562 <indexterm><primary><literal>divModInteger#</literal></primary></indexterm>
563 <indexterm><primary><literal>quotRemInteger#</literal></primary></indexterm>
564 <indexterm><primary><literal>integer2Int#</literal></primary></indexterm>
565 <indexterm><primary><literal>int2Integer#</literal></primary></indexterm>
566 <indexterm><primary><literal>word2Integer#</literal></primary></indexterm>
567 <indexterm><primary><literal>addr2Integer#</literal></primary></indexterm>
573 <title>Words and addresses</title>
576 <indexterm><primary>word, primitive type</primary></indexterm>
577 <indexterm><primary>address, primitive type</primary></indexterm>
578 <indexterm><primary>unsigned integer, primitive type</primary></indexterm>
579 <indexterm><primary>pointer, primitive type</primary></indexterm>
583 A <literal>Word#</literal> is used for bit-twiddling operations.
584 It is the same size as an <literal>Int#</literal>, but has no sign
585 nor any arithmetic operations.
588 type Word# -- Same size/etc as Int# but *unsigned*
589 type Addr# -- A pointer from outside the "Haskell world" (from C, probably);
590 -- described under "arrays"
593 <indexterm><primary><literal>Word#</literal></primary></indexterm>
594 <indexterm><primary><literal>Addr#</literal></primary></indexterm>
598 <literal>Word#</literal>s and <literal>Addr#</literal>s have
599 the usual comparison operations. Other
600 unboxed-<literal>Word</literal> ops (bit-twiddling and coercions):
606 {gt,ge,eq,ne,lt,le}Word# :: Word# -> Word# -> Bool
608 and#, or#, xor# :: Word# -> Word# -> Word#
611 quotWord#, remWord# :: Word# -> Word# -> Word#
612 -- word (i.e. unsigned) versions are different from int
613 -- versions, so we have to provide these explicitly.
615 not# :: Word# -> Word#
617 shiftL#, shiftRL# :: Word# -> Int# -> Word#
618 -- shift left, right logical
620 int2Word# :: Int# -> Word# -- just a cast, really
621 word2Int# :: Word# -> Int#
624 <indexterm><primary>bit operations, Word and Addr</primary></indexterm>
625 <indexterm><primary><literal>gtWord#</literal></primary></indexterm>
626 <indexterm><primary><literal>geWord#</literal></primary></indexterm>
627 <indexterm><primary><literal>eqWord#</literal></primary></indexterm>
628 <indexterm><primary><literal>neWord#</literal></primary></indexterm>
629 <indexterm><primary><literal>ltWord#</literal></primary></indexterm>
630 <indexterm><primary><literal>leWord#</literal></primary></indexterm>
631 <indexterm><primary><literal>and#</literal></primary></indexterm>
632 <indexterm><primary><literal>or#</literal></primary></indexterm>
633 <indexterm><primary><literal>xor#</literal></primary></indexterm>
634 <indexterm><primary><literal>not#</literal></primary></indexterm>
635 <indexterm><primary><literal>quotWord#</literal></primary></indexterm>
636 <indexterm><primary><literal>remWord#</literal></primary></indexterm>
637 <indexterm><primary><literal>shiftL#</literal></primary></indexterm>
638 <indexterm><primary><literal>shiftRA#</literal></primary></indexterm>
639 <indexterm><primary><literal>shiftRL#</literal></primary></indexterm>
640 <indexterm><primary><literal>int2Word#</literal></primary></indexterm>
641 <indexterm><primary><literal>word2Int#</literal></primary></indexterm>
645 Unboxed-<literal>Addr</literal> ops (C casts, really):
648 {gt,ge,eq,ne,lt,le}Addr# :: Addr# -> Addr# -> Bool
650 int2Addr# :: Int# -> Addr#
651 addr2Int# :: Addr# -> Int#
652 addr2Integer# :: Addr# -> (# Int#, ByteArray# #)
655 <indexterm><primary><literal>gtAddr#</literal></primary></indexterm>
656 <indexterm><primary><literal>geAddr#</literal></primary></indexterm>
657 <indexterm><primary><literal>eqAddr#</literal></primary></indexterm>
658 <indexterm><primary><literal>neAddr#</literal></primary></indexterm>
659 <indexterm><primary><literal>ltAddr#</literal></primary></indexterm>
660 <indexterm><primary><literal>leAddr#</literal></primary></indexterm>
661 <indexterm><primary><literal>int2Addr#</literal></primary></indexterm>
662 <indexterm><primary><literal>addr2Int#</literal></primary></indexterm>
663 <indexterm><primary><literal>addr2Integer#</literal></primary></indexterm>
667 The casts between <literal>Int#</literal>,
668 <literal>Word#</literal> and <literal>Addr#</literal>
669 correspond to null operations at the machine level, but are required
670 to keep the Haskell type checker happy.
674 Operations for indexing off of C pointers
675 (<literal>Addr#</literal>s) to snatch values are listed under
676 “arrays”.
682 <title>Arrays</title>
685 <indexterm><primary>arrays, primitive</primary></indexterm>
689 The type <literal>Array# elt</literal> is the type of primitive,
690 unpointed arrays of values of type <literal>elt</literal>.
699 <indexterm><primary><literal>Array#</literal></primary></indexterm>
703 <literal>Array#</literal> is more primitive than a Haskell
704 array—indeed, the Haskell <literal>Array</literal> interface is
705 implemented using <literal>Array#</literal>—in that an
706 <literal>Array#</literal> is indexed only by
707 <literal>Int#</literal>s, starting at zero. It is also more
708 primitive by virtue of being unboxed. That doesn't mean that it isn't
709 a heap-allocated object—of course, it is. Rather, being unboxed
710 means that it is represented by a pointer to the array itself, and not
711 to a thunk which will evaluate to the array (or to bottom). The
712 components of an <literal>Array#</literal> are themselves boxed.
716 The type <literal>ByteArray#</literal> is similar to
717 <literal>Array#</literal>, except that it contains just a string
718 of (non-pointer) bytes.
727 <indexterm><primary><literal>ByteArray#</literal></primary></indexterm>
731 Arrays of these types are useful when a Haskell program wishes to
732 construct a value to pass to a C procedure. It is also possible to use
733 them to build (say) arrays of unboxed characters for internal use in a
734 Haskell program. Given these uses, <literal>ByteArray#</literal>
735 is deliberately a bit vague about the type of its components.
736 Operations are provided to extract values of type
737 <literal>Char#</literal>, <literal>Int#</literal>,
738 <literal>Float#</literal>, <literal>Double#</literal>, and
739 <literal>Addr#</literal> from arbitrary offsets within a
740 <literal>ByteArray#</literal>. (For type
741 <literal>Foo#</literal>, the $i$th offset gets you the $i$th
742 <literal>Foo#</literal>, not the <literal>Foo#</literal> at
743 byte-position $i$. Mumble.) (If you want a
744 <literal>Word#</literal>, grab an <literal>Int#</literal>,
749 Lastly, we have static byte-arrays, of type
750 <literal>Addr#</literal> [mentioned previously]. (Remember
751 the duality between arrays and pointers in C.) Arrays of this types
752 are represented by a pointer to an array in the world outside Haskell,
753 so this pointer is not followed by the garbage collector. In other
754 respects they are just like <literal>ByteArray#</literal>. They
755 are only needed in order to pass values from C to Haskell.
761 <title>Reading and writing</title>
764 Primitive arrays are linear, and indexed starting at zero.
768 The size and indices of a <literal>ByteArray#</literal>, <literal>Addr#</literal>, and
769 <literal>MutableByteArray#</literal> are all in bytes. It's up to the program to
770 calculate the correct byte offset from the start of the array. This
771 allows a <literal>ByteArray#</literal> to contain a mixture of values of different
772 type, which is often needed when preparing data for and unpicking
773 results from C. (Umm…not true of indices…WDP 95/09)
777 <emphasis>Should we provide some <literal>sizeOfDouble#</literal> constants?</emphasis>
781 Out-of-range errors on indexing should be caught by the code which
782 uses the primitive operation; the primitive operations themselves do
783 <emphasis>not</emphasis> check for out-of-range indexes. The intention is that the
784 primitive ops compile to one machine instruction or thereabouts.
788 We use the terms “reading” and “writing” to refer to accessing
789 <emphasis>mutable</emphasis> arrays (see <xref LinkEnd="sect-mutable">), and
790 “indexing” to refer to reading a value from an <emphasis>immutable</emphasis>
795 Immutable byte arrays are straightforward to index (all indices are in
796 units of the size of the object being read):
799 indexCharArray# :: ByteArray# -> Int# -> Char#
800 indexIntArray# :: ByteArray# -> Int# -> Int#
801 indexAddrArray# :: ByteArray# -> Int# -> Addr#
802 indexFloatArray# :: ByteArray# -> Int# -> Float#
803 indexDoubleArray# :: ByteArray# -> Int# -> Double#
805 indexCharOffAddr# :: Addr# -> Int# -> Char#
806 indexIntOffAddr# :: Addr# -> Int# -> Int#
807 indexFloatOffAddr# :: Addr# -> Int# -> Float#
808 indexDoubleOffAddr# :: Addr# -> Int# -> Double#
809 indexAddrOffAddr# :: Addr# -> Int# -> Addr#
810 -- Get an Addr# from an Addr# offset
813 <indexterm><primary><literal>indexCharArray#</literal></primary></indexterm>
814 <indexterm><primary><literal>indexIntArray#</literal></primary></indexterm>
815 <indexterm><primary><literal>indexAddrArray#</literal></primary></indexterm>
816 <indexterm><primary><literal>indexFloatArray#</literal></primary></indexterm>
817 <indexterm><primary><literal>indexDoubleArray#</literal></primary></indexterm>
818 <indexterm><primary><literal>indexCharOffAddr#</literal></primary></indexterm>
819 <indexterm><primary><literal>indexIntOffAddr#</literal></primary></indexterm>
820 <indexterm><primary><literal>indexFloatOffAddr#</literal></primary></indexterm>
821 <indexterm><primary><literal>indexDoubleOffAddr#</literal></primary></indexterm>
822 <indexterm><primary><literal>indexAddrOffAddr#</literal></primary></indexterm>
826 The last of these, <function>indexAddrOffAddr#</function>, extracts an <literal>Addr#</literal> using an offset
827 from another <literal>Addr#</literal>, thereby providing the ability to follow a chain of
832 Something a bit more interesting goes on when indexing arrays of boxed
833 objects, because the result is simply the boxed object. So presumably
834 it should be entered—we never usually return an unevaluated
835 object! This is a pain: primitive ops aren't supposed to do
836 complicated things like enter objects. The current solution is to
837 return a single element unboxed tuple (see <xref LinkEnd="unboxed-tuples">).
843 indexArray# :: Array# elt -> Int# -> (# elt #)
846 <indexterm><primary><literal>indexArray#</literal></primary></indexterm>
852 <title>The state type</title>
855 <indexterm><primary><literal>state, primitive type</literal></primary></indexterm>
856 <indexterm><primary><literal>State#</literal></primary></indexterm>
860 The primitive type <literal>State#</literal> represents the state of a state
861 transformer. It is parameterised on the desired type of state, which
862 serves to keep states from distinct threads distinct from one another.
863 But the <emphasis>only</emphasis> effect of this parameterisation is in the type
864 system: all values of type <literal>State#</literal> are represented in the same way.
865 Indeed, they are all represented by nothing at all! The code
866 generator “knows” to generate no code, and allocate no registers
867 etc, for primitive states.
879 The type <literal>GHC.RealWorld</literal> is truly opaque: there are no values defined
880 of this type, and no operations over it. It is “primitive” in that
881 sense - but it is <emphasis>not unlifted!</emphasis> Its only role in life is to be
882 the type which distinguishes the <literal>IO</literal> state transformer.
896 <title>State of the world</title>
899 A single, primitive, value of type <literal>State# RealWorld</literal> is provided.
905 realWorld# :: State# RealWorld
908 <indexterm><primary>realWorld# state object</primary></indexterm>
912 (Note: in the compiler, not a <literal>PrimOp</literal>; just a mucho magic
913 <literal>Id</literal>. Exported from <literal>GHC</literal>, though).
918 <sect2 id="sect-mutable">
919 <title>Mutable arrays</title>
922 <indexterm><primary>mutable arrays</primary></indexterm>
923 <indexterm><primary>arrays, mutable</primary></indexterm>
924 Corresponding to <literal>Array#</literal> and <literal>ByteArray#</literal>, we have the types of
925 mutable versions of each. In each case, the representation is a
926 pointer to a suitable block of (mutable) heap-allocated storage.
932 type MutableArray# s elt
933 type MutableByteArray# s
936 <indexterm><primary><literal>MutableArray#</literal></primary></indexterm>
937 <indexterm><primary><literal>MutableByteArray#</literal></primary></indexterm>
941 <title>Allocation</title>
944 <indexterm><primary>mutable arrays, allocation</primary></indexterm>
945 <indexterm><primary>arrays, allocation</primary></indexterm>
946 <indexterm><primary>allocation, of mutable arrays</primary></indexterm>
950 Mutable arrays can be allocated. Only pointer-arrays are initialised;
951 arrays of non-pointers are filled in by “user code” rather than by
952 the array-allocation primitive. Reason: only the pointer case has to
953 worry about GC striking with a partly-initialised array.
959 newArray# :: Int# -> elt -> State# s -> (# State# s, MutableArray# s elt #)
961 newCharArray# :: Int# -> State# s -> (# State# s, MutableByteArray# s elt #)
962 newIntArray# :: Int# -> State# s -> (# State# s, MutableByteArray# s elt #)
963 newAddrArray# :: Int# -> State# s -> (# State# s, MutableByteArray# s elt #)
964 newFloatArray# :: Int# -> State# s -> (# State# s, MutableByteArray# s elt #)
965 newDoubleArray# :: Int# -> State# s -> (# State# s, MutableByteArray# s elt #)
968 <indexterm><primary><literal>newArray#</literal></primary></indexterm>
969 <indexterm><primary><literal>newCharArray#</literal></primary></indexterm>
970 <indexterm><primary><literal>newIntArray#</literal></primary></indexterm>
971 <indexterm><primary><literal>newAddrArray#</literal></primary></indexterm>
972 <indexterm><primary><literal>newFloatArray#</literal></primary></indexterm>
973 <indexterm><primary><literal>newDoubleArray#</literal></primary></indexterm>
977 The size of a <literal>ByteArray#</literal> is given in bytes.
983 <title>Reading and writing</title>
986 <indexterm><primary>arrays, reading and writing</primary></indexterm>
992 readArray# :: MutableArray# s elt -> Int# -> State# s -> (# State# s, elt #)
993 readCharArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Char# #)
994 readIntArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Int# #)
995 readAddrArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Addr# #)
996 readFloatArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Float# #)
997 readDoubleArray# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Double# #)
999 writeArray# :: MutableArray# s elt -> Int# -> elt -> State# s -> State# s
1000 writeCharArray# :: MutableByteArray# s -> Int# -> Char# -> State# s -> State# s
1001 writeIntArray# :: MutableByteArray# s -> Int# -> Int# -> State# s -> State# s
1002 writeAddrArray# :: MutableByteArray# s -> Int# -> Addr# -> State# s -> State# s
1003 writeFloatArray# :: MutableByteArray# s -> Int# -> Float# -> State# s -> State# s
1004 writeDoubleArray# :: MutableByteArray# s -> Int# -> Double# -> State# s -> State# s
1007 <indexterm><primary><literal>readArray#</literal></primary></indexterm>
1008 <indexterm><primary><literal>readCharArray#</literal></primary></indexterm>
1009 <indexterm><primary><literal>readIntArray#</literal></primary></indexterm>
1010 <indexterm><primary><literal>readAddrArray#</literal></primary></indexterm>
1011 <indexterm><primary><literal>readFloatArray#</literal></primary></indexterm>
1012 <indexterm><primary><literal>readDoubleArray#</literal></primary></indexterm>
1013 <indexterm><primary><literal>writeArray#</literal></primary></indexterm>
1014 <indexterm><primary><literal>writeCharArray#</literal></primary></indexterm>
1015 <indexterm><primary><literal>writeIntArray#</literal></primary></indexterm>
1016 <indexterm><primary><literal>writeAddrArray#</literal></primary></indexterm>
1017 <indexterm><primary><literal>writeFloatArray#</literal></primary></indexterm>
1018 <indexterm><primary><literal>writeDoubleArray#</literal></primary></indexterm>
1024 <title>Equality</title>
1027 <indexterm><primary>arrays, testing for equality</primary></indexterm>
1031 One can take “equality” of mutable arrays. What is compared is the
1032 <emphasis>name</emphasis> or reference to the mutable array, not its contents.
1038 sameMutableArray# :: MutableArray# s elt -> MutableArray# s elt -> Bool
1039 sameMutableByteArray# :: MutableByteArray# s -> MutableByteArray# s -> Bool
1042 <indexterm><primary><literal>sameMutableArray#</literal></primary></indexterm>
1043 <indexterm><primary><literal>sameMutableByteArray#</literal></primary></indexterm>
1049 <title>Freezing mutable arrays</title>
1052 <indexterm><primary>arrays, freezing mutable</primary></indexterm>
1053 <indexterm><primary>freezing mutable arrays</primary></indexterm>
1054 <indexterm><primary>mutable arrays, freezing</primary></indexterm>
1058 Only unsafe-freeze has a primitive. (Safe freeze is done directly in Haskell
1059 by copying the array and then using <function>unsafeFreeze</function>.)
1065 unsafeFreezeArray# :: MutableArray# s elt -> State# s -> (# State# s, Array# s elt #)
1066 unsafeFreezeByteArray# :: MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
1069 <indexterm><primary><literal>unsafeFreezeArray#</literal></primary></indexterm>
1070 <indexterm><primary><literal>unsafeFreezeByteArray#</literal></primary></indexterm>
1078 <title>Synchronizing variables (M-vars)</title>
1081 <indexterm><primary>synchronising variables (M-vars)</primary></indexterm>
1082 <indexterm><primary>M-Vars</primary></indexterm>
1086 Synchronising variables are the primitive type used to implement
1087 Concurrent Haskell's MVars (see the Concurrent Haskell paper for
1088 the operational behaviour of these operations).
1094 type MVar# s elt -- primitive
1096 newMVar# :: State# s -> (# State# s, MVar# s elt #)
1097 takeMVar# :: SynchVar# s elt -> State# s -> (# State# s, elt #)
1098 putMVar# :: SynchVar# s elt -> State# s -> State# s
1101 <indexterm><primary><literal>SynchVar#</literal></primary></indexterm>
1102 <indexterm><primary><literal>newSynchVar#</literal></primary></indexterm>
1103 <indexterm><primary><literal>takeMVar</literal></primary></indexterm>
1104 <indexterm><primary><literal>putMVar</literal></primary></indexterm>
1112 ;;; Local Variables: ***
1114 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter" "sect1") ***