---
title: Monadic Reflection in CBPV
url: https://www.emergentmind.com/topics/monadic-reflection
type: topic
---

# Monadic Reflection in CBPV

Monadic reflection is a programming abstraction for user-defined computational effects, introduced and analyzed within the context of the call-by-push-value (CBPV) paradigm. It provides a discipline for effectful computations by separating pure values from effectful actions, enabling fine-grained control over the introduction and elimination of user-specified monads via explicit operators for reflection and reification. The monadic reflection calculus λ-mon, as developed by Forster, Kammar, Lindley, and Pretnar, formalizes this approach by extending CBPV with monad definitions, and new primitives for embedding computations into, and extracting them from, monadic contexts [1610.09161].

## 1. Formal Syntax and Monad Definitions

The λ-mon calculus extends the CBPV core syntax (values $V$, computations $M$) with three new forms:

1. Monad-definition form: $T\,x\,N_u\,y\,f\,N_b\ \text{where}\,\ldots$, 
   where $x : \alpha$, $y : T\,\alpha$, $f : T\,\alpha \to (\alpha \to T\,\beta) \to T\,\beta$ are bound in $N_u$ (the unit clause for return) and $N_b$ (the bind clause for monadic composition). The monad definition binds a fresh type variable $\alpha$, specifying the required algebraic structure for $T$.

2. Reflection: $\reflect{N}$, embedding an $E$-computation into the monad $T$.

3. Reification: $\reify{M}$, extracting a $T$-computation back into an $E$-computation.

Thus, the grammar is:

\[
\begin{array}{rcl}
T & ::= & *\, x\, N_u\, y\, f\, N_b\ \text{where}\ \ldots \\
M,N & ::= & \ldots \mid \reflect{N} \mid \reify{M}
\end{array}
\]

This expressive syntax enables user-defined monads whose return and bind are specified operationally.

## 2. Operational Semantics

The operational semantics of λ-mon is formulated by extending CBPV’s $\beta$-reductions with rules for reflect and reify, using evaluation contexts $C[\cdot]$ and hoisting contexts $H[\cdot]$ (up to the nearest reify):

- **Reflection:** 
  \[
  \reflect{N} \longrightarrow N \qquad (\mathsf{Refl})
  \]
  This rule simply ‘opens’ a reflection, delegating to the underlying computation.

- **Reification over Return:** 
  \[
  H[\reify{\return V}] \longrightarrow N_u[V/x] \qquad (\mathsf{Reify}\ \mathsf{Ret})
  \]
  A reify over a returned value invokes the unit clause of the monad definition.

- **Reification over a Suspended Reflect:**
  \[
  H[\reify{\reflect{N}}] \longrightarrow N_b[N/x, (\lambda y.\, H[\return y])/f] \qquad (\mathsf{Reify}\ \mathsf{Op})
  \]
  Here, a reify over a reflected computation invokes the bind clause, passing the sub-continuation as $f$.

These operational rules provide direct and local reasoning about the interaction between computational effects and user-defined monads.

## 3. Type-and-Effect System

The type-and-effect discipline of λ-mon decorates each computation type with an *effect*, logically modeled as an ordered stack of monads. The kinding and typing judgments are:

\[
\Gamma \vdash E : \mathsf{Eff} \quad \Gamma \vdash A : \mathsf{Val} \quad \Gamma \vdash C : \mathsf{Comp}\,E
\]

Key typing rules include:

- **(Define):** Ensures that $N_u$ and $N_b$ implement a monad instance for $T$ over an effect stub $E$.
- **(Reflect):** $\Gamma \vdash N : F_E\,A \implies \Gamma \vdash \reflect N : T\,A, E$
- **(Reify):** $\Gamma \vdash M : C,\quad \Gamma \vdash T\,\text{where}\ldots : \mathsf{Eff} \implies \Gamma \vdash \reify M : F_E\,A$

This system enables compositional type checking for computations with complex effect stacks.

