Skip to content

Commit 2936289

Browse files
committed
Initial commit
0 parents  commit 2936289

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output
2+
bower_components
3+
.psci
4+
.psci_modules

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Important notice
2+
3+
This module should not yet be depended on, it is for the upcoming 0.7 compiler release.
4+
5+
# Module Documentation
6+
7+
## Module Control.Monad.ST
8+
9+
#### `ST`
10+
11+
``` purescript
12+
data ST :: * -> !
13+
```
14+
15+
The `ST` effect represents _local mutation_, i.e. mutation which does not "escape" into the surrounding computation.
16+
17+
An `ST` computation is parameterized by a phantom type which is used to restrict the set of reference cells it is allowed to access.
18+
19+
The `runST` function can be used to handle the `ST` effect.
20+
21+
#### `STRef`
22+
23+
``` purescript
24+
data STRef :: * -> * -> *
25+
```
26+
27+
The type `STRef s a` represents a mutable reference holding a value of type `a`, which can be used with the `ST s` effect.
28+
29+
#### `newSTRef`
30+
31+
``` purescript
32+
newSTRef :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
33+
```
34+
35+
Create a new mutable reference.
36+
37+
#### `readSTRef`
38+
39+
``` purescript
40+
readSTRef :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
41+
```
42+
43+
Read the current value of a mutable reference.
44+
45+
#### `modifySTRef`
46+
47+
``` purescript
48+
modifySTRef :: forall a h r. STRef h a -> (a -> a) -> Eff (st :: ST h | r) a
49+
```
50+
51+
Modify the value of a mutable reference by applying a function to the current value.
52+
53+
#### `writeSTRef`
54+
55+
``` purescript
56+
writeSTRef :: forall a h r. STRef h a -> a -> Eff (st :: ST h | r) a
57+
```
58+
59+
Set the value of a mutable reference.
60+
61+
#### `runST`
62+
63+
``` purescript
64+
runST :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
65+
```
66+
67+
Run an `ST` computation.
68+
69+
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
70+
to the surrounding computation.
71+
72+
It may cause problems to apply this function using the `$` operator. The recommended approach is to use parentheses instead.
73+
74+
#### `pureST`
75+
76+
``` purescript
77+
pureST :: forall a. (forall h r. Eff (st :: ST h | r) a) -> a
78+
```
79+
80+
A convenience function which combines `runST` with `runPure`, which can be used when the only required effect is `ST`.
81+
82+
Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach
83+
is to use parentheses instead.
84+
85+
86+

bower.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "purescript-st",
3+
"version": "0.1.0",
4+
"moduleType": [
5+
"node"
6+
],
7+
"ignore": [
8+
"**/.*",
9+
"node_modules",
10+
"bower_components",
11+
"output"
12+
],
13+
"dependencies": {
14+
"purescript-eff": "~0.1.0"
15+
}
16+
}

src/Control/Monad/ST.purs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module Control.Monad.ST where
2+
3+
import Control.Monad.Eff
4+
5+
-- | The `ST` effect represents _local mutation_, i.e. mutation which does not "escape" into the surrounding computation.
6+
-- |
7+
-- | 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+
-- |
9+
-- | The `runST` function can be used to handle the `ST` effect.
10+
foreign import data ST :: * -> !
11+
12+
-- | The type `STRef s a` represents a mutable reference holding a value of type `a`, which can be used with the `ST s` effect.
13+
foreign import data STRef :: * -> * -> *
14+
15+
-- | Create a new mutable reference.
16+
foreign import newSTRef
17+
"""
18+
function newSTRef(val) {
19+
return function() {
20+
return { value: val };
21+
};
22+
}
23+
""" :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
24+
25+
-- | Read the current value of a mutable reference.
26+
foreign import readSTRef
27+
"""
28+
function readSTRef(ref) {
29+
return function() {
30+
return ref.value;
31+
};
32+
}
33+
""" :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
34+
35+
-- | Modify the value of a mutable reference by applying a function to the current value.
36+
foreign import modifySTRef
37+
"""
38+
function modifySTRef(ref) {
39+
return function(f) {
40+
return function() {
41+
return ref.value = f(ref.value);
42+
};
43+
};
44+
}
45+
""" :: forall a h r. STRef h a -> (a -> a) -> Eff (st :: ST h | r) a
46+
47+
-- | Set the value of a mutable reference.
48+
foreign import writeSTRef
49+
"""
50+
function writeSTRef(ref) {
51+
return function(a) {
52+
return function() {
53+
return ref.value = a;
54+
};
55+
};
56+
}
57+
""" :: forall a h r. STRef h a -> a -> Eff (st :: ST h | r) a
58+
59+
-- | Run an `ST` computation.
60+
-- |
61+
-- | 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
62+
-- | to the surrounding computation.
63+
-- |
64+
-- | It may cause problems to apply this function using the `$` operator. The recommended approach is to use parentheses instead.
65+
foreign import runST
66+
"""
67+
function runST(f) {
68+
return f;
69+
}
70+
""" :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
71+
72+
-- | A convenience function which combines `runST` with `runPure`, which can be used when the only required effect is `ST`.
73+
-- |
74+
-- | Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach
75+
-- | is to use parentheses instead.
76+
pureST :: forall a. (forall h r. Eff (st :: ST h | r) a) -> a
77+
pureST st = runPure (runST st)

0 commit comments

Comments
 (0)