← Back to context

Comment by mrkeen

3 months ago

> Think about how you would model a laundry mat. It isn't enough to say you could use optimistic locks for access to the machines.

One machine? Too easy, I want multiple machines!

I want to reserve this machine and that machine. And this is happening while someone else wants to grab that machine and this machine. It costs money to grab a machine, so I'll insert a coin into this machine, and then insert a coin into that machine. The other guy grabbed that machine before me, so am I locked out? It turns out he only had one coin, not two: not only did this mean he untook that machine, but the coin he spent flew out of the machine and back into his pocket. While that was going on, I took a nap - instead of busily waiting for that machines - and the laundromat operator woke me up when it was time to try to grab both machines again.

  myActions :: Wallets -> Machines -> STM ()
  myActions wallets machines = do
    bookMachine "this machine"
    bookMachine "that machine"

    where
    bookMachine :: Machine -> STM ()
    bookMachine m = do
      reserveMachine machines m
      spendCoin

    spendCoin :: STM ()
    spendCoin = do
      decrement wallets
      newBalance <- getBalance wallets
      when (newBalance < 0)
           (throwSTM "Ran out of money")

Right, but my point is that you are still guarding a calculation, there. I suppose it is fair to say that this code just describes the paying for it side of things. My point is that I often want the code to describe everything I can actively do while the machine is in use by someone else.

To that end, if I was to "script out" a trip to the laundry mat, it would not be as simple as "reserve both, when successful, pay." It would be "Confirm driers are working, find an empty washer to fill, if found fill it if not check times and plan to come back later if above a threshold start over when I'm back, otherwise pay it, come back later when it is it done, ...."