3.13 Problem: Threads

► |  | Redex: Practical Semantics Engineering

1 | Amb: A Redex Tutorial
2 | Long Tutorial
3 | Extended Exercises
4 | The Redex Reference
5 | Automated Testing Benchmark
| Bibliography
| Index

▼ | 3 | Extended Exercises

3.1 | Problem: Objects
3.2 | Solution: Objects
3.3 | Problem: Types
3.4 | Solution: Types
3.5 | Problem: Missionaries and Cannibals
3.6 | Solution: Missionaries and Cannibals
3.7 | Problem: Towers of Hanoi
3.8 | Solution: Towers of Hanoi
3.9 | Problem: GC
3.10 | Solution: GC
3.11 | Problem: Finite State Machines
3.12 | Solution: Finite State Machines
3.13 | Problem: Threads
3.14 | Solution: Threads
3.15 | Problem: Contracts
3.16 | Solution: Contracts
3.17 | Problem: Binary Addition
3.18 | Solution: Binary Addition

top contents ← prev up next →

#### 3.13`` Problem: Threads🔗ℹ

Add process forking and channel-based communication to a call-by-value functional language. Here is the proposed grammar:

> (define-language Lambda
> (e ::=
> x (lambda (x_!_ ...) e) (e e ...)
> n (+ e e)
> (if0 e e e)
> (spawn e)
> (put c e)
> (get c)
> (void))
> (n ::= number)
> (c ::= variable-not-otherwise-mentioned)
> (x ::= variable-not-otherwise-mentioned))

A (spawn e) expression creates a new thread from the given sub-expression, while put and get expressions allow these threads to communicate. Specifically, when one thread evaluates (put c v) and another evaluates (get c) for the same c, the get thread receives value v while the put thread’s expression evaluates to (void).

Hints Instead of a single expression, your reductions must deal with a set of expressions, one per thread. Reducing (spawn e) in one of these expressions thus adds an e to that set; use (void) as the result of this action. When any one thread has (get c) as its redex and another has (put c v), the two redexes are simultaneously replaced with their contractions.

In Redex, sets are currently realized with sequences. The key difference is that sets are unordered and sequences are ordered. Keep this in mind when you formulate reduction relations for put-get communications.

top contents ← prev up next →