Repository navigation
Description
Summary
Add the optional prelude operator traits Add, Sub, Mul, Div, MatMul, and Neg promised by §11.7. Operator dispatch already uses fixed method names, but users need these traits for explicit bounds, documentation, and generic APIs.
Spec
§11.7 says arithmetic/comparison operators use fixed method names on concrete types, while prelude traits Add, Sub, Mul, Div, MatMul, Neg, Eq, and Ord remain available for explicit bounds. The spec gives signatures for Add[Rhs, Output], Sub[Rhs, Output], Mul[Rhs, Output], Div[Rhs, Output], MatMul[Rhs, Output], and Neg[Output].
Current state
lib/std/traits.w declares Eq, Ord, Hash, Debug, Display, Default, Clone, Drop, iterator and syntax traits, but no Add, Sub, Mul, Div, MatMul, or Neg (lib/std/traits.w:8-58, lib/std/traits.w:171-198). User-defined binary operator dispatch itself exists by fixed method names in src/SemaCheck.w:3703-3800 and is covered for add/sub/right-side dispatch in test/spec/spec_ss11_7_operator_one_impl_rule.w:6-34; @ dispatch coverage exists at test/spec/spec_ss11_7_multi_index_and_dispatch.w:84-115.
Implementation plan
- Add public trait declarations in
lib/std/traits.w:
Add[Rhs, Output],Sub[Rhs, Output],Mul[Rhs, Output],Div[Rhs, Output],MatMul[Rhs, Output], andNeg[Output]. - Match receiver style to the spec guidance: examples should prefer
self: &Selfandrhs: &Rhsfor non-Copy types to avoid accidental moves, while still allowing implementations with other compatible receiver modes through normal method resolution. - Register the new traits as builtin/prelude trait names anywhere
sema_is_builtin_trait_nameor equivalent allowlists live, so explicit bounds resolve without a manual import. - Do not route operator dispatch through trait selection unless that is already the local pattern; unbounded generics should still compile and check on instantiation by method names.
- Add primitive impls only if needed for explicit bounds on primitives. If primitive impls would require compiler magic, phase them carefully and test that they do not create recursive operator lowering.
- Coordinate with Bug: user-defined unary neg fails in codegen (§11.7) #464 so unary
-usesnegmethod dispatch and theNegtrait is useful.
Test plan
Add test/spec/spec_ss11_7_operator_prelude_traits.w:
- generic
fn add_one[T: Add[i32, T]](...) - generic bounds for
Sub,Mul,Div,MatMul, andNeg - user-defined type implements the traits and uses both explicit method calls and operator syntax
Add compile-error cases for unknown/missing bounds if an implementation does not satisfy the declared trait.
Acceptance criteria
- All six §11.7 operator traits are available through the prelude.
- Explicit generic bounds using those traits resolve.
- Operator syntax continues to dispatch through the existing fixed method-name mechanism.
- Primitive operator behavior is not regressed.
with build && with build :fixpoint && with build :testpass.
Dependencies
#464 for unary Neg behavior. #391 improves generic bound reliability but should not be used as an excuse to add compiler-only dispatch.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.7.1.37, 11.7.1.5.
Activity
ehartford commentedon Jun 13, 2026
Implemented in c057927. Added the §11.7 prelude operator traits Add, Sub, Mul, Div, MatMul, and Neg to std.traits, with positive coverage for explicit generic bounds plus fixed-name operator dispatch and negative coverage for missing Add bounds. Verification passed: build, fixpoint, full test suite, and test-green.