Skip to reference

std.ops

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.ops — the operator traits: the protocols + - * / % and unary - dispatch through when their operand is a type parameter or a user type. Stability tier: core.

**This module is what [type.trait.op] asks std for, and the shapes are the clause's, not this module's opinion.** Since s155 (wolf-lang #5, spec/10 §11) an operator whose left operand is a type parameter or a user type is a call to a trait method chosen BY NAME: a + b is Add.add(a, b), -a is Neg.neg(a). The compiler refuses any other method shape with E0514, a missing impl with E0502 and no trait in scope with E0301, so the six declarations below are transcriptions and a rename here is a break for every caller.

On primitives the impls ARE the builtin operations — the clause says so in as many words, so [T: Add] instantiated at int is the machine add and nothing is interposed. Every body below is one operator expression for that reason; a body that did arithmetic of its own would be a different function wearing the operator's name.

**Eq and Ord are NOT here.** They are std.cmp's and they stay there: Ordering is one enum in std (API-CONVENTIONS §6) and the compiler reads Ord.cmp's answer by VARIANT NAME, so a second Ordering would be a second answer to a settled question. A bound that wants both writes [T: ops.Add + cmp.Ord]; the composed alias Num is the next paragraph's subject.

**trait Num SHIPS HERE, AND IT IS THE LAST ITEM OF s155's SURFACE THIS MODULE OWED.** [type.trait.op.alias] spells it trait Num = Add + Sub + Mul + Div + Rem + Eq + Ord and wolf-std#26 asked for it by name; sc44 withheld it and wrote its own trigger — "the wolf-interp release whose conformance pin is at or past wolf-lang c9237c1 (v0.2.11)". is46 is that release: lupin 0.1.34 pins c9237c1 and parses the alias form (wolf-interp#92). Measured before a line was written, not inherited from the release note. Eq and Ord are qualified because wolf binds whole modules and has no item imports, so the composed bound a caller writes is [T: ops.Num] and nothing else.

**The second trigger is met too, so Num is satisfiable at f64 on the day it lands rather than a release later.** impl Rem for f64 was the half sc44 withheld with Num: its body is self % other by the clause, and at 0.2.11 BOTH compiler tiers refused that expression (no frem op natively, unruled: fmod vs remainder checked), which cost every importer a lane rather than costing this one impl. s156's [type.float.rem] (wolf-lang#327) rules it and lowers it — truncated-division remainder, sign of the dividend, x % 0.0 is NaN, nothing traps — and the impl is below, measured running on all three lanes.

INTERIM SPELLINGS (each filed, flipped by the pin bump that fixes it): - **Num's own examples are PROSE, and the six traits' are not.** The two reasons sc44 wrote here are both gone: a qualified trait in a BOUND counts as a use of its import on lupin now (wolf-interp#97), and trait dispatch through an imported module's trait executes there (wolf-interp#96) — so every example below is a fenced block that runs on the reference machine, which is what §4 wants and what this module could not give until this release. The ONE exception is the alias: an alias reached through an import expands no bound under lupin 0.1.34, so fn f[T: ops.Num] is E0501: the bounds on T say nothing about +` at resolve while fn f[T: ops.Add] through the same import runs, and both compiler rungs run the alias at int and at f64. Filed as wolf-interp#102. Doc-truth wants the reference machine at exit(0) (§4), so Num's example stays prose until it closes — and tests/ops/num_alias_tier.lu carries the measurement as the ledger's first mirror-lag(E0501) row, which is the word sc46 added for exactly this shape (wolf-std#27, F-0121). - impl Div for int is integer division, truncating toward zero, and it traps div-zero on a zero divisor exactly as the operator does — the impl IS the operator, traps included, which is what tests/ops/div_zero_trap.lu` is for.

Types and traits

Num

trait · Source line 96

pub trait Num = Add + Sub + Mul + Div + Rem + cmp.Eq + cmp.Ord

The composed bound, [type.trait.op.alias]'s own example and the one the book's chapter 5 writes: fn total[T: ops.Num](xs: List[T], zero: T) -> T. An alias is a memberless trait carrying a bound — there is no supertrait clause this edition, so composition is spelled here and Ord requires Eq stays documentary in std.cmp.

Eq and Ord are cmp.-qualified and stay std.cmp's: Ordering is one enum in std (API-CONVENTIONS §6) and the compiler reads Ord.cmp's answer by VARIANT NAME, so a second Ordering here would be a second answer to a settled question.

Prose, not a fenced example, and the reason is filed: an alias reached through an IMPORT expands no bound under lupin 0.1.34 (wolf-interp#102), so a fence here could not reach exit(0) on the reference machine and §4 sends it to prose. Under fn total[T: ops.Num](xs: List[T], zero: T), total compiles and runs at int and at f64 on both compiler rungs; tests/ops/num_alias_tier.lu is the running witness and the ledger row that reds the day wolf-interp#102 closes.

Add

trait · Source line 113

pub trait Add { fn add(self, other: Self) -> Self }

Addition, a + b. The compiler dispatches + here when the left operand is a type parameter bounded by Add or a user type with an Add impl in scope ([type.trait.op]); both operands are the same type, which the clause calls homogeneity and E0514 enforces.

Under fn total[T: ops.Add](xs: List[T], zero: T), acc = acc + x is Add.add(acc, x), and at int that is the machine add — which is what the fence asserts, since a doc example declares no functions of its own.

let a: int = 2
let b: int = 3
ops.Add.add(a, b) == 5

Sub

trait · Source line 124

pub trait Sub { fn sub(self, other: Self) -> Self }

Subtraction, a - b. Binary only: unary minus is Neg.

let a: int = 9
let b: int = 4
ops.Sub.sub(a, b) == 5

Mul

trait · Source line 135

pub trait Mul { fn mul(self, other: Self) -> Self }

Multiplication, a * b.

let a: int = 6
let b: int = 7
ops.Mul.mul(a, b) == 42

Div

trait · Source line 151

pub trait Div { fn div(self, other: Self) -> Self }

Division, a / b. The impl inherits the operator's traps: at int a zero divisor traps divide_by_zero, at f64 it is IEEE and traps nothing.

let a: int = 7
let b: int = 2
ops.Div.div(a, b) == 3
let x: f64 = 7.5
let y: f64 = 2.0
ops.Div.div(x, y) == 3.75

Rem

trait · Source line 170

pub trait Rem { fn rem(self, other: Self) -> Self }

Remainder, a % b. At int the sign follows the dividend (truncated remainder) and a zero divisor traps divide_by_zero; at f64 it is fmod by [type.float.rem] — the remainder of the truncated division, the sign of the dividend again, and nothing traps. The f64 impl landed at sc46 with the clause that lowered the operator (wolf-lang#327).

let a: int = 17
let b: int = 5
ops.Rem.rem(a, b) == 2
let x: f64 = 7.5
let y: f64 = 2.0
ops.Rem.rem(x, y) == 1.5

Neg

trait · Source line 182

pub trait Neg { fn neg(self) -> Self }

Arithmetic negation, unary -a. One argument, unlike the five binary traits, and the clause reads the shape: a neg taking two parameters is E0514.

let a: int = 5
ops.Neg.neg(a) == 0 - 5

Functions and methods

No public declarations in this section.

Constants

No public declarations in this section.

Errors and row tags

No public declarations in this section.