tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

52 lignes · 5241 octetsBrut · Historique

ADR 0027: <> is Semigroup

Status: Accepted by the project owner on 2026-10-12; proposed 2026-10-11 (issue #95); needs engineering review (#44). It extends ADR 0007's rule that operators are trait methods.

Context

  • 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.
  • Lists and Bytes have their own append functions (list.append, bytes.concat) but no operator. Generic code cannot join values of an unknown type.
  • 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.
  • String concatenation is everywhere: HTML pages, logs, derived Show. It must not get slower.

Decision

<> is the method of a prelude trait:

pub trait Semigroup<a> {
  fn append :: x: a y: a -> a
}
  • 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.
  • 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.
  • 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 +.
  • 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.

Impls in the prelude:

Typex <> yWhy
Stringx then yThe operator's original meaning.
List<a>x's elements then y'sThe other everyday sequence. A unique x reuses its cells in place (Perceus reuse); a shared one is copied.
Bytesx's bytes then y'sSame as bytes.concat: it copies both. To build Bytes from many pieces, a Builder stays the efficient way.
Orderx, unless it is Eq: then yBreaking 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.

Rejected impls:

  • 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.
  • 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.
  • Dict<k, v>: a union must choose which value wins for a shared key. A named dict.merge can say so.
  • Builder: builders append Bytes to a builder (bytes.append(builder, data)), not one builder to another.
  • Tuples: joining tuples field by field is rare. (Since ADR 0018 Rev. 1 an impl for tuples could be written in the prelude.)
  • Monoid (an empty value): <> doesn't need it. Generic code that does can take the empty value as an argument, as list.fold does.

Consequences

  • 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.
  • New errors: <> on a type without an impl is E0401 with a note, where it was a mismatch with String before.
  • 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).
  • Name: user code may define its own append: a module's definitions shadow the prelude, as for show.
  • 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.

Alternatives rejected

  • 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.
  • A different operator for lists (++). It's one more operator to learn, and it would still not work on Bytes or user types.
  • 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.