Skip to content

Instantly share code, notes, and snippets.

@vic
Last active July 1, 2025 21:35
Show Gist options
  • Save vic/e33fea2e46810faf1848cc8d0b3ceba3 to your computer and use it in GitHub Desktop.
Save vic/e33fea2e46810faf1848cc8d0b3ceba3 to your computer and use it in GitHub Desktop.
fx-rs

Composable Effectful Computations in Rust: Integrating Algebraic Effects, Lenses, and Contextual Adaptation

fx-rs: An algebraic effects system for Rust.

Abstract

Effect systems have become a powerful tool for structuring modular, composable, and type-safe effectful computations. While algebraic effects and handlers have seen widespread adoption in languages like Haskell and OCaml, their integration in Rust remains limited. We present Fx, a novel effect system for Rust that unifies algebraic effects, lenses, and contextual adaptation in a single, extensible abstraction. Fx enables ergonomic composition of effectful computations, modular interpretation via handlers, and fine-grained state manipulation using lenses. We demonstrate how Fx supports advanced patterns such as context adaptation and effectful accumulation, and evaluate its expressiveness and ergonomics through practical examples. Our work shows that Rust’s type system and ownership model can support advanced effectful programming idioms, opening new avenues for safe and modular systems programming.

1. Introduction

Effectful programming is essential for building real-world software, but managing effects in a modular and type-safe way remains challenging. Algebraic effects and handlers have emerged as a promising solution, enabling composable effectful computations and modular interpreters. However, most effect systems are designed for functional languages with advanced type systems, and their adoption in Rust has been limited.

In this paper, we introduce Fx, an extensible effect system for Rust. Fx brings together algebraic effects, lenses, and contextual adaptation, providing a unified abstraction for effectful programming. Our contributions are:

  • A Rust-native effect system inspired by algebraic effects and handlers.
  • Integration of lenses for fine-grained state manipulation within effectful computations.
  • Generalized context adaptation, enabling modular and reusable effect handlers.
  • Flexible effectful accumulation patterns.
  • An evaluation of Fx’s expressiveness and ergonomics in practical scenarios.

2. Background

2.1 Algebraic Effects and Handlers

Algebraic effects [Plotkin & Pretnar, 2009] model effectful operations as abstract requests, interpreted by handlers. This approach enables modular effect composition and separation of effect specification from interpretation.

2.2 Lenses

Lenses [Kmett, 2012] are composable abstractions for focusing on substructures within data, enabling modular state manipulation.

2.3 Effect Systems in Rust

Rust’s ownership and type system present unique challenges and opportunities for effectful programming. Existing effect systems in Rust are limited in compositionality and extensibility.

3. The Fx System

3.1 Core Abstractions

Fx models effectful computations as a sequence of effect requests, parameterized by an Ability (effect signature) and interpreted by a Handler. The core types are:

  • Fx<S, V>: An effectful computation with S pending effects, producing output V.
  • Ability: Describes the set of effects available.
  • Handler: Interprets effect requests.

3.2 Lenses and State

Fx integrates lenses for focusing on subparts of state, enabling modular stateful computations. The Lens abstraction allows handlers to operate on substructures, supporting patterns like zooming and partial provision of state.

3.3 Contextual Adaptation

Fx provides combinators (adapt, contra_map) for adapting effectful computations to different contexts, enabling modular and reusable handlers.

3.4 Accumulation

The Acc trait and related combinators enable flexible accumulation of outcomes, supporting patterns like folding, collecting, and batching.

4. Key Innovations

  • Unified abstraction: Fx unifies algebraic effects, lenses, and context adaptation in a single system.
  • Contextual adaptation: Generalized combinators for adapting computations to new contexts.
  • Effectful accumulation: Flexible accumulation patterns, supporting effectful folds and collections.
  • Rust integration: Leverages Rust’s type system and ownership model for safe, modular effectful programming.

5. Evaluation

We evaluate Fx through several case studies:

  • Stateful computations: Modular state threading using lenses.
  • Compositional interpreters: Building interpreters for embedded DSLs using handlers and context adaptation.
  • Effectful accumulation: Implementing streaming and batching patterns.

We discuss ergonomics, performance, and expressiveness, comparing Fx to existing Rust and Haskell effect systems.

6. Related Work

  • Algebraic effects and handlers [Plotkin & Pretnar, 2009; Kammar et al., 2013]
  • Free monads and extensible effects [Swierstra, 2008; Kiselyov et al., 2013]
  • Lenses [Kmett, 2012]
  • Kyo, an algebraic effects system for Scala whose kernel served as inspiration for the development of fx-rs.

7. Conclusion

Fx demonstrates that advanced effectful programming idioms can be realized in Rust, enabling modular, composable, and type-safe effectful computations. Our integration of algebraic effects, lenses, and context adaptation opens new possibilities for safe and expressive systems programming in Rust.


References

  • Plotkin, G., & Pretnar, M. (2009). Handlers of Algebraic Effects.
  • Kammar, O., Lindley, S., & Oury, N. (2013). Handlers in Action.
  • Swierstra, W. (2008). Data types à la carte.
  • Kiselyov, O., Ishii, H., & Sabry, A. (2013). Freer Monads, More Extensible Effects.
  • Kmett, E. (2012). Lens: Lenses, Folds, and Traversals.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment