Opened 9 years ago

Closed 9 years ago

#4343 closed proposal (fixed)

Add throwSTM and generalize catchSTM

Reported by: basvandijk Owned by:
Priority: normal Milestone:
Component: libraries (other) Version: 7.1
Keywords: stm, exception, throw, throwIO, throwSTM, catch, catchSTM Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Currently the only way to throw an exception in an STM transaction is via throw. The IO monad has the throwIO function which guarantees ordering with respect to other IO actions. It would be nice to have a similar function for the STM monad:

throwSTM :: Exception e => e -> STM a

Catching exceptions in the IO monad can be done with catch:

catch :: Exception e => IO a -> e -> IO a -> IO a

Catching exception in the STM monad can be done with catchSTM:

catchSTM :: STM a -> (SomeException -> STM a) -> STM a

However, it would be nice to generalize this function to any exception:

catchSTM :: Exception e => STM a -> (e -> STM a) -> STM a

This makes it consistent with the IO catch function and allows packages like: exception-monads-fd/tf to define instances for MonadException.

Attachments (4)

STMError.hs (671 bytes) - added by AntoineLatter 9 years ago.
Proposed implementation
add_throwSTM.dpatch (1.1 KB) - added by basvandijk 9 years ago.
Patch for stm
add_throwSTM_and_generalize_catchSTM.dpatch (51.3 KB) - added by basvandijk 9 years ago.
Patch for base
export_throwSTM_when_base-4.3.dpatch (1.5 KB) - added by basvandijk 9 years ago.

Download all attachments as: .zip

Change History (11)

Changed 9 years ago by AntoineLatter

Attachment: STMError.hs added

Proposed implementation

Changed 9 years ago by basvandijk

Attachment: add_throwSTM.dpatch added

Patch for stm

Changed 9 years ago by basvandijk

Patch for base

comment:2 Changed 9 years ago by basvandijk

Status: newpatch

comment:3 Changed 9 years ago by simonmar

Resolution: fixed
Status: patchclosed

Applied, thanks! I've also bumped the version of the stm package to 2.2.0.0 since catchSTM's type has changed, and I'll upload a new version to Hackage.

comment:4 in reply to:  3 Changed 9 years ago by basvandijk

Replying to simonmar:

Applied, thanks! I've also bumped the version of the stm package to 2.2.0.0 since catchSTM's type has changed, and I'll upload a new version to Hackage.

Thanks.

The new stm doesn't build however because we need to depend on the new base-4.3 which will export throwSTM from GHC.Conc. Sorry for not putting this in the patch.

Changed 9 years ago by basvandijk

comment:5 Changed 9 years ago by basvandijk

Resolution: fixed
Status: closednew

The latest attached patch conditionally exports throwSTM from Control.Monad.GHC when base >= 4.3.

comment:6 Changed 9 years ago by judahj

FYI: The latest RC (ghc-7.0.0.20101028) doesn't work with the recently-uploaded stm-2.2.0.0, even using your new patch. From what I can tell, throwSTM hasn't yet been merged into the ghc-7 branch.

comment:7 Changed 9 years ago by basvandijk

Resolution: fixed
Status: newclosed

The latest stm-2.2.0.1 seems to build fine on both ghc-6.12.3 and on ghc-HEAD. So I'm closing this bug.

The only strange thing is that my up to date stm repository does not include the fix and my stm.cabal still lists 2.2.0.0 as the version. Simon Marlow probably didn't push his patches yet.

Note: See TracTickets for help on using tickets.