planet-mitchell-0.0.0: Planet Mitchell

Safe HaskellNone
LanguageHaskell2010

ST

Contents

Synopsis

ST

data ST s a #

The strict state-transformer monad. A computation of type ST s a transforms an internal state indexed by s, and returns a value of type a. The s parameter is either

  • an uninstantiated type variable (inside invocations of runST), or
  • RealWorld (inside invocations of stToIO).

It serves to keep the internal states of different invocations of runST separate from each other and from invocations of stToIO.

The >>= and >> operations are strict in the state (though not in values stored in the state). For example,

runST (writeSTRef _|_ v >>= f) = _|_
Instances
Monad (ST s)

Since: base-2.1

Instance details

Defined in GHC.ST

Methods

(>>=) :: ST s a -> (a -> ST s b) -> ST s b #

(>>) :: ST s a -> ST s b -> ST s b #

return :: a -> ST s a #

fail :: String -> ST s a #

Functor (ST s)

Since: base-2.1

Instance details

Defined in GHC.ST

Methods

fmap :: (a -> b) -> ST s a -> ST s b #

(<$) :: a -> ST s b -> ST s a #

MonadFix (ST s)

Since: base-2.1

Instance details

Defined in Control.Monad.Fix

Methods

mfix :: (a -> ST s a) -> ST s a #

MonadFail (ST s)

Since: base-4.11.0.0

Instance details

Defined in GHC.ST

Methods

fail :: String -> ST s a #

Applicative (ST s)

Since: base-4.4.0.0

Instance details

Defined in GHC.ST

Methods

pure :: a -> ST s a #

(<*>) :: ST s (a -> b) -> ST s a -> ST s b #

liftA2 :: (a -> b -> c) -> ST s a -> ST s b -> ST s c #

(*>) :: ST s a -> ST s b -> ST s b #

(<*) :: ST s a -> ST s b -> ST s a #

PrimMonad (ST s) 
Instance details

Defined in Control.Monad.Primitive

Associated Types

type PrimState (ST s) :: * #

Methods

