Skip to content

enhancement: self-host math.sum in stdlib #63

Description

@vycdev2

Summary

Move the monomorphic math.sum(list[int64]) returns int64 helper from its hardcoded checker signature and Rust interpreter body into the exported compiler-shipped .jett math module, while preserving consuming ownership and deterministic overflow behavior.

This is a narrow follow-up to #28 / PR #30. It covers only an aggregate helper whose existing public type is directly expressible in Jett; it does not broaden the closed compiler-owned numeric overload boundary.

Source documentation

Current state

math.sum has a hardcoded list[int64] -> int64 signature in jett_typecheck and an interpreter dispatch arm that loops over a runtime list. The existing run-pass fixture covers a representative non-empty sum. PR #47 separately changes unchecked host accumulation into a deterministic overflow error and adds an int64.MAX + 1 runtime-failure regression.

Unlike math.abs, math.min, and math.max, math.sum does not require the compiler-owned int64/float64 overload table. Its body can be expressed with an int64 accumulator, ordinary list iteration, and checked source-level addition. The current helper consumes its list argument rather than borrowing it; extraction must preserve that ownership behavior.

Scope

Includes:

  • add exported math.sum(values: list[int64]) returns int64 source under the compiler-shipped math stdlib module;
  • remove only the dedicated hardcoded typechecker signature and Rust interpreter dispatch arm for math.sum;
  • preserve an empty-list result of 0, normal and negative accumulation, argument/return diagnostics, consuming ownership, and deterministic overflow reporting;
  • add focused loader/query coverage proving the helper resolves from compiler-shipped source;
  • align docs/design.md, docs/architecture.md, and docs/progress.md with the resulting source/kernel boundary.

Explicitly excludes:

  • math.abs, math.min, and math.max, which remain compiler-owned exact numeric overloads;
  • math.average and math.median, whose generic numeric and empty-input contracts are not source-expressible without a separate decision;
  • math.clamp, whose invalid-bound and NaN behavior remains host-dependent;
  • math.gcd, math.lcm, and math.factorial, whose int64.MIN, overflow, negative-input, or saturation contracts need separate treatment;
  • changing list ownership, generic numeric constraints, implicit coercion, module/import policy, or trusted stdlib origin;
  • HIR, MIR, native code generation, or runtime ABI work.

Acceptance criteria

  • Compiler-shipped stdlib source exports math.sum(values: list[int64]) returns int64 with the existing public call spelling.
  • The dedicated math.sum hardcoded checker signature and Rust execution body are removed.
  • Focused tests pin empty, singleton, positive/negative, and order-sensitive cancellation cases.
  • Passing a list to math.sum retains the current consuming ownership behavior, including a compile-fail regression for reuse after the call.
  • The int64.MAX + 1 case remains a deterministic runtime error after fix: report math.sum overflow #47; any source-boundary diagnostic wording is decided explicitly and pinned rather than changed accidentally.
  • Existing argument/return-type diagnostics remain covered.
  • A query or loader regression identifies compiler-shipped math source as the definition location.
  • Retained primitive/overloaded math operations remain explicitly Rust-backed in design, architecture, and progress documentation.
  • cargo fmt --check, cargo build, and cargo test -q pass.

Dependencies / open questions

Land or rebase on #47 so the intended overflow regression is present before removing the Rust dispatch arm. Coordinate the physical stdlib/math.jett module with #28 / PR #30 rather than creating a second competing math namespace file. The implementation should decide whether ordinary source arithmetic's integer overflow wording is sufficient or whether call-context diagnostics must retain the existing math.sum: prefix.

This was generated by an AI agent (vycdev2). Please verify any changes before merging or applying.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions