Source-linked AI summary

A monadic interpreter and type-and-effect checker

Stefano Raviola, Paola Giannini, Francesco Dagnino

arXiv:2609.07667v1cs.PL

TL;DR

Generic effect operations can require ad hoc definitions, while prior work established a soundness framework for monadic type-and-effect reasoning. This paper implements that framework in modular Haskell with a small-step interpreter and checker, illustrating it with exception and nondeterminism examples.

  • Problem

    Monads model computational effects, but do not intrinsically provide effect-raising operations, which must otherwise be defined ad hoc.

    Source: §1 · p. 1
  • Method

    The paper implements a small-step interpreter and type-and-effect checker for a lambda-calculus extension with generic effects and handlers, separating the framework from language and effect implementations.

    Source: §1 · pp. 1–2
  • Results

    The implementation demonstrates that the theoretical framework can be translated into a concrete, modular, executable Haskell system.

    Source: §1 · p. 2, §7 · p. 11
  • Takeaways & Limitations

    The reduction engine can delegate effect operations to different underlying monads, and the examples cover exception handling and nondeterminism.

    Source: §1 · p. 1

    Representing effect types as sets of operations cannot approximate effects in which operation order matters, such as sequences of output writes.

    Source: §7 · p. 11

Abstract

from arXiv · show

We present a concrete implementation in Haskell of a monadic framework that includes both a small-step interpreter and a type-and-effect checker for the corresponding language. Our approach separates the language syntax from the semantics of its effects. This design allows the interpreter to remain parametric over the underlying monad, while the static checker approximates effects independently of their concrete implementation. The theoretical foundation of this framework-applied to a call-by-value lambda calculus with generic effects represented by operations that produce monadic values and are managed through handlers-was introduced in previous work, where the focus was on proving the soundness of the approach. In contrast, the present work leverages Haskell's support for modular programming and monads to demonstrate that the framework is practically implementable and usable. We illustrate the approach with examples using the monad of exceptions and the one of nondeterminism and expressions both with and without handlers.

1 Introduction

Monads provide a modular way to model computational effects, but effect-raising operations require additional structure. This paper presents a modular Haskell implementation of a small-step interpreter and type-and-effect checker for generic effects and handlers.

1 Introduction

  • Small-step semantics models computation as atomic reduction steps and is preferred for establishing properties such as type soundness.The theoretical framework also develops a monadic type-and-effect soundness meta-theory [5].Source: §1 · p. 1
  • The Haskell implementation provides a small-step interpreter and type-and-effect checker for a lambda-calculus extension with generic effects and handlers.Its front end lets users specify the monad for interpreting effects and the expression to evaluate.Source: §1 · p. 1
  • The reduction engine delegates effectful operations to an abstract interface, allowing runtime behavior to vary between monads without changing core rules.The checker infers types and effects and checks compatibility with those specified in expressions.Source: §1 · pp. 1–2
  • The implementation extends Dagnino et al. [5] with conditionals, base-type subtyping, and meet and join operators in the effect-type algebra.Sections 2 and 3 also instantiate abstract effect types as sets of operations.Source: §1 · p. 2

2 Background

The background develops monads, monadic operational semantics, generic effect operations, and a type-and-effect system. Together, these provide the abstractions used to describe computations and approximate the effects they may raise.

2 Background

  • A monad lifts pure values into monadic values, while bind composes effectful computations sequentially, with the latter computation depending on the former’s result.The monad is presented as a structure with a mapping and two operations.Source: §2 · p. 2
  • Monadic operational semantics reduces expressions to monadic expressions in single steps that may raise effects, with requirements ensuring deterministic reduction and irreducible values.The language comprises expressions, values, and an injective embedding of values as expressions.Source: §2 · pp. 2–3
  • Generic effects are represented by operations whose partial functions return monadic values, allowing calls to express raised effects.The examples include exception-raising operations and a nondeterministic choose operation.Source: §2 · p. 3
  • A type-and-effect system indexes expressions by types and effect types, where an ordered monoid represents effect absence, composition, and specificity.Its predicates describe values and expressions; embedding a value preserves its type and assigns an effect type that forces no effect.Source: §2 · p. 3

