---
title: "Fun with Algebraic Effects - from Toy Examples to Hardcaml Simulations"
description: "Algebraic effects in OCaml 5/OxCaml can replace monads for most uses, and Jane Street ported Hardcaml_step_testbench from a monadic API to effects."
url: "https://swe.quest/articles/fun-with-algebraic-effects-from-toy-examples-to-hardcaml-simulations-5f2853fd03f84a3a"
canonical: "https://blog.janestreet.com/fun-with-algebraic-effects-hardcaml/"
source: "Jane Street Tech Blog"
published: "2026-01-06"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 17
tags: ["functional-programming","language-design","concurrency","testing"]
---

# Fun with Algebraic Effects - from Toy Examples to Hardcaml Simulations

A post on [Jane Street Tech Blog](https://swe.quest/sources/jane-street.md), published 2026-01-06, 17 min read, Deep dive. Original: [https://blog.janestreet.com/fun-with-algebraic-effects-hardcaml/](https://blog.janestreet.com/fun-with-algebraic-effects-hardcaml/)

## Summary

Algebraic effects in OCaml 5/OxCaml can replace monads for most uses, and Jane Street ported Hardcaml\_step\_testbench from a monadic API to effects. Monads spread through every caller, require special combinators and returns, and block OxCaml features such as locally allocated values and unboxed layouts, because bind needs globally allocated closures returning value-layout types. Using the type-safe Handled\_effect library, a computation performs operations through a local handler, and the handler code receives a continuation it can resume. The approach is then applied to interleaving multiple testbench threads that synchronize on clock cycles in FPGA circuit simulations.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Functional programming](https://swe.quest/tags/functional-programming.md) (Languages)
- [Language design](https://swe.quest/tags/language-design.md) (Compilers & runtimes)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)

## Prerequisites

- OCaml syntax ([easy posts on Functional programming](https://swe.quest/tags/functional-programming.md?difficulty=easy))
- monads
- GADTs

## Keywords

OCaml 5, OxCaml, Handled\_effect, Hardcaml, Hardcaml\_step\_testbench, Async, algebraic effects

## Related posts

- [Advent of Hardcaml](https://swe.quest/articles/advent-of-hardcaml-7a69fcd00d2b9f17.md): Jane Street Tech Blog · 2025-03-22 · Hard · 8 min read · Hardware, Functional programming, Algorithms
  Several Advent of Code 2024 puzzles were solved entirely on a small Lattice ECP5 FPGA (84K LUTs, 4 Mbits RAM) using Hardcaml, Jane Street's OCaml-embedded HDL, with the open-source Yosys + NextPNR toolchain.
- [Writing a DSL for high performance parallel programming in OxCaml](https://swe.quest/articles/writing-a-dsl-for-high-performance-parallel-programming-in-oxcaml-5f7bd51c2038a2c3.md): Jane Street Tech Blog · 2026-11-06 · Hard · 17 min read · Compilers, Functional programming, Language design, Performance engineering, Hardware
  A Jane Street intern built a JIT compiler for an SPMD-style DSL in OxCaml so SIMD kernels for options pricing and risk checks can be written once as ordinary-looking OxCaml functions instead of architecture-specific C intrinsics.
- [What the interns have wrought, 2022 edition](https://swe.quest/articles/what-the-interns-have-wrought-2022-edition-0f4dcf7e53f3e8d4.md): Jane Street Tech Blog · 2022-08-25 · Medium · 8 min read · Functional programming, Testing, Performance engineering, Developer tools, Engineering culture
  Three Jane Street intern projects from 2022 show substantial tooling work in OCaml. magic-trace gained a sampling mode that can cover 20 seconds to 20 minutes on Intel or AMD hardware, plus multi-process tracing, CPU-frequency transition reporting and a perf dlfilter backend that speeds trace decoding by 10–50%.
- [What the interns have wrought, 2023 edition](https://swe.quest/articles/what-the-interns-have-wrought-2023-edition-f070c30cff9e3c3e.md): Jane Street Tech Blog · 2023-09-12 · Medium · 9 min read · Functional programming, Observability, Developer tools, LLMs, Concurrency
  Three Jane Street intern projects, all in OCaml. The first is an SQL-like temporal query language, parsed with Angstrom and compiled onto a Linear Temporal Logic engine, for finding event sequences in Concord's transaction stream.
- [ICFP 2024](https://swe.quest/articles/icfp-2024-a2b848db9d4cf383.md): Jane Street Tech Blog · 2024-08-29 · Hard · 8 min read · Functional programming, Type systems, Language design, Memory management, Compilers
  Jane Street's OCaml Language team brings nine papers and talks to ICFP 2024 and describes its compiler-branch extensions: modal types (modes like local and unique) for Rust-like control over allocation and data-race-free parallelism, and a kind system for unboxed layouts.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
