Papers
Topics
Authors
Recent
Search
2000 character limit reached

Calculus of Compositional Copatterns

Updated 8 July 2026
  • The paper refactors copatterns into a formal calculus with explicit responses, nested options, and delimited questions, enabling disciplined modular reasoning in codata.
  • It establishes a clear correspondence between monolithic and compositional copatterns through CPS, abstract machines, and small-step reduction systems.
  • The Agda case study concretely demonstrates how copatterns facilitate coinductive normalization via modular composition, delayed beta-reduction, and eta-expansion.

Searching arXiv for the cited copattern papers to ground the article. I’ll look up the arXiv entries for the two papers to anchor the summary in the current record. Searching (Abel et al., 2014) and (Downen, 17 Aug 2025). The calculus of compositional copatterns denotes a line of work in which copatterns are treated not merely as a notation for codata, but as a disciplined foundation for programming and reasoning about context-sensitive behavior. In one formulation, copatterns are refactored into a formal calculus with explicit responses, nested options, and delimited questions, yielding a semantics presented in CPS, as an abstract machine, and as a small-step reduction system (Downen, 17 Aug 2025). In another, earlier formulation, the same compositional idea appears as a concrete Agda development for coinductive normalization by evaluation in the sized delay monad, where coinductive objects are defined by copattern clauses for their observations and then composed monadically (Abel et al., 2014).

1. Copatterns as context-sensitive observations

Copatterns are the dual of ordinary patterns. Pattern matching inspects data produced by a term, whereas copattern matching describes how a value responds to the observations imposed by its context. In the 2025 calculus, the relevant observations are application to arguments, M NM~N, and projection by indices, M XM~X; a copattern is a sequence of such interrogations that leads to a result (Downen, 17 Aug 2025). The paper explicitly characterizes copatterns as matching on the context of use of a value rather than on the shape of input data.

The Agda case study realizes the same idea in a coinductive setting. There, coinductive objects are records whose behavior is given by equations for observable fields instead of by constructors. The central example is the coinductive record λ{LiMi}\lambda\{L_i \to M_i\}8 An element of ∞Delay is therefore determined by what happens when it is forced. The canonical divergent computation λ{LiMi}\lambda\{L_i \to M_i\}9 is defined by a copattern clause on force, and the same style is used for beta and eta in the normalizer (Abel et al., 2014).

Across both formulations, the common principle is that coinductive or object-like behavior is specified through observable eliminations. The 2014 development presents coinductive objects as observation transformers whose definitions unfold stepwise through force, while the 2025 development generalizes that perspective to a calculus in which the calling context itself becomes an explicit semantic object (Abel et al., 2014, Downen, 17 Aug 2025).

2. From monolithic clauses to compositional copatterns

The 2025 paper begins from a calculus of monolithic copatterns. Its syntax includes terms, options, and copatterns:

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$

A copattern lambda λ{LiMi}\lambda\{L_i \to M_i\} is evaluated against a context and selects the first matching clause. Its equational basis consists of open recursion,

(δ)M.=M M,(\delta)\quad M. = M ~ M,

and copattern resolution,

$(\beta)\quad C[\lambda\{\many[1 \leq i \leq n]{L_i \to M_i}\}] = M_j\vec{\sigma},$

subject to the matching condition stated in the paper (Downen, 17 Aug 2025).

The paper then isolates the limitations of this monolithic form: non-compositional failure handling, absence of nested option structure, and limited modularity for vertical composition. These limitations motivate the calculus of compositional copatterns, in which matching behavior is decomposed into smaller units that can be nested and chained via failure continuations (Downen, 17 Aug 2025).

The resulting syntax is:

$\begin{aligned} \mathit{Response} \ni R &::= q \mid \varepsilon \mid M \ans R \ \mathit{Term} \ni M, N &::= x \mid M~N \mid M~X \mid M. \mid \Raise \mid O \ask M \mid \ans q \to R \ \mathit{Option} \ni O &::= x \to O \mid X \to O \mid \ask x \to M \end{aligned}$

Here, $O \ask M$ means “try option OO first; if it cannot handle the current context, fall through to MM.” The forms M XM~X0, M XM~X1, and M XM~X2 decompose copattern matching into argument consumption, index checking, and successful completion, respectively (Downen, 17 Aug 2025).

