Stories: Operators

See also: spec/operators.md — the rules these chapters explain.

A small vocabulary worth overloading

Operators posed two questions that are easy to conflate: whether programmers could change the meaning of familiar notation for their own types, and whether they could invent new notation altogether. We wanted the first and rejected the second. Refusing overloading, as Go does, would keep the language surface exceptionally plain, but it would also make every user-defined numeric, geometric, or collection type a second-class citizen. A vector that cannot use the same addition notation as a scalar has lost much of what operators are for. Overloading the built-in set was therefore essential.

That did not imply an open-ended operator language. Custom tokens bring custom precedence, or at least the question of it, and make a reader learn a local dialect before an expression can be grouped. We instead kept one small, fixed vocabulary whose implementations may vary while its grammar does not. This is a deliberate middle ground: abstractions may participate in ordinary notation, but they may not extend the notation itself. The cost is that a domain whose natural operation has no place in the fixed set must use a named verb, even where a bespoke symbol would be concise.

Overloading also exposed a broader question. An overloaded name is easy to resolve at a call, where the operands and their types are present, but ambiguous when passed as a value to another overloaded verb. We initially met that as an obstacle specific to functions. Operators showed that it was really a lapse in orthogonality: they work cleanly because an operator token is call-only and can never be detached from its operands. We applied the same rule to every verb. A programmer who wants behavior as a value writes a lambda, which is an ordinary expression and can be stored or passed without asking an overloaded name to resolve to an implementation in a vacuum. That gives up the convenience of passing a bare function or operator name, but it gives all verbs one resolution model instead of adding special rules for each callable form.

One operator for flipping a value

The unary vocabulary became constrained before we chose its spelling. ! had already been reserved to make mutating method calls visible, so it could not also serve as boolean negation. At the same time, a preference for orthogonality made separate unary - and boolean-NOT operations look wasteful. Additive inverse and logical complement perform the same higher-level act: they flip a value. That idea extends beyond numbers and booleans; vectors and other composite values can define what their own flip means without requiring another unary concept.

We still needed a symbol for it. Unary - was familiar for numbers, but reusing the subtraction token would obscure the fact that this was a universal operation and would produce the jarring -true for booleans. ! was already occupied, and using it in the derived spelling of subtraction would make that operation look alien. ~ resembled - closely enough to suggest reversal while remaining visibly distinct, and Lua’s established ~= spelling made the resulting inequality notation look less invented. So ~ became the sole unary operator: logical complement for booleans, additive inverse for numbers, and the general flip point for types that define one.

The price is immediate familiarity. Readers coming from most C-family languages will first read ~value as bitwise complement, and ~true is less conventional than !true. Zane spends that familiarity to keep the unary model small and to leave ! with one unambiguous job.

Deriving the laws instead of trusting them

Once flip was primitive, several other operators no longer needed independent definitions. Subtraction could be addition of a flipped value; inequality could be flipped equality; and three comparisons could be expressed through < and flip. Letting a type implement those spellings separately would add flexibility only by permitting it to lie—for example, to make a > b disagree with b < a. Where a derivation is trivial, we preferred to make law violations inexpressible. The smaller implementation surface is useful, but it is the consequence rather than the point. It also rewards a type for defining the broadly useful ~: one implementation unlocks several derived forms.

Division is the deliberate boundary. Deriving a / b as a * (1 / b) assumes a multiplicative inverse exists, and floating-point arithmetic makes even the apparently equivalent reciprocal-multiply transformation observably different. Compilers place such transformations behind relaxed floating-point semantics for exactly that reason. / therefore remains primitive: a type can implement division directly without pretending it is multiplication in disguise.

Derivation has a cost of its own. A domain cannot provide an efficient specialized > or - whose behavior merely matches the law; the fixed expansion is the only spelling the language recognizes. We accept that lost optimization hook because an operator law that tools and readers can rely on is more valuable than an extra implementation point.

Grouping is grammar all the way down

Allowing implementations to vary made it more important that grouping never does. Precedence belongs to the grammar, not to types or declarations, so changing an operand type cannot reshape the expression around it. and and or remain keywords because their short-circuit behavior is control flow: an ordinary overloaded verb would receive already evaluated operands and could not preserve it.

