Santa repeatedly sleeps until wakened by either all of his nine reindeer, back from their holidays, or by a group of three of his ten elves. If awakened by the reindeer, he harnesses each of them to his sleigh, delivers toys with them and finally unharnesses them (allowing them to go off on holiday). If awakened by a group of elves, he shows each of the group into his study, consults with them on toy R&D and finally shows them each out (allowing them to go back to work). Santa should give priority to the reindeer in the case that there is both a group of elves and a group of reindeer waiting.
Using a well-known example allows you to directly compare my solution with well-described solutions in other languages. In particular, Trono's paper gives a semaphore-based solution that is partially correct. Ben-Ari gives a solution in Ada95 and in Ada. Benton gives a solution in Polyphonic C#.
The basic idea of the STM Haskell implementation is this. Santa makes one
Group" for the elves and one for
the reindeer. Each elf (or reindeer) tries to join its Group. If it
succeeds, it gets two "
return. The first
Gate allows Santa
to control when the elf can enter the study and also lets Santa know
when they are all inside. Similarly, the second
Gate controls the elves leaving ...