louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#91 · main · 15 min 44 s
0Étoiles
0Forks
40Issues
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:
==usesEq,<usesOrd,+usesNum.<>was hard-wired toString: the checker forced both sides toString, and lowering emittedtl_string_concat. - Lists and
Byteshave 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
Semigroupis Haskell's, for the same operator and law:(a <> b) <> cequalsa <> (b <> c). The method isappend, as in Haskell'smappendand Gleam'slist.append/string.append. The prelude'sappendis visible everywhere, likeshoworcompare;list.appendstays, qualified. - Typing follows
+: the left side's type is inferred, the right side is checked against it, and the result has that type, with aSemigroupconstraint.fn joined(a, b) { a <> b }getsfn(a, a) -> a where Semigroup<a>.fn exclaim(s) { s <> "!" }is stillfn(String) -> String. - Lowering: where the type is
String,<>is still a directtl_string_concat, in both backends. Elsewhere it calls the dictionary'sappend; known dictionaries are then specialised by ADR 0013's pass, as for+. - Errors:
1 <> 2is E0401, "Intdoes not implementSemigroup", with a note listing the types that do."total: " <> 3is still a type mismatch:expected String, found Int.
Impls in the prelude:
| Type | x <> y | Why |
|---|---|---|
String | x then y | The operator's original meaning. |
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. |
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. |
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. |
Rejected impls:
IntandFloat: there are two equally good semigroups, sum and product, and+and*already exist. Haskell needsSum/Productwrappers for the same reason.Option<a>,Result<a, e>: it's not clear whether<>should keep the firstSome, or combine the contents.option.orandresult.or(issue #95) say which.Dict<k, v>: a union must choose which value wins for a shared key. A nameddict.mergecan say so.Builder: builders appendBytesto 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(anemptyvalue):<>doesn't need it. Generic code that does can take the empty value as an argument, aslist.folddoes.
Consequences
- Inference changes for unannotated code: a function whose only use of a parameter is
<>used to inferStringand now inferswhere Semigroup<a>. Callers with Strings see no difference. A program that relied on<>to fix a type toString, 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 withStringbefore. - Performance is unchanged for Strings: on every program of
tests/benchandtests/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.htmlran in 276 ms before and 274 ms after;dictin 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 forshow. - The prelude declares two externs it uses for the impls,
tl_string_concatandtl_bytes_concat.std/byteskeeps its ownconcatfor 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 onBytesor user types. - Always going through the dictionary, relying on specialisation alone. The interpreter does not specialise (ADR 0013), so
louise runwould get slower on strings. The direct path costs one type test in lowering.