The same orthogonality settles comparison chains. Python gives a < b < c a special chained-comparison meaning. We did not adopt that exception, nor did we prohibit the form to protect programmers from C-like surprises. Comparisons group left like the other binary operators, so the expression parses as (a < b) < c. Usually that second < has no matching overload and the program is rejected; if a package deliberately defines < for the boolean result and the third operand’s type, the expression is valid. The grammar gives no sequence of operands a special interpretation.

This rule permits code that many readers will initially assume is either forbidden or Python-like, and a deliberately unusual boolean comparison overload can make it meaningful. That is the honest cost of total regularity here. Parentheses remain the way to communicate an intended grouping when the conventional reading would be uncertain.

Imports may add names, not meanings

The last pressure came from separate compilation and ordinary maintenance. If any imported helper package could contribute an operator overload, adding or removing an import could silently change the meaning of an existing a + b. We wanted edits to imports to affect the names a file may mention, not reinterpret expressions that were already valid.

Operator implementations therefore belong to the home package of at least one operand type. A unary operator lives with its operand; a binary operator lives with either side. Resolution can find the candidates from the types already present in the expression, without treating the import list as an invisible extension registry. This follows the same coherence instinct as home-package methods, but operators make the danger sharper because their names are punctuation and the declaring package never appears at the call site.

The restriction closes off retroactive adaptation in a neutral third package. When two foreign types ought to interoperate, their operators cannot be supplied by an unrelated bridge package; one of the home packages must own the integration, or the caller must use a named conversion or verb. We take that friction over allowing a seemingly harmless import to rewrite the meaning of existing notation.

and and or become * and +

Once branching had left the language, one oddity was left standing where everything around it had a category. and and or were not operators — the fixed set is symbolic and they are words — and they were not functions either, because a function receives evaluated arguments and short-circuiting is precisely the refusal to evaluate one. They were keywords, which is a category with exactly two members and no principle behind it.

The first diagnosis we reached for was wrong and worth recording, because it sent us down a road. The complaint sounds like these are tied to Bool, and the language should not be tied to a type — the same complaint that moved if into core. It is not. ~ is tied to Bool too, and cleanly: it is an ordinary primitive operator with a Bool implementation, and nobody minds. The difference in and was never the type; it was the unevaluated operand. Get deferral and the oddity is gone.

That reframing made the answer smaller than expected, because a and b is a conditional — run b when a holds, otherwise false — so it needed no mechanism the block work had not already built. And since the operator vocabulary is fixed, the honest question became which existing tokens carry the meaning. * and + do: a Boolean algebra is a semiring, conjunction is the product and disjunction the sum, and the language already names sums and products elsewhere for the same underlying reason. So Bool implements the primitive set like any other type (operators.md §2.4), and the derived operators supply the rest for free — ~= is inequality, which on two booleans is exclusive or, and a - b is a + ~b, which is implication.

Two consequences fell out that we did not choose and could not have chosen otherwise. - cannot be exclusive or, however much one might want it there, because subtraction is already a fixed desugaring to a + ~b; the meaning was assigned before the question was asked. And the logical reading cannot generalize past Bool. Conjunction and disjunction are interderivable through the complement, and ~ on Int is an additive inverse rather than a complement, so the identity simply fails there — ~(~2 * ~3) is 4 where 2 + 3 is 5. There is no coherent 3 and 4 waiting to be defined, and the tokens on Int stay arithmetic. The question whether a type may have the logical reading turns out to be the question whether its ~ is a complement, and that is a real answer rather than a rule we imposed.

The short-circuiting went, and we let it go deliberately. Operands are evaluated, because these are ordinary operator calls and nothing about a token should silently change evaluation order. What replaces it is an overload taking a deferred right operand, which puts the choice at the call site where a reader can see it — the same move as marking a mutating call with ! rather than leaving it to be inferred from a declaration elsewhere. The cost is that cache:has(k) * expensive(k) runs expensive unconditionally, and someone will be bitten by it; the compensation is that the version that does not is visibly different on the page.

The loose operator forms and the ' prefix

Reusing * and + put conjunction at the wrong precedence, and we did not see it until we tried to write a condition with it, because the break was silent.

