I want to compose operations that may fail, but there is a way to roll back.
For example - an external call to book a hotel room, and an external call to charge a credit card. Both of those calls may fail such as no rooms left, invalid credit card. Both have ways to roll back - cancel hotel room, cancel credit charge.
- Is there a name for this type of (not real) atomic. Whenever i search for haskell transaction, I get
STM
. - Is there an abstraction, a way to compose them, or a library in haskell or any other language?
I feel you could write a monad Atomic T
which will track these operations and roll them back if there is an exception.
Edit:
These operations may be IO
operations. If the operations were only memory operations, as the two answers suggest, STM would suffice.
For example booking hotels would via HTTP requests. Database operations such as inserting records via socket communication.
In the real world, for irreversible operations there is a grace period before the operation will be done - e.g. credit cards payments and hotel bookings may be settled at the end of the day, and therefore it is fine to cancel before then.