1 <!doctype linuxdoc system>
4 o Add indexing support (to linuxdoc)
5 o Fix citations in html
10 <title>The Hugs-GHC Extension Libraries
11 <author>Alastair Reid <tt/reid-alastair@cs.yale.edu/
12 Simon Marlow <tt/simonm@dcs.gla.ac.uk/
13 <date>v0.8, 14 December 1997
15 Hugs and GHC provide a common set of libraries to aid portability.
16 This document specifies the interfaces to these libraries and documents
17 known differences. We hope that these modules will be adopted for inclusion
18 as Standard Haskell Libraries sometime soon.
27 This library provides support for <em/strict/ state threads, as
28 described in the PLDI '94 paper by John Launchbury and Simon Peyton
29 Jones <cite id="LazyStateThreads">. In addition to the monad <tt/ST/,
30 it also provides mutable variables <tt/STRef/ and mutable arrays
34 module ST( module ST, module Monad ) where
37 data ST s a -- abstract type
38 runST :: forall a. (forall s. ST s a) -> a
39 fixST :: (a -> ST s a) -> ST s a
40 unsafeInterleaveST :: ST s a -> ST s a
41 instance Functor (ST s)
44 data STRef s a -- mutable variables in state thread s
45 -- containing values of type a.
46 newSTRef :: a -> ST s (STRef s a)
47 readSTRef :: STRef s a -> ST s a
48 writeSTRef :: STRef s a -> a -> ST s ()
49 instance Eq (STRef s a)
51 data STArray s ix elt -- mutable arrays in state thread s
52 -- indexed by values of type ix
53 -- containing values of type a.
54 newSTArray :: Ix ix => (ix,ix) -> elt -> ST s (STArray s ix elt)
55 boundsSTArray :: Ix ix => STArray s ix elt -> (ix, ix)
56 readSTArray :: Ix ix => STArray s ix elt -> ix -> ST s elt
57 writeSTArray :: Ix ix => STArray s ix elt -> ix -> elt -> ST s ()
58 thawSTArray :: Ix ix => Array ix elt -> ST s (STArray s ix elt)
59 freezeSTArray :: Ix ix => STArray s ix elt -> ST s (Array ix elt)
60 unsafeFreezeSTArray :: Ix ix => STArray s ix elt -> ST s (Array ix elt)
61 instance Eq (STArray s ix elt)
68 GHC also supports ByteArrays --- these aren't supported by Hugs yet.
71 The operations <tt/freezeSTArray/ and <tt/thawSTArray/ convert mutable
72 arrays to and from immutable arrays. Semantically, they are identical
73 to copying the array and they are usually implemented that way. The
74 operation <tt/unsafeFreezeSTArray/ is a faster version of
75 <tt/freezeSTArray/ which omits the copying step. It's a safe substitute for
76 <tt/freezeSTArray/ if you don't modify the mutable array after freezing it.
79 In the current version of Hugs, the <tt/<idx/runST// operation,
80 used to specify encapsulation, is implemented as a language construct,
81 and <tt/runST/ is treated as a keyword. We plan to change this to match
86 Note that it is possible to install Hugs 1.4 without support for lazy
87 state threads, and hence the primitives described here may not be
88 available in all implementations. Also, in contrast with the
89 implementation of lazy state threads in previous releases of Hugs and
90 Gofer, there is no direct relationship between the
91 <tt/<idx/ST monad// and the <tt/<idx/IO monad//.
95 Hugs provides <tt/thenLazyST/ and <tt/thenStrictST/ so that you can
96 import <tt/LazyST/ (say) and still use the strict instance in those
97 places where it matters. GHC implements LazyST and ST using different
98 types, so this isn't possible.
104 <label id="sec:LazyST">
107 This library is identical to <tt/ST/ except that the <tt/ST/ monad
108 instance is <em/lazy/. The lazy ST monad tends to be more prone to
109 space leaks than the strict version, so most programmers will use the
110 former unless laziness is explicitly required. <tt/LazyST/ provides
111 two additional operations:
114 lazyToStrictST :: LazyST.ST s a -> ST.ST s a
115 strictToLazyST :: ST.ST s a -> LazyST.ST s a
118 These are used to convert between lazy and strict state threads. The
119 semantics with respect to laziness are as you would expect: the strict
120 state thread passed to <tt/strictToLazyST/ is not performed until the
121 result of the lazy state thread it returns is demanded.
124 <label id="sec:IOExts">
127 This library provides the following extensions to the IO monad:
130 The operations <tt/fixIO/, <tt/unsafePerformIO/ and <tt/unsafeInterleaveIO/
131 described in <cite id="ImperativeFP">
134 References (aka mutable variables) and mutable arrays (but no form of
138 <tt/performGC/ triggers an immediate garbage collection
141 When called, <tt/trace/ prints the string in its first argument, and then
142 returns the second argument as its result. The <tt/trace/ function is not
143 referentially transparent, and should only be used for debugging, or for
144 monitoring execution.
147 You should also be warned that, unless you understand some of the
148 details about the way that Haskell programs are executed, results
149 obtained using <tt/trace/ can be rather confusing. For example, the
150 messages may not appear in the order that you expect. Even ignoring the
151 output that they produce, adding calls to <tt/trace/ can change the
152 semantics of your program. Consider this a warning!
156 <tt/unsafePtrEq/ compares two values for pointer equality without
157 evaluating them. The results are not referentially transparent and
158 may vary significantly from one compiler to another or in the face of
159 semantics-preserving program changes. However, pointer equality is useful
160 in creating a number of referentially transparent constructs such as this
161 simplified memoisation function:
164 > cache :: (a -> b) -> (a -> b)
165 > cache f = \x -> unsafePerformIO (check x)
167 > ref = unsafePerformIO (newIORef (error "cache", error "cache"))
168 > check x = readIORef ref >>= \ (x',a) ->
169 > if x `unsafePtrEq` x' then
173 > writeIORef ref (x, a) >>
183 fixIO :: (a -> IO a) -> IO a
184 unsafePerformIO :: IO a -> a
185 unsafeInterleaveIO :: IO a -> IO a
187 data IORef a -- mutable variables containing values of type a
188 newIORef :: a -> IO (IORef a)
189 readIORef :: IORef a -> IO a
190 writeIORef :: IORef a -> a -> IO ()
191 instance Eq (IORef a)
193 data IOArray ix elt -- mutable arrays indexed by values of type ix
194 -- containing values of type a.
195 newIOArray :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
196 boundsIOArray :: Ix ix => IOArray ix elt -> (ix, ix)
197 readIOArray :: Ix ix => IOArray ix elt -> ix -> IO elt
198 writeIOArray :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
199 freezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
200 instance Eq (IOArray ix elt)
203 trace :: String -> a -> a
204 unsafePtrEq :: a -> a -> Bool
208 <sect> <idx/GlaExts/ <p>
210 This library provides a convenient bundle of most of the extensions
211 available in GHC and Hugs. This module is generally more stable than
212 the other modules of non-standard extensions so you might choose to
213 import them from here rather than going straight to the horses mouth.
216 module GlaExts( module GlaExts, module IOExts, module ST, module Addr ) where
220 trace :: String -> a -> a
224 The GHC version also provides the types <tt/PrimIO/, <tt/RealWorld/,
225 <tt/ByteArray/, <tt/Lift/ and operations on these types. It also
226 provides the unboxed views of the types
234 and a number of ``primitive operations'' (<tt/+&num/,
235 <tt/plusFloat&num/, etc.).
240 <label id="sec:Bits">
243 This library defines bitwise operations for signed and unsigned ints.
247 infixl 8 `shift`, `rotate`
253 (.&.), (.|.), xor :: a -> a -> a
255 shift :: a -> Int -> a
256 rotate :: a -> Int -> a
258 setBit :: a -> Int -> a
259 clearBit :: a -> Int -> a
260 complementBit :: a -> Int -> a
261 testBit :: a -> Int -> Bool
263 isSigned :: a -> Bool
265 shiftL, shiftR :: Bits a => a -> Int -> a
266 rotateL, rotateR :: Bits a => a -> Int -> a
267 shiftL a i = shift a i
268 shiftR a i = shift a (-i)
269 rotateL a i = rotate a i
270 rotateR a i = rotate a (-i)
276 <tt/bitSize/ and <tt/isSigned/ are like <tt/floatRadix/ and <tt/floatDigits/
277 -- they return parameters of the <em/type/ of their argument rather than
278 of the particular argument they are applied to. <tt/bitSize/ returns
279 the number of bits in the type (or <tt/Nothing/ for unbounded types); and
280 <tt/isSigned/ returns whether the type is signed or not.
282 <tt/shift/ performs sign extension.
283 That is, right shifts fill the top bits with 1 if the number is negative
284 and with 0 otherwise.
285 (Since unsigned types are always positive, the top bit is always filled with
288 Bits are numbered from 0 with bit 0 being the least significant bit.
290 <tt/shift x i/ and <tt/rotate x i/ shift to the left if <tt/i/ is
291 positive and to the right otherwise.
294 <tt/rotate/ is well defined only if bitSize returns a number.
295 (Maybe we should impose a Bounded constraint on it?)
298 <tt/bit i/ is the value with the i'th bit set.
302 <label id="sec:Word">
305 This library provides unsigned integers of various sizes.
306 The types supported are as follows:
309 type | number of bits @
318 For each type <it/W/ above, we provide the following functions and
319 instances. The type <it/I/ refers to the signed integer type of the
323 data W -- Unsigned Ints
338 word8ToWord32 :: Word8 -> Word32
339 word32ToWord8 :: Word32 -> Word8
340 word16ToWord32 :: Word16 -> Word32
341 word32ToWord16 :: Word32 -> Word16
343 word8ToInt :: Word8 -> Int
344 intToWord8 :: Int -> Word8
345 word16ToInt :: Word16 -> Int
346 intToWord16 :: Int -> Word16
347 word32ToInt :: Word32 -> Int
348 intToWord32 :: Int -> Word32
354 All arithmetic is performed modulo 2^n
356 One non-obvious consequequence of this is that <tt/negate/
357 should <em/not/ raise an error on negative arguments.
360 The coercion <tt/wToI/ converts an unsigned n-bit value to the
361 signed n-bit value with the same representation. For example,
362 <tt/word8ToInt8 0xff = -1/.
363 Likewise, <tt/iToW/ converts signed n-bit values to the
364 corresponding unsigned n-bit value.
367 ToDo: complete the set of coercion functions.
370 Use <tt/Prelude.fromIntegral :: (Integral a, Num b) => a -> b/ to
371 coerce between different sizes or to preserve sign when converting
372 between values of the same size.
375 It would be very natural to add a type a type <tt/Natural/ providing
376 an unbounded size unsigned integer --- just as <tt/Integer/ provides
377 unbounded size signed integers. We do not do that yet since there is
378 no demand for it. Doing so would require <tt/Bits.bitSize/ to return
382 The <tt/Enum/ instances stop when they reach their upper or lower
383 bound --- they don't overflow the way the <tt/Int/ and <tt/Float/
387 It would be useful to provide a function (or a family of functions?)
388 which coerced between any two Word types (without going through
393 Hugs only provides <tt/Eq/, <tt/Ord/, <tt/Read/ and <tt/Show/
394 instances for <tt/Word64/ at the moment.
400 This library provides signed integers of various sizes. The types
401 supported are as follows:
404 type | number of bits @
413 For each type <it/I/ above, we provide the following instances.
416 data I -- Signed Ints
417 iToInt :: I -> Int -- not provided for Int64
418 intToi :: Int -> I -- not provided for Int64
433 int8ToInt :: Int8 -> Int
434 intToInt8 :: Int -> Int8
435 int16ToInt :: Int16 -> Int
436 intToInt16 :: Int -> Int16
437 int32ToInt :: Int32 -> Int
438 intToInt32 :: Int -> Int32
443 Hugs does not provide <tt/Int64/ at the moment.
446 ToDo: complete the set of coercion functions.
451 <label id="sec:Addr">
454 This library provides machine addresses and is primarily intended for
455 use in creating foreign function interfaces using GreenCard.
459 data Addr -- Address type
463 plusAddr :: Addr -> Int -> Addr
465 -- read value out of _immutable_ memory
466 indexCharOffAddr :: Addr -> Int -> Char
467 indexIntOffAddr :: Addr -> Int -> Int -- should we drop this?
468 indexAddrOffAddr :: Addr -> Int -> Addr
469 indexFloatOffAddr :: Addr -> Int -> Float
470 indexDoubleOffAddr :: Addr -> Int -> Double
471 indexWord8OffAddr :: Addr -> Int -> Word8
472 indexWord16OffAddr :: Addr -> Int -> Word16
473 indexWord32OffAddr :: Addr -> Int -> Word32
474 indexWord64OffAddr :: Addr -> Int -> Word64
475 indexInt8OffAddr :: Addr -> Int -> Int8
476 indexInt16OffAddr :: Addr -> Int -> Int16
477 indexInt32OffAddr :: Addr -> Int -> Int32
478 indexInt64OffAddr :: Addr -> Int -> Int64
480 -- read value out of mutable memory
481 readCharOffAddr :: Addr -> Int -> IO Char
482 readIntOffAddr :: Addr -> Int -> IO Int -- should we drop this?
483 readAddrOffAddr :: Addr -> Int -> IO Addr
484 readFloatOffAddr :: Addr -> Int -> IO Float
485 readDoubleOffAddr :: Addr -> Int -> IO Double
486 readWord8OffAddr :: Addr -> Int -> IO Word8
487 readWord16OffAddr :: Addr -> Int -> IO Word16
488 readWord32OffAddr :: Addr -> Int -> IO Word32
489 readWord64OffAddr :: Addr -> Int -> IO Word64
490 readInt8OffAddr :: Addr -> Int -> IO Int8
491 readInt16OffAddr :: Addr -> Int -> IO Int16
492 readInt32OffAddr :: Addr -> Int -> IO Int32
493 readInt64OffAddr :: Addr -> Int -> IO Int64
495 -- write value into mutable memory
496 writeCharOffAddr :: Addr -> Int -> Char -> IO ()
497 writeIntOffAddr :: Addr -> Int -> Int -> IO () -- should we drop this?
498 writeAddrOffAddr :: Addr -> Int -> Addr -> IO ()
499 writeFloatOffAddr :: Addr -> Int -> Float -> IO ()
500 writeDoubleOffAddr :: Addr -> Int -> Double -> IO ()
501 writeWord8OffAddr :: Addr -> Int -> Word8 -> IO ()
502 writeWord16OffAddr :: Addr -> Int -> Word16 -> IO ()
503 writeWord32OffAddr :: Addr -> Int -> Word32 -> IO ()
504 writeWord64OffAddr :: Addr -> Int -> Word64 -> IO ()
505 writeInt8OffAddr :: Addr -> Int -> Int8 -> IO ()
506 writeInt16OffAddr :: Addr -> Int -> Int16 -> IO ()
507 writeInt32OffAddr :: Addr -> Int -> Int32 -> IO ()
508 writeInt64OffAddr :: Addr -> Int -> Int64 -> IO ()
511 Hugs provides <tt/Addr/ and <tt/nullAddr/ but does not provide any of
512 the index, read or write functions. They can be implemented using
513 GreenCard if required.
515 <sect> <idx/ForeignObj/
516 <label id="sec:ForeignObj">
519 This module is provided by GHC but not by Hugs.
520 GreenCard for Hugs provides the <tt/ForeignObj/ type.
522 <sect> <idx/Concurrent/
523 <label id="sec:Concurrent">
526 This library provides the Concurrent Haskell extensions
527 <cite id="concurrentHaskell:popl96">.
529 We are grateful to the Glasgow Haskell Project for allowing us to
530 redistribute their implementation of this module.
533 module Concurrent where
535 data ThreadId -- thread identifiers
537 instance Ord ThreadId
539 forkIO :: IO () -> IO ThreadId
540 killThread :: ThreadId -> IO ()
542 data MVar a -- Synchronisation variables
543 newEmptyMVar :: IO (MVar a)
544 newMVar :: a -> IO (MVar a)
545 takeMVar :: MVar a -> IO a
546 putMVar :: MVar a -> a -> IO ()
547 swapMVar :: MVar a -> a -> IO a
548 readMVar :: MVar a -> IO a
551 data Chan a -- channels
552 newChan :: IO (Chan a)
553 writeChan :: Chan a -> a -> IO ()
554 readChan :: Chan a -> IO a
555 dupChan :: Chan a -> IO (Chan a)
556 unReadChan :: Chan a -> a -> IO ()
557 getChanContents :: Chan a -> IO [a]
558 writeList2Chan :: Chan a -> [a] -> IO ()
560 data CVar a -- one element channels
561 newCVar :: IO (CVar a)
562 putCVar :: CVar a -> a -> IO ()
563 getCVar :: CVar a -> IO a
565 data QSem -- General/quantity semaphores
566 newQSem :: Int -> IO QSem
567 waitQSem :: QSem -> IO ()
568 signalQSem :: QSem -> IO ()
570 data QSemN -- General/quantity semaphores
571 newQSemN :: Int -> IO QSemN
572 waitQSemN :: QSemN -> Int -> IO ()
573 signalQSemN :: QSemN -> Int -> IO ()
575 type SampleVar a -- Sample variables
576 newEmptySampleVar:: IO (SampleVar a)
577 newSampleVar :: a -> IO (SampleVar a)
578 emptySampleVar :: SampleVar a -> IO ()
579 readSampleVar :: SampleVar a -> IO a
580 writeSampleVar :: SampleVar a -> a -> IO ()
587 GHC uses preemptive multitasking:
588 Context switches can occur at any time, except if you call a C
589 function (like \verb"getchar") that blocks waiting for input.
591 Hugs uses cooperative multitasking:
592 Context switches only occur when you use one of the primitives
593 defined in this module. This means that programs such as:
596 main = forkIO (write 'a') >> write 'b'
597 where write c = putChar c >> write c
600 will print either <tt/aaaaaaaaaaaaaa.../ or <tt/bbbbbbbbbbbb.../,
601 instead of some random interleaving of <tt/a/s and <tt/b/s.
603 In practice, cooperative multitasking is sufficient for writing
604 simple graphical user interfaces.
607 Hugs does not provide the functions <tt/mergeIO/ or <tt/nmergeIO/ since these
608 require preemptive multitasking.
611 Thread identities and <tt/killThread/ has not been implemented yet on
612 either system. The plan is that <tt/killThread/ will raise an IO
613 exception in the killed thread which it can catch --- perhaps allowing -->
614 --it to kill its children before exiting.
617 The <tt/Ord/ instance for <tt/ThreadId/s provides an arbitrary total ordering
618 which might be used to build an ordered binary tree, say.
623 <label id="sec:Pretty">
626 This library contains Simon Peyton Jones' implementation of John
627 Hughes's pretty printer combinators.
634 data Doc -- the Document datatype
636 -- The primitive Doc values
638 text :: String -> Doc
641 integer :: Integer -> Doc
642 float :: Float -> Doc
643 double :: Double -> Doc
644 rational :: Rational -> Doc
645 semi, comma, colon, space, equals :: Doc
646 lparen, rparen, lbrack, rbrack, lbrace, rbrace :: Doc
647 parens, brackets, braces :: Doc -> Doc
648 quotes, doubleQuotes :: Doc -> Doc
650 -- Combining Doc values
651 (<>) :: Doc -> Doc -> Doc -- Beside
652 hcat :: [Doc] -> Doc -- List version of <>
653 (<+>) :: Doc -> Doc -> Doc -- Beside, separated by space
654 hsep :: [Doc] -> Doc -- List version of <+>
655 ($$) :: Doc -> Doc -> Doc -- Above; if there is no
656 -- overlap it "dovetails" the two
657 vcat :: [Doc] -> Doc -- List version of $$
658 cat :: [Doc] -> Doc -- Either hcat or vcat
659 sep :: [Doc] -> Doc -- Either hsep or vcat
660 fcat :: [Doc] -> Doc -- ``Paragraph fill'' version of cat
661 fsep :: [Doc] -> Doc -- ``Paragraph fill'' version of sep
662 nest :: Int -> Doc -> Doc -- Nested
663 hang :: Doc -> Int -> Doc -> Doc
664 punctuate :: Doc -> [Doc] -> [Doc]
665 -- punctuate p [d1, ... dn] = [d1 <> p, d2 <> p, ... dn-1 <> p, dn]
667 -- Displaying Doc values
669 render :: Doc -> String -- Uses default style
670 renderStyle :: Style -> Doc -> String
671 data Style = Style { lineLength :: Int, -- In chars
672 ribbonsPerLine :: Float, -- Ratio of ribbon length
676 data Mode = PageMode -- Normal
677 | ZigZagMode -- With zig-zag cuts
678 | LeftMode -- No indentation, infinitely long lines
679 | OneLineMode -- All on one line
682 <biblio files="refs" style="abbrv">