Skip to content

add substFreeT #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Control/Monad/Free/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Control.Monad.Free.Trans
, hoistFreeT
, interpret
, bimapFreeT
, substFreeT
, resume
, runFreeT
) where
Expand Down Expand Up @@ -119,6 +120,16 @@ bimapFreeT :: forall f g m n a. Functor f => Functor n => (f ~> g) -> (m ~> n) -
bimapFreeT nf nm (Bind e) = runExists (\(Bound a f) -> bound (bimapFreeT nf nm <<< a) (bimapFreeT nf nm <<< f)) e
bimapFreeT nf nm (FreeT m) = FreeT \_ -> map (nf <<< map (bimapFreeT nf nm)) <$> nm (m unit)


-- | Like `runFreeT`, but for running into some other FreeT without the
-- | overhead that `MonadRec` incurs.
substFreeT :: forall a m f g. Monad m => Functor g => (f ~> FreeT g m) -> FreeT f m a -> FreeT g m a
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: Monad m is needed because of pure val

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not Applicative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Monad m => Applicative (FreeT f m) because Apply is implemented using ap

substFreeT fBind (Bind e) = runExists (\(Bound a f) -> bound (substFreeT fBind <<< a) (substFreeT fBind <<< f)) e
substFreeT fBind (FreeT m) = join $ FreeT \_ -> m unit <#> case _ of
Left val -> Left $ pure val
Right fFree -> Left $ bound (\_ -> fBind fFree) (substFreeT fBind)


-- | Run a `FreeT` computation to completion.
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
runFreeT interp = tailRecM (go <=< resume)
Expand Down