Cover by Andy Oram, Greg Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Software Transactional Memory

Software Transactional Memory is a promising new approach to the challenge of concurrency, as I will explain in this section. I shall explain STM using Haskell, the most beautiful programming language I know, because STM fits into Haskell particularly elegantly. If you don't know any Haskell, don't worry; we'll learn it as we go.

Side Effects and Input/Output in Haskell

Here is the beginning of the code for transfer in Haskell:

	transfer :: Account -> Account -> Int -> IO ( )
	-- Transfer 'amount' from account 'from' to account 'to'
	transfer from to amount = ...

The second line of this definition, starting with --, is a comment. The first line gives the type signature for transfer.[54] This signature says that transfer takes as its arguments two values of type Account (the source and destination accounts) and an Int (the amount to transfer), and returns a value of type IO ( ). This result type says, "transfer returns an action that, when performed, may have some side effects, and then returns a value of type ( )." The type ( ), pronounced "unit," has just one value, which is also written ( ); it is akin to void in C. So, transfer's result type IO ( ) announces that its side effects constitute the only reason for calling it. Before we go further, we must explain how side effects are handled in Haskell.

A side effect is anything that reads or writes mutable state. Input/output is a prominent example of a side effect. For example, here are the signatures of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required