Fun with Algebraic Effects - from Toy Examples to Hardcaml Simulations
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.
Related posts
- Advent of Hardcaml
Jane Street Tech Blog hardMasterSeveral 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. Day 4's word search becomes a convolution over a shift register holding the last four grid rows, with a higher-order function generating sliding windows of any dimensions. Day 7's operator search becomes a deep pipeline that evaluates one operator combination per clock cycle, testing hundreds of millions of combinations in seconds. OCaml's type system, functors and metaprogramming make unrolled, parametrized hardware concise and reusable, and the post closes by challenging other HDL communities to solve Advent of Code puzzles too.
- Writing a DSL for high performance parallel programming in OxCaml
Jane Street Tech Blog hardMasterA 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. Kernels describe the computation for a single output index, and the compiler generates the loop, which keeps each iteration writing exactly one output and makes vectorization safe. The front end builds a GADT-typed DAG that tracks uniform vs. varying rates and provides three control-flow forms (select, if_, if_var) with different evaluation semantics. The backend uses OxCaml's runtime metaprogramming (quotes and splices) to emit OxCaml code, partially evaluating runtime-known values such as the chosen distance metric away and relying on the OxCaml compiler for further optimization.
- What the interns have wrought, 2022 edition
Jane Street Tech Blog mediumAdeptThree 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%. Datafetcher jobs can now be compiled into Incremental computations, using a Response_or_waiting type to represent async state and mandatory memoization of fetch nodes. Quickcheck gained a [@@contract] syntax for deriving property tests from interface signatures, along with early support for bisimulation testing of two implementations of the same API.
- What the interns have wrought, 2023 edition
Jane Street Tech Blog mediumAdeptThree 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. The second is a pure-OCaml tiktoken-compatible tokenizer with a handcrafted regex automaton and an imperative rewrite; it matched or beat the Rust reference implementations, though GC pauses gave it higher variance. The third adds async support to ppx_tracing by storing correlation IDs in the Async scheduler's execution context, which helped the Iron code review team cut server startup time by about 50%.
- ICFP 2024
Jane Street Tech Blog hardMasterJane 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. Examples include a redesigned Bonsai API that uses a local-mode witness and escape analysis to enforce its two-phase graph-building model, mixed blocks for records combining boxed and unboxed fields, ppx_template for stamping out kind/mode-specialized code until real polymorphism lands, and a zero-overhead or_null option type that uses kinds to forbid nesting. The team also released a bleeding-edge opam repo with the extended compiler and its libraries so outsiders can try the features ahead of any upstreaming.