ArticleslgStudy

computer science

Concurrent Haskell

Concurrent Haskell is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Concurrent Haskell rather than just read about it. In short: 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.

Key takeaways

  • Concurrent Haskell belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Concurrent Haskell to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Concurrent Haskell from memory before moving on to harder problems.

Reference excerpt

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.

Software transactional memory The software transactional memory (STM) extension to Glasgow Haskell Compiler (GHC) reuses the process forking primitives of Concurrent Haskell. STM however:

avoids MVar in favour of TVar. introduces the retry and orElse primitives, allowing alternative atomic actions to be composed together.

STM monad The STM monad is an implementation of software transactional memory in Haskell. It is implemented in GHC, and allows for mutable variables to be modified in transactions.

Traditional approach Consider a banking application as an example, and a transaction in it—the transfer function, which takes money from one account, and puts it into another account. In the IO monad, this might look like:

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:

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:

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.

Atomic transactions To avoid this, one can use the STM monad, which allows one to write atomic transactions. This means that all operations inside the transaction fully complete, without any other threads modifying the variables that our transaction is using, or it fails, and the state is rolled back to where it was before the transaction was begun. In short, atomic transactions either complete fully, or it is as if they were never run at all. The lock-based code above translates in a relatively straightforward way:

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:

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:

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

External links Official website

Worked examples

Example 1 — a first encounter with Concurrent Haskell

Start with the simplest possible case. Write down what Concurrent Haskell claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Concurrent Haskell before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Concurrent Haskell ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Concurrent Haskell

In research
Concurrent Haskell appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Concurrent Haskell in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Concurrent Haskell is common in secondary-school and first-year university syllabi. It links to neighbouring topics 1996 software, Cross-platform free software, Free and open source compilers, so understanding it makes those chapters shorter.
In everyday life
Look for Concurrent Haskell outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Concurrent Haskell” →

How to study Concurrent Haskell in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Concurrent Haskell means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Concurrent Haskell out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Concurrent Haskell in simple terms?

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 includ…

Why does Concurrent Haskell matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Concurrent Haskell?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Concurrent Haskell.

Tags

  • 1996 software
  • Cross-platform free software
  • Free and open source compilers
  • Free software programmed in Haskell
  • Functional languages
  • Haskell programming language family
  • Programming languages
  • Programming languages created in 1996
  • Statically typed programming languages

Keep exploring