Overview

Chapter 15. Type-safe concurrent programming

In this chapter, concurrency in Idris is introduced as a way to run multiple processes at the same time while still keeping communication structured and predictable. The runtime provides basic primitives for creating processes, connecting them, and sending messages through channels, which makes it possible to build responsive programs such as services that handle requests in the background. The examples show why this matters: concurrency can keep a user interface responsive during long computations, provide progress feedback, and better use multiple CPU cores. At the same time, the chapter makes clear that concurrent programming is harder than sequential programming because incorrect coordination can lead to deadlock or race conditions.

The first part of the chapter explores the limitations of the raw channel primitives. Although they allow message passing, they do not guarantee that two processes agree on the type or order of messages, so programs can type-check yet still fail at runtime by crashing or blocking forever. To address this, the chapter introduces a descriptive Process type that models a communication protocol rather than just a sequence of actions. This type begins with a simple structure for actions, pure values, spawning, requesting, and responding, then is refined to capture totality and control when looping is allowed. By making the progression of a process explicit in its type, the design rules out invalid behaviors such as looping before replying or spawning a service that cannot be shown to respond properly.

The final refinement makes the process type generic by parameterizing it over an interface that describes which request types map to which response types. This allows the same framework to model different services, such as an adder, a list-processing service, and a word-counting service, while ensuring at compile time that clients send only valid requests and receive the right kind of reply. The chapter also shows how to package the machinery into a reusable module and how to execute processes through a fuel-based runner that preserves totality. The overall result is a type-safe model of concurrent programming where well-typed, total process descriptions provide strong guarantees about communication, coordination, and eventual responses.

Figure 15.1. Pseudocode for client and server, in which each process reads and writes a shared variable, var. This could lead to an unexpected result if lines A and C are executed before lines B and D.

Figure 15.2. Message-passing between processes. A main process sends an Add 2 3 message to an adder process, which replies by sending a 5 message back to main.

Figure 15.3. A state transition diagram showing the states and operations in a server process. A process begins in the NoRequest state and must end in the Complete state, meaning that it has responded to at least one request.

Figure 15.4. A refined Process type, including the interface that the process responds to as part of the type

Summary

  • Concurrent programming involves multiple processes executing simultaneously.
  • Processes in Idris cooperate with each other by sending messages.
  • The System.Concurrency.Channels library provides primitive, but unsafe, operations for message passing.
  • Primitive operations are unsafe because they provide no guarantees about when processes send and receive messages, or about the correspondence between the types of sent and received messages.
  • You can define a type for describing safe message-passing processes, implemented using Channel as a primitive.
  • Using Inf, you can guarantee that looping processes continue to perform IO actions.
  • By defining a state machine in the type, you can be sure that a process will respond to messages on every iteration of a loop.
  • The type of a process can be generic and describe the types of messages to which a process will respond.

FAQ

What is the main goal of Chapter 15 in Type-Driven Development with Idris?The chapter shows how to use types to model concurrent processes safely, so that communicating processes follow a consistent protocol and avoid common runtime failures such as deadlock and type mismatches.
What concurrency primitive does Idris use for process communication in this chapter?The chapter uses message passing through the System.Concurrency.Channels library, which provides primitives for spawning processes, connecting to them, listening for connections, and sending or receiving messages.
Why are concurrent programs harder to write correctly than sequential programs?Concurrent programs introduce coordination problems between processes. Even if each process is correct on its own, the overall program can fail due to deadlock, race conditions, unexpected message ordering, or mismatched communication protocols.
What is a deadlock in the context of concurrent processes?A deadlock happens when two or more processes are each waiting for the other to do something before they can continue. In the chapter’s example, a client and server can freeze if both wait to receive a message from each other.
What is a race condition?A race condition occurs when the behavior of a concurrent program depends on the order in which actions from different processes happen. Different interleavings can lead to different results, especially when processes share mutable state.
Why are unsafeSend and unsafeRecv considered unsafe?They are unsafe because they do not enforce that both sides agree on message types or on the order of sends and receives. A program may type-check but still fail or block at runtime if the communication protocol is inconsistent.
What does the initial Process type model before refinement?The initial version models a process as a description of IO actions, pure values, sequencing, spawning, requesting, and responding. It provides a way to describe concurrent behavior as data that can later be executed.
How does the chapter make a process total when it can loop forever?It introduces Inf and a Loop constructor to explicitly mark potentially infinite behavior. Then a Fuel type is used by the interpreter to limit execution so that the runner remains total.
How do Service and Client differ in the refined type?A Service is a process that begins in NoRequest and ends in Complete, meaning it responds to requests and keeps looping. A Client begins and ends in NoRequest, meaning it sends requests but does not itself handle incoming ones.
What is the benefit of adding an interface type to Process?Adding an interface lets Idris track what kind of request a process accepts and what response type each request produces. This makes it possible to define generic services, such as list processing or word counting, while ensuring at compile time that requests and replies match correctly.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Type-Driven Development with Idris Type-Driven Development with Idris ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Type-Driven Development with Idris Type-Driven Development with Idris ebook for free