Skip to content

Commit c85451a

Browse files
Merge pull request #18 from safareli/patch-1
add substFreeT
2 parents 46e9c75 + 8f05d0c commit c85451a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Control/Monad/Free/Trans.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Control.Monad.Free.Trans
77
, hoistFreeT
88
, interpret
99
, bimapFreeT
10+
, substFreeT
1011
, resume
1112
, runFreeT
1213
) where
@@ -119,6 +120,16 @@ bimapFreeT :: forall f g m n a. Functor f => Functor n => (f ~> g) -> (m ~> n) -
119120
bimapFreeT nf nm (Bind e) = runExists (\(Bound a f) -> bound (bimapFreeT nf nm <<< a) (bimapFreeT nf nm <<< f)) e
120121
bimapFreeT nf nm (FreeT m) = FreeT \_ -> map (nf <<< map (bimapFreeT nf nm)) <$> nm (m unit)
121122

123+
124+
-- | Like `runFreeT`, but for running into some other FreeT without the
125+
-- | overhead that `MonadRec` incurs.
126+
substFreeT :: forall a m f g. Monad m => Functor g => (f ~> FreeT g m) -> FreeT f m a -> FreeT g m a
127+
substFreeT fBind (Bind e) = runExists (\(Bound a f) -> bound (substFreeT fBind <<< a) (substFreeT fBind <<< f)) e
128+
substFreeT fBind (FreeT m) = join $ FreeT \_ -> m unit <#> case _ of
129+
Left val -> Left $ pure val
130+
Right fFree -> Left $ bound (\_ -> fBind fFree) (substFreeT fBind)
131+
132+
122133
-- | Run a `FreeT` computation to completion.
123134
runFreeT :: forall f m a. Functor f => MonadRec m => (f (FreeT f m a) -> m (FreeT f m a)) -> FreeT f m a -> m a
124135
runFreeT interp = tailRecM (go <=< resume)

0 commit comments

Comments
 (0)