* sits at precedence level three and comparisons sit at five, the loosest. So conjunction binds tighter than comparison — backwards for every compound condition anyone writes. age > Int(18) * hasId groups as age > (Int(18) * hasId), which at least fails loudly on the missing Int * Bool. a == b * c == d with four booleans does not fail at all: it groups as (a == (b * c)) == d, type-checks, returns a Bool, and is not the conjunction of two equalities that was written. and and or never had this problem for a reason nobody had had to state: they sat below everything.

The escape we could not take was giving * one level for Bool and another for Int. Grouping in Zane is syntax and never depends on types — that rule predates all of this and is not worth spending here, since an expression whose shape you cannot read without knowing the operand types is worse than any spelling problem it would fix. That left parenthesising every compound condition, which works and is what the language already offers, but turns the common case into a pile of brackets.

So a binary operator gained a loose form: the same token with a leading ', calling the same implementation from a mirror of the table placed below every unprefixed level (operators.md §3.1). age > Int(18) '* hasId groups the way it reads. The mirror preserves relative order rather than flattening to a single level, so '* still binds tighter than '+ and a chain of conditions nests the way conjunction and disjunction ought to.

Three bounds keep this from becoming the custom precedence the fixed-precedence rule forbids. It is one tier deep — ''* is not a further shift, because iterated shifting would be a program choosing its own precedence levels in all but name. It is binary only: unary ~ binds tightest and has nothing to separate itself from, and pipe syntax was never part of the operator set. And it introduces no token, since ' selects an operator that already exists at a level that is already fixed. The vocabulary stays closed and grouping stays a property of the grammar.

The character was the last question, and . — the obvious choice — was the wrong character. It already carries field access, named constructors, and variant case forms, and the whole delimiter scheme rests on one character carrying one meaning per context. ' was free, and unusually cleanly: it had been the borrow marker for exactly one release of the design, and when that mode was removed it went back to being a character the lexis does not use. Taking it back was worth being deliberate about rather than defaulting into, but a mark that had already been reserved once, and is followed by punctuation rather than an identifier, costs nothing to reserve again.

Written order survives the swap

Deriving the comparisons from < settled which operand each primitive receives, and left a question the table could not answer: when. Two of the five desugarings hand < its operands the other way round — a > b is b < a, and a <= b is ~(b < a) — so a compiler that rewrote the text literally would evaluate the written right operand first. That would not matter if operators were arithmetic on plain values, but they are ordinary verbs, and only ~ is required to be pure. f(log) > g(log) can make two observable writes, and a literal reading of the table puts g’s first.

We could have left the order unspecified, as C does for most of its operators and as effects.md §3.2 already permits for a Pure verb, whose calls may be reordered because no program can observe the difference. For a verb with effects that freedom is a trap: the program has one meaning on one compiler and another on the next, and nothing at the call site shows which. We could instead have let the order follow the desugaring, which keeps the table a complete description of behaviour but makes > and <= evaluate right to left while >= does not. A reader would then need to remember which of the five swap — the very detail the derivation exists to keep out of sight.

So the operands of every operator are evaluated left to right, as written, and only then passed in the positions the desugaring gives them (operators.md §2.3). The table still fixes the meaning of >; it no longer fixes when its operands run. That is the order a reader assumes before they have ever seen the table, and it matches the order the spec already fixes for map literal entries.

The cost lands on implementations. The desugaring is no longer a rewrite of the source text, because the text it produces would evaluate in the wrong order: a compiler has to keep the operands where they were written and apply the swap separately, by marking the call or by binding both operands to temporaries before the reordered call. Where both operands are Pure, the old freedom returns as a consequence — no program can observe the difference, so an implementation may still evaluate them in whichever order suits it.

Primitives lose their operators to @operators$ functions

Every operator so far had been declared by a package, on a type that package owns, and that left the storage primitives in an awkward spot. Some operation had to add two machine words, and the first answer was that the primitives themselves did: @primitives$Int came with a +, a *, a < of its own, found by the same home rule as any operator, with the namespace standing in for a package. That made the spec’s claim that every intrinsic has exactly one signature quietly false, since an operator overloaded on its operands was a second exception beside methods that nobody had written down. And it caused a worse problem than a broken sentence: it made the primitives a numeric type a program could be written in. A file could alias @primitives$Int, compute with + and <, and never touch core. That is the opposite of what the primitives are for. They are the storage floor under core’s types, and nothing above core should stand on them directly.

So we took their operators away entirely. The home rule already gives the answer: a binary operator lives in the home package of one of its operand types, and a storage primitive’s home is the intrinsic namespace that holds it, which is not a package (operators.md §2.2). No package can declare an operator over primitives alone, so none exists, and a package that needs + declares it on a type of its own. What is left is the machine operation underneath, which has to live in some namespace, and we gave it a namespace of its own, @operators$, named for its members’ purpose: a package’s operators are written over them (operators.md §2.6).

They are functions, not operators, and that was the part worth arguing. The reason Zane uses operators at all is that the notation carries the mathematics: a + b reads as a sum, a < b < c can be chained in its loose form, and the left-operand-right shape is how people already write sums and comparisons. All of that is the surface a type presents to the programs that use it. Inside core, where Int +(left Int, right Int) is written once and read rarely, there is no notation to preserve, and a named call states exactly which machine operation runs. It is the same move that put if over @controlflow$branch: the familiar form is a declaration, and the intrinsic beneath it is a plain call. The set follows the primitive operators of operators.md §2.1 — add, multiply, divide, equal, lessThan, negate — and has no subtract, because subtraction is already defined as a + ~b (operators.md §4.2), and a machine operation for it would be an independent - beneath the derivation.

Bool was the first real fork. Its operators reuse the arithmetic tokens — * is conjunction and + disjunction, as the earlier chapter argued — so the obvious move was to reuse the arithmetic names too, with add overloaded on two Bools as or. We turned that down. The case for * and + was that the notation fits the algebra and buys the infix form, and neither half survives the move to a named function. @operators$add(ready, checked) does not read as a semiring; it reads as arithmetic on booleans, and a newcomer meeting it would be right to be confused. So Bool has and, or, and not, and String has concat, each named for its operation. equal is the one name that spans all three kinds of primitive, because equality has the same meaning on every type.

The second fork was about mixing. The first sketch overloaded every arithmetic operation on all four pairings of an integer and a float, so that @operators$add would take an Int and a Float in either order. That looked convenient until we asked how the machine adds, which is not a mixed add at all: it converts one operand and then adds two operands of the same type. A mixed overload would hide that conversion, and the conversion is a decision — an integer above 2⁵³ does not survive becoming a double — that belongs where a reader can see it, in core’s own source, not inside a compiler table. So every machine operation takes operands of one type, with one overload per type, and converting is a separate, written step; how that step is spelled is the subject of the types story.

Stating the operations exactly meant deciding their edges, which the old Int operators had never had to. An integer add can overflow, and division has a divisor of zero and the one quotient, the most negative value over -1, that no integer of the type holds. Floats were already settled: they are IEEE 754, which rounds to nearest with ties to even and gives an infinity or a NaN for a division by zero, so a float operation never fails. For integers the choice was between stopping the program and giving a value, and we gave a value every time. Overflow wraps, as the compiler always had it, and a division by zero gives zero, the definition Pony uses so that arithmetic cannot fail at run time and the one proof assistants such as Lean use because every function there must be total (operators.md §2.6). The usual argument for stopping is that a wrong number travels: in C a bad integer becomes a pointer offset or an index, and the damage surfaces far from the division. Zane has no pointer arithmetic, and an index out of range is checked and stops the program, so a wrong integer can be a wrong answer and nothing worse. Against that, a division that stopped would have been the one arithmetic operation that could end a program whose overflow already wraps without a word. The price is debugging: a zero from a division by zero is silent where a stop would have pointed at the line, and a program that should have checked its divisor learns it from a wrong result instead. A core that wants a checked division can build one over equal.

The cost falls on anything below core. Until a core exists, a program that computes on primitives writes @operators$add(a, b) where it would have written a + b, and the derived operators are harder to reach than they look. > is b < a and comes free with any <, but <= is ~(b < a), so it needs a ~ on whatever < returns, and nothing can declare ~ on a primitive; the derivation needs a declared truth type whose package complements it, which core’s Bool will be. We accept that, because it is the design working as intended: the operators belong to the types programs are written in, and the primitives are not those types.

Literal operands find no operator, and the home package stands in for a qualifier

The rule of the imports chapter met its first real test in a line nobody expected to fail. res Float = 3.0 * 3.0; was rejected with no operator * found for two @concepts$Float operands, while mul(3.0, 3.0), calling a function with exactly the signature of core’s *, compiled. An operator is meant to be an ordinary verb with a different spelling, core declares an implicit constructor from the float literal’s concept type to Float, and an operand is a coercion site (types.md §4.2). Everything we expected of operators suggested the line should work, and we had to find out why it did not before deciding whether it should.

The two calls differ in where their candidates come from, not in how they convert. mul is an identifier, and an identifier is resolved through the file’s scope and imports without looking at its arguments, so the call finds Float mul(Float, Float) first and only then coerces each literal into a parameter that is already fixed. An operator token is not an identifier. Its candidates are the operators declared in the home packages of its operand types (operators.md §2.2), and a concept type lives in the compiler’s @concepts$ namespace, which is not a package. Two literal operands therefore name no package at all, the candidate set is empty, and coercion never runs, because it would only run against a parameter that some candidate supplied.

The first road we looked at was to resolve operators the way functions resolve, by name through scope and imports, since that is how we had been thinking of them anyway. It turned out to cost less than it first seemed. The declaration rule would stay, so an overload whose parameters match the operand types exactly still has to live with one of those types, and home-package lookup already finds every one of them; an import could add only overloads that need a conversion. But that is exactly the case at hand: a bare literal always needs one. A package Units that declares Meters, an implicit Meters from @concepts$Float, and Meters *(Meters, Meters) does nothing its own home rule forbids, and once it could contribute operator candidates, 3.0 * 3.0 would multiply Floats in a file that imports core, Meters in a file that imports Units, and fail as ambiguous in a file that imports both. The guarantee would shrink from “an import never changes the meaning of a * b” to “an import never changes it when the operands match an overload directly”, and every literal operand would fall on the wrong side of the line.

The second road kept the home rule and gave literal operands a home by fiat: a @concepts$Float operand would also bring core’s operators into the candidate set. That preserves the guarantee, but it makes core the place every literal operand resolves into. That runs against core becoming an ordinary package whose conversions are found by the same rules as anyone else’s, and under it a literal could take part in core’s operators and no other package’s.

The third road went further and questioned whether implicit constructors were needed at all, since a function can take a concept type as a parameter and lower the literal itself. It can, but a leaf concept type stands for a single compile-time value (syntax.md §2.8), so a parameter of type @concepts$Float rejects a runtime Float. A verb that accepts either in each slot needs every combination, two overloads for one such parameter and sixteen for four, repeated across every function, constructor and operator that should take a literal, where one implicit constructor in core serves them all. And the overload that would make 3.0 * 3.0 itself work, * over two concept types, has no home package to be declared in. Every way of making literals reach operators without a conversion either multiplied declarations or needed a new rule, and the third road brought us back to the first two.

So we kept the rule, and what settled it was an intuition about operators rather than a technicality. A function call reads as a pipeline that carries values into a destination, and converting a value on the way in is part of a pipeline’s job. An operator reads as notation over the values written on either side of it, and we do not expect it to invent a type for those values to meet in. Float(3.0) * 3.0 asks for no such invention: the Float operand has already selected core’s *, and the 3.0 is converted into the parameter that overload fixed, the same as the second argument of mul(Float(3.0), 3.0). 3.0 * 3.0 asks the compiler to pick the type itself. The line falls between those two, and the rule already drew it there.

The anchor does not make Float(3.0) * 3.0 unambiguous. If core declared *(Float, Float) beside *(Float, Double), and Double accepted a float literal too, both would be viable once the literal is converted, and the call would be an ambiguity error under functions.md §5. What it guarantees is that the outcome, resolved or ambiguous, is the same in every file and every program, decided by the package that owns Float, and that no import can turn it into another outcome.

That left the question of why functions are allowed to do what operators are not, because functions are not immune to imports either. Two imports may merge their overloads under one bare name (packages.md §3.8), and two verbs collide at the import line only when their parameter types match. A file whose show(Int(3)) resolves to one package’s generic show can import a second package with show(x Int), and the call quietly moves to the direct match. That move is acceptable because the programmer can always decline it: import pkg spells every member as pkg$show at the use site (packages.md §3.3), and a qualified call considers that package’s overloads and nothing else. A method can be qualified at the call site in the same way (functions.md §6.2). An operator cannot. It is call-only and has no spelling but its token (operators.md §5.3), so there is nowhere to write a package. The home-package rule is the qualifier an operator cannot write, applied to every operator call: where a function is pinned to a package when its programmer asks, an operator is pinned to its operands’ packages always. That is a sharper reason than the reason the imports chapter gave. It is not only that the declaring package is invisible at an operator call, but that nothing the programmer could write there would make it visible.

The cost is a wrapped literal wherever no operand has a type of its own. 3.0 * 3.0 is written Float(3.0) * 3.0, and a package that overloads an operator across several types a literal converts into makes its users wrap both sides, a cost the package that owns the type decides and its users pay. A literal-only operator expression is rare, since a constant computed from constants is usually written as its result, but the error it gives is a real surprise to anyone who reads an operator as a function, which is how this chapter began.

and and or return as intersection and union, and the loose forms go

The loose forms had never sat comfortably, and in time the discomfort spread to the operators as a whole. They had come to feel like a wart beside the rest of the language: a second precedence table, reached through a sigil, existing so that two operators could be used at a level the first table did not give them. We were not sure '* was a spelling anyone would choose for conjunction, and we could not drop operators either, because without them arithmetic and conditions alike turn into a pile of nested calls. When we traced the discomfort back, every part of it led to one decision. The earlier chapter gave Bool the tokens * and +, those tokens sit above comparison, and the loose forms existed only to carry them back below it. A semiring is a true account of a Boolean algebra, but it put conjunction at the precedence of multiplication, and the language had paid for that ever since.

The obvious fix was two new tokens for Bool at a level below comparison, and we hesitated over exactly that. The operator set is small and fixed on purpose, and spending two of its members on one type would have been the loose forms again in a different place: a patch for Bool’s precedence that gave the rest of the language nothing. If the vocabulary was to grow, the new members had to earn their place on other types too.

The meaning that earned it was intersection and union. Conjunction is what two truth values share and disjunction what either covers, and the same pair is what two sets share and what either covers, and the flags two permission masks both hold and the flags either holds. So and and or joined the fixed set as the operators of intersection and union, and Bool became one type among several that implements them (operators.md §2.4). They sit below every comparison, and tighter than or, so age > 18 and hasId groups the way it reads, and with that the loose tier had no job left. It went entirely, its three levels, its sigil and its rules about tiers with it, and ' went back to being a character the lexis does not use.

Numbers were the case that tested the framing. The mathematics behind intersection and union is a lattice, and every total order is one, whose meet and join are the smaller and the larger of two values. Giving Int and as min and or as max would have been consistent, and a law tying the pair to < would even have fixed that meaning against any other. We turned it down. The smaller of two numbers is a different idea from the common part of two values, and a reader meeting budget and request would not guess that it yields the lesser of them. The pair belongs to types whose values have parts to share, and the numeric types implement neither.

We did keep the freedom every other operator has over its types. An and or or declares its own operand and result types under the home-package rule like any other operator, so a package may take two members of its own enum and return a set of them, and Permission.read or Permission.write builds a permission set without a constructor in sight. Fixing the signature to one type throughout would have bought a cleaner set of laws and lost the case that makes flags pleasant to write. The laws went in where they apply: where both operands and the result are one type, the implementations should be commutative, associative, idempotent, and absorptive, and the identity of De Morgan that the earlier chapter stated over * and + now holds over and and or for any type whose ~ is a complement (operators.md §4.4).

The spelling came down to words or doubled symbols. && and || are what most programmers would type first, and either would have worked, but & already marks a reference, and a && b reads as a reference operation before it reads as anything else. Words carry the meaning in prose already, so and and or became reserved words, the only words in the operator set (lexical.md §4.4). They were keywords once before, when grouping was settled, and the reason they left still holds: they return as ordinary overloadable operators, both operands are evaluated, and an operand that should run only when it determines the result is written inside a branch, as we decided in the earlier chapter.

Reserving the words took them from two places that had used them. No identifier may be called and or or, and the machine operations beneath Bool, which the primitives chapter had named @operators$and and @operators$or after their operations, became conjoin and disjoin, named after the same operations. Bool itself gave up * and +, so that a conjunction has one spelling, and with + went - as implication, which is now written a or ~b. ~= stays exclusive or, since it rests only on equality and the complement.

