Skip to content

Commit

Permalink
Generalize Haxl.Core.Monad.mapWrites
Browse files Browse the repository at this point in the history
Summary:
On my last diff I forgot to generalize `mapWrites` in Haxl core, leaving it working only with `WriteTree`s. Mapping functionality is needed if we want to use alternative data structures.

Luckily, the function can be trivially generalized by mapping the `Monoid` instead and exposing `mapWriteTree` to lift a function mapping elements to one mapping `WriteTree`s.

Reviewed By: josefs

Differential Revision: D39500863

fbshipit-source-id: c0c140544e135814575bc1500fec649fabc1fdec
  • Loading branch information
ruippeixotog authored and facebook-github-bot committed Sep 15, 2022
1 parent 260a97b commit ef52a52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Haxl/Core/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Haxl.Core.Monad
, appendWTs
, mbModifyWLRef
, mapWrites
, mapWriteTree

-- * Cont
, Cont(..)
Expand Down Expand Up @@ -912,10 +913,10 @@ nextCallId env = atomicModifyIORef' (callIdRef env) $ \x -> (x+1,x+1)
-- Memoization behavior is unchanged, meaning if a memoized computation is run
-- once inside @mapWrites@ and then once without, the writes from the second run
-- will NOT be transformed.
mapWrites :: (w -> w) -> GenHaxl u (WriteTree w) a -> GenHaxl u (WriteTree w) a
mapWrites :: Monoid w => (w -> w) -> GenHaxl u w a -> GenHaxl u w a
mapWrites f action = GenHaxl $ \curEnv -> do
wlogs <- newIORef NilWrites
wlogsNoMemo <- newIORef NilWrites
wlogs <- newIORef mempty
wlogsNoMemo <- newIORef mempty
let
!newEnv = curEnv { writeLogsRef = wlogs, writeLogsRefNoMemo = wlogsNoMemo }
unHaxl (mapWritesImpl curEnv newEnv action) newEnv
Expand All @@ -924,9 +925,9 @@ mapWrites f action = GenHaxl $ \curEnv -> do
let
pushTransformedWrites = do
wt <- readIORef $ writeLogsRef curEnv
mbModifyWLRef (mapWriteTree f wt) (writeLogsRef oldEnv)
modifyIORef' (writeLogsRef oldEnv) (<> f wt)
wtNoMemo <- readIORef $ writeLogsRefNoMemo curEnv
mbModifyWLRef (mapWriteTree f wtNoMemo) (writeLogsRefNoMemo oldEnv)
modifyIORef' (writeLogsRefNoMemo oldEnv) (<> f wtNoMemo)

r <- m curEnv

Expand Down
5 changes: 3 additions & 2 deletions tests/WriteTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Data.Hashable
import Data.IORef
import qualified Data.Text as Text

import Haxl.Core.Monad (mapWrites, flattenWT, WriteTree)
import Haxl.Core.Monad (mapWrites, mapWriteTree, flattenWT, WriteTree)
import Haxl.Core
import Haxl.Prelude as Haxl

Expand Down Expand Up @@ -182,7 +182,8 @@ writeLogsCorrectnessTest = TestLabel "writeLogs_correctness" $ TestCase $ do

mapWritesTest :: Test
mapWritesTest = TestLabel "mapWrites" $ TestCase $ do
let func (SimpleWrite s) = SimpleWrite $ Text.toUpper s
let funcSingle (SimpleWrite s) = SimpleWrite $ Text.toUpper s
func = mapWriteTree funcSingle
env0 <- emptyEnv ()
(res0, wrts0) <- runHaxlWithWriteList env0 $ mapWrites func doNonMemoWrites
assertEqual "Expected computation result" 0 res0
Expand Down

0 comments on commit ef52a52

Please sign in to comment.