Monadic Reflection in CBPV
- Monadic Reflection is a programming abstraction that separates pure values from effectful actions within the call-by-push-value framework.
- It employs explicit reflection and reification operators to control user-defined monads and manage computational effects effectively.
- The λ-mon calculus formalizes this approach through extended syntax, operational semantics, and a rigorous type-and-effect system.
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 (Forster et al., 2016).
1. Formal Syntax and Monad Definitions
The λ-mon calculus extends the CBPV core syntax (values , computations ) with three new forms:
- Monad-definition form: , where , , are bound in (the unit clause for return) and (the bind clause for monadic composition). The monad definition binds a fresh type variable , specifying the required algebraic structure for .
- Reflection: 0, embedding an 1-computation into the monad 2.
- Reification: 3, extracting a 4-computation back into an 5-computation.
Thus, the grammar is:
6
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 7-reductions with rules for reflect and reify, using evaluation contexts 8 and hoisting contexts 9 (up to the nearest reify):
- Reflection:
0
This rule simply ‘opens’ a reflection, delegating to the underlying computation.
- Reification over Return:
1
A reify over a returned value invokes the unit clause of the monad definition.
- Reification over a Suspended Reflect:
2
Here, a reify over a reflected computation invokes the bind clause, passing the sub-continuation as 3.
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:
4
Key typing rules include:
- (Define): Ensures that 5 and 6 implement a monad instance for 7 over an effect stub 8.
- (Reflect): 9
- (Reify): 0
This system enables compositional type checking for computations with complex effect stacks.
4. Set-Theoretic Denotational Semantics
The denotational semantics interprets each effect stack 1 as a set-theoretic monad 2 and each computation type 3 as the 4–algebra 5. Monad types 6 are interpreted as 7. The semantics for the new forms is:
8
where 9 is the 0–algebra structure of 1.
Soundness and adequacy theorems hold:
- If 2 and 3, then 4.
- If 5 are closed of ground type 6 and 7, then 8.
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 9 with:
0
Given a handler term, the translation is:
1
Correctness: This translation simulates the operational behavior of algebraic handlers up to a finite number of 2-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 3, either 4, or 5 and 6.
- Termination: For every proper closed 7, there is no infinite reduction sequence, and eventually 8 uniquely.
- Soundness: If 9, then 0.
- Adequacy: If 1 are closed, ground-type computations in the identity monad, and 2, then 3.
These properties guarantee the operational reliability, semantic faithfulness, and contextual equivalence for effectful computations modeled in λ-mon (Forster et al., 2016).
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 (Forster et al., 2016).