1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
<HTML>
<HEAD>
<TITLE>The Hugs-GHC Extension Libraries: LazyST </TITLE>
</HEAD>
<BODY>
Previous
<A HREF="libs-2.html">Next</A>
<A HREF="libs.html#toc1">Table of Contents</A>
<HR>
<H2><A NAME="s1">1. LazyST </A></H2>
<P>This library provides support for both <EM>lazy</EM> and <EM>strict</EM> state
threads, as described in the PLDI '94 paper by John Launchbury and
Simon Peyton Jones </I>]. In addition to the
monad <CODE>ST</CODE>, it also provides mutable variables <CODE>STRef</CODE> and
mutable arrays <CODE>STArray</CODE>. As the name suggests, the monad <CODE>ST</CODE>
instance is <EM>lazy</EM>.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
module LazyST( module LazyST, module Monad ) where
import Monad
data ST s a -- abstract type
runST :: forall a. (forall s. ST s a) -> a
returnST :: a -> ST s a
thenLazyST :: ST s a -> (a -> ST s b) -> ST s b
thenStrictST :: ST s a -> (a -> ST s b) -> ST s b
fixST :: (a -> ST s a) -> ST s a
unsafeInterleaveST :: ST s a -> ST s a
instance Functor (ST s)
instance Monad (ST s)
data STRef s a -- mutable variables in state thread s
-- containing values of type a.
newSTRef :: a -> ST s (STRef s a)
readSTRef :: STRef s a -> ST s a
writeSTRef :: STRef s a -> a -> ST s ()
instance Eq (STRef s a)
data STArray s ix elt -- mutable arrays in state thread s
-- indexed by values of type ix
-- containing values of type a.
newSTArray :: Ix ix => (ix,ix) -> elt -> ST s (STArray s ix elt)
boundsSTArray :: Ix ix => STArray s ix elt -> (ix, ix)
readSTArray :: Ix ix => STArray s ix elt -> ix -> ST s elt
writeSTArray :: Ix ix => STArray s ix elt -> ix -> elt -> ST s ()
thawSTArray :: Ix ix => Array ix elt -> ST s (STArray s ix elt)
freezeSTArray :: Ix ix => STArray s ix elt -> ST s (Array ix elt)
unsafeFreezeSTArray :: Ix ix => STArray s ix elt -> ST s (Array ix elt)
instance Eq (STArray s ix elt)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Notes:
<UL>
<LI>
GHC also supports ByteArrays --- these aren't supported by Hugs yet.
</LI>
<LI>
The operations <CODE>freezeSTArray</CODE> and <CODE>thawSTArray</CODE> convert mutable
arrays to and from immutable arrays. Semantically, they are identical
to copying the array and they are usually implemented that way. The
operation <CODE>unsafeFreezeSTArray</CODE> is a faster version of
<CODE>freezeSTArray</CODE> which omits the copying step. It's a safe substitute for
<CODE>freezeSTArray</CODE> if you don't modify the mutable array after freezing it.
</LI>
<LI>In the current version of Hugs, the <CODE>runST</CODE> operation,
used to specify encapsulation, is implemented as a language construct,
and <CODE>runST</CODE> is treated as a keyword. We plan to change this to match
GHC soon.
</LI>
<LI>The only difference between the lazy and strict instances of the
<CODE>ST</CODE> monad is in their bind operators. The monadic bind operators
<CODE>thenLazyST</CODE> and <CODE>thenStrictST</CODE> are provided so that you can
import <CODE>LazyST</CODE> (say) and still use the strict instance in those
places where it matters. GHC also allows you to write <CODE>LazyST.>>=</CODE>
and <CODE>ST.>>=</CODE> but this is not supported by Hugs yet.
</LI>
</UL>
</P>
<HR>
Previous
<A HREF="libs-2.html">Next</A>
<A HREF="libs.html#toc1">Table of Contents</A>
</BODY>
</HTML>
|