-
Notifications
You must be signed in to change notification settings - Fork 22
Add MonadCatch instance for STM #13
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
Closed
yogeshsajanikar
wants to merge
11
commits into
main
from
CAD-4738-implement-monad-catch-instance-for-stm-in-io-sim
Closed
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e542235
Add MonadCatch instance for STM
b5840bc
Add Catch semantics for Test.STM
7fdec2f
CAD-4738 Remove extra files
07e448a
WIP Handling throw and catch semantics
c637d59
Add `Catch` support for generator
1ae2f72
Add exception semantics
1f74a5a
Add unwind support to IOSimPOR
5868e9c
Add more tests to reproduce the error
4d6d6ec
Add style changes
6afd94f
Unwind the stack on exception
7db68ca
WIP remove accumulator from CatchHandlerStmFrame
nfrisby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} | ||
{-# OPTIONS_GHC -Wno-partial-fields #-} | ||
{-# LANGUAGE MultiWayIf #-} | ||
|
||
module Control.Monad.IOSim.Types | ||
( IOSim (..) | ||
|
@@ -175,6 +176,8 @@ runSTM (STM k) = k ReturnStm | |
data StmA s a where | ||
ReturnStm :: a -> StmA s a | ||
ThrowStm :: SomeException -> StmA s a | ||
-- Catch with continuation | ||
CatchStm :: StmA s a -> (SomeException -> StmA s a) -> (a -> StmA s b) -> StmA s b | ||
|
||
NewTVar :: Maybe String -> x -> (TVar s x -> StmA s b) -> StmA s b | ||
LabelTVar :: String -> TVar s a -> StmA s b -> StmA s b | ||
|
@@ -224,19 +227,19 @@ instance Monad (IOSim s) where | |
{-# INLINE (>>) #-} | ||
(>>) = (*>) | ||
|
||
#if !(MIN_VERSION_base(4,13,0)) | ||
fail = Fail.fail | ||
#endif | ||
|
||
|
||
|
||
|
||
instance Semigroup a => Semigroup (IOSim s a) where | ||
(<>) = liftA2 (<>) | ||
|
||
instance Monoid a => Monoid (IOSim s a) where | ||
mempty = pure mempty | ||
|
||
#if !(MIN_VERSION_base(4,11,0)) | ||
mappend = liftA2 mappend | ||
#endif | ||
|
||
|
||
|
||
|
||
instance Fail.MonadFail (IOSim s) where | ||
fail msg = IOSim $ oneShot $ \_ -> Throw (toException (IO.Error.userError msg)) | ||
|
@@ -269,9 +272,9 @@ instance Monad (STM s) where | |
{-# INLINE (>>) #-} | ||
(>>) = (*>) | ||
|
||
#if !(MIN_VERSION_base(4,13,0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reinstantiate these changes. |
||
fail = Fail.fail | ||
#endif | ||
|
||
|
||
|
||
|
||
instance Fail.MonadFail (STM s) where | ||
fail msg = STM $ oneShot $ \_ -> ThrowStm (toException (ErrorCall msg)) | ||
|
@@ -313,6 +316,23 @@ instance MonadThrow (STM s) where | |
instance Exceptions.MonadThrow (STM s) where | ||
throwM = MonadThrow.throwIO | ||
|
||
instance MonadCatch (STM s) where | ||
|
||
catch action handler = STM $ oneShot $ \k -> CatchStm (runSTM action) (runSTM . handler') k | ||
where | ||
handler' :: SomeException -> STM s a | ||
handler' exc = | ||
if | ||
| Just exc' <- fromException exc -> handler exc' | ||
| otherwise -> throwIO exc | ||
|
||
-- Default implmentation uses mask. For STM, mask is not necessary. | ||
generalBracket = generalBracketSTM | ||
|
||
instance Exceptions.MonadCatch (STM s) where | ||
|
||
catch = MonadThrow.catch | ||
|
||
instance MonadCatch (IOSim s) where | ||
catch action handler = | ||
IOSim $ oneShot $ \k -> Catch (runIOSim action) (runIOSim . handler) k | ||
|
@@ -853,6 +873,14 @@ data StmStack s b a where | |
-> StmStack s b c | ||
-> StmStack s a c | ||
|
||
-- | Executing in the context of the /action/ part of the 'catch' | ||
CatchStmFrame :: (SomeException -> StmA s a) -- exception handler | ||
-> (a -> StmA s b) -- subsequent continuation | ||
-> Map TVarId (SomeTVar s) -- saved written vars set | ||
-> [SomeTVar s] -- saved written vars list | ||
-> [SomeTVar s] -- created vars list (allocations) | ||
-> StmStack s b c | ||
-> StmStack s a c | ||
--- | ||
--- Schedules | ||
--- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.