Skip to content

Refactorisation of MonadTimers & monad transformer instances #57

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 34 commits into from
Apr 14, 2023

Conversation

coot
Copy link
Collaborator

@coot coot commented Dec 7, 2022

No description provided.

@coot coot requested a review from dcoutts December 7, 2022 16:00
@coot coot force-pushed the coot/monad-timers branch from 85d9a8c to d8b5f14 Compare December 8, 2022 14:09
@coot coot linked an issue Dec 8, 2022 that may be closed by this pull request
@coot coot force-pushed the coot/monad-timers branch 2 times, most recently from c1cdd26 to 5cb6170 Compare December 9, 2022 18:47
@coot coot changed the title coot/monad timers Refactorisation of MonadTimers & monad transformer instances Dec 9, 2022
@coot coot requested a review from bolt12 December 9, 2022 21:05
@coot coot force-pushed the coot/monad-timers branch 2 times, most recently from 4cbb906 to 2776115 Compare December 12, 2022 17:57
@coot
Copy link
Collaborator Author

coot commented Dec 12, 2022

I removed the rather controversial monad transformer instances for MonadFork & MonadAsync; If we get requests, we can think about it.

@coot coot force-pushed the coot/monad-timers branch from 2776115 to c8d0de2 Compare December 12, 2022 18:00
@coot coot mentioned this pull request Dec 13, 2022
Comment on lines 427 to 428
instance MonadCatch m => MonadCatch (StateT s m) where
catch (StateT m) f = StateT $ \s -> catch (m s) (\e -> runStateT (f e) s)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

compare with exceptions package

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@coot
Copy link
Collaborator Author

coot commented Dec 13, 2022

notes after a conversation with @dcoutts:

  • MonadTimerSI -> IOSim, remove MonadTimeout
  • Make io-sim and si-timers Time independent.
  • check which instances excpetions package provides
  • io-classes-mtl (leave ReaderT instances in io-classes)

I think my original notes were not adequate with respect to the second point. As I remember we concluded that Time needs to be defined in si-timers.

@coot coot force-pushed the coot/monad-timers branch from c13c2fe to a82d7b8 Compare December 14, 2022 14:56

-- | @since 1.0.0.0
instance MonadCatch m => MonadCatch (StateT s m) where
catch (StateT m) f = StateT $ \s -> catch (m s) (\e -> runStateT (f e) s)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is compatible with what exception package does; it's using liftCatch.

@coot coot force-pushed the coot/monad-timers branch from a82d7b8 to 280f200 Compare December 14, 2022 18:27
instance MonadCatch m => MonadCatch (ExceptT e m) where
catch (ExceptT m) f = ExceptT $ catch m (runExceptT . f)

generalBracket acquire release use = ExceptT $ do
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

instance (Monoid w, MonadCatch m) => MonadCatch (WriterT w m) where
catch (WriterT m) f = WriterT $ catch m (runWriterT . f)

generalBracket acquire release use = WriterT $ fmap f $
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

catch (RWST m) f = RWST $ \r s -> catch (m r s) (\e -> runRWST (f e) r s)

-- | general bracket ignores the state produced by the release callback
generalBracket acquire release use = RWST $ \r s ->
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

catch (StateT m) f = StateT $ \s -> catch (m s) (\e -> runStateT (f e) s)

-- | general bracket ignores the state produced by the release callback
generalBracket acquire release use = StateT $ \s -> fmap f $
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@coot coot force-pushed the coot/monad-timers branch from 3f834ff to 7037211 Compare December 15, 2022 19:43
@coot coot linked an issue Dec 15, 2022 that may be closed by this pull request
coot added 26 commits April 14, 2023 14:54
The latter one returns time in nanoseconds as a `Word64` value, the same
as `GHC.Clock.getMonotonicTimeNSec`.  `MonadMonotonicTime` has a single
method which returns time measured in `DiffTime`.

For `IOSim` the latter one is more precise, `IOSim` measures time using
`DiffTime`.
This means we also remove `registerDelayCancellable` from `io-classes`.
* MonadDelay
* MonadTimer
`ReaderT`, `WriterT`, `StateT` and `RWST` have now instances which also
transform the `STM m` monad.  This open a possibility to provide
`MonadAsync` instances for monad transformers stacks.
We don't need anymore `WrappedSTM` as it's only used for `ContT` monad
transformer.
`si-timers` also provides `MonadTimer` and `MonadDelay`.  They use the
same name as one in `io-classes` as it's not expected to depend on both
in one module.

The `si-timers` provides `defaultRegisterDelayCancellable` which is used
for two purposes:

* provide `IO` instance
* test the `IO` instance in `IOSim`

This patch also reverts a change in one of the previous patches.
`IOSim` is using `DiffTime` for its `NewTimeout` primitive operation.
The `UpdateTimeout` is removed as it is not used.
@coot coot force-pushed the coot/monad-timers branch from dcdb3b6 to 01509df Compare April 14, 2023 12:56
@coot coot merged commit 3671872 into main Apr 14, 2023
@coot coot deleted the coot/monad-timers branch April 14, 2023 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants