Skip to content

Prelude operator traits Add/Sub/Mul/Div/MatMul/Neg (§11.7) #469

Closed

Description

@ehartford

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

  1. Add public trait declarations in lib/std/traits.w:
    Add[Rhs, Output], Sub[Rhs, Output], Mul[Rhs, Output], Div[Rhs, Output], MatMul[Rhs, Output], and Neg[Output].
  2. Match receiver style to the spec guidance: examples should prefer self: &Self and rhs: &Rhs for non-Copy types to avoid accidental moves, while still allowing implementations with other compatible receiver modes through normal method resolution.
  3. Register the new traits as builtin/prelude trait names anywhere sema_is_builtin_trait_name or equivalent allowlists live, so explicit bounds resolve without a manual import.
  4. 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.
  5. 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.
  6. Coordinate with Bug: user-defined unary neg fails in codegen (§11.7) #464 so unary - uses neg method dispatch and the Neg trait 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, and Neg
  • 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 :test pass.

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

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions