Module

Control.Monad.ST.Internal

Package
purescript-st
Repository
purescript/purescript-st

#Region Source

data Region :: Type

ST is concerned with restricted mutation. Mutation is restricted to a region of mutable references. This kind is inhabited by phantom types which represent regions in the type system.

#ST Source

data ST :: Region -> Type -> Type

The ST type constructor allows local mutation, i.e. mutation which does not "escape" into the surrounding computation.

An ST computation is parameterized by a phantom type which is used to restrict the set of reference cells it is allowed to access.

The run function can be used to run a computation in the ST monad.

Instances

#map_ Source

map_ :: forall b a r. (a -> b) -> ST r a -> ST r b

#pure_ Source

pure_ :: forall a r. a -> ST r a

#bind_ Source

bind_ :: forall b a r. ST r a -> (a -> ST r b) -> ST r b

#run Source

run :: forall a. (forall r. ST r a) -> a

Run an ST computation.

Note: the type of run uses a rank-2 type to constrain the phantom type h, such that the computation must not leak any mutable references to the surrounding computation. It may cause problems to apply this function using the $ operator. The recommended approach is to use parentheses instead.

#while Source

while :: forall a r. ST r Boolean -> ST r a -> ST r Unit

Loop while a condition is true.

while b m is ST computation which runs the ST computation b. If its result is true, it runs the ST computation m and loops. If not, the computation ends.

#for Source

for :: forall a r. Int -> Int -> (Int -> ST r a) -> ST r Unit

Loop over a consecutive collection of numbers

ST.for lo hi f runs the computation returned by the function f for each of the inputs between lo (inclusive) and hi (exclusive).

#foreach Source

foreach :: forall a r. Array a -> (a -> ST r Unit) -> ST r Unit

Loop over an array of values.

ST.foreach xs f runs the computation returned by the function f for each of the inputs xs.

#STRef Source

data STRef :: Region -> Type -> Type

The type STRef r a represents a mutable reference holding a value of type a, which can be used with the ST r effect.

#new Source

new :: forall r a. a -> ST r (STRef r a)

Create a new mutable reference.

#read Source

read :: forall r a. STRef r a -> ST r a

Read the current value of a mutable reference.

#modify' Source

modify' :: forall b a r. (a -> { state :: a, value :: b }) -> STRef r a -> ST r b

Update the value of a mutable reference by applying a function to the current value, computing a new state value for the reference and a return value.

#modify Source

modify :: forall a r. (a -> a) -> STRef r a -> ST r a

Modify the value of a mutable reference by applying a function to the current value. The modified value is returned.

#write Source

write :: forall r a. a -> STRef r a -> ST r a

Set the value of a mutable reference.