Skip to content

Commit 51169ff

Browse files
committed
Revert fancy rows in instances. Simplify liftEff/makeAff wrappers.
1 parent 1eff561 commit 51169ff

File tree

7 files changed

+54
-78
lines changed

7 files changed

+54
-78
lines changed

src/Control/Monad/Aff.purs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
module Control.Monad.Aff
22
( module Internal
3+
, liftEff'
34
, forkAff
45
, runAff
56
, killThread
67
, joinThread
78
) where
89

910
import Prelude
10-
import Control.Monad.Aff.Internal (ASYNC, Aff, AffModality, Thread(..), attempt, launchAff, unsafeLaunchAff, unsafeLiftEff)
11-
import Control.Monad.Aff.Internal (Aff, AffModality, ParAff, Thread, Canceler(..), ASYNC, attempt, bracket, delay, launchAff, makeAff, nonCanceler) as Internal
11+
import Control.Monad.Aff.Internal (ASYNC, Aff, Thread(..), attempt, launchAff, unsafeLaunchAff)
12+
import Control.Monad.Aff.Internal (Aff, ParAff, Thread, Canceler(..), ASYNC, attempt, bracket, delay, launchAff, makeAff, nonCanceler) as Internal
1213
import Control.Monad.Eff (Eff)
1314
import Control.Monad.Eff.Class (liftEff)
14-
import Control.Monad.Eff.Exception (Error)
15+
import Control.Monad.Eff.Exception (Error, EXCEPTION)
16+
import Control.Monad.Eff.Unsafe (unsafeCoerceEff)
1517
import Data.Either (Either)
1618

17-
runAff eff a. (Either Error a Eff (AffModality eff) Unit) Aff eff a Eff (async ASYNC | eff) Unit
19+
liftEff' eff a. Eff (exception EXCEPTION | eff) a Aff eff a
20+
liftEff' = liftEff <<< unsafeCoerceEff
21+
22+
runAff eff a. (Either Error a Eff eff Unit) Aff eff a Eff (async ASYNC | eff) Unit
1823
runAff k aff = void $ launchAff $ liftEff <<< k =<< attempt aff
1924

2025
forkAff eff a. Aff eff a Aff eff (Thread eff a)
21-
forkAff = unsafeLiftEff <<< unsafeLaunchAff
26+
forkAff = liftEff <<< unsafeLaunchAff
2227

2328
killThread eff a. Error Thread eff a Aff eff Unit
2429
killThread e (Thread t) = t.kill e

src/Control/Monad/Aff/Class.purs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ module Control.Monad.Aff.Class where
22

33
import Prelude
44

5-
import Control.Monad.Aff (Aff, ASYNC)
5+
import Control.Monad.Aff (Aff)
66
import Control.Monad.Cont.Trans (ContT)
77
import Control.Monad.Eff.Class (class MonadEff)
8-
import Control.Monad.Eff.Exception (EXCEPTION)
98
import Control.Monad.Except.Trans (ExceptT)
109
import Control.Monad.List.Trans (ListT)
1110
import Control.Monad.Maybe.Trans (MaybeT)
@@ -17,8 +16,8 @@ import Control.Monad.Writer.Trans (WriterT)
1716

1817
import Data.Monoid (class Monoid)
1918

20-
class MonadEff (exceptionEXCEPTION, asyncASYNC | eff) m MonadAff eff m | m eff where
21-
liftAff :: forall a. Aff eff a -> m a
19+
class MonadEff eff m MonadAff eff m | m eff where
20+
liftAff a. Aff eff a m a
2221

2322
instance monadAffAffMonadAff e (Aff e) where
2423
liftAff = id

src/Control/Monad/Aff/Internal.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ exports._bind = function (aff) {
6666
};
6767
};
6868

69-
exports.unsafeLiftEff = function (eff) {
69+
exports._liftEff = function (eff) {
7070
return new Aff(SYNC, eff);
7171
};
7272

73-
exports.unsafeMakeAff = function (k) {
73+
exports.makeAff = function (k) {
7474
return new Aff(ASYNC, k);
7575
};
7676

@@ -86,19 +86,6 @@ exports.bracket = function (acquire) {
8686
};
8787
};
8888

89-
exports._makeAff = function (left, right, aff) {
90-
return new Aff(ASYNC, function (k) {
91-
return function () {
92-
try {
93-
return aff(k)();
94-
} catch (error) {
95-
k(left(error))();
96-
return nonCanceler;
97-
}
98-
};
99-
});
100-
};
101-
10289
exports._delay = function () {
10390
var setDelay = function (n, k) {
10491
if (n === 0 && typeof setImmediate !== "undefined") {
@@ -219,7 +206,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
219206

220207
case SYNC:
221208
status = BLOCKED;
222-
result = runSync(step._1);
209+
result = runSync(left, right, step._1);
223210
if (isLeft(result)) {
224211
status = RETURN;
225212
fail = result;
@@ -234,7 +221,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
234221

235222
case ASYNC:
236223
status = BLOCKED;
237-
canceler = step._1(function (result) {
224+
canceler = runAsync(left, step._1, function (result) {
238225
return function () {
239226
if (runTick !== localRunTick) {
240227
return;
@@ -260,7 +247,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
260247
localRunTick = ++runTick;
261248
}
262249
};
263-
})();
250+
});
264251
// If the callback was resolved synchronously, the status will have
265252
// switched to CONTINUE, and we should not move on to PENDING.
266253
if (status === BLOCKED) {
@@ -395,14 +382,6 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
395382
}
396383
}
397384

398-
function runSync (eff) {
399-
try {
400-
return right(eff());
401-
} catch (error) {
402-
return left(error)
403-
}
404-
}
405-
406385
function addJoinCallback (cb) {
407386
var jid = joinId++;
408387
joins[jid] = cb;
@@ -492,3 +471,20 @@ function runJoin (result, cb) {
492471
}, 0)
493472
}
494473
}
474+
475+
function runSync (left, right, eff) {
476+
try {
477+
return right(eff());
478+
} catch (error) {
479+
return left(error)
480+
}
481+
}
482+
483+
function runAsync (left, eff, k) {
484+
try {
485+
return eff(k)();
486+
} catch (error) {
487+
k(left(error))();
488+
return nonCanceler;
489+
}
490+
}

src/Control/Monad/Aff/Internal.purs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Control.Monad.Aff.Internal
22
( Aff
3-
, AffModality
43
, ParAff(..)
54
, Thread(..)
65
, Canceler(..)
@@ -12,17 +11,15 @@ module Control.Monad.Aff.Internal
1211
, bracket
1312
, delay
1413
, unsafeLaunchAff
15-
, unsafeLiftEff
16-
, unsafeMakeAff
1714
) where
1815

1916
import Prelude
2017
import Control.Alt (class Alt)
2118
import Control.Alternative (class Alternative)
2219
import Control.Apply (lift2)
2320
import Control.Monad.Eff (Eff, kind Effect)
24-
import Control.Monad.Eff.Class (class MonadEff)
25-
import Control.Monad.Eff.Exception (EXCEPTION, Error, error)
21+
import Control.Monad.Eff.Class (class MonadEff, liftEff)
22+
import Control.Monad.Eff.Exception (Error, error)
2623
import Control.Monad.Eff.Ref (newRef, readRef, writeRef)
2724
import Control.Monad.Eff.Ref.Unsafe (unsafeRunRef)
2825
import Control.Monad.Error.Class (class MonadError, class MonadThrow, throwError)
@@ -39,19 +36,12 @@ import Data.Monoid (class Monoid, mempty)
3936
import Data.Newtype (class Newtype)
4037
import Data.Time.Duration (Milliseconds(..))
4138
import Partial.Unsafe (unsafeCrashWith)
42-
import Type.Row.Effect.Equality (class EffectRowEquals)
4339
import Unsafe.Coerce (unsafeCoerce)
4440

4541
foreign import data Aff ∷ # Effect Type Type
4642

4743
foreign import data ASYNCEffect
4844

49-
type AffModality eff =
50-
( exception EXCEPTION
51-
, async ASYNC
52-
| eff
53-
)
54-
5545
instance functorAffFunctor (Aff eff) where map = _map
5646
instance applyAffApply (Aff eff) where apply = ap
5747
instance applicativeAffApplicative (Aff eff) where pure = _pure
@@ -99,27 +89,24 @@ instance monadErrorAff ∷ MonadError Error (Aff eff) where
9989
Left err → k err
10090
Right r → pure r
10191

102-
instance monadEffAffEffectRowEquals eff1 (exceptionEXCEPTION, asyncASYNC | eff2) MonadEff eff1 (Aff eff2) where
103-
liftEff eff = unsafeLiftEff (coerceEff eff)
104-
where
105-
coerceEff Eff eff1 ~> Eff eff2
106-
coerceEff = unsafeCoerce
92+
instance monadEffAffMonadEff eff (Aff eff) where
93+
liftEff = _liftEff
10794

10895
newtype ParAff eff a = ParAff (Aff eff a)
10996

11097
derive instance newtypeParAffNewtype (ParAff eff a) _
11198
derive newtype instance functorParAffFunctor (ParAff eff)
11299

113100
instance applyParAffApply (ParAff eff) where
114-
apply (ParAff ff) (ParAff fa) = ParAff (unsafeMakeAff go)
101+
apply (ParAff ff) (ParAff fa) = ParAff (makeAff go)
115102
where
116103
go k = do
117104
Thread t1 ← unsafeLaunchAff ff
118105
Thread t2 ← unsafeLaunchAff fa
119106
Thread t3 ← unsafeLaunchAff do
120107
f ← attempt t1.join
121108
a ← attempt t2.join
122-
unsafeLiftEff (k (f <*> a))
109+
liftEff (k (f <*> a))
123110
pure $ Canceler \err →
124111
parSequence_
125112
[ t3.kill err
@@ -137,7 +124,7 @@ instance monoidParAff ∷ Monoid a ⇒ Monoid (ParAff eff a) where
137124
mempty = pure mempty
138125

139126
instance altParAffAlt (ParAff eff) where
140-
alt (ParAff a1) (ParAff a2) = ParAff (unsafeMakeAff go)
127+
alt (ParAff a1) (ParAff a2) = ParAff (makeAff go)
141128
where
142129
go k = do
143130
ref ← unsafeRunRef $ newRef Nothing
@@ -149,11 +136,11 @@ instance altParAff ∷ Alt (ParAff eff) where
149136
error "Alt ParAff: early exit"
150137

151138
runK t r = do
152-
res ← unsafeLiftEff $ unsafeRunRef $ readRef ref
139+
res ← liftEff $ unsafeRunRef $ readRef ref
153140
case res, r of
154-
Nothing, Left _ → unsafeLiftEff $ unsafeRunRef $ writeRef ref (Just r)
155-
Nothing, Right _ → t.kill earlyError *> unsafeLiftEff (k r)
156-
Just r', _ → t.kill earlyError *> unsafeLiftEff (k r')
141+
Nothing, Left _ → liftEff $ unsafeRunRef $ writeRef ref (Just r)
142+
Nothing, Right _ → t.kill earlyError *> liftEff (k r)
143+
Just r', _ → t.kill earlyError *> liftEff (k r')
157144

158145
Thread t3 ← unsafeLaunchAff $ runK t2 =<< attempt t1.join
159146
Thread t4 ← unsafeLaunchAff $ runK t1 =<< attempt t2.join
@@ -203,9 +190,6 @@ launchAff aff = Fn.runFn6 _launchAff isLeft unsafeFromLeft unsafeFromRight Left
203190
unsafeLaunchAff eff a. Aff eff a Eff eff (Thread eff a)
204191
unsafeLaunchAff = unsafeCoerce launchAff
205192

206-
makeAff eff a. ((Either Error a Eff (AffModality eff) Unit) Eff (AffModality eff) (Canceler eff)) Aff eff a
207-
makeAff k = Fn.runFn3 _makeAff Left Right k
208-
209193
delay eff. Milliseconds Aff eff Unit
210194
delay (Milliseconds n) = Fn.runFn2 _delay Right n
211195

@@ -214,18 +198,10 @@ foreign import _throwError ∷ ∀ eff a. Error → Aff eff a
214198
foreign import _map eff a b. (a b) Aff eff a Aff eff b
215199
foreign import _bind eff a b. Aff eff a (a Aff eff b) Aff eff b
216200
foreign import _delay a eff. Fn.Fn2 (Unit Either a Unit) Number (Aff eff Unit)
201+
foreign import _liftEff eff a. Eff eff a Aff eff a
217202
foreign import attempt eff a. Aff eff a Aff eff (Either Error a)
218203
foreign import bracket eff a b. Aff eff a (a Aff eff Unit) (a Aff eff b) Aff eff b
219-
foreign import unsafeLiftEff eff a. Eff eff a Aff eff a
220-
foreign import unsafeMakeAff eff a. ((Either Error a Eff eff Unit) Eff eff (Canceler eff)) Aff eff a
221-
222-
foreign import _makeAff
223-
eff a
224-
. Fn.Fn3
225-
(Error Either Error a)
226-
(a Either Error a)
227-
((Either Error a Eff (AffModality eff) Unit) Eff (AffModality eff) (Canceler eff))
228-
(Aff eff a)
204+
foreign import makeAff eff a. ((Either Error a Eff eff Unit) Eff eff (Canceler eff)) Aff eff a
229205

230206
foreign import _launchAff
231207
eff a

src/Control/Monad/Aff/Unsafe.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module Control.Monad.Aff.Unsafe
22
( unsafeCoerceAff
3-
, module Internal
3+
, module Control.Monad.Aff.Internal
44
) where
55

66
import Control.Monad.Aff (Aff)
7-
import Control.Monad.Aff.Internal (unsafeLaunchAff, unsafeLiftEff, unsafeMakeAff) as Internal
7+
import Control.Monad.Aff.Internal (unsafeLaunchAff)
88
import Unsafe.Coerce (unsafeCoerce)
99

1010
unsafeCoerceAff eff1 eff2 a. Aff eff1 a -> Aff eff2 a

test/Test/Bench.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ main = do
5959
bench \_ → runPure $ unsafeCoerceEff $ void $ Aff.launchAff $ loop2 10000
6060

6161
Console.log "\nAff fib:"
62-
bench \_ → runPure $ unsafeCoerceEff $ void $ Aff.launchAff $ fib1 100
62+
bench \_ → runPure $ unsafeCoerceEff $ void $ Aff.launchAff $ fib1 20
6363

test/Test/Main.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Data.Traversable (traverse)
1818
import Data.Time.Duration (Milliseconds(..))
1919
import Test.Assert (assert', ASSERT)
2020

21-
type TestEffects eff = (assert ASSERT, console CONSOLE, ref REF | eff)
21+
type TestEffects eff = (assert ASSERT, console CONSOLE, ref REF, exception EXCEPTION | eff)
2222
type TestEff eff = Eff (TestEffects (async ASYNC | eff))
2323
type TestAff eff = Aff (TestEffects eff)
2424

@@ -34,10 +34,10 @@ writeRef r = liftEff <<< Ref.writeRef r
3434
modifyRef m eff a. MonadEff (ref REF | eff) m Ref a (a a) m Unit
3535
modifyRef r = liftEff <<< Ref.modifyRef r
3636

37-
assertEff eff. String Either Error Boolean TestEff (exception EXCEPTION | eff) Unit
37+
assertEff eff. String Either Error Boolean Eff (TestEffects eff) Unit
3838
assertEff s = case _ of
3939
Left err → do
40-
Console.error ("[Error] " <> s)
40+
Console.log ("[Error] " <> s)
4141
throwException err
4242
Right r → do
4343
assert' s r

0 commit comments

Comments
 (0)