3 ΛΣ: A Lambda Calculus with Generic Effects

ΛΣ extends a fine-grain lambda calculus with generic operations and handlers, and defines both a monadic small-step semantics and a type-and-effect system. Its effects are abstracted through an algebra instantiated in the implementation as sets of operations.

3.1 Syntax

  • The language distinguishes inert values from potentially effectful computations, and includes functions, recursive functions, conditionals, operations, handlers, sequencing, and return.A c-clause replaces an effect while preserving execution flow; an s-clause interrupts the normal computation after handling it.Source: §3.1 · pp. 3–4

3.2 Operational Semantics

  • Pure reduction handles application, conditionals, and handlers; monadic reduction handles remaining expressions and lifts reductions into the chosen monad.For an unhandled operation, the handler is removed so the operation can be handled at an enclosing level.Source: §3.2 · pp. 4–5

3.3 Type-and-Effect System

  • The effect algebra is instantiated as sets of operations, with subeffecting as inclusion and meet, join, and composition as intersection, union, and union.The composition unit is the empty set; the effect algebra also provides operation injection and effect filtering.Source: §3.3 · p. 5
  • The implementation's effect algebra does not yet account for operation sequences; a sequence-aware instantiation is planned as future work.The authors state that this refinement would not require changes to the existing interpreter or type checker.Source: §3.3 · p. 5
  • Effect types statically over-approximate the effects expressions may raise; value judgments carry types, while expression judgments carry a type and an effect.Function types are decorated with effects approximating those raised by evaluating the function body.Source: §3.3 · p. 5
  • Handlers filter effects by replacing caught operations with catcher effects while retaining uncaught operations and effects from the continuation.Handler typing also checks clause result types against the handler's final result or the operation's return type, depending on clause mode.Source: §3.3 · p. 6

4 The Implementation

The Haskell implementation separates the monadic reduction framework from language syntax and effect definitions, while providing a type-and-effect checker for the language. Its modules support generic reductions, effect descriptions, and static checking.

4 The Implementation

  • The reduction framework is independent of language syntax and effects, so reductions remain operational when either changes.The implementation is in Haskell, whose functional paradigm, type system, and monad support are cited as reasons for its choice.Source: §4 · p. 6

4.1 Modules

  • The reduction interface equips a language with monadic operational semantics through value recognition and partial one-step reduction.The interface uses Maybe to represent partiality in value recognition and reduction.Source: §4.1 · pp. 6–7
  • Generic language reductions bridge pure steps with effectful Magic expressions by using operation semantics in the chosen monad.The syntax abstracts over operation signatures and effect algebras; Magic represents an operation call and its arguments.Source: §4.1 · p. 7
  • The type-and-effect algebra defines value and expression types, with effect types structured to support composition, sub-effecting, and handler filters.The effect approximation can represent effects as sets of operations.Source: §4.1 · p. 8
  • The checker verifies judgments Γ ⊢ e : T!E, where T gives the result value type and E upper-bounds possible effects.It enforces subtyping and subeffecting constraints and computes effect transformations for handled expressions.Source: §4.1 · p. 8
  • Effect modules specify operation names and arities, then connect the syntax to a monad by defining how each operation produces a monadic value.The implementation describes exception and nondeterminism instantiations.Source: §4.1 · pp. 8–9

5 Examples

The examples demonstrate list-monad nondeterminism by evaluating a Boolean XOR expression across its possible inputs, and exception handlers by abstracting specific failures as generic math errors. The reduction traces show the resulting state-space expansion and the handling of raised exceptions.

5 Examples

  • The examples use the list monad for nondeterminism and demonstrate handler semantics in the context of exceptions.The nondeterministic example produces the output vector of a Boolean function’s truth table.Source: §5 · p. 9

