Search code examples
clojurefunctional-programmingstmpurely-functionalreferential-transparency

Can we consider Clojure's STM 'functional'?


We know that pure functions:

  1. Always return the same result for a given input
  2. Produce no side-effects

This leads us to referential transparency - where an expression can be replaced with a value without changing the behaviour of the program.

This tells us that a program can be said to be purely functional if it excludes destructive modifications (updates) of entities in the program's running environment.

When we look at Software Transactional Memory, we see a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. But nothing about that is particularly functional on its own.

My question is: Can we consider Clojure's STM 'functional'?


Solution

  • Clojure STM is intentionally not "pure functional" because it is intended to manage state, and updating state is a side-effect. This reflects Clojure's design philosophy as a language that prefers functional programming by default, but still supplies you with the tools to do useful/stateful things, in a hopefully controlled manner.