ki-effectful 0.1.0.0 → 0.1.1.0
raw patch · 5 files changed
+44/−67 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Effectful.Ki: withAwaitAll :: StructuredConcurrency :> es => (STM () -> STM a) -> Eff es a
- Effectful.Ki: withCurrentScope :: StructuredConcurrency :> es => ((forall es' a. StructuredConcurrency :> es' => Eff es' a -> Eff es' a) -> Eff es b) -> Eff es b
- Effectful.Ki: fork :: StructuredConcurrency :> es => Eff es a -> Eff es (Thread a)
+ Effectful.Ki: fork :: StructuredConcurrency :> es => Scope -> Eff es a -> Eff es (Thread a)
- Effectful.Ki: forkTry :: Exception e => StructuredConcurrency :> es => Eff es a -> Eff es (Thread (Either e a))
+ Effectful.Ki: forkTry :: Exception e => StructuredConcurrency :> es => Scope -> Eff es a -> Eff es (Thread (Either e a))
- Effectful.Ki: forkTryWith :: Exception e => StructuredConcurrency :> es => ThreadOptions -> Eff es a -> Eff es (Thread (Either e a))
+ Effectful.Ki: forkTryWith :: Exception e => Scope -> StructuredConcurrency :> es => ThreadOptions -> Eff es a -> Eff es (Thread (Either e a))
- Effectful.Ki: forkWith :: StructuredConcurrency :> es => ThreadOptions -> Eff es a -> Eff es (Thread a)
+ Effectful.Ki: forkWith :: StructuredConcurrency :> es => Scope -> ThreadOptions -> Eff es a -> Eff es (Thread a)
- Effectful.Ki: forkWith_ :: StructuredConcurrency :> es => ThreadOptions -> Eff es Void -> Eff es ()
+ Effectful.Ki: forkWith_ :: StructuredConcurrency :> es => Scope -> ThreadOptions -> Eff es Void -> Eff es ()
- Effectful.Ki: fork_ :: StructuredConcurrency :> es => Eff es Void -> Eff es ()
+ Effectful.Ki: fork_ :: StructuredConcurrency :> es => Scope -> Eff es Void -> Eff es ()
- Effectful.Ki: scoped :: StructuredConcurrency :> es => Eff es a -> Eff es a
+ Effectful.Ki: scoped :: StructuredConcurrency :> es => (Scope -> Eff es a) -> Eff es a
Files
- CHANGELOG.md +7/−2
- README.md +2/−12
- ki-effectful.cabal +1/−1
- src/Effectful/Ki.hs +17/−35
- test/Main.hs +17/−17
CHANGELOG.md view
@@ -1,4 +1,9 @@ # CHANGELOG -## v0.1.0.0-* Initial release+## 0.1.1.0++- The Scope is now an explicit argument. (#6)++## 0.1.0.0++- Initial release
README.md view
@@ -23,18 +23,8 @@ example: ```haskell-scoped :: StructuredConcurrency :> es => Eff es a -> Eff es a-fork :: StructuredConcurrency :> es => Eff es a -> Eff es (Thread a)-```--* Helper function to access the current scope:--```haskell-testScopeLifting :: StructuredConcurrency :> es => Eff es Int-testScopeLifting = withCurrentScope $ \runInScope -> do- child <- scoped $ do- runInScope $ fork $ pure 42- atomically $ await child+scoped :: StructuredConcurrency :> es => (Scope -> Eff es a) -> Eff es a+fork :: StructuredConcurrency :> es => Scope -> Eff es a -> Eff es (Thread a) ``` [effectful]: https://github.com/haskell-effectful/effectful
ki-effectful.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: ki-effectful-version: 0.1.0.0+version: 0.1.1.0 category: Concurrency stability: experimental synopsis: Adaptation of the ki library for the effectful ecosystem.
src/Effectful/Ki.hs view
@@ -13,7 +13,6 @@ -- * Handlers runStructuredConcurrency,- withCurrentScope, -- * Core API Scope,@@ -23,7 +22,6 @@ forkTry, await, awaitAll,- withAwaitAll, -- * Extended API fork_,@@ -63,34 +61,23 @@ data StructuredConcurrency :: Effect type instance DispatchOf StructuredConcurrency = 'Static 'WithSideEffects-data instance StaticRep StructuredConcurrency = StructuredConcurrency Scope+data instance StaticRep StructuredConcurrency = StructuredConcurrency -- | Run the 'StructuredConcurrency' effect. runStructuredConcurrency :: IOE :> es => Eff (StructuredConcurrency : es) a -> Eff es a-runStructuredConcurrency k = withEffToIO $ \runInIO ->- Ki.scoped $ \scope ->- runInIO $ evalStaticRep (StructuredConcurrency scope) k+runStructuredConcurrency = evalStaticRep StructuredConcurrency -scoped :: StructuredConcurrency :> es => Eff es a -> Eff es a-scoped k = reallyUnsafeUnliftIO $ \runInIO ->+scoped :: StructuredConcurrency :> es => (Scope -> Eff es a) -> Eff es a+scoped action = reallyUnsafeUnliftIO $ \runInIO -> Ki.scoped $ \scope ->- runInIO $ localStaticRep (const (StructuredConcurrency scope)) k---- | Provide a callback function to run an action within the current `Scope`.-withCurrentScope ::- StructuredConcurrency :> es =>- ((forall es' a. StructuredConcurrency :> es' => Eff es' a -> Eff es' a) -> Eff es b) ->- Eff es b-withCurrentScope f = do- rep <- getStaticRep @StructuredConcurrency- f (localStaticRep (const rep))+ runInIO $ action scope fork :: StructuredConcurrency :> es =>+ Scope -> Eff es a -> Eff es (Thread a)-fork action = do- StructuredConcurrency scope <- getStaticRep+fork scope action = do unsafeEff $ \es -> do es' <- cloneEnv es Ki.fork scope (unEff action es')@@ -98,62 +85,57 @@ forkTry :: Exception e => StructuredConcurrency :> es =>+ Scope -> Eff es a -> Eff es (Thread (Either e a))-forkTry action = do- StructuredConcurrency scope <- getStaticRep+forkTry scope action = do unsafeEff $ \es -> do es' <- cloneEnv es Ki.forkTry scope (unEff action es') fork_ :: StructuredConcurrency :> es =>+ Scope -> Eff es Void -> Eff es ()-fork_ action = do- StructuredConcurrency scope <- getStaticRep+fork_ scope action = do unsafeEff $ \es -> do es' <- cloneEnv es Ki.fork_ scope (unEff action es') forkWith :: StructuredConcurrency :> es =>+ Scope -> ThreadOptions -> Eff es a -> Eff es (Thread a)-forkWith threadOptions action = do- StructuredConcurrency scope <- getStaticRep+forkWith scope threadOptions action = do unsafeEff $ \es -> do es' <- cloneEnv es Ki.forkWith scope threadOptions (unEff action es') forkWith_ :: StructuredConcurrency :> es =>+ Scope -> ThreadOptions -> Eff es Void -> Eff es ()-forkWith_ threadOptions action = do- StructuredConcurrency scope <- getStaticRep+forkWith_ scope threadOptions action = do unsafeEff $ \es -> do es' <- cloneEnv es Ki.forkWith_ scope threadOptions (unEff action es') forkTryWith :: Exception e =>+ Scope -> StructuredConcurrency :> es => ThreadOptions -> Eff es a -> Eff es (Thread (Either e a))-forkTryWith threadOptions action = do- StructuredConcurrency scope <- getStaticRep+forkTryWith scope threadOptions action = do unsafeEff $ \es -> do es' <- cloneEnv es Ki.forkTryWith scope threadOptions (unEff action es')--withAwaitAll :: StructuredConcurrency :> es => (STM () -> STM a) -> Eff es a-withAwaitAll f = do- StructuredConcurrency scope <- getStaticRep- unsafeEff_ $ STM.atomically $ f $ Ki.awaitAll scope atomically :: StructuredConcurrency :> es => STM a -> Eff es a atomically = unsafeEff_ . STM.atomically
test/Main.hs view
@@ -39,34 +39,34 @@ runStructuredEff = runEff . runStructuredConcurrency testFork :: StructuredConcurrency :> es => Eff es Int-testFork = do- child <- fork $ pure 42+testFork = scoped $ \scope -> do+ child <- fork scope $ pure 42 atomically $ await child testThrow :: StructuredConcurrency :> es => Eff es Int-testThrow = do- _ <- fork $ error "oops"- child <- fork $ pure 42- withAwaitAll $ \waitAll -> do- waitAll- await child+testThrow = scoped $ \scope -> do+ _ <- fork scope $ error "oops"+ child <- fork scope $ pure 42+ atomically $ do+ awaitAll scope+ await child testScopeLifting :: StructuredConcurrency :> es => Eff es Int-testScopeLifting = withCurrentScope $ \runInScope -> do- child <- scoped $ do- runInScope $ fork $ pure 42+testScopeLifting = scoped $ \parentScope -> do+ child <- scoped $ \_localScope -> do+ fork parentScope $ pure 42 atomically $ await child testClient :: StructuredConcurrency :> es => Eff es (Maybe Int)-testClient = do+testClient = scoped $ \scope -> do hitman <- newEmptyTMVarIO- child <- fork $ client hitman (pure 42)+ child <- fork scope $ client hitman (pure 42) atomically $ await child testClientCancel :: (IOE :> es, StructuredConcurrency :> es) => Eff es (Maybe Int)-testClientCancel = do+testClientCancel = scoped $ \scope -> do hitman <- newEmptyTMVarIO- child <- fork $ client hitman $ do+ child <- fork scope $ client hitman $ do -- liftIO $ putStrLn "running" liftIO (threadDelay 500000) pure 42@@ -77,8 +77,8 @@ -- | cancellable client implementation proposed in https://github.com/awkward-squad/ki/issues/11#issuecomment-1214159154 client :: StructuredConcurrency :> es => TMVar () -> Eff es a -> Eff es (Maybe a)-client doneVar action = do- thread <- fork action+client doneVar action = scoped $ \scope -> do+ thread <- fork scope action let waitDone = do () <- readTMVar doneVar pure Nothing