|
1 | 1 | module Control.Monad.ST where
|
2 | 2 |
|
3 |
| -import Prelude |
4 |
| - |
5 | 3 | import Control.Monad.Eff (Eff(), runPure)
|
6 | 4 |
|
7 |
| --- | The `ST` effect represents _local mutation_, i.e. mutation which does not "escape" into the surrounding computation. |
| 5 | +-- | The `ST` effect represents _local mutation_, i.e. mutation which does not |
| 6 | +-- | "escape" into the surrounding computation. |
8 | 7 | -- |
|
9 |
| --- | An `ST` computation is parameterized by a phantom type which is used to restrict the set of reference cells it is allowed to access. |
| 8 | +-- | An `ST` computation is parameterized by a phantom type which is used to |
| 9 | +-- | restrict the set of reference cells it is allowed to access. |
10 | 10 | -- |
|
11 | 11 | -- | The `runST` function can be used to handle the `ST` effect.
|
12 | 12 | foreign import data ST :: * -> !
|
13 | 13 |
|
14 |
| --- | The type `STRef s a` represents a mutable reference holding a value of type `a`, which can be used with the `ST s` effect. |
| 14 | +-- | The type `STRef s a` represents a mutable reference holding a value of |
| 15 | +-- | type `a`, which can be used with the `ST s` effect. |
15 | 16 | foreign import data STRef :: * -> * -> *
|
16 | 17 |
|
17 | 18 | -- | Create a new mutable reference.
|
18 |
| -foreign import newSTRef :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a) |
| 19 | +foreign import newSTRef |
| 20 | + :: forall a h r |
| 21 | + . a |
| 22 | + -> Eff (st :: ST h | r) (STRef h a) |
19 | 23 |
|
20 | 24 | -- | Read the current value of a mutable reference.
|
21 |
| -foreign import readSTRef :: forall a h r. STRef h a -> Eff (st :: ST h | r) a |
| 25 | +foreign import readSTRef |
| 26 | + :: forall a h r |
| 27 | + . STRef h a |
| 28 | + -> Eff (st :: ST h | r) a |
22 | 29 |
|
23 |
| --- | Modify the value of a mutable reference by applying a function to the current value. |
24 |
| -foreign import modifySTRef :: forall a h r. STRef h a -> (a -> a) -> Eff (st :: ST h | r) a |
| 30 | +-- | Modify the value of a mutable reference by applying a function to the |
| 31 | +-- | current value. |
| 32 | +foreign import modifySTRef |
| 33 | + :: forall a h r |
| 34 | + . STRef h a -> (a -> a) |
| 35 | + -> Eff (st :: ST h | r) a |
25 | 36 |
|
26 | 37 | -- | Set the value of a mutable reference.
|
27 |
| -foreign import writeSTRef :: forall a h r. STRef h a -> a -> Eff (st :: ST h | r) a |
| 38 | +foreign import writeSTRef |
| 39 | + :: forall a h r |
| 40 | + . STRef h a |
| 41 | + -> a |
| 42 | + -> Eff (st :: ST h | r) a |
28 | 43 |
|
29 | 44 | -- | Run an `ST` computation.
|
30 | 45 | -- |
|
31 |
| --- | Note: the type of `runST` uses a rank-2 type to constrain the phantom type `s`, such that the computation must not leak any mutable references |
| 46 | +-- | Note: the type of `runST` uses a rank-2 type to constrain the phantom |
| 47 | +-- | type `s`, such that the computation must not leak any mutable references |
32 | 48 | -- | to the surrounding computation.
|
33 | 49 | -- |
|
34 |
| --- | It may cause problems to apply this function using the `$` operator. The recommended approach is to use parentheses instead. |
35 |
| -foreign import runST :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a |
| 50 | +-- | It may cause problems to apply this function using the `$` operator. The |
| 51 | +-- | recommended approach is to use parentheses instead. |
| 52 | +foreign import runST |
| 53 | + :: forall a r |
| 54 | + . (forall h. Eff (st :: ST h | r) a) |
| 55 | + -> Eff r a |
36 | 56 |
|
37 |
| --- | A convenience function which combines `runST` with `runPure`, which can be used when the only required effect is `ST`. |
| 57 | +-- | A convenience function which combines `runST` with `runPure`, which can be |
| 58 | +-- | used when the only required effect is `ST`. |
38 | 59 | -- |
|
39 |
| --- | Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach |
40 |
| --- | is to use parentheses instead. |
| 60 | +-- | Note: since this function has a rank-2 type, it may cause problems to apply |
| 61 | +-- | this function using the `$` operator. The recommended approach is to use |
| 62 | +-- | parentheses instead. |
41 | 63 | pureST :: forall a. (forall h. Eff (st :: ST h) a) -> a
|
42 | 64 | pureST st = runPure (runST st)
|
0 commit comments