Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert monad to IO [duplicate]

Tags:

haskell

monads

I'd like to use finally, signature IO a -> IO b -> IO a.

However, the operations I wanna use are based on a different monad than IO (namely Servant's ClientM).

I knew liftIO, but that seems to do the opposite -- IO a -> m a.

How can I transform my monads into IOs, or lift finally such as to operate on my monads instead?

like image 253
Kiara Grouwstra Avatar asked Dec 24 '22 07:12

Kiara Grouwstra


1 Answers

Note that ClientM also has a MonadBaseControl IO ClientM instance which is for this sort of thing. For instance, I think the following should typecheck (and can be used with m ~ ClientM).

finally' :: MonadBaseControl IO m => m a -> m b -> m a
finally' x y = control $ \runInIO -> catch (runInIO x) (runInIO y)

EDIT

Not only does the above typecheck, but it is defined in lifted-base as finally.

like image 56
Alec Avatar answered Jan 02 '23 15:01

Alec