## 4. Set-Theoretic Denotational Semantics

The denotational semantics interprets each effect stack $E$ as a set-theoretic monad $(T_E,\ \mathsf{return}^E,\ \mathsf{bind}^E)$ and each computation type $F_E\,A$ as the $T_E$–algebra $(T_E\,\sem{A},\,\mathsf{bind}^E\,\mathsf{id})$. Monad types $T\,A$ are interpreted as $T_{\sem{T}}(\sem{A})$. The semantics for the new forms is:

\[
\begin{aligned}
\sem{\reflect N}(\gamma) &= \sem{N}(\gamma) : \sem{T\,A} \\
\sem{\reify M}(\gamma) &= c_C(\sem{M}(\gamma)) : \sem{C}
\end{aligned}
\]

where $c_C$ is the $T_E$–algebra structure of $\sem{C}$.

**Soundness** and **adequacy** theorems hold:

- If $\Gamma \vdash P : X$ and $P \to^* Q$, then $\sem{P} = \sem{Q}$.
- If $P, Q$ are closed of ground type $F_E\,G$ and $\sem{P} = \sem{Q}$, then $P \approx_{\mathit{ctx}} Q$.

## 5. Macro-Translation from Algebraic Effect Handlers

A macro-translation from the calculus of algebraic effect handlers (EFF) into λ-mon demonstrates the expressive power of monadic reflection. This translation fixes a continuation monad $T_{\mathit{Cont}}$ with:

\[
\begin{aligned}
\mathsf{return}_T\,x &= \lambda k.\,k\,x \\
\mathsf{bind}_T\,m\,f &= \lambda k.\,m\,(\lambda a.\,f(a)\,k)\,k
\end{aligned}
\]

Given a handler term, the translation is:

\[
\begin{aligned}
\trans{\op\,V} &= \reflect\ (\lambda k.\,k(\inj_\op(\trans{V},\,k))) \\
\trans{\handle{M}{H}} &= \reify(\trans{M})\,(\lambda x.\,\trans{N_{\mathit{ret}}(x)})\,(\lambda p\,k.\,\trans{N_\op(p,k)})
\end{aligned}
\]

**Correctness:** This translation simulates the operational behavior of algebraic handlers up to a finite number of $\beta$-steps.

**Typeability:** In monomorphic λ-mon, this translation does not type-check due to the polymorphism of handlers in EFF. Introducing first-class quantified (polymorphic) operations restores typeability, enabling a uniform expressive correspondence.

## 6. Meta-Theoretic Properties

The λ-mon calculus enjoys fundamental meta-theoretic properties:

- **Safety:** For $\emptyset \vdash M : F_E\,A$, either $M = \return V$, or $M \to M'$ and $\emptyset \vdash M' : F_E\,A$.
- **Termination:** For every proper closed $M : F_E\,A$, there is no infinite reduction sequence, and eventually $M \to^* \return V$ uniquely.
- **Soundness:** If $M \to^* N$, then $\sem{M} = \sem{N}$.
- **Adequacy:** If $M, N$ are closed, ground-type computations in the identity monad, and $\sem{M} = \sem{N}$, then $M \approx_{\mathit{ctx}} N$.

These properties guarantee the operational reliability, semantic faithfulness, and contextual equivalence for effectful computations modeled in λ-mon [1610.09161].

## 7. Expressive Power and Limitations

Felleisen's notion of macro translation establishes that monadic reflection, effect handlers, and delimited control (without answer-type modification) can macro-express each other, subject to typeability constraints. However, using the set-theoretic denotational semantics for λ-mon, it is shown that effect handlers cannot be macro-expressed while preserving typeability either by monadic reflection or delimited control. This demonstrates precise boundaries in the expressive power of these abstractions and highlights the sensitivity of their relative expressiveness to language features such as type polymorphism [1610.09161].

Source: https://www.emergentmind.com/topics/monadic-reflection