Skip to content

Commit

Permalink
Fix copyRefs in Effect.Dispatch.Dynamic (#240)
Browse files Browse the repository at this point in the history
Fixes #239.
  • Loading branch information
arybczak authored Sep 4, 2024
1 parent adea8b4 commit 06cfe1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion effectful-core/src/Effectful/Dispatch/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ copyRefs (Env soffset srefs sstorage) (Env doffset drefs dstorage) = do
error "storages do not match"
let size = sizeofPrimArray drefs - doffset
es = reifyIndices @es @srcEs
esSize = length es
esSize = 2 * length es
mrefs <- newPrimArray (esSize + size)
copyPrimArray mrefs esSize drefs doffset size
let writeRefs i = \case
Expand Down
20 changes: 13 additions & 7 deletions effectful/tests/EnvTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,30 @@ doubleB = interpose_ $ \case

test_borrowLend :: Assertion
test_borrowLend = runEff $ do
runX 1 2 . evalState @[Int] [3, 4] . runReader () . runReader @[Int] [5, 6] $ do
U.assertEqual "expected result" [1, 2, 3, 4, 5, 6] =<< send X
runX 1 2 . evalState @[Int] [3] . runReader () . runReader @[Int] [4] $ do
U.assertEqual "expected result" [1,2,3,4,1,2,3,4] =<< send X

data X :: Effect where
X :: (State [Int] :> es, Reader [Int] :> es) => X (Eff es) [Int]
X :: (State [Int] :> es, Reader [Int] :> es, Reader () :> es) => X (Eff es) [Int]
type instance DispatchOf X = Dynamic

runX :: Int -> Int -> Eff (X : es) a -> Eff es a
runX s0 r0 = reinterpret (evalState s0 . evalState () . runReader r0) $ \env -> \case
X -> localSeqUnlift env $ \unlift -> do
as <- localSeqLend @[State Int, Reader Int] env $ \withHandlerEffs -> do
unlift . withHandlerEffs $ do
() <- ask
s <- get @Int
r <- ask @Int
pure [s, r]
bs <- localSeqBorrow @[State [Int], Reader [Int]] env $ \withLocalEffs -> do
withLocalEffs $ do
ss <- get @[Int]
rs <- ask @[Int]
pure $ ss ++ rs
pure $ [s, r] ++ ss ++ rs
bs <- localSeqBorrow @[Reader [Int], State [Int], Reader ()] env $ \withEffs -> do
withEffs $ do
() <- ask
s <- get @Int
r <- ask @Int
ss <- get @[Int]
rs <- ask @[Int]
pure $ [s, r] ++ ss ++ rs
pure $ as ++ bs

0 comments on commit 06cfe1d

Please sign in to comment.