5.1 Nondeterminism

  • The nondeterminism example binds choose() results to x, y, and z, then evaluates their XOR expression to obtain all possible outcomes.The Boolean XOR is defined through curried lambda abstractions and nested conditionals.Source: §5.1 · p. 9, §5 · p. 9
  • Eight configurations arise after the third choose() reduction, one for each assignment; results appear in inverse truth-table order because runchoose returns [true, false].The trace shows state-space expansion under the list monad.Source: §5.1 · p. 9, Figure 14 · p. 9

5.2 Exceptions with handlers

  • The exception example uses safe_pred and safe_div to raise underflow and division-by-zero exceptions, respectively, before evaluating f a b.The target expression (a/b) − 1 is undefined in the naturals when either input is zero; the example classifies these cases as generic math errors.Source: §5.2 · pp. 9–10
  • The handler’s continue-mode clauses intercept specific exceptions and replace them with a more general exception in both scenarios.The specific exceptions are first raised by the corresponding inner function.Source: §5.2 · p. 10, Figure 15 · p. 10

6 Related Work

The section situates ΛΣ among algebraic-effect systems and distinguishes it through fixed-mode handlers and monadic operations interpreted within a small-step semantics parametric over the underlying monad.

Related systems

  • Koka tracks effects with row polymorphism and uses delimited continuations, while Links uses row polymorphism and a higher-order CPS translation.Links makes effect absence explicit and supports deep and shallow handler rules.Source: §6 · pp. 10–11
  • Frank [12] handles effects through application of multihandlers, with shallow continuations that require explicit recursion for deep handling.Its bidirectional type system does not support effect inference.Source: §6 · p. 10
  • Effekt embeds effects as Scala capability objects passed to functions and runs on a monad for multi-prompt delimited continuations.Possibly nested handlers introduce capabilities and delimit continuation capture.Source: §6 · pp. 10–11
  • Helium uses existential and local effects to conceal implementations and prevent external handlers from intercepting internal operations.Effect coercions guide operations through the handler stack.Source: §6 · p. 11
  • Handlers in Action uses open handlers that process selected operations and forward the rest, supporting modular composition in a Haskell library.The framework supports deep and shallow handlers and has termination and soundness proofs for λ_eff.Source: §6 · p. 11
  • Multicore OCaml [25] implements effects with extensible variants and heap-allocated fibers for one-shot continuations, without static effect-safety.The implementation is reported efficient for new handler-using code, with minimal overhead on existing programs.Source: §6 · p. 11

Comparison

  • The comparison centers on continuations and semantics: ΛΣ uses fixed continue/stop handler modes and gives operations monadic actions while permitting handler interception.Unlike the cited systems, it has no explicit continuations or resume construct.Source: §6 · p. 11

7 Conclusions

The paper presents a Haskell implementation of the ΛΣ framework as a modular, executable small-step interpreter and type-and-effect checker. Its current effect representation limits order-sensitive approximations, while planned extensions target language expressiveness and richer effect descriptions.

Conclusions

  • The Haskell implementation provides a small-step interpreter, an evaluator obtained by iterating it, and a type-and-effect checker for ΛΣ [5].The implementation demonstrates that the framework can be translated into a concrete, modular, executable system.Source: §7 · p. 11
  • The prototype accepts an expression and target monad, checks the program, then executes step-by-step reduction.It is a foundational prototype rather than a complete language environment.Source: §7 · p. 11

Future work

  • Planned extensions include additional language constructs, user-defined monads, and regular-expression effect types for describing operation sequences and recursive-function effects.Template Haskell is being explored to generate the required MonSem instance.Source: §7 · pp. 11–12

Limitations

  • Because effect types are sets of operations, the implementation cannot approximate effects where operation order matters, such as sequences of output writes.The prior model in [5] uses sets of possibly infinite operation sequences.Source: §7 · p. 11
Loading 2609.07667v1…