Papers
Topics
Authors
Recent
Search
2000 character limit reached

Calculus of Monolithic Copatterns

Updated 8 July 2026
  • 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

Variablex,y,z::=\mathit{Variable} \ni x, y, z ::= \dots

IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots

$\mathit{Term} \ni M, N ::= x \mid M~N \mid M~X \mid M. \mid \lambda\{\many{O}\}$

OptionO::=LM\mathit{Option} \ni O ::= L \to M

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L

A term MM is therefore either a variable, an application M NM~N, an indexed projection/application M XM~X, a self-reference M.M., or an object λ{}\lambda\{\dots\}. An option IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots0 specifies that if the surrounding context matches copattern IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots1, the object returns IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots2. A copattern IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots3 is a sequence of demands on the context: IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots4 requires an argument term in the context, IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots5 requires a particular index or projection tag, and IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots6 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): IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots7 and

IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots8

under the conditions that IndexX,Y,Z::=\mathit{Index} \ni X, Y, Z ::= \dots9, 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: M XM~X6 The paper then defines M XM~X7 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 M XM~X8 where Introspect m corresponds to OptionO::=LM\mathit{Option} \ni O ::= L \to M0, 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: M XM~X9 with M.M.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: M.M.1 The paper explicitly notes that this is the call-by-name evaluation order: evaluation contexts correspond exactly to questions.

The refocusing formulation is M.M.2 with M.M.3 and the key lemma

OptionO::=LM\mathit{Option} \ni O ::= L \to M1

The direct-style evaluator iterates decomposition, reduction, and recomposition: M.M.4 This yields the corresponding reduction semantics with inside-out evaluation contexts

OptionO::=LM\mathit{Option} \ni O ::= L \to M2

and rules

OptionO::=LM\mathit{Option} \ni O ::= L \to M3

OptionO::=LM\mathit{Option} \ni O ::= L \to M4

if OptionO::=LM\mathit{Option} \ni O ::= L \to M5 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 M.M.5 and the object-matching continuation is M.M.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 OptionO::=LM\mathit{Option} \ni O ::= L \to M6, and a copattern-matching state OptionO::=LM\mathit{Option} \ni O ::= L \to M7. The initial state is

OptionO::=LM\mathit{Option} \ni O ::= L \to M8

and the final states are: OptionO::=LM\mathit{Option} \ni O ::= L \to M9 for an unknown variable,

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L0

for an unhandled question, and

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L1

with CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L2, for an underspecified question.

Its transition system makes the control structure of monolithic copattern matching explicit: CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L3

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L4

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L5

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L6

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L7

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L8

CopatL::=εx LX L\mathit{Copat} \ni L ::= \varepsilon \mid x~L \mid X~L9

MM0

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, MM1-reducing, and applying transitions eagerly (Downen, 17 Aug 2025). The Haskell-like semantic domains are: M.M.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 M.M.8 and option lists are translated by M.M.9 while clause translation is given by λ{}\lambda\{\dots\}0

The mathematical CPS equations summarize the same translation: MM2

MM3

For option lists: MM4

MM5

and for a clause: MM6

MM7

with an analogous equation for MM8.

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: MM9 such that

M NM~N0

M NM~N1

and

M NM~N2

where

M NM~N3

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 M NM~N4 and first-class delimited questions: M NM~N5

M NM~N6

with CPS clauses

M NM~N7

Second, copatterns are nested into smaller units: M NM~N8

M NM~N9

where M XM~X0 means “try option M XM~X1, and if it fails continue with M XM~X2,” and M XM~X3 marks the success/failure split. Third, the CPS treatment is refined through Opt and Opt' to avoid duplication of continuations: M XM~X4 and

M XM~X5

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.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (1)

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 Monolithic Copatterns.