The cost is mostly in what the low level now forces. Because and binds below comparison, a and b == c groups as a and (b == c), and comparing the results of two conjunctions takes brackets. That is the rarer expression by far, and the price of the common expression reading correctly. The reserved words are a small tax on naming, and a type author must now decide whether a type’s values have parts in common before reaching for and and or, which is a real question with no rule to answer it beyond the laws.

Operators never coerce, and a literal operand has an overload of its own

The question that started the review of the operators came from a short program. If Int and Float each had an implicit constructor from the other, what would x + y do, with x an Int and y a Float? The answer was an ambiguity error. An operand was a coercion site, the direct and generic phases of overload resolution found nothing, and in the implicit phase both Int +(Int, Int) and Float +(Float, Float) became viable, one by converting y and the other by converting x. Neither operand had any claim over the other, so the compiler could not choose, and the error, though correct, gave no hint of the conversion the programmer had meant.

The first remedy we looked at made an operator a method spelled infix: a + b would mean a:+(b), and since a method’s subject is never converted, the left operand would determine the overload and x + y would resolve to the + that Int declares. That is deterministic, and it is exactly the trouble, because it resolves by quietly truncating y the moment a conversion from Float to Int exists. It also made the operands unequal: a scalar on the left of a vector, Int(2) * vec, would have had to live with Int in core, or be found through a second lookup on the right operand the way Python’s __radd__ is. The unease was with the conversion, not with which side picks the operator, so we left the operands as equals.

The next thought was to forbid conversion at an operand altogether, and that broke at once on literals. A literal reaches Int through the implicit constructor core declares from @concepts$Int, so a ban on coercion would have made n == 8 an error and turned every comparison with a literal into n == Int(8), which is the clutter operators exist to remove. A narrower rule followed: an operand would accept a conversion from a compiler concept type and no other, so that an operator could give a literal its type but never turn one value into another. That was the rule we were about to write down when we noticed it described a case that needed no coercion at all. An overload can declare a concept type as its parameter, and Int +(left Int, right @concepts$Int) was already a valid declaration. The literal did not have to be converted on its way in; an overload could simply take it.

The literal-operands chapter had considered concept-typed parameters and rejected them, because a verb that accepts either a literal or a value in each slot needs every combination of the two, sixteen overloads for four parameters. That objection is real for functions and does not reach operators. An operator has two operands, so a type needs at most two more overloads for each operator, one with the literal on the left and one with it on the right, and the derived operators follow from the primitive operators without any of their own. For core that is twenty-four declarations across Int, Float, and String, written once.

So an operator converts no operand (operators.md §2.7). Operands left the list of coercion sites (types.md §4.2), and the implicit phase of overload resolution has nothing to offer an operator call. x + y over an Int and a Float is now a plain no-match error whatever conversions exist, and Float(x) + y shows the conversion where it happens. count + 1 resolves directly through core’s literal overload, and so does Float(3.0) * 3.0, which the literal-operands chapter explained as a conversion into a parameter the Float operand had already fixed. 3.0 * 3.0 still finds no operator, for the reason that chapter gave: no operand names a package.

Review found the one operator the overloads did not reach. count - 1 is not a call to -, which no type declares, but count + ~1, and ~1 is a flip of a literal with no package to find a ~ in. The + overload taking a literal was there and never got the chance. We could have desugared a subtraction with a literal on the right by a separate rule, but that is a second table beside the derivation, and the laws chapter had made the derivation fixed. We could have left count - Int(1) as the spelling, which keeps the rule intact and brings back exactly the clutter this chapter set out to remove, in the most common arithmetic there is. The answer lay in the literal’s existing definition. An integer or float literal is a single value fixed at compile time, so flipping one needs no operator: ~1 is just the literal of the opposite sign, of the same concept type (operators.md §2.7). Subtraction then reaches the literal overload of + like any addition, and the language gained a spelling for a negative literal at no extra cost, where ~Int(1) had been the only spelling.

The cost is declarations. A package whose type should take literal operands writes the overloads for them, and a Meters with an implicit constructor from @concepts$Float no longer gets m * 2.0 from that constructor alone. Arithmetic across two types needs either a written conversion or a mixed overload declared by one of the types’ packages, where before an implicit constructor could have bridged them silently. We think the silence was the problem, and what an operator accepts is now visible in its declarations rather than in the conversions that happen to exist around it.