Calculus of Monolithic Copatterns
- The paper introduces a core calculus where copattern matching treats programs as responses to their calling contexts, enabling context-driven clause selection.
- It employs a small-step operational semantics and an abstract machine to implement controlled matching, handling self-reference and first-match behavior.
- Its CPS interpretation and semantic correspondence demonstrate that diverse evaluative strategies converge on equivalent observable behavior.
Searching arXiv for the cited paper and closely related work to ground the article. {"query":"(Downen, 17 Aug 2025) Controlling Copatterns: There and Back Again (Extended Version)", "max_results": 5} {"query":"copatterns Danvy functional syntactic correspondence refocusing abstract machine CPS arXiv", "max_results": 10} Calculus of monolithic copatterns is a core formulation of copattern matching in which programs are treated not merely as functions over arguments, but as responses to a calling context whose shape determines clause selection. In "Controlling Copatterns: There and Back Again (Extended Version)" the monolithic copattern calculus is presented as a deliberately small core language that captures the essential behavior of copattern matching used in the paper’s Scheme/Racket examples, while omitting the extra compositional features introduced later. Its semantic development proceeds through a small-step operational semantics, an abstract machine, and a continuation-passing style (CPS) interpretation, derived by standard semantic correspondences (Downen, 17 Aug 2025).
1. Core language and contextual interpretation
The monolithic copattern calculus is introduced as a language with ordinary variables, ordinary application, a projection or index form, a dot form for explicit self-application, and objects written as a list of copattern clauses (Downen, 17 Aug 2025). Its syntax is given by
$\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$
A term is therefore either a variable, an application , an indexed projection/application , a self-reference , or an object . An option 0 specifies that if the surrounding context matches copattern 1, the object returns 2. A copattern 3 is a sequence of demands on the context: 4 requires an argument term in the context, 5 requires a particular index or projection tag, and 6 requires no further context.
The defining intuition is that a copattern is a context pattern. This is the dual of ordinary pattern matching: instead of matching the shape of a value, the program matches the shape of the call context. A common misconception is to assimilate copattern clauses to ordinary elimination forms over already constructed data. In the monolithic calculus, however, the operative object is the surrounding evaluation context itself. Clause selection is controlled by how the object is observed.
The calculus is described as monolithic because this matching is all-or-nothing and packaged into a single, multi-clause object definition. This suggests a semantics in which a single object performs one large matching problem against the current question posed by its context, rather than composing smaller matching components.
2. Primitive equations, first-match behavior, and self-reference
The equational specification of the calculus is organized around two primitive equations (Downen, 17 Aug 2025): 7 and
8
under the conditions that 9, and for all $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$0, there is no matching decomposition $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$1.
The $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$2 equation makes self-reference explicit. The dot form means “pass me myself as the first argument,” so $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$3 expands to $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$4. The paper characterizes this as the mechanism by which open recursion is represented in the calculus. Self is therefore not implicit in the metalanguage; it is an explicit operational construct of the object language.
The $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$5 equation is the core copattern-matching rule. A lambda-object examines the surrounding context $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$6, tries its clauses in order, and selects the first matching one. The semantics is therefore first-match: the first clause whose copattern matches the context is the clause that fires. If $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$7 matches the context, then the corresponding right-hand side $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$8 is returned with the variables bound by the match substituted in.
The paper’s stream-like counter example exhibits this control structure: $\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$9
Here the object’s behavior is determined by the observation made of it: asking for Head returns the current number, while asking for Tail returns the next stream object by recursive self-application. This makes explicit that behavior is context-sensitive in a precise operational sense.
3. Small-step operational semantics
The small-step operational semantics is derived in an algorithmic form, first implemented in Haskell and then re-expressed as a relational stepping system (Downen, 17 Aug 2025). The concrete syntax is represented by the following data structures:
6
The paper then defines
7
so that a Question is literally an evaluation context in the same shape as a copattern.
Reduction is split between a redex-reduction phase and a search or refocus phase. The redex structures are
8
where Introspect m corresponds to 0, FreeVar x records an unbound variable, and Respond ops dispatches object clauses.
The central matching procedure is comatch, which compares a clause’s left-hand copattern with the current question:
9
with
0
An empty copattern succeeds immediately, leaving the remaining context as followup; a nonempty copattern against empty context is Unasked; matching argument prefixes binds variables; matching index prefixes checks equality of tags; otherwise the result is Mismatch.
Search for the next redex builds the surrounding question: 1 The paper explicitly notes that this is the call-by-name evaluation order: evaluation contexts correspond exactly to questions.
The refocusing formulation is 2 with 3 and the key lemma
1
The direct-style evaluator iterates decomposition, reduction, and recomposition: 4 This yields the corresponding reduction semantics with inside-out evaluation contexts
2
and rules
3
4
if 5 and earlier clauses do not match. The operational semantics is thus a call-by-name reduction semantics specialized to copattern-matching objects, with the context as an active participant in matching.
4. Abstract machine and explicit control flow
The abstract machine is derived through the standard functional and syntactic correspondence pipeline: CPS-transform the evaluator, defunctionalize continuations, fuse loops, and simplify (Downen, 17 Aug 2025). The resulting machine is tail-recursive and structurally aligned with the small-step semantics. Its core interpreter is
5
and the object-matching continuation is
6
The paper explicitly notes that this machine is not yet environment-based because it performs substitution directly by rhs // ..., and that an environment-based version appears later in the appendix.
The machine states are summarized as two forms: a refocusing or reduction state 6, and a copattern-matching state 7. The initial state is
8
and the final states are: 9 for an unknown variable,
0
for an unhandled question, and
1
with 2, for an underspecified question.
Its transition system makes the control structure of monolithic copattern matching explicit: 3
4
5
6
7
8
9
0
This machine exposes a key structural fact about the calculus: monolithic copattern matching is a controlled traversal of the demanded context. The machine either binds argument positions, checks index demands, succeeds by exhausting the copattern, blocks because the question is too short, or falls through to the next clause because the present clause cannot answer.
5. CPS interpretation and semantic correspondence
The CPS translation is extracted from the abstract machine by desugaring nested patterns, 1-reducing, and applying transitions eagerly (Downen, 17 Aug 2025). The Haskell-like semantic domains are:
7
The role of CPSVar is that a variable may be either a source-level name, Name, or a semantic substitution, Subs, when translating under binders.
The term translation is 8 and option lists are translated by 9 while clause translation is given by 0
The mathematical CPS equations summarize the same translation: 2
3
For option lists: 4
5
and for a clause: 6
7
with an analogous equation for 8.
The defining idea is that the continuation is not merely the rest of computation; it is a representation of the question being asked of the object. This is the CPS counterpart of the operational identification of evaluation contexts with questions.
The semantic correspondence theorem states that the three evaluators are extensionally equal. The paper isolates the observation of “raising an unanswered question” through the equivalence of the following conditions: 9 such that
0
1
and
2
where
3
Accordingly, the direct small-step semantics, the abstract machine, and the CPS translation determine the same observable behavior.
6. Relation to compositional copatterns and broader significance
The paper develops the monolithic calculus first and then refactors it into a more expressive compositional calculus within CPS (Downen, 17 Aug 2025). This later system is not simply additional surface syntax. It changes the control structure by introducing explicit delimitation of context, explicit handling of match failure, and nested matching forms.
Three refactorings are singled out. First, delimiting the context introduces a delimiter 4 and first-class delimited questions: 5
6
with CPS clauses
7
Second, copatterns are nested into smaller units: 8
9
where 0 means “try option 1, and if it fails continue with 2,” and 3 marks the success/failure split. Third, the CPS treatment is refined through Opt and Opt' to avoid duplication of continuations: 4
and
5
Against this background, the role of the monolithic calculus becomes clearer. It is the minimal setting in which copattern matching can be studied as a semantics of context-sensitive behavior. In the monolithic system, a context is implicit in the surrounding evaluation context, failure is implicit in clause ordering, and a clause’s left-hand side is a single large copattern. In the compositional system, questions become first-class and delimited, failure becomes explicit and composable, copatterns become nested, CPS gains an extra response layer, and the machine tracks a meta-continuation.
The paper presents this progression as a semantic pipeline: source-level reduction yields an executable abstract machine, which in turn yields a CPS account. A plausible implication is that the monolithic calculus is valuable precisely because it isolates the “one big matching problem” structure in its purest form. That simplicity makes it suitable for semantic derivation, while still supporting open recursion and behavior controlled by observation. The subsequent passage to compositional copatterns then shows that the same semantic story can be refactored to accommodate explicit delimiters, first-class failure, and nested matching without abandoning the original contextual interpretation of copatterns.