| @@ -0,0 +1,52 @@ |
| 1 | +# ADR 0027: `<>` is `Semigroup` |
| 2 | + |
| 3 | +**Status:** Proposed — 2026-10-11 (issue #95); needs engineering review (#44). It extends [ADR 0007](0007-type-inference-and-traits.md)'s rule that operators are trait methods. |
| 4 | + |
| 5 | +## Context |
| 6 | +- Every operator but one is a trait method: `==` uses `Eq`, `<` uses `Ord`, `+` uses `Num`. `<>` was hard-wired to `String`: the checker forced both sides to `String`, and lowering emitted `tl_string_concat`. |
| 7 | +- Lists and `Bytes` have their own append functions (`list.append`, `bytes.concat`) but no operator. Generic code cannot join values of an unknown type. |
| 8 | +- Issue #95 surveyed Haskell's type classes. It rejected new symbolic operators, but kept this one open: `<>` already exists and everyone reads it, so giving it a trait adds no syntax. |
| 9 | +- String concatenation is everywhere: HTML pages, logs, derived `Show`. It must not get slower. |
| 10 | + |
| 11 | +## Decision |
| 12 | +**`<>` is the method of a prelude trait:** |
| 13 | + |
| 14 | +```louise |
| 15 | +pub trait Semigroup<a> { |
| 16 | + fn append(x: a, y: a) -> a |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +- **The name** `Semigroup` is Haskell's, for the same operator and law: `(a <> b) <> c` equals `a <> (b <> c)`. The method is `append`, as in Haskell's `mappend` and Gleam's `list.append`/`string.append`. The prelude's `append` is visible everywhere, like `show` or `compare`; `list.append` stays, qualified. |
| 21 | +- **Typing** follows `+`: the left side's type is inferred, the right side is checked against it, and the result has that type, with a `Semigroup` constraint. `fn joined(a, b) { a <> b }` gets `fn(a, a) -> a where Semigroup<a>`. `fn exclaim(s) { s <> "!" }` is still `fn(String) -> String`. |
| 22 | +- **Lowering:** where the type is `String`, `<>` is still a direct `tl_string_concat`, in both backends. Elsewhere it calls the dictionary's `append`; known dictionaries are then specialised by ADR 0013's pass, as for `+`. |
| 23 | +- **Errors:** `1 <> 2` is E0401, "`Int` does not implement `Semigroup`", with a note listing the types that do. `"total: " <> 3` is still a type mismatch: `expected String, found Int`. |
| 24 | + |
| 25 | +**Impls in the prelude:** |
| 26 | + |
| 27 | +| Type | `x <> y` | Why | |
| 28 | +|---|---|---| |
| 29 | +| `String` | `x` then `y` | The operator's original meaning. | |
| 30 | +| `List<a>` | `x`'s elements then `y`'s | The other everyday sequence. A unique `x` reuses its cells in place (Perceus reuse); a shared one is copied. | |
| 31 | +| `Bytes` | `x`'s bytes then `y`'s | Same as `bytes.concat`: it copies both. To build `Bytes` from many pieces, a `Builder` stays the efficient way. | |
| 32 | +| `Order` | `x`, unless it is `Eq`: then `y` | Breaking ties when comparing by several fields: `compare(a.last, b.last) <> compare(a.first, b.first)`. This is Haskell's `Ordering` semigroup. Both sides are evaluated: `<>` is not lazy. | |
| 33 | + |
| 34 | +**Rejected impls:** |
| 35 | +- `Int` and `Float`: there are two equally good semigroups, sum and product, and `+` and `*` already exist. Haskell needs `Sum`/`Product` wrappers for the same reason. |
| 36 | +- `Option<a>`, `Result<a, e>`: it's not clear whether `<>` should keep the first `Some`, or combine the contents. `option.or` and `result.or` (issue #95) say which. |
| 37 | +- `Dict<k, v>`: a union must choose which value wins for a shared key. A named `dict.merge` can say so. |
| 38 | +- `Builder`: builders append `Bytes` to a builder (`bytes.append(builder, data)`), not one builder to another. |
| 39 | +- Tuples: impls need a named type at their head (ADR 0018), and joining tuples field by field is rare. |
| 40 | +- `Monoid` (an `empty` value): `<>` doesn't need it. Generic code that does can take the empty value as an argument, as `list.fold` does. |
| 41 | + |
| 42 | +## Consequences |
| 43 | +- **Inference changes for unannotated code:** a function whose only use of a parameter is `<>` used to infer `String` and now infers `where Semigroup<a>`. Callers with Strings see no difference. A program that relied on `<>` to fix a type to `String`, and then needed that type to be known elsewhere, may now get E0403 (ambiguous) and need an annotation. No program in the repository did. |
| 44 | +- **New errors:** `<>` on a type without an impl is E0401 with a note, where it was a mismatch with `String` before. |
| 45 | +- **Performance is unchanged for Strings:** on every program of `tests/bench` and `tests/bench/compile/std_tour.lse`, the generated C is the same as before up to the numbering of generated names, because the prelude gained functions. `html` ran in 276 ms before and 274 ms after; `dict` in 108 ms both times (best of 3, release, one thread). |
| 46 | +- **Name:** user code may define its own `append`: a module's definitions shadow the prelude, as for `show`. |
| 47 | +- The prelude declares two externs it uses for the impls, `tl_string_concat` and `tl_bytes_concat`. `std/bytes` keeps its own `concat` for the same symbol. |
| 48 | + |
| 49 | +## Alternatives rejected |
| 50 | +- **Keep `<>` for Strings only, with separate list and bytes functions.** It is simpler, but generic code cannot join values, and `<>` stays the only operator that isn't a trait method. |
| 51 | +- **A different operator for lists (`++`).** It's one more operator to learn, and it would still not work on `Bytes` or user types. |
| 52 | +- **Always going through the dictionary, relying on specialisation alone.** The interpreter does not specialise (ADR 0013), so `louise run` would get slower on strings. The direct path costs one type test in lowering. |