Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composable atomic-like operations

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.

  1. Is there a name for this type of (not real) atomic. Whenever i search for haskell transaction, I get STM.
  2. 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.

like image 258
user1138184 Avatar asked Jul 12 '12 17:07

user1138184


1 Answers

This is exactly the purpose of STM. Actions are composed so that they succeed or fail together, automatically.

Very similar to your hotel room problem is the bank transaction example in Simon Peyton-Jones's chapter in "Beautiful Code": http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/beautiful.pdf

like image 101
amindfv Avatar answered Sep 26 '22 03:09

amindfv