primitive :: (State# (PrimState (ST s)) -> (#State# (PrimState (ST s)), a#)) -> ST s a #

PrimBase (ST s) 
Instance details

Defined in Control.Monad.Primitive

Methods

internal :: ST s a -> State# (PrimState (ST s)) -> (#State# (PrimState (ST s)), a#) #

MonadBase (ST s) (ST s) 
Instance details

Defined in Control.Monad.Base

Methods

liftBase :: ST s α -> ST s α #

Show (ST s a)

Since: base-2.1

Instance details

Defined in GHC.ST

Methods

showsPrec :: Int -> ST s a -> ShowS #

show :: ST s a -> String #

showList :: [ST s a] -> ShowS #

Semigroup a => Semigroup (ST s a)

Since: base-4.11.0.0

Instance details

Defined in GHC.ST

Methods

(<>) :: ST s a -> ST s a -> ST s a #

sconcat :: NonEmpty (ST s a) -> ST s a #

stimes :: Integral b => b -> ST s a -> ST s a #

Monoid a => Monoid (ST s a)

Since: base-4.11.0.0

Instance details

Defined in GHC.ST

Methods

mempty :: ST s a #

mappend :: ST s a -> ST s a -> ST s a #

mconcat :: [ST s a] -> ST s a #

Strict (ST s a) (ST s a) 
Instance details

Defined in Control.Lens.Iso

Methods

strict :: Iso' (ST s a) (ST0 s a) #

type PrimState (ST s) 
Instance details

Defined in Control.Monad.Primitive

type PrimState (ST s) = s

runST :: (forall s. ST s a) -> a #

Return the value computed by a state transformer computation. The forall ensures that the internal state used by the ST computation is inaccessible to the rest of the program.

fixST :: (a -> ST s a) -> ST s a #

Allow the result of a state transformer computation to be used (lazily) inside the computation. Note that if f is strict, fixST f = _|_.

data RealWorld #

RealWorld is deeply magical. It is primitive, but it is not unlifted (hence ptrArg). We never manipulate values of type RealWorld; it's only used in the type system, to parameterise State#.

stToIO :: ST RealWorld a -> IO a #

Embed a strict state transformer in an IO action. The RealWorld parameter indicates that the internal state used by the ST computation is a special one supplied by the IO monad, and thus distinct from those used by invocations of runST.

STE

data STE e s a #

Instances
Monad (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

Methods

(>>=) :: STE e s a -> (a -> STE e s b) -> STE e s b #

(>>) :: STE e s a -> STE e s b -> STE e s b #

return :: a -> STE e s a #

fail :: String -> STE e s a #

Functor (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

Methods

fmap :: (a -> b) -> STE e s a -> STE e s b #

(<$) :: a -> STE e s b -> STE e s a #

MonadFix (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

Methods

mfix :: (a -> STE e s a) -> STE e s a #

Applicative (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

Methods

pure :: a -> STE e s a #

(<*>) :: STE e s (a -> b) -> STE e s a -> STE e s b #

liftA2 :: (a -> b -> c) -> STE e s a -> STE e s b -> STE e s c #

(*>) :: STE e s a -> STE e s b -> STE e s b #

(<*) :: STE e s a -> STE e s b -> STE e s a #

SomeException ~ err => MonadThrow (STE err s) 
Instance details

Defined in Control.Monad.STE.Internal

Methods

throwM :: Exception e => e -> STE err s a #

PrimMonad (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

Associated Types

type PrimState (STE e s) :: * #

Methods

primitive :: (State# (PrimState (STE e s)) -> (#State# (PrimState (STE e s)), a#)) -> STE e s a #

PrimBase (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

Methods

internal :: STE e s a -> State# (PrimState (STE e s)) -> (#State# (PrimState (STE e s)), a#) #

type PrimState (STE e s) 
Instance details

Defined in Control.Monad.STE.Internal

type PrimState (STE e s) = s

runSTE :: (forall s. STE e s a) -> (Either e a -> b) -> b #

runSTE is the workhorse of the STE monad. Runs an STE computation, and also does the toplevel handling of the abortive throwSTE operator. The naive way to handle errors is to simply write handleSTE id md. runSTE does not and cannot (by design) handle pure or async exceptions.

fixSTE :: (a -> STE e s a) -> STE e s a #

Allow the result of a state transformer computation to be used (lazily) inside the computation. Note that if f is strict, fixSTE f = _|_.

throwSTE :: e -> STE e s a #

throwSTE is the STE sibling of throwIO, and its argument must match the e parameter in STE e s a. There is also no Exception e constraint. throwSTE should be thought of as an "abort" operation which is guaranteed to be caught/handled by runSTE.

handleSTE :: (Either e a -> b) -> (forall s. STE e s a) -> b #

handleSTE is a flipped convenience function version of runSTE

unsafeInterleaveSTE :: STE e s a -> STE e s a #

unsafeIOToSTE :: IO a -> STE e s a #

unsafeSTEToIO :: STE e s a -> IO a #

STRef

data STRef s a #

a value of type STRef s a is a mutable variable in state thread s, containing a value of type a

>>> :{
runST (do
    ref <- newSTRef "hello"
    x <- readSTRef ref
    writeSTRef ref (x ++ "world")
    readSTRef ref )
:}
"helloworld"
Instances
NFData2 STRef

Since: deepseq-1.4.3.0

Instance details

Defined in Control.DeepSeq

Methods

liftRnf2 :: (a -> ()) -> (b -> ()) -> STRef a b -> () #

NFData1 (STRef s)

Since: deepseq-1.4.3.0

Instance details

Defined in Control.DeepSeq

Methods

liftRnf :: (a -> ()) -> STRef s a -> () #

Eq (STRef s a)

Pointer equality.

Since: base-2.1

Instance details

Defined in GHC.STRef

Methods

(==) :: STRef s a -> STRef s a -> Bool #

(/=) :: STRef s a -> STRef s a -> Bool #

NFData (STRef s a)

NOTE: Only strict in the reference and not the referenced value.

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: STRef s a -> () #

newSTRef :: a -> ST s (STRef s a) #

Build a new STRef in the current state thread

readSTRef :: STRef s a -> ST s a #

Read the value of an STRef

writeSTRef :: STRef s a -> a -> ST s () #

Write a new value into an STRef

modifySTRef :: STRef s a -> (a -> a) -> ST s () #

Mutate the contents of an STRef.

>>> :{
runST (do
    ref <- newSTRef ""
    modifySTRef ref (const "world")
    modifySTRef ref (++ "!")
    modifySTRef ref ("Hello, " ++)
    readSTRef ref )
:}
"Hello, world!"

Be warned that modifySTRef does not apply the function strictly. This means if the program calls modifySTRef many times, but seldomly uses the value, thunks will pile up in memory resulting in a space leak. This is a common mistake made when using an STRef as a counter. For example, the following will leak memory and may produce a stack overflow:

>>> import Control.Monad (replicateM_)
>>> :{
print (runST (do
    ref <- newSTRef 0
    replicateM_ 1000 $ modifySTRef ref (+1)
    readSTRef ref ))
:}
1000

To avoid this problem, use modifySTRef' instead.

modifySTRef' :: STRef s a -> (a -> a) -> ST s () #

Strict version of modifySTRef

Since: base-4.6.0.0