Urgent.News

What's breaking now, across thousands of outlets.

Tech

Tiny, Untyped Monads (2024)

Monads are structures used to abstract context in programming, simplifying programs and making them more comprehensible. However, their implementation can appear overly complex due to convoluted types and instantiations. By examining the fundamentals of monads, one can uncover the underlying simplicity and elegance. In a pure, untyped language called "bruijn," the author discovered this beauty and aims to share it through a simplified JavaScript implementation.

This article will explain the concepts using plain JavaScript syntax, focusing on educational purposes and not for production use.

To begin, let's explore functional data structures, which involve encoding state without relying on traditional structures like objects or arrays. These data structures must have two capabilities: storing data and extracting data. When storing, the data should remain uninterpreted as a function to ensure recoverability. For extraction, we employ selector functions that apply the stored data to specific selectors, making it easily extractable.

Imagine a data structure capable of holding two distinct types of items: "person" and "animal." Two selector functions are required for each type, serving as arguments that, when called with the stored value, reveal the item's type. Constructors for these types can be created with a value argument, enabling the separation of "person" and "animal" types. The selectors can then determine which type the constructed data holds.

The concept of multiple arguments and selector functions is not unique to monads. Many formally studied encodings of pure lambda calculus utilize this same principle. A common example is the Maybe monad, which can store either one or zero elements and supports checks for whether an item is stored or not. For instance, a division function can be modified to return a Maybe monad, signifying either a successful division ("Just") or an error case of dividing by zero ("Nothing"). This simplifies error handling and eliminates the need for numerous if statements.

The bind operation, a key feature of monads, plays a crucial role in chaining actions together, eliminating the need for explicit state tracking. In JavaScript, this can be achieved using a do-notation-like syntax inspired by Magnus Tovslid's work. Generators in JavaScript can simplify monadic code by allowing for cleaner, more readable syntax.

Another monad worth discussing is the Either monad, similar to the Maybe monad but capable of storing two distinct values with different tags ("Left" or "Right"). For example, the division-by-zero error case can be represented with the "Left" tag, while successful divisions are tagged as "Right." Chaining the Either monadically involves only passing "Right" values, while the "Left" case is immediately returned, streamlining error handling and making the code more concise and readable.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at text.marvinborner.de →

More in Tech

More from Monday 24 August →