Frege is a Haskell for the JVM and can as such support STM with all the benefits of a purely functional language. In particular, Frege guarantees through the type system that no transactional variable (TVar) can be accessed outside a transaction and no side effect can happen inside a transaction.
17. STM
Works like compare and swap but for all
TVars inside an atomically function
plus invariants (check)
plus alternatives (orElse)
plus proper exception handling
No locks -> No deadlocks
19. STM
Replaces all your locks just like GC
replaces manual memory management
Makes compositional and modular code
Is optimistic and thus works best in low
contention scenarios
Is not a panacea (sorry).
20. Classic: Account transfer
deposit :: Account -> Int -> STM ()
deposit account amount = do
balance <- account.read
account.write (balance + amount)
withdraw :: Account -> Int -> STM ()
withdraw account amount =
deposit account (- amount) -- composed
limitedWithdraw :: Account -> Int -> STM ()
limitedWithdraw account amount = do
withdraw account amount -- composed
balance <- account.read
check (balance >= 0)
transfer :: Account -> Account -> Int -> STM ()
transfer from to amount = do
limitedWithdraw from amount -- composed
deposit to amount -- composed
24. STM relies upon
No access to TVars outside transactions
No side effects inside transactions
25. STM relies upon
No access to TVars outside transactions
No side effects inside transactions
TYPE SYSTEM (pure FP)
Developer discipline (everybody else)
26. STM relies upon
No access to TVars outside transactions
No side effects inside transactions
TYPE SYSTEM (pure FP)
Developer discipline (everybody else)
www.frege-lang.org
is the only option on the JVM