on master help sign in
Tighten type signatures of withLiftMap(IO)
commit5084f03a45f743169d5348d897c7bb9d09076cdd authorAndrzej Rybczak <andrzej@rybczak.net> dateTue 12 Oct 2021, 12:51 (5 years ago) parent6df98a7
2 files changed+17−9
effectful-core/src/Effectful/Handler.hs+15−7 effectful/bench/Concurrency.hs+2−2
effectful-core/src/Effectful/Handler.hs+15−7
@@ -124,16 +124,20 @@ localUnliftIO (LocalEnv les) strategy k = case strategy of
124124--
125125-- to
126126--
127−-- @forall localEs. 'Eff' localEs a -> 'Eff' localEs b@
127+-- @'Eff' localEs a -> 'Eff' localEs b@
128128--
129129-- /Note:/ the operation must not run its argument in a separate thread,
130130-- attempting to do so will result in a runtime error.
131131withLiftMap
132132 :: HasCallStack
133− => ((forall a b localEs. (Eff es a -> Eff es b) -> Eff localEs a -> Eff localEs b) -> Eff es r)
133+ => LocalEnv localEs
134+ -- ^ Local environment.
135+ -> ((forall a b. (Eff es a -> Eff es b) -> Eff localEs a -> Eff localEs b) -> Eff es r)
134136 -- ^ Continuation with the lifting function in scope.
135137 -> Eff es r
136−withLiftMap k = unsafeEff $ \es -> do
138+withLiftMap !_ k = unsafeEff $ \es -> do
139+ -- The LocalEnv parameter is not used, but we need it to constraint the
140+ -- localEs type variable. It's also strict so that callers don't cheat.
137141 (`unEff` es) $ k $ \mapEff m -> unsafeEff $ \localEs -> do
138142 seqUnliftEff localEs $ \unlift -> do
139143 (`unEff` es) . mapEff . unsafeEff_ $ unlift m
@@ -144,7 +148,7 @@ withLiftMap k = unsafeEff $ \es -> do
144148--
145149-- to
146150--
147−-- @forall localEs. 'Eff' localEs a -> 'Eff' localEs b@
151+-- @'Eff' localEs a -> 'Eff' localEs b@
148152--
149153-- /Note:/ the operation must not run its argument in a separate thread,
150154-- attempting to do so will result in a runtime error.
@@ -159,16 +163,20 @@ withLiftMap k = unsafeEff $ \es -> do
159163--
160164-- >>> :{
161165-- runFork :: IOE :> es => Eff (Fork : es) a -> Eff es a
162−-- runFork = interpret $ \env (ForkWithUnmask m) -> withLiftMapIO $ \liftMap -> do
166+-- runFork = interpret $ \env (ForkWithUnmask m) -> withLiftMapIO env $ \liftMap -> do
163167-- localUnliftIO env (ConcUnlift Ephemeral $ Limited 1) $ \unlift -> do
164168-- forkIOWithUnmask $ \unmask -> unlift $ m $ liftMap unmask
165169-- :}
166170withLiftMapIO
167171 :: (HasCallStack, IOE :> es)
168− => ((forall a b localEs. (IO a -> IO b) -> Eff localEs a -> Eff localEs b) -> Eff es r)
172+ => LocalEnv localEs
173+ -- ^ Local environment.
174+ -> ((forall a b. (IO a -> IO b) -> Eff localEs a -> Eff localEs b) -> Eff es r)
169175 -- ^ Continuation with the lifting function in scope.
170176 -> Eff es r
171−withLiftMapIO k = k $ \mapIO m -> unsafeEff $ \es -> do
177+withLiftMapIO !_ k = k $ \mapIO m -> unsafeEff $ \es -> do
178+ -- The LocalEnv parameter is not used, but we need it to constraint the
179+ -- localEs type variable. It's also strict so that callers don't cheat.
172180 seqUnliftEff es $ \unlift -> mapIO $ unlift m
173181
174182----------------------------------------
effectful/bench/Concurrency.hs+2−2
@@ -60,14 +60,14 @@ data Fork :: Effect where
6060-- | Uses 'localUnliftIO' and 'withLiftMapIO'.
6161runFork1 :: IOE :> es => Eff (Fork : es) a -> Eff es a
6262runFork1 = interpret $ \env -> \case
63− ForkWithUnmask m -> withLiftMapIO $ \liftMap -> do
63+ ForkWithUnmask m -> withLiftMapIO env $ \liftMap -> do
6464 localUnliftIO env (ConcUnlift Ephemeral $ Limited 1) $ \unlift -> do
6565 asyncWithUnmask $ \unmask -> unlift $ m $ liftMap unmask
6666
6767-- | Uses 'localUnlift' and 'withLiftMap'.
6868runFork2 :: IOE :> es => Eff (Fork : es) a -> Eff es a
6969runFork2 = reinterpret A.runAsyncE $ \env -> \case
70− ForkWithUnmask m -> withLiftMap $ \liftMap -> do
70+ ForkWithUnmask m -> withLiftMap env $ \liftMap -> do
7171 localUnlift env (ConcUnlift Ephemeral $ Limited 1) $ \unlift -> do
7272 A.asyncWithUnmask $ \unmask -> unlift $ m $ liftMap unmask
7373