Concurrent Haskell
Concurrent Haskell (also Control.Concurrent, or Concurrent and Parallel Haskell) is an extension to the functional programming language Haskell, which adds explicit primitive data types for concurrency. It was first added to Haskell 98, and has since become a library named Control.Concurrent included as part of the Glasgow Haskell Compiler. Its two main underlying concepts are: * A primitive data type MVar α implementing a bounded/single-place asynchronous channel, which is either empty or holds a value of type α . * The ability to spawn a concurrent thread via the forkIO primitive. Built on this is a set of useful concurrency and synchronizing abstractions such as unbounded channels, semaphores and sample variables. Haskell threads have very low overhead: creating, context-switching, and scheduling are all internal to the Haskell runtime system. These Haskell-level threads are mapped onto a configurable number of operating system (OS) level threads, usually one per processor core.
Concurrent Haskell (also Control.Concurrent, or Concurrent and Parallel Haskell) is an extension to the functional programming language Haskell, which adds explicit primitive data types for concurrency. It was first added to Haskell 98, and has since become a library named Control.Concurrent included as part of the Glasgow Haskell Compiler. Its two main underlying concepts are: A primitive data type MVar α implementing a bounded/single-place asynchronous channel, which is either empty or holds a value of type α . The ability to spawn a concurrent thread via the forkIO primitive. Built on this is a set of useful concurrency and synchronizing abstractions such as unbounded channels, semaphores and sample variables. Haskell threads have very low overhead: creating, context-switching, and scheduling are all internal to the Haskell runtime system. These Haskell-level threads are mapped onto a configurable number of operating system (OS) level threads, usually one per processor core. type Account = IORef Integer transfer :: Integer -> Account -> Account -> IO () transfer amount from to = do fromVal This causes problems in concurrent situations where multiple transfers might be taking place on the same account at the same time. If there were two transfers transferring money from account from , and both calls to transfer ran line (A) before either of them had written their new values, it is possible that money would be put into the other two accounts, with only one of the amounts being transferred being removed from account from , thus creating a race condition. This would leave the banking application in an inconsistent state. A traditional solution to such a problem is locking. For instance, locks can be placed around modifications to an account to ensure that credits and debits occur atomically. In Haskell, locking is accomplished with MVars: type Account = MVar Integer credit :: Integer -> Account -> IO () credit amount account = do current Account -> IO () debit amount account = do current Using such procedures will ensure that money will never be lost or gained due to improper interleaving of reads and writes to any individual account. However, if one tries to compose them together to create a procedure like transfer: transfer :: Integer -> Account -> Account -> IO () transfer amount from to = do debit amount from credit amount to a race condition still exists: the first account may be debited, then execution of the thread may be suspended, leaving the accounts as a whole in an inconsistent state. Thus, additional locks must be added to ensure correctness of composite operations, and in the worst case, one might need to simply lock all accounts regardless of how many are used in a given operation. type Account = TVar Integer credit :: Integer -> Account -> STM () credit amount account = do current Account -> STM () debit amount account = do current Account -> Account -> STM () transfer amount from to = do debit amount from credit amount to The return types of STM () may be taken to indicate that we are composing scripts for transactions. When the time comes to actually execute such a transaction, a function atomically :: STM a -> IO a is used. The above implementation will make sure that no other transactions interfere with the variables it is using (from and to) while it is executing, allowing the developer to be sure that race conditions like that above are not encountered. More improvements can be made to make sure that some other "business logic" is maintained in the system, i.e. that the transaction should not try to take money from an account until there is enough money in it: transfer :: Integer -> Account -> Account -> STM () transfer amount from to = do fromVal = 0 then do debit amount from credit amount to else retry Here the retry function has been used, which will roll back a transaction, and try it again. Retrying in STM is smart in that it won't try to run the transaction again until one of the variables it references during the transaction has been modified by some other transactional code. This makes the STM monad quite efficient. An example program using the transfer function might look like this: module Main where import Control.Concurrent (forkIO) import Control.Concurrent.STM import Control.Monad (forever) import System.Exit (exitSuccess) type Account = TVar Integer main = do bob IO a -> IO a repeatIO 1 m = m repeatIO n m = m >> repeatIO (n - 1) m newAccount :: Integer -> IO Account newAccount amount = newTVarIO amount transfer :: Integer -> Account -> Account -> STM () transfer amount from to = do fromVal = 0 then do debit amount from credit amount to else retry credit :: Integer -> Account -> STM () credit amount account = do current Account -> STM () debit amount account = do current which should print out "Bob's balance: 8000, Jill's balance: 6000". Here the atomically function has been used to run STM actions in the IO monad.
References
No reference rows published for this article.
Cite this article
MLA
"Concurrent Haskell." encyclopegia.org, 2026, https://encyclopegia.org/wiki/concurrent-haskell. Accessed 24 November 2026.
Chicago
"Concurrent Haskell." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/concurrent-haskell.
APA
encyclopegia.org. (2026). Concurrent Haskell. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/concurrent-haskell
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON