Skip to content

Commit acef60c

Browse files
committed
Dependency updates, drop ST naming from functions, put STRef argument last
1 parent 19bd190 commit acef60c

File tree

6 files changed

+106
-90
lines changed

6 files changed

+106
-90
lines changed

bower.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"bower.json",
1717
"package.json"
1818
],
19-
"devDependencies": {
20-
"purescript-console": "^3.0.0"
21-
},
2219
"dependencies": {
23-
"purescript-prelude": "^3.1.1"
20+
"purescript-prelude": "#compiler/0.12"
21+
},
22+
"devDependencies": {
23+
"purescript-console": "#compiler/0.12"
2424
}
2525
}

src/Control/Monad/ST.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,37 @@
11
"use strict";
22

3-
exports.mapST = function (f) {
3+
exports.map_ = function (f) {
44
return function (a) {
55
return function () {
66
return f(a());
77
};
88
};
99
};
1010

11-
exports.pureST_ = function (a) {
11+
exports.pure_ = function (a) {
1212
return function () {
1313
return a;
1414
};
1515
};
1616

17-
exports.bindST_ = function (a) {
17+
exports.bind_ = function (a) {
1818
return function (f) {
1919
return function () {
2020
return f(a())();
2121
};
2222
};
2323
};
2424

25-
exports.newSTRef = function (val) {
26-
return function () {
27-
return { value: val };
28-
};
29-
};
30-
31-
exports.readSTRef = function (ref) {
32-
return function () {
33-
return ref.value;
34-
};
35-
};
36-
37-
exports.modifySTRef = function (ref) {
38-
return function (f) {
39-
return function () {
40-
return ref.value = f(ref.value); // eslint-disable-line no-return-assign
41-
};
42-
};
25+
exports.run = function (f) {
26+
return f();
4327
};
4428

45-
exports.writeSTRef = function (ref) {
29+
exports["while"] = function (f) {
4630
return function (a) {
4731
return function () {
48-
return ref.value = a; // eslint-disable-line no-return-assign
32+
while (f()) {
33+
a();
34+
}
4935
};
5036
};
5137
};
52-
53-
exports.pureST = function (f) {
54-
return f();
55-
};

src/Control/Monad/ST.purs

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,58 @@
11
module Control.Monad.ST
22
( kind Region
33
, ST
4-
, STRef
5-
, newSTRef
6-
, readSTRef
7-
, modifySTRef
8-
, writeSTRef
9-
, pureST
4+
, run
5+
, while
106
) where
117

128
import Prelude
139

14-
-- | `ST` is concerned with _restricted_ mutation. Mutation
15-
-- | is restricted to a _region_ of mutable references.
16-
-- | This kind is inhabited by phantom types which represent
17-
-- | regions in the type system.
10+
-- | `ST` is concerned with _restricted_ mutation. Mutation is restricted to a
11+
-- | _region_ of mutable references. This kind is inhabited by phantom types
12+
-- | which represent regions in the type system.
1813
foreign import kind Region
1914

20-
-- | The `ST` type constructor allows _local mutation_, i.e.
21-
-- | mutation which does not "escape" into the surrounding
22-
-- | computation.
15+
-- | The `ST` type constructor allows _local mutation_, i.e. mutation which
16+
-- | does not "escape" into the surrounding computation.
2317
-- |
2418
-- | An `ST` computation is parameterized by a phantom type which is used to
2519
-- | restrict the set of reference cells it is allowed to access.
2620
-- |
27-
-- | The `runST` function can be used to run a computation in the
28-
-- | `ST` monad.
21+
-- | The `run` function can be used to run a computation in the `ST` monad.
2922
foreign import data ST :: Region -> Type -> Type
3023

31-
foreign import mapST
32-
:: forall r a b
33-
. (a -> b)
34-
-> ST r a
35-
-> ST r b
24+
foreign import map_ :: forall r a b. (a -> b) -> ST r a -> ST r b
3625

37-
foreign import pureST_ :: forall r a. a -> ST r a
26+
foreign import pure_ :: forall r a. a -> ST r a
3827

39-
foreign import bindST_
40-
:: forall r a b
41-
. ST r a
42-
-> (a -> ST r b)
43-
-> ST r b
28+
foreign import bind_ :: forall r a b. ST r a -> (a -> ST r b) -> ST r b
4429

4530
instance functorST :: Functor (ST r) where
46-
map = mapST
31+
map = map_
4732

4833
instance applyST :: Apply (ST r) where
4934
apply = ap
5035

5136
instance applicativeST :: Applicative (ST r) where
52-
pure = pureST_
37+
pure = pure_
5338

5439
instance bindST :: Bind (ST r) where
55-
bind = bindST_
40+
bind = bind_
5641

5742
instance monadST :: Monad (ST r)
5843

59-
-- | The type `STRef r a` represents a mutable reference holding a value of
60-
-- | type `a`, which can be used with the `ST r` effect.
61-
foreign import data STRef :: Region -> Type -> Type
62-
63-
-- | Create a new mutable reference.
64-
foreign import newSTRef :: forall a r. a -> ST r (STRef r a)
65-
66-
-- | Read the current value of a mutable reference.
67-
foreign import readSTRef :: forall a r. STRef r a -> ST r a
68-
69-
-- | Modify the value of a mutable reference by applying a function to the
70-
-- | current value.
71-
foreign import modifySTRef :: forall a r. STRef r a -> (a -> a) -> ST r a
72-
73-
-- | Set the value of a mutable reference.
74-
foreign import writeSTRef :: forall a r. STRef r a -> a -> ST r a
75-
7644
-- | Run an `ST` computation.
7745
-- |
78-
-- | Note: the type of `runST` uses a rank-2 type to constrain the phantom
46+
-- | Note: the type of `run` uses a rank-2 type to constrain the phantom
7947
-- | type `h`, such that the computation must not leak any mutable references
80-
-- | to the surrounding computation.
48+
-- | to the surrounding computation. It may cause problems to apply this
49+
-- | function using the `$` operator. The recommended approach is to use
50+
-- | parentheses instead.
51+
foreign import run :: forall a. (forall r. ST r a) -> a
52+
53+
-- | Loop while a condition is `true`.
8154
-- |
82-
-- | It may cause problems to apply this function using the `$` operator. The
83-
-- | recommended approach is to use parentheses instead.
84-
foreign import pureST :: forall a. (forall r. ST r a) -> a
55+
-- | `while b m` is ST computation which runs the ST computation `b`. If its
56+
-- | result is `true`, it runs the ST computation `m` and loops. If not, the
57+
-- | computation ends.
58+
foreign import while :: forall r a. ST r Boolean -> ST r a -> ST r Unit

src/Control/Monad/ST/Ref.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
exports.new = function (val) {
4+
return function () {
5+
return { value: val };
6+
};
7+
};
8+
9+
exports.read = function (ref) {
10+
return function () {
11+
return ref.value;
12+
};
13+
};
14+
15+
exports["modify'"] = function (f) {
16+
return function (ref) {
17+
return function () {
18+
var t = f(ref.value);
19+
ref.value = t.state;
20+
return t.value;
21+
};
22+
};
23+
};
24+
25+
exports.write = function (a) {
26+
return function (ref) {
27+
return function () {
28+
return ref.value = a; // eslint-disable-line no-return-assign
29+
};
30+
};
31+
};

src/Control/Monad/ST/Ref.purs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Control.Monad.ST.Ref where
2+
3+
import Prelude
4+
5+
import Control.Monad.ST (ST, kind Region)
6+
7+
-- | The type `STRef r a` represents a mutable reference holding a value of
8+
-- | type `a`, which can be used with the `ST r` effect.
9+
foreign import data STRef :: Region -> Type -> Type
10+
11+
-- | Create a new mutable reference.
12+
foreign import new :: forall a r. a -> ST r (STRef r a)
13+
14+
-- | Read the current value of a mutable reference.
15+
foreign import read :: forall a r. STRef r a -> ST r a
16+
17+
-- | Update the value of a mutable reference by applying a function
18+
-- | to the current value, computing a new state value for the reference and
19+
-- | a return value.
20+
foreign import modify' :: forall r a b. (a -> { state :: a, value :: b }) -> STRef r a -> ST r b
21+
22+
-- | Modify the value of a mutable reference by applying a function to the
23+
-- | current value.
24+
modify :: forall r a. (a -> a) -> STRef r a -> ST r Unit
25+
modify f = modify' (\s -> { state: f s, value: unit })
26+
27+
-- | Set the value of a mutable reference.
28+
foreign import write :: forall a r. a -> STRef r a -> ST r a

test/Main.purs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ module Test.Main where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, logShow)
7-
import Control.Monad.ST (modifySTRef, newSTRef, readSTRef, pureST)
5+
import Effect (Effect)
6+
import Effect.Console (logShow)
7+
import Control.Monad.ST as ST
8+
import Control.Monad.ST.Ref as STRef
89

910
sumOfSquares :: Int
10-
sumOfSquares = pureST do
11-
total <- newSTRef 0
12-
let loop 0 = readSTRef total
11+
sumOfSquares = ST.run do
12+
total <- STRef.new 0
13+
let loop 0 = STRef.read total
1314
loop n = do
14-
_ <- modifySTRef total (_ + (n * n))
15+
_ <- STRef.modify (_ + (n * n)) total
1516
loop (n - 1)
1617
loop 100
1718

18-
main :: Eff (console :: CONSOLE) Unit
19+
main :: Effect Unit
1920
main = do
2021
logShow sumOfSquares

0 commit comments

Comments
 (0)