| @@ -0,0 +1,78 @@ |
| 1 | +# ADR 0026: Functors and monads in the standard library |
| 2 | + |
| 3 | +**Status:** Proposed — 2026-10-11 (issue #95); needs engineering review (#44). |
| 4 | + |
| 5 | +## Context |
| 6 | +- Since [ADR 0018](0018-multi-parameter-traits-and-hkt.md), Louise can express `Functor`, `Applicative` and `Monad` as traits over type constructors. `tests/run/hkt.lse` does it for `List`, `Option` and `Result<a>`. The standard library has none of them: it has named functions per type (`list.map`, `option.then`, `result.try`), as Gleam does. |
| 7 | +- Issue #95 compared Haskell's type classes and standard monads with Louise, to decide what `std` should have before v1 freezes it. |
| 8 | +- Louise has algebraic effects ([ADR 0008](0008-effect-typing.md), [ADR 0010](0010-lexical-handlers.md)), multi-shot resumptions ([ADR 0017](0017-multishot.md)), regions, and `use`. Most of what Haskell programs do with monads, Louise programs do with these. |
| 9 | + |
| 10 | +## Decision |
| 11 | + |
| 12 | +### No `Functor`/`Applicative`/`Monad` hierarchy in `std` |
| 13 | +Effects and `use` cover what monads do in Haskell. A hierarchy would bring Haskell's learning curve without Haskell's payoff: |
| 14 | + |
| 15 | +| Haskell | Louise | |
| 16 | +|---|---| |
| 17 | +| `IO` | The `<io>` effect, in direct style | |
| 18 | +| `Maybe`, `MaybeT` | `Option`, with `use x <- option.then(o)` | |
| 19 | +| `Either e`, `ExceptT` | `Result` ([ADR 0024](0024-errors-as-values.md)), with `use x <- result.try(r)`; or a `final ctl` effect when a handler should decide | |
| 20 | +| `State s`, `StateT` | `region { }` with `Ref`s (`st<h>`), or an effect with `get` and `put` operations | |
| 21 | +| `Reader r`, `ReaderT` | An effect with an `ask()` operation (`fn` clause). The demo's `users` effect is one | |
| 22 | +| `Writer w`, `WriterT` | An effect with a `tell(w)` operation. The demo's tests collect the `log` effect this way | |
| 23 | +| `[]` (nondeterminism) | `multi ctl choose()` ([ADR 0017](0017-multishot.md)), or `list.flat_map` | |
| 24 | +| `Cont`, `ContT` | `ctl` operations and `resume`; `use` for continuation-passing style | |
| 25 | +| `ST s` | `region { }`: the same guarantee, from effect rows instead of a rank-2 type | |
| 26 | +| `STM`, `Async` | None. Actors and message passing instead ([ADR 0004](0004-actor-model.md)) | |
| 27 | +| Transformer stacks (`RWST`, `lift`, mtl classes) | Effect rows compose: `/ <users, log, io>` needs no stacking and no lifting | |
| 28 | +| `do` notation | `use`, and direct style for effects | |
| 29 | + |
| 30 | +Higher-kinded traits stay in the language for libraries that want them; `tests/run/hkt.lse` shows how. |
| 31 | + |
| 32 | +### Concrete functions instead |
| 33 | +The parts of `Functor`, `Applicative`, `Traversable` and `Alternative` that programs actually use are named functions on each type: |
| 34 | + |
| 35 | +| Function | Haskell | |
| 36 | +|---|---| |
| 37 | +| `list.flat_map(xs, f)` | `concatMap`, `>>=` on lists | |
| 38 | +| `list.try_map(xs, f) -> Result<List<b>, e>`: stops at the first `Error`, after which `f` is not called again | `traverse` / `mapM` on `Either` | |
| 39 | +| `list.zip`, `list.unzip`, `list.zip_with` (up to the shorter list) | `zip`, `unzip`, `zipWith` | |
| 40 | +| `option.or(a, b)`, `option.lazy_or(a, f)` | `<\|>` on `Maybe` | |
| 41 | +| `result.or(a, b)`: the first `Ok`, else the last `Error`; `result.lazy_or(a, f)` | `<\|>` on `Either` (as in `Alternative (Either e)` libraries) | |
| 42 | +| `option.map2(a, b, f)`, `result.map2(a, b, f)`: the first `Error` | `liftA2` | |
| 43 | + |
| 44 | +They were there already: `map`, `then`/`try`, `all`, `values`, `map_error` (ADR 0024). Every callback is effect-polymorphic (`/ <e>`), so `f` may print, send messages, or perform any effect, like `list.map`'s. `lazy_or` calls its fallback only when needed, so the fallback may be expensive or effectful. |
| 45 | + |
| 46 | +`try_map`, `flat_map`, `zip`, `unzip` and `zip_with` are not tail-recursive, like `list.map`: they use stack in proportion to the list. |
| 47 | + |
| 48 | +### `Result<a, e>` keeps the value type first, on purpose |
| 49 | +[ADR 0018](0018-multi-parameter-traits-and-hkt.md) noted that with this order, `Result<a>` fixes the value type, so a generic `Functor<Result<a>>` maps over the error. Haskell's `Either e a` puts the error first for that reason. |
| 50 | + |
| 51 | +That only matters with a generic `Functor` in `std`, which this ADR rules out. Gleam, which has no higher-kinded types, has the same order (`Result(value, error)`). Value-first also reads as the common case first, and ADR 0024 built the whole `Result` API on it. So the order stays, and this ADR records that it is chosen, not inherited. |
| 52 | + |
| 53 | +### No symbolic operators |
| 54 | +`<$>`, `<*>`, `>>=`, `<|>`, `<?>`, `*>` and `<*` are not added: |
| 55 | +- **Pipes, `use` and named functions say the same:** `x |> option.map(f)`, `use v <- result.try(x)`, `option.or(a, b)`, `p |> parser.label("x")`. |
| 56 | +- **Names explain themselves:** a named function shows in hover and completion with its documentation; `<$>` explains nothing until one knows it. |
| 57 | +- **They pay off only with the hierarchy:** without a generic `Functor`, each operator would work on one type, or need a trait of its own. |
| 58 | +- **The grammar stays fixed:** Louise's operators and their precedence are fixed, and there are no user-defined operators. These would grow the lexer, the precedence table, the formatter and the error messages. |
| 59 | +- **Gleam made the same choice.** A parser-combinator library can offer `parser.map`, `parser.or`, `parser.keep` and `parser.label`, chained with pipes. |
| 60 | + |
| 61 | +`<>` is the exception being considered: it is already an operator, and [ADR 0027](0027-semigroup.md) puts it behind a `Semigroup` trait. |
| 62 | + |
| 63 | +### Rejected classes |
| 64 | +- **`Bifunctor`:** `result.map` and `result.map_error` cover `Result`, the only bifunctor programs use. `result.map_both`, or `pair.map_first` and `pair.map_second`, can come when callers need them. |
| 65 | +- **`MonadFail`:** a `match` handles the case a failing pattern would. |
| 66 | +- **`Foldable`, `Traversable` as traits:** `list.fold`, `dict.fold`, `list.try_map` and `result.all` cover them per type. |
| 67 | +- **`Contravariant`, `Comonad`, `Arrow`, `Category`, `MonadFix`, `MonadZip`:** rarely used even in Haskell. |
| 68 | +- **`Identity`, `Const`, `Proxy`:** useful only together with a generic hierarchy. |
| 69 | + |
| 70 | +## Consequences |
| 71 | +- Generic code over "any functor" is not written with `std`. A library that needs it defines its trait, as `tests/run/hkt.lse` does. Adding a hierarchy later would not break code that uses the named functions. |
| 72 | +- New types get the same small set of named functions (`map`, `then`/`try`, `or`, `map2`) when they need them, rather than impls. |
| 73 | +- Tests: `tests/run/combinators.lse`, on both backends, covers every function added here, including that `try_map` and `lazy_or` stop calling their callbacks. |
| 74 | + |
| 75 | +## Alternatives rejected |
| 76 | +- **A `Functor`/`Applicative`/`Monad` hierarchy in `std`:** see above. It would also force the question of `Result`'s parameter order. |
| 77 | +- **Swapping `Result` to `Result<e, a>`:** it would break every `Result` annotation, for the sake of a hierarchy `std` does not have. |
| 78 | +- **Symbolic operators as functions with names** (`map` exported as `<$>` too): two names for one thing, and still the grammar change. |