Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

sum

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Returns the sum of numeric list elements.

Syntax: <list>.sum()

Category#

reduction

Examples#

radix/corpus/reference/concept/summa/summa.fab (canonical · concept)#

Sequential sum-reduce expression: reduce via sum from <tensor> at [i] let s.

# =============================================================================
# sum — one-expression sequential sum-reduce over a tensor
# =============================================================================
#
# What this teaches:
#   • Sequential fold — `reduce via sum from` lowers to a MIR fold over the
#     tensor's at iteration arm: a `+` accumulator seeded at zero, one term per
#     element, `return` inside the body yields the term value.
#   • Identity term — `return s` is the sequential spelling of `a.summa()`;
#     the printed pair matches under the numeric tolerance contract.
#   • Transformed term — the body may compute any numeric scalar term from
#     the bound element (here `s + 1.0` per lane).
#
# Common mistakes:
#   • non-scalar terms — the term must be a numeric scalar (float/numerus)
#   • the distributed `thread` spelling is admitted only inside `@ kernel`
#     kernels (v1)
#
# See also: tensor, at, thread
# =============================================================================

fn identity(tensor<f32, [8]> a) → f32 {
    return reduce via sum from a at [i] let s {
        return s
    }
}

fn shifted(tensor<f32, [8]> a) → f32 {
    return reduce via sum from a at [i] let s {
        mut f32 t ← s
        t ← t + 1.0
        return t
    }
}

main {
    let list<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
    let tf32[8] a ← flat ↦ tensor<f32, [8]>
    let f32 per_summam ← a.sum()
    let f32 plicatum ← identity(a)
    let f32 summam_totam ← shifted(a)
    # The identity term must match `.summa()` under the numeric tolerance
    # contract (SFR-4), as the retired Rust property test checked.
    assert (per_summam - plicatum).abs() ≤ 1e-5 * per_summam.abs().maximum(1.0) panic "identitatis terminus cum metodo .summa() congruat"
    print per_summam
    print plicatum
    print summam_totam
}

Expected output:

36.0
36.0
44.0

radix/corpus/reference/existing-home/summa/methodi-functionales.fab (canonical · existing-home)#

Returns the sum of numeric list elements.

# =============================================================================
# sum — Returns the sum of numeric list elements.
# =============================================================================
#
# What this teaches:
#   • higher-order intrinsics for lists — `filtrata` (filter), `mappata` (map), `reduce` (fold), `cumulata` (cumulate)
#   • closure syntax with `∴` — inline anonymous functions for collection operations
#
# Common mistakes:
#   • Omitting the initial accumulator value in reduce/cumulata — both require an explicit seed argument.
#
# See also: first, ultima, list
# =============================================================================

# list<T> higher-order intrinsics
#
# res.filter(T x ∴ <pred>)                        -- filter
# res.map(T x ∴ <expr>)                         -- map
# res.reduce((acc, x) ∴ <expr>, init)              -- fold
# res.cumulata((acc, x) ∴ <expr>, init)             -- cumulate (serial prefix scan)
#
# GRAMMAR:
#   listMethod :← expr '.' ('filtrata' | 'mappata' | 'reduce' | 'cumulata') '(' closure ')'
#
# EXPECTED OUTPUT:
#   [2, 4]
#   [2, 4, 6, 8, 10]
#   15
#   [1, 3, 6, 10, 15]
#
# BACKEND: Go e2e whitelist — list intrinsic methods not yet lowered for Go
# (whitelist: list/methodi-functionales.fab).

main {
    let list<int> res ← [1, 2, 3, 4, 5] ∷ list<int>
    let list<int> pares ← res.filter(int x ∴ x % 2 ≡ 0)
    let list<int> duplicata ← res.map(int x ∴ x * 2)
    let int sum ← res.reduce((int collectus, int x) ∴ collectus + x, 0)
    let list<int> cumulata ← res.cumulate((int collectus, int x) ∴ collectus + x, 0)
    print pares
    print duplicata
    print sum
    print cumulata
}

Expected output:

[2, 4]
[2, 4, 6, 8, 10]
15
[1, 3, 6, 10, 15]

radix/corpus/reference/reject/summa/summa-decline.fab (supporting · reject)#

Reducta sum declines misplaced thread and non-scalar terms; partial tails pass.

# =============================================================================
# sum decline — placement and scalar terms fail closed; partial tails pass.
# =============================================================================
#
# Decline rows are listed alongside the admitted partial-tail regression:
#   1. placement — `thread` admits only inside an `@ kernel` kernel;
#      this host function declines:
#      SEM010 reducta_filum_requires_nucleum_kernel
#   2. partial tail — K = 33 uses the guarded lane-strided loop and is admitted.
#   3. non-scalar term — `string` is not a numeric scalar:
#      SEM010 reducta_term_type_non_scalar
#
# Common mistakes:
#   • expecting `thread` outside a `@ kernel` kernel — placement is rejected
#   • expecting list/tensor terms to combine element-wise — the term type
#     must be a scalar of the element family
#
# See also: sum, thread, kernel, at, tensor
# =============================================================================

# 1. placement — thread outside @ kernel declines
fn placement(tensor<f32, [32]> a) → f32 {
    return reduce via sum from a at [i] thread f let s { return s }
}

# 2. partial tail — K = 33 is admitted and guarded by the emitted lane loop
@ kernel
@ public
fn tail(tensor<f32, [33]> a, mut tf32[1] out) → void {
    let f32 total ← reduce via sum from a at [i] thread f let s { return s }
    out[0] ← total
}

# 3. non-scalar term — a string element is not a numeric scalar
fn non_scalar(tensor<string, [8]> t) → string {
    return reduce via sum from t at [i] let s { return s }
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/summa/summa-ex-retired.fab (supporting · reject)#

The retired sum from form gives a named migration diagnostic.

fn total(tensor<int, [4]> values) → int {
    return sum from values
}

Expected: compilation rejects this example.