The paper also gives explicit translations between monolithic and compositional syntax. In particular,

M XM~X3

and nested options can encode a list of monolithic clauses. Conversely, monolithic copattern lambdas can be translated into iterated uses of M XM~X4 ending in M XM~X5. The paper therefore describes the compositional calculus as a conservative extension of the monolithic one (Downen, 17 Aug 2025).

3. Semantic architecture: CPS, abstract machines, and small-step reduction

A central feature of the compositional calculus is that it is presented through multiple, corresponding semantic artifacts. For the monolithic calculus, the paper derives a small-step operational semantics, then an abstract machine, then a CPS transformation using Danvy’s syntactic and functional correspondences. Within CPS, the semantics is refactored to obtain the more general compositional calculus; then the process is reversed to derive an abstract machine and a small-step semantics for the new language (Downen, 17 Aug 2025).

For monolithic copatterns, evaluation contexts are represented as questions:

M XM~X6

The abstract machine uses refocusing states M XM~X7 and copattern-matching states M XM~X8, with steps such as

M XM~X9

and

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$0

Copattern matching consumes the question incrementally until either a clause succeeds or control falls through to the remaining options (Downen, 17 Aug 2025).

The refactoring to compositional copatterns introduces responses and answer abstractions:

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$1

Operationally, $\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$2 delimits a question, and the constant

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$3

represents “raise the current question as an unhandled error.” The paper identifies this structure as a form of call-by-name delimited control; nested options and failure handlers thereby acquire a direct control-theoretic interpretation (Downen, 17 Aug 2025).

The final CPS transformation for the compositional calculus makes this explicit. For example,

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$4

while options propagate failure by reconstructing the continuation shape. The return journey yields an abstract machine with terms, continuations, meta-continuations, and environments, as well as a direct small-step semantics with evaluation contexts

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$5

and delimiter contexts

$\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$6

The paper states semantic equivalence across the small-step semantics, abstract machine, and CPS account for both the monolithic and compositional calculi, and uses that equivalence to argue that the added expressiveness remains controlled (Downen, 17 Aug 2025).

4. The Agda case study: a concrete calculus in the sized delay monad

The 2014 paper does not present a separate formal calculus of compositional copatterns. Instead, it gives what it explicitly describes as a very concrete, worked-out calculus for programming with copatterns in a compositional style, in the setting of coinductive normalization by evaluation via the sized delay monad (Abel et al., 2014).

The delay monad is split into an inductive layer and a coinductive layer: (δ)M.=M M,(\delta)\quad M. = M ~ M,0 now a makes a result immediately available, while later a∞ delays the computation by one observable step. A value of type Delay ∞ A may therefore contain infinitely many laters and model nontermination (Abel et al., 2014).

At each size, Delay i is given a monad structure. Return is now, and bind is defined mutually with its coinductive part: (δ)M.=M M,(\delta)\quad M. = M ~ M,1 This is a paradigmatic compositional copattern definition: the coinductive part _∞>>=_ is defined by a force clause, while the inductive layer guarantees that corecursive unfolding remains guarded (Abel et al., 2014).

The main coinductive components of the normalizer are the evaluator, the application function, and the delayed $\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$7-step: (δ)M.=M M,(\delta)\quad M. = M ~ M,2 The reifier is defined in a parallel style, with delayed $\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$8-expansion captured by: (δ)M.=M M,(\delta)\quad M. = M ~ M,3 The normalizer itself is then the composition (δ)M.=M M,(\delta)\quad M. = M ~ M,4 The paper’s central methodological point is that independently defined coinductive components such as beta, eta, and _∞>>=_ can be combined in a modular way, with copattern clauses specifying observable steps and monadic combinators handling composition (Abel et al., 2014).

5. Productivity, bisimilarity, and a posteriori totality

The Agda development separates productivity from termination. Productivity is enforced by sized types: if (δ)M.=M M,(\delta)\quad M. = M ~ M,5 then every observation decreases the size index. The paper states that “The use of copatterns reduces productivity checking to termination checking,” because Agda checks that the recursive call receives a strictly smaller size parameter. This is applied directly to never, beta, and eta, whose force clauses expose one step of the delayed computation at smaller depth (Abel et al., 2014).

The monad laws are not proved by definitional equality, but up to strong bisimilarity. The paper defines a coinductive bisimilarity relation (δ)M.=M M,(\delta)\quad M. = M ~ M,6 and uses copatterns again to specify how equality unfolds through force (Abel et al., 2014).

Termination is then proved a posteriori by logical relations. The strong computability predicates on values and delayed computations are: (δ)M.=M M,(\delta)\quad M. = M ~ M,7 with a parallel predicate on environments: (δ)M.=M M,(\delta)\quad M. = M ~ M,8 The paper proves weakening lemmas, soundness of delayed $\begin{aligned} \mathit{Term} \ni M, N &::=&~ x \mid M ~ N \mid M ~ X \mid M . \mid \lambda \{\many{O}\} \ \mathit{Option} \ni O &::=&~ L \to M \ \mathit{Copat} \ni L &::=&~ \varepsilon \mid x ~ L \mid X ~ L \end{aligned}$9-expansion, the fundamental theorem term, and mutual reify/reflect lemmas for readback. The final result is: (δ)M.=M M,(\delta)\quad M. = M ~ M,9 Thus the composed program nf is defined coinductively and may in principle diverge, but for simply typed λ{LiMi}\lambda\{L_i \to M_i\}0-terms it always converges. This is the paper’s strongest evidence that copattern-based, monadically composed coinductive programs can remain total when supported by an appropriate semantic argument (Abel et al., 2014).

6. Examples, connections, and open directions

The 2025 paper illustrates the monolithic calculus with a counting stream:

λ{LiMi}\lambda\{L_i \to M_i\}1

The reduction of the third element proceeds by alternating the open-recursion rule λ{LiMi}\lambda\{L_i \to M_i\}2 with copattern resolution λ{LiMi}\lambda\{L_i \to M_i\}3 until the result λ{LiMi}\lambda\{L_i \to M_i\}4 is obtained (Downen, 17 Aug 2025). It also presents Scheme macro examples for object-like programming, including an arithmetic interpreter arith, a vertical composition arith-ext, and environment-carrying objects such as with-env and alg. These examples are used to explain how λ{LiMi}\lambda\{L_i \to M_i\}5 expresses vertical composition by “try this behavior first; otherwise fall through” (Downen, 17 Aug 2025).

The 2014 case study connects the same compositional perspective to normalization by evaluation. There, beta embodies delayed λ{LiMi}\lambda\{L_i \to M_i\}6-reduction, eta embodies delayed λ{LiMi}\lambda\{L_i \to M_i\}7-expansion, and _>>=_ provides the monadic composition law that combines evaluator and reifier into a single coinductive normalizer (Abel et al., 2014). This suggests a methodological continuity: the later formal calculus makes explicit, in control-theoretic syntax and semantics, a style of modular copattern programming that the earlier Agda development already used in a typed and coinductive setting.

Both papers situate copatterns in broader theoretical contexts. The 2025 paper links compositional copatterns to codata, coalgebraic models, object-oriented interfaces, delimited control, and algebraic effects; it treats the calling context as a continuation and nested options as first-class handlers in a call-by-name setting (Downen, 17 Aug 2025). The 2014 paper places its approach alongside Capretta’s delay/partiality monad, Danielsson’s partiality monad, copattern theory by Abel et al., and guarded definitions in Coq and earlier Agda, while contrasting sized types with guarded recursion via later modalities (Abel et al., 2014).

The limitations are also explicit. In the Agda work, copatterns and sized types were experimental features, and the “calculus” remained concrete, embedded in Agda rather than developed as a separate formal system (Abel et al., 2014). The later paper addresses precisely that gap by deriving a formal calculus of compositional copatterns with syntax and semantics at several levels of abstraction (Downen, 17 Aug 2025). A plausible implication is that the two papers collectively define a trajectory for the subject: from a proof-of-concept of compositional copattern programming in a dependently typed implementation to a direct semantic theory in which composition, failure, and control are themselves first-class components of the calculus.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Calculus of Compositional Copatterns.