11module Control.Monad.Aff
2- ( Aff ()
2+ ( Aff
33 , Canceler (..)
44 , PureAff (..)
55 , apathize
@@ -17,8 +17,8 @@ module Control.Monad.Aff
1717 , makeAff'
1818 , nonCanceler
1919 , runAff
20- )
21- where
20+ , ParAff (.. )
21+ ) where
2222
2323import Prelude
2424
@@ -30,15 +30,16 @@ import Control.Monad.Eff (Eff)
3030import Control.Monad.Eff.Class (class MonadEff )
3131import Control.Monad.Eff.Exception (Error , EXCEPTION , throwException , error )
3232import Control.Monad.Error.Class (class MonadError , throwError )
33- import Control.Monad.Rec.Class (class MonadRec )
33+ import Control.Monad.Rec.Class (class MonadRec , Step (..) )
3434import Control.MonadPlus (class MonadZero , class MonadPlus )
35- import Control.Parallel.Class (class MonadRace , class MonadPar )
36- import Control.Plus (class Plus )
35+ import Control.Parallel (class Parallel )
36+ import Control.Plus (class Plus , empty )
3737
38- import Data.Either (Either (..), either , isLeft )
38+ import Data.Either (Either (..), either )
3939import Data.Foldable (class Foldable , foldl )
4040import Data.Function.Uncurried (Fn2 , Fn3 , runFn2 , runFn3 )
4141import Data.Monoid (class Monoid , mempty )
42+ import Data.Newtype (class Newtype )
4243
4344import Unsafe.Coerce (unsafeCoerce )
4445
@@ -202,7 +203,10 @@ instance monadZero :: MonadZero (Aff e)
202203instance monadPlusAff :: MonadPlus (Aff e )
203204
204205instance monadRecAff :: MonadRec (Aff e ) where
205- tailRecM f a = runFn3 _tailRecM isLeft f a
206+ tailRecM f a = runFn3 _tailRecM isLoop f a
207+ where
208+ isLoop (Loop _) = true
209+ isLoop _ = false
206210
207211instance monadContAff :: MonadCont (Aff e ) where
208212 callCC f = makeAff (\eb cb -> void $ runAff eb cb (f \a -> makeAff (\_ _ -> cb a)))
@@ -213,20 +217,34 @@ instance semigroupCanceler :: Semigroup (Canceler e) where
213217instance monoidCanceler :: Monoid (Canceler e ) where
214218 mempty = Canceler (const (pure true ))
215219
216- instance monadParAff :: MonadPar (Aff e ) where
217- par f ma mb = do
220+ newtype ParAff e a = ParAff (Aff e a )
221+
222+ derive instance newtypeParAff :: Newtype (ParAff e a ) _
223+
224+ instance semigroupParAff :: (Semigroup a ) => Semigroup (ParAff e a ) where
225+ append a b = append <$> a <*> b
226+
227+ instance monoidParAff :: (Monoid a ) => Monoid (ParAff e a ) where
228+ mempty = pure mempty
229+
230+ derive newtype instance functorParAff :: Functor (ParAff e )
231+
232+ instance applyParAff :: Apply (ParAff e ) where
233+ apply (ParAff ff) (ParAff fa) = ParAff do
218234 va <- makeVar
219235 vb <- makeVar
220- c1 <- forkAff (putOrKill va =<< attempt ma )
221- c2 <- forkAff (putOrKill vb =<< attempt mb )
222- f <$> (takeVar va) <*> ( takeVar vb)
236+ c1 <- forkAff (putOrKill va =<< attempt ff )
237+ c2 <- forkAff (putOrKill vb =<< attempt fa )
238+ (takeVar va <*> takeVar vb) `cancelWith` (c1 <> c2 )
223239 where
224240 putOrKill :: forall a . AVar a -> Either Error a -> Aff e Unit
225241 putOrKill v = either (killVar v) (putVar v)
226242
227- instance monadRaceAff :: MonadRace (Aff e ) where
228- stall = throwError $ error " Stalled"
229- race a1 a2 = do
243+ derive newtype instance applicativeParAff :: Applicative (ParAff e )
244+
245+ -- | Returns the first value, or the first error if both error.
246+ instance altParAff :: Alt (ParAff e ) where
247+ alt (ParAff a1) (ParAff a2) = ParAff do
230248 va <- makeVar -- the `a` value
231249 ve <- makeVar -- the error count (starts at 0)
232250 putVar ve 0
@@ -237,9 +255,18 @@ instance monadRaceAff :: MonadRace (Aff e) where
237255 maybeKill :: forall a . AVar a -> AVar Int -> Error -> Aff e Unit
238256 maybeKill va ve err = do
239257 e <- takeVar ve
240- if e == 1 then killVar va err else pure unit
258+ when ( e == 1 ) $ killVar va err
241259 putVar ve (e + 1 )
242260
261+ instance plusParAff :: Plus (ParAff e ) where
262+ empty = ParAff empty
263+
264+ instance alternativeParAff :: Alternative (ParAff e )
265+
266+ instance parallelParAff :: Parallel (ParAff e ) (Aff e ) where
267+ parallel = ParAff
268+ sequential (ParAff ma) = ma
269+
243270makeVar :: forall e a . Aff e (AVar a )
244271makeVar = fromAVBox $ _makeVar nonCanceler
245272
@@ -281,4 +308,4 @@ foreign import _runAff :: forall e a. Fn3 (Error -> Eff e Unit) (a -> Eff e Unit
281308
282309foreign import _liftEff :: forall e a . Fn2 (Canceler e ) (Eff e a ) (Aff e a )
283310
284- foreign import _tailRecM :: forall e a b . Fn3 (Either a b -> Boolean ) (a -> Aff e (Either a b )) a (Aff e b )
311+ foreign import _tailRecM :: forall e a b . Fn3 (Step a b -> Boolean ) (a -> Aff e (Step a b )) a (Aff e b